📄 geojson.js
字号:
} lines.push(l); } return new OpenLayers.Geometry.MultiLineString(lines); }, /** * Method: parseCoords.polygon * Convert a coordinate array from GeoJSON into an * <OpenLayers.Geometry>. * * Returns: * {<OpenLayers.Geometry>} A geometry. */ "polygon": function(array) { var rings = []; var r, l; for(var i=0; i<array.length; ++i) { try { l = this.parseCoords["linestring"].apply(this, [array[i]]); } catch(err) { throw err; } r = new OpenLayers.Geometry.LinearRing(l.components); rings.push(r); } return new OpenLayers.Geometry.Polygon(rings); }, /** * Method: parseCoords.multipolygon * Convert a coordinate array from GeoJSON into an * <OpenLayers.Geometry>. * * Parameters: * array - {Object} The coordinates array from the GeoJSON fragment. * * Returns: * {<OpenLayers.Geometry>} A geometry. */ "multipolygon": function(array) { var polys = []; var p = null; for(var i=0; i<array.length; ++i) { try { p = this.parseCoords["polygon"].apply(this, [array[i]]); } catch(err) { throw err; } polys.push(p); } return new OpenLayers.Geometry.MultiPolygon(polys); }, /** * Method: parseCoords.box * Convert a coordinate array from GeoJSON into an * <OpenLayers.Geometry>. * * Parameters: * array - {Object} The coordinates array from the GeoJSON fragment. * * Returns: * {<OpenLayers.Geometry>} A geometry. */ "box": function(array) { if(array.length != 2) { throw "GeoJSON box coordinates must have 2 elements"; } return new OpenLayers.Geometry.Polygon([ new OpenLayers.Geometry.LinearRing([ new OpenLayers.Geometry.Point(array[0][0], array[0][1]), new OpenLayers.Geometry.Point(array[1][0], array[0][1]), new OpenLayers.Geometry.Point(array[1][0], array[1][1]), new OpenLayers.Geometry.Point(array[0][0], array[1][1]), new OpenLayers.Geometry.Point(array[0][0], array[0][1]) ]) ]); } }, /** * APIMethod: write * Serialize a feature, geometry, array of features, or array of geometries * into a GeoJSON string. * * Parameters: * obj - {Object} An <OpenLayers.Feature.Vector>, <OpenLayers.Geometry>, * or an array of either features or geometries. * pretty - {Boolean} Structure the output with newlines and indentation. * Default is false. * * Returns: * {String} The GeoJSON string representation of the input geometry, * features, array of geometries, or array of features. */ write: function(obj, pretty) { var geojson = { "type": null }; if(obj instanceof Array) { if(obj[0] instanceof OpenLayers.Feature.Vector) { geojson.features = []; } else if (obj[0].CLASS_NAME.search("OpenLayers.Geometry") == 0) { geojson.geometries = []; } for(var i=0; i<obj.length; ++i) { var element = obj[i]; if(element instanceof OpenLayers.Feature.Vector) { if(geojson.type == null) { geojson.type = "FeatureCollection"; if(element.layer && element.layer.projection) { geojson.crs = this.createCRSObject(element); } } else if(geojson.type != "FeatureCollection") { OpenLayers.Console.error("FeatureCollection only supports collections of features: " + element); break; } geojson.features.push(this.extract.feature.apply(this, [element])); } else if (element.CLASS_NAME.search("OpenLayers.Geometry") == 0) { if(geojson.type == null) { geojson.type = "GeometryCollection"; } else if(geojson.type != "GeometryCollection") { OpenLayers.Console.error("GeometryCollection only supports collections of geometries: " + element); break; } geojson.geometries.push(this.extract.geometry.apply(this, [element])); } } } else if (obj.CLASS_NAME.search("OpenLayers.Geometry") == 0) { geojson = this.extract.geometry.apply(this, [obj]); } else if (obj instanceof OpenLayers.Feature.Vector) { geojson = this.extract.feature.apply(this, [obj]); if(obj.layer && obj.layer.projection) { geojson.crs = this.createCRSObject(obj); } } return OpenLayers.Format.JSON.prototype.write.apply(this, [geojson, pretty]); }, /** * Method: createCRSObject * Create the CRS object for an object. * * Parameters: * object - {<OpenLayers.Feature.Vector>} * * Returns: * {Object} An object which can be assigned to the crs property * of a GeoJSON object. */ createCRSObject: function(object) { var proj = object.layer.projection; var crs = {} if (proj.match(/epsg:/i)) { var code = parseInt(proj.substring(proj.indexOf(":") + 1)); if (code == 4326) { crs = { "type": "OGC", "properties": { "urn": "urn:ogc:def:crs:OGC:1.3:CRS84" } }; } else { crs = { "type": "EPSG", "properties": { "code": code } }; } } return crs; }, /** * Property: extract * Object with properties corresponding to the GeoJSON types. * Property values are functions that do the actual value extraction. */ extract: { /** * Method: extract.feature * Return a partial GeoJSON object representing a single feature. * * Parameters: * feature - {<OpenLayers.Feature.Vector>} * * Returns: * {Object} An object representing the point. */ 'feature': function(feature) { var geom = this.extract.geometry.apply(this, [feature.geometry]); return { "type": "Feature", "id": feature.fid == null ? feature.id : feature.fid, "properties": feature.attributes, "geometry": geom } }, /** * Method: extract.geometry * Return a GeoJSON object representing a single geometry. * * Parameters: * geometry - {<OpenLayers.Geometry>} * * Returns: * {Object} An object representing the geometry. */ 'geometry': function(geometry) { var geometryType = geometry.CLASS_NAME.split('.')[2]; var data = this.extract[geometryType.toLowerCase()].apply(this, [geometry]); return { "type": geometryType, "coordinates": data } }, /** * Method: extract.poin * Return an array of coordinates from a point. * * Parameters: * point - {<OpenLayers.Geometry.Point>} * * Returns: * {Array} An array of coordinates representing the point. */ 'point': function(point) { return [point.x, point.y]; }, /** * Method: extract.multipoint * Return an array of point coordinates from a multipoint. * * Parameters: * multipoint - {<OpenLayers.Geometry.MultiPoint>} * * Returns: * {Array} An array of point coordinate arrays representing * the multipoint. */ 'multipoint': function(multipoint) { var array = []; for(var i=0; i<multipoint.components.length; ++i) { array.push(this.extract.point.apply(this, [multipoint.components[i]])); } return array; }, /** * Method: extract.linestring * Return an array of coordinate arrays from a linestring. * * Parameters: * linestring - {<OpenLayers.Geometry.LineString>} * * Returns: * {Array} An array of coordinate arrays representing * the linestring. */ 'linestring': function(linestring) { var array = []; for(var i=0; i<linestring.components.length; ++i) { array.push(this.extract.point.apply(this, [linestring.components[i]])); } return array; }, /** * Method: extract.multilinestring * Return an array of linestring arrays from a linestring. * * Parameters: * linestring - {<OpenLayers.Geometry.MultiLineString>} * * Returns: * {Array} An array of linestring arrays representing * the multilinestring. */ 'multilinestring': function(multilinestring) { var array = []; for(var i=0; i<multilinestring.components.length; ++i) { array.push(this.extract.linestring.apply(this, [multilinestring.components[i]])); } return array; }, /** * Method: extract.polygon * Return an array of linear ring arrays from a polygon. * * Parameters: * polygon - {<OpenLayers.Geometry.Polygon>} * * Returns: * {Array} An array of linear ring arrays representing the polygon. */ 'polygon': function(polygon) { var array = []; for(var i=0; i<polygon.components.length; ++i) { array.push(this.extract.linestring.apply(this, [polygon.components[i]])); } return array; }, /** * Method: extract.multipolygon * Return an array of polygon arrays from a multipolygon. * * Parameters: * multipolygon - {<OpenLayers.Geometry.MultiPolygon>} * * Returns: * {Array} An array of polygon arrays representing * the multipolygon */ 'multipolygon': function(multipolygon) { var array = []; for(var i=0; i<multipolygon.components.length; ++i) { array.push(this.extract.polygon.apply(this, [multipolygon.components[i]])); } return array; } }, CLASS_NAME: "OpenLayers.Format.GeoJSON" });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -