Skip to content

Instantly share code, notes, and snippets.

@george-silva
Created July 24, 2013 19:04
Show Gist options
  • Select an option

  • Save george-silva/6073477 to your computer and use it in GitHub Desktop.

Select an option

Save george-silva/6073477 to your computer and use it in GitHub Desktop.
jeffheards tastypie format for geojson. Ill update it.
OpenLayers.Format.TastyPie = OpenLayers.Class(OpenLayers.Format.JSON, {
geoJsonReader : new OpenLayers.Format.GeoJSON(),
geometryField : null,
read : function(json, type, filter) {
var results = null;
var obj = null;
if (typeof json == "string") {
obj = OpenLayers.Format.JSON.prototype.read.apply(this,
[json, filter]);
} else {
obj = json;
}
if(!obj) {
OpenLayers.Console.error("Bad JSON: " + json);
} else if(typeof(obj.type) != "string") {
OpenLayers.Console.error("Bad GeoJSON - no type: " + json);
} else if(this.isValidType(obj)) {
return this.makeFeatureCollectionFromResultSet(obj);
}
},
write: function(obj, pretty) {
var output = null;
if(OpenLayers.Util.isArray(obj)) {
output = [];
for(var i in obj) { if(obj.hasOwnProperty(i)) {
if (!obj[i] instanceof OpenLayers.Feature.Vector) {
console.log('should have been a vector feature');
console.log(obj[i]);
}
var o = $.extend({}, obj[i].attributes);
o[geometryField] = this.geoJsonReader.extract.geometry.apply(this, [obj.geometry]);
output.push(o)
}}
}
else {
if (!obj instanceof OpenLayers.Feature.Vector) {
console.log('should have been a vector feature');
console.log(obj);
}
output = $.extend({}, obj.attributes);
output[this.geometryField] = this.geoJsonReader.extract.geometry.apply(this, [obj.geometry]);
}
return OpenLayers.Format.JSON.prototype.write.apply(this,
[output, pretty]);
},
isValidType: function(obj) {
var geometryTypes = ['Feature','FeatureCollection','LineString','MultiLineString','Point','MultiPoint','Polygon','MultiPolygon','LinearRing','GeometryCollection'];
var containsGeometry = false;
if(!obj.objects) { return false; }
else for(var i in obj.objects) {if(obj.hasOwnProperty(i)) {
if(this.geometryField && obj[i][this.geometryField] && obj[i][this.geometryField].type && geometryTypes.indexOf(obj[i][this.geometryField]) != -1) {
containsGeometry = true;
break;
}
for(var f in obj[i]) {if(obj[i].hasOwnProperty(f)) {
if(obj[i][f].type && geometryTypes.indexOf(obj[i][f].type) != -1) {
containsGeometry = true;
break;
}
}}
}}
return containsGeometry;
},
makeFeatureFromObject: function(obj) {
if(!this.geometryField) {
for(var f in obj) {if(obj.hasOwnProperty(f)) {
if(obj[f].type && geometryTypes.indexOf(obj[f].type) != -1) {
this.geometryField = f;
break;
}
}}
}
if(obj.geometryField) {
o = $.extend({}, obj);
delete o[this.geometryField]
return {
"type" : "Feature",
"properties" : o,
"geometry" : obj[this.geometryField]
}
}
},
makeFeatureCollectionFromResultSet: function (data) {
return {
"type" : "FeatureCollection",
"features" : mapping(data.objects, function(o) {
return this.makeFeatureFromObject(o);
})
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment