[OpenLayers-Commits] r3140 - sandbox/tschaub/geojson/lib/OpenLayers/Format
commits at openlayers.org
commits at openlayers.org
Mon May 7 15:56:03 EDT 2007
Author: tschaub
Date: 2007-05-07 15:56:03 -0400 (Mon, 07 May 2007)
New Revision: 3140
Modified:
sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js
Log:
basic read support for GeoJSON - Point, Line, and Polygon
Modified: sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js
===================================================================
--- sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js 2007-05-07 16:49:42 UTC (rev 3139)
+++ sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js 2007-05-07 19:56:03 UTC (rev 3140)
@@ -11,7 +11,7 @@
OpenLayers.Class.inherit(OpenLayers.Format.JSON, {
/**
- * Deserialize a json string.
+ * Deserialize a GeoJSON string.
* @param {String} json A GeoJSON string
* @param {Function} filter A function which will be called for every key
* and value at every level of the final result.
@@ -19,13 +19,126 @@
* the filter function. This can be used to reform
* generic objects into instances of classes, or to
* transform date strings into Date objects.
- * @returns {Object} An object representing features - this will eventually
- * return an array of OpenLayers.Geometry
+ * @returns {Array(OpenLayers.Feature.Vector)} An array of features
*/
read: function(json, filter) {
- // not really implemented yet
- return OpenLayers.Format.JSON.prototype.read.apply(this, [json, filter]);
+ var obj = OpenLayers.Format.JSON.prototype.read.apply(this, [json, filter]);
+ var features = [];
+ var f = null;
+ if(!(obj.features instanceof Array)) {
+ // deal with bad GeoJSON
+ //OpenLayers.console.error("Bad GeoJSON: " + obj);
+ } else {
+ for(var i=0; i<obj.features.length; ++i) {
+ try {
+ f = this.parseFeature(obj.features[i]);
+ features.push(f);
+ } catch(err) {
+ // deal with bad features
+ //OpenLayers.console.error('Bad Features:' + err);
+ }
+ }
+ }
+ return features;
},
+
+ /**
+ * Convert a feature object from GeoJSON into an OpenLayers.Feature.Vector.
+ * @param {Object} obj An object created from a GeoJSON fragment
+ * @returns {OpenLayers.Feature.Vector} A feature
+ * @private
+ */
+ parseFeature: function(obj) {
+ var feature, geometry, attributes;
+ attributes = (obj.properties) ? obj.properties : {};
+ try {
+ geometry = this.parseGeometry(obj.geometry);
+ } catch(err) {
+ // deal with bad geometries
+ throw err;
+ }
+ feature = new OpenLayers.Feature.Vector(geometry, attributes);
+ return feature;
+ },
+
+ /**
+ * Convert a geometry object from GeoJSON into an OpenLayers.Geometry.
+ * @param {Object} obj An object created from a GeoJSON fragment
+ * @returns {OpenLayers.Geometry} A geometry
+ * @private
+ */
+ parseGeometry: function(obj) {
+ var geometry;
+ if(!(obj.coordinates instanceof Array)) {
+ throw "Geometry must have coordinates array: " + obj;
+ }
+ if(!this.parseCoords[obj.type]) {
+ throw "Unsupported geometry type: " + obj.type;
+ }
+ try {
+ geometry = this.parseCoords[obj.type].apply(this, [obj.coordinates]);
+ } catch(err) {
+ // deal with bad coordinates
+ throw err;
+ }
+ return geometry;
+ },
+
+ /**
+ * Object with properties corresponding to the GeoJSON geometry types.
+ * Property values are functions that do the actual parsing.
+ * @private
+ */
+ parseCoords: {
+ /**
+ * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry.
+ * @param {Object} array The coordinates array from the GeoJSON fragment
+ * @returns {OpenLayers.Geometry} A geometry
+ * @private
+ */
+ "Point": function(array) {
+ if(array.length != 1) {
+ throw "Bad point coordinates: " + array;
+ }
+ if(array[0].length != 2) {
+ throw "Only 2D points are supported: " + array[0];
+ }
+ return new OpenLayers.Geometry.Point(array[0][0], array[0][1]);
+ },
+
+ /**
+ * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry.
+ * @param {Object} array The coordinates array from the GeoJSON fragment
+ * @returns {OpenLayers.Geometry} A geometry
+ * @private
+ */
+ "Line": function(array) {
+ var points = [];
+ var p = null;
+ for(var i=0; i<array.length; ++i) {
+ p = this.parseCoords["Point"].apply(this, [[array[i]]]);
+ points.push(p);
+ }
+ return new OpenLayers.Geometry.LineString(points);
+ },
+
+ /**
+ * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry.
+ * @param {Object} array The coordinates array from the GeoJSON fragment
+ * @returns {OpenLayers.Geometry} A geometry
+ * @private
+ */
+ "Polygon": function(array) {
+ var rings = [];
+ var r, l;
+ for(var i=0; i<array.length; ++i) {
+ l = this.parseCoords["Line"].apply(this, [array[i]]);
+ r = new OpenLayers.Geometry.LinearRing(l.components);
+ rings.push(r);
+ }
+ return new OpenLayers.Geometry.Polygon(rings);
+ }
+ },
/**
* Serialize an array of features into a GeoJSON string.
More information about the Commits
mailing list