Skip to content

Instantly share code, notes, and snippets.

@ungoldman
Created June 1, 2013 21:35
Show Gist options
  • Save ungoldman/5691799 to your computer and use it in GitHub Desktop.
Save ungoldman/5691799 to your computer and use it in GitHub Desktop.
ArcGIS JS spatial reference helpers
define([], function(){
return {
// check if an object is a geometry
isGeometry: function(object){
return object.isInstanceOf(esri.geometry.Geometry);
},
// check if an object is a graphic
isGraphic: function(object){
return object.isInstanceOf(esri.Graphic);
},
// Check if a geometry or graphic is Geographic (Lat,Lng)
isGeographic: function(object){
if(this.isGraphic(object)){
return object.geometry.spatialReference.wkid === 4326;
} else if(this.isGeometry(object)) {
return object.spatialReference.wkid === 4326;
} else {
return false;
}
},
// Check if a geometry or graphic is In Web Mercator
isWebMercator: function(object){
if(this.isGraphic(object)){
return object.geometry.spatialReference.wkid === 102100;
} else if(this.isGeometry(object)) {
return object.spatialReference.wkid === 102100;
} else {
return false;
}
},
// Convert a geometry to webmercator if neccessary
convertToWebMercator: function(geometry){
if(this.isGeographic(geometry)) {
return esri.geometry.geographicToWebMercator(geometry);
} else {
return geometry;
}
},
// Convert a geometry to webmercator if neccessary
convertToGeographic: function(geometry){
if(this.isWebMercator(geometry)) {
return esri.geometry.webMercatorToGeographic(geometry);
} else {
return geometry;
}
},
convert: function(geometry) {
if (this.isWebMercator(geometry)) {
return this.convertToGeographic(geometry);
} else if (this.isGeographic(geometry)) {
return this.convertToWebMercator(geometry);
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment