1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Format/JSON.js |
---|
8 | * @requires OpenLayers/Feature/Vector.js |
---|
9 | * @requires OpenLayers/Geometry/Point.js |
---|
10 | * @requires OpenLayers/Geometry/MultiPoint.js |
---|
11 | * @requires OpenLayers/Geometry/LineString.js |
---|
12 | * @requires OpenLayers/Geometry/MultiLineString.js |
---|
13 | * @requires OpenLayers/Geometry/Polygon.js |
---|
14 | * @requires OpenLayers/Geometry/MultiPolygon.js |
---|
15 | * @requires OpenLayers/Console.js |
---|
16 | */ |
---|
17 | |
---|
18 | /** |
---|
19 | * Class: OpenLayers.Format.GeoJSON |
---|
20 | * Read and write GeoJSON. Create a new parser with the |
---|
21 | * <OpenLayers.Format.GeoJSON> constructor. |
---|
22 | * |
---|
23 | * Inherits from: |
---|
24 | * - <OpenLayers.Format.JSON> |
---|
25 | */ |
---|
26 | OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { |
---|
27 | |
---|
28 | /** |
---|
29 | * APIProperty: ignoreExtraDims |
---|
30 | * {Boolean} Ignore dimensions higher than 2 when reading geometry |
---|
31 | * coordinates. |
---|
32 | */ |
---|
33 | ignoreExtraDims: false, |
---|
34 | |
---|
35 | /** |
---|
36 | * Constructor: OpenLayers.Format.GeoJSON |
---|
37 | * Create a new parser for GeoJSON. |
---|
38 | * |
---|
39 | * Parameters: |
---|
40 | * options - {Object} An optional object whose properties will be set on |
---|
41 | * this instance. |
---|
42 | */ |
---|
43 | initialize: function(options) { |
---|
44 | OpenLayers.Format.JSON.prototype.initialize.apply(this, [options]); |
---|
45 | }, |
---|
46 | |
---|
47 | /** |
---|
48 | * APIMethod: read |
---|
49 | * Deserialize a GeoJSON string. |
---|
50 | * |
---|
51 | * Parameters: |
---|
52 | * json - {String} A GeoJSON string |
---|
53 | * type - {String} Optional string that determines the structure of |
---|
54 | * the output. Supported values are "Geometry", "Feature", and |
---|
55 | * "FeatureCollection". If absent or null, a default of |
---|
56 | * "FeatureCollection" is assumed. |
---|
57 | * filter - {Function} A function which will be called for every key and |
---|
58 | * value at every level of the final result. Each value will be |
---|
59 | * replaced by the result of the filter function. This can be used to |
---|
60 | * reform generic objects into instances of classes, or to transform |
---|
61 | * date strings into Date objects. |
---|
62 | * |
---|
63 | * Returns: |
---|
64 | * {Object} The return depends on the value of the type argument. If type |
---|
65 | * is "FeatureCollection" (the default), the return will be an array |
---|
66 | * of <OpenLayers.Feature.Vector>. If type is "Geometry", the input json |
---|
67 | * must represent a single geometry, and the return will be an |
---|
68 | * <OpenLayers.Geometry>. If type is "Feature", the input json must |
---|
69 | * represent a single feature, and the return will be an |
---|
70 | * <OpenLayers.Feature.Vector>. |
---|
71 | */ |
---|
72 | read: function(json, type, filter) { |
---|
73 | type = (type) ? type : "FeatureCollection"; |
---|
74 | var results = null; |
---|
75 | var obj = null; |
---|
76 | if (typeof json == "string") { |
---|
77 | obj = OpenLayers.Format.JSON.prototype.read.apply(this, |
---|
78 | [json, filter]); |
---|
79 | } else { |
---|
80 | obj = json; |
---|
81 | } |
---|
82 | if(!obj) { |
---|
83 | OpenLayers.Console.error("Bad JSON: " + json); |
---|
84 | } else if(typeof(obj.type) != "string") { |
---|
85 | OpenLayers.Console.error("Bad GeoJSON - no type: " + json); |
---|
86 | } else if(this.isValidType(obj, type)) { |
---|
87 | switch(type) { |
---|
88 | case "Geometry": |
---|
89 | try { |
---|
90 | results = this.parseGeometry(obj); |
---|
91 | } catch(err) { |
---|
92 | OpenLayers.Console.error(err); |
---|
93 | } |
---|
94 | break; |
---|
95 | case "Feature": |
---|
96 | try { |
---|
97 | results = this.parseFeature(obj); |
---|
98 | results.type = "Feature"; |
---|
99 | } catch(err) { |
---|
100 | OpenLayers.Console.error(err); |
---|
101 | } |
---|
102 | break; |
---|
103 | case "FeatureCollection": |
---|
104 | // for type FeatureCollection, we allow input to be any type |
---|
105 | results = []; |
---|
106 | switch(obj.type) { |
---|
107 | case "Feature": |
---|
108 | try { |
---|
109 | results.push(this.parseFeature(obj)); |
---|
110 | } catch(err) { |
---|
111 | results = null; |
---|
112 | OpenLayers.Console.error(err); |
---|
113 | } |
---|
114 | break; |
---|
115 | case "FeatureCollection": |
---|
116 | for(var i=0, len=obj.features.length; i<len; ++i) { |
---|
117 | try { |
---|
118 | results.push(this.parseFeature(obj.features[i])); |
---|
119 | } catch(err) { |
---|
120 | results = null; |
---|
121 | OpenLayers.Console.error(err); |
---|
122 | } |
---|
123 | } |
---|
124 | break; |
---|
125 | default: |
---|
126 | try { |
---|
127 | var geom = this.parseGeometry(obj); |
---|
128 | results.push(new OpenLayers.Feature.Vector(geom)); |
---|
129 | } catch(err) { |
---|
130 | results = null; |
---|
131 | OpenLayers.Console.error(err); |
---|
132 | } |
---|
133 | } |
---|
134 | break; |
---|
135 | } |
---|
136 | } |
---|
137 | return results; |
---|
138 | }, |
---|
139 | |
---|
140 | /** |
---|
141 | * Method: isValidType |
---|
142 | * Check if a GeoJSON object is a valid representative of the given type. |
---|
143 | * |
---|
144 | * Returns: |
---|
145 | * {Boolean} The object is valid GeoJSON object of the given type. |
---|
146 | */ |
---|
147 | isValidType: function(obj, type) { |
---|
148 | var valid = false; |
---|
149 | switch(type) { |
---|
150 | case "Geometry": |
---|
151 | if(OpenLayers.Util.indexOf( |
---|
152 | ["Point", "MultiPoint", "LineString", "MultiLineString", |
---|
153 | "Polygon", "MultiPolygon", "Box", "GeometryCollection"], |
---|
154 | obj.type) == -1) { |
---|
155 | // unsupported geometry type |
---|
156 | OpenLayers.Console.error("Unsupported geometry type: " + |
---|
157 | obj.type); |
---|
158 | } else { |
---|
159 | valid = true; |
---|
160 | } |
---|
161 | break; |
---|
162 | case "FeatureCollection": |
---|
163 | // allow for any type to be converted to a feature collection |
---|
164 | valid = true; |
---|
165 | break; |
---|
166 | default: |
---|
167 | // for Feature types must match |
---|
168 | if(obj.type == type) { |
---|
169 | valid = true; |
---|
170 | } else { |
---|
171 | OpenLayers.Console.error("Cannot convert types from " + |
---|
172 | obj.type + " to " + type); |
---|
173 | } |
---|
174 | } |
---|
175 | return valid; |
---|
176 | }, |
---|
177 | |
---|
178 | /** |
---|
179 | * Method: parseFeature |
---|
180 | * Convert a feature object from GeoJSON into an |
---|
181 | * <OpenLayers.Feature.Vector>. |
---|
182 | * |
---|
183 | * Parameters: |
---|
184 | * obj - {Object} An object created from a GeoJSON object |
---|
185 | * |
---|
186 | * Returns: |
---|
187 | * {<OpenLayers.Feature.Vector>} A feature. |
---|
188 | */ |
---|
189 | parseFeature: function(obj) { |
---|
190 | var feature, geometry, attributes, bbox; |
---|
191 | attributes = (obj.properties) ? obj.properties : {}; |
---|
192 | bbox = (obj.geometry && obj.geometry.bbox) || obj.bbox; |
---|
193 | try { |
---|
194 | geometry = this.parseGeometry(obj.geometry); |
---|
195 | } catch(err) { |
---|
196 | // deal with bad geometries |
---|
197 | throw err; |
---|
198 | } |
---|
199 | feature = new OpenLayers.Feature.Vector(geometry, attributes); |
---|
200 | if(bbox) { |
---|
201 | feature.bounds = OpenLayers.Bounds.fromArray(bbox); |
---|
202 | } |
---|
203 | if(obj.id) { |
---|
204 | feature.fid = obj.id; |
---|
205 | } |
---|
206 | return feature; |
---|
207 | }, |
---|
208 | |
---|
209 | /** |
---|
210 | * Method: parseGeometry |
---|
211 | * Convert a geometry object from GeoJSON into an <OpenLayers.Geometry>. |
---|
212 | * |
---|
213 | * Parameters: |
---|
214 | * obj - {Object} An object created from a GeoJSON object |
---|
215 | * |
---|
216 | * Returns: |
---|
217 | * {<OpenLayers.Geometry>} A geometry. |
---|
218 | */ |
---|
219 | parseGeometry: function(obj) { |
---|
220 | if (obj == null) { |
---|
221 | return null; |
---|
222 | } |
---|
223 | var geometry, collection = false; |
---|
224 | if(obj.type == "GeometryCollection") { |
---|
225 | if(!(obj.geometries instanceof Array)) { |
---|
226 | throw "GeometryCollection must have geometries array: " + obj; |
---|
227 | } |
---|
228 | var numGeom = obj.geometries.length; |
---|
229 | var components = new Array(numGeom); |
---|
230 | for(var i=0; i<numGeom; ++i) { |
---|
231 | components[i] = this.parseGeometry.apply( |
---|
232 | this, [obj.geometries[i]] |
---|
233 | ); |
---|
234 | } |
---|
235 | geometry = new OpenLayers.Geometry.Collection(components); |
---|
236 | collection = true; |
---|
237 | } else { |
---|
238 | if(!(obj.coordinates instanceof Array)) { |
---|
239 | throw "Geometry must have coordinates array: " + obj; |
---|
240 | } |
---|
241 | if(!this.parseCoords[obj.type.toLowerCase()]) { |
---|
242 | throw "Unsupported geometry type: " + obj.type; |
---|
243 | } |
---|
244 | try { |
---|
245 | geometry = this.parseCoords[obj.type.toLowerCase()].apply( |
---|
246 | this, [obj.coordinates] |
---|
247 | ); |
---|
248 | } catch(err) { |
---|
249 | // deal with bad coordinates |
---|
250 | throw err; |
---|
251 | } |
---|
252 | } |
---|
253 | // We don't reproject collections because the children are reprojected |
---|
254 | // for us when they are created. |
---|
255 | if (this.internalProjection && this.externalProjection && !collection) { |
---|
256 | geometry.transform(this.externalProjection, |
---|
257 | this.internalProjection); |
---|
258 | } |
---|
259 | return geometry; |
---|
260 | }, |
---|
261 | |
---|
262 | /** |
---|
263 | * Property: parseCoords |
---|
264 | * Object with properties corresponding to the GeoJSON geometry types. |
---|
265 | * Property values are functions that do the actual parsing. |
---|
266 | */ |
---|
267 | parseCoords: { |
---|
268 | /** |
---|
269 | * Method: parseCoords.point |
---|
270 | * Convert a coordinate array from GeoJSON into an |
---|
271 | * <OpenLayers.Geometry>. |
---|
272 | * |
---|
273 | * Parameters: |
---|
274 | * array - {Object} The coordinates array from the GeoJSON fragment. |
---|
275 | * |
---|
276 | * Returns: |
---|
277 | * {<OpenLayers.Geometry>} A geometry. |
---|
278 | */ |
---|
279 | "point": function(array) { |
---|
280 | if (this.ignoreExtraDims == false && |
---|
281 | array.length != 2) { |
---|
282 | throw "Only 2D points are supported: " + array; |
---|
283 | } |
---|
284 | return new OpenLayers.Geometry.Point(array[0], array[1]); |
---|
285 | }, |
---|
286 | |
---|
287 | /** |
---|
288 | * Method: parseCoords.multipoint |
---|
289 | * Convert a coordinate array from GeoJSON into an |
---|
290 | * <OpenLayers.Geometry>. |
---|
291 | * |
---|
292 | * Parameters: |
---|
293 | * array {Object} The coordinates array from the GeoJSON fragment. |
---|
294 | * |
---|
295 | * Returns: |
---|
296 | * {<OpenLayers.Geometry>} A geometry. |
---|
297 | */ |
---|
298 | "multipoint": function(array) { |
---|
299 | var points = []; |
---|
300 | var p = null; |
---|
301 | for(var i=0, len=array.length; i<len; ++i) { |
---|
302 | try { |
---|
303 | p = this.parseCoords["point"].apply(this, [array[i]]); |
---|
304 | } catch(err) { |
---|
305 | throw err; |
---|
306 | } |
---|
307 | points.push(p); |
---|
308 | } |
---|
309 | return new OpenLayers.Geometry.MultiPoint(points); |
---|
310 | }, |
---|
311 | |
---|
312 | /** |
---|
313 | * Method: parseCoords.linestring |
---|
314 | * Convert a coordinate array from GeoJSON into an |
---|
315 | * <OpenLayers.Geometry>. |
---|
316 | * |
---|
317 | * Parameters: |
---|
318 | * array - {Object} The coordinates array from the GeoJSON fragment. |
---|
319 | * |
---|
320 | * Returns: |
---|
321 | * {<OpenLayers.Geometry>} A geometry. |
---|
322 | */ |
---|
323 | "linestring": function(array) { |
---|
324 | var points = []; |
---|
325 | var p = null; |
---|
326 | for(var i=0, len=array.length; i<len; ++i) { |
---|
327 | try { |
---|
328 | p = this.parseCoords["point"].apply(this, [array[i]]); |
---|
329 | } catch(err) { |
---|
330 | throw err; |
---|
331 | } |
---|
332 | points.push(p); |
---|
333 | } |
---|
334 | return new OpenLayers.Geometry.LineString(points); |
---|
335 | }, |
---|
336 | |
---|
337 | /** |
---|
338 | * Method: parseCoords.multilinestring |
---|
339 | * Convert a coordinate array from GeoJSON into an |
---|
340 | * <OpenLayers.Geometry>. |
---|
341 | * |
---|
342 | * Parameters: |
---|
343 | * array - {Object} The coordinates array from the GeoJSON fragment. |
---|
344 | * |
---|
345 | * Returns: |
---|
346 | * {<OpenLayers.Geometry>} A geometry. |
---|
347 | */ |
---|
348 | "multilinestring": function(array) { |
---|
349 | var lines = []; |
---|
350 | var l = null; |
---|
351 | for(var i=0, len=array.length; i<len; ++i) { |
---|
352 | try { |
---|
353 | l = this.parseCoords["linestring"].apply(this, [array[i]]); |
---|
354 | } catch(err) { |
---|
355 | throw err; |
---|
356 | } |
---|
357 | lines.push(l); |
---|
358 | } |
---|
359 | return new OpenLayers.Geometry.MultiLineString(lines); |
---|
360 | }, |
---|
361 | |
---|
362 | /** |
---|
363 | * Method: parseCoords.polygon |
---|
364 | * Convert a coordinate array from GeoJSON into an |
---|
365 | * <OpenLayers.Geometry>. |
---|
366 | * |
---|
367 | * Returns: |
---|
368 | * {<OpenLayers.Geometry>} A geometry. |
---|
369 | */ |
---|
370 | "polygon": function(array) { |
---|
371 | var rings = []; |
---|
372 | var r, l; |
---|
373 | for(var i=0, len=array.length; i<len; ++i) { |
---|
374 | try { |
---|
375 | l = this.parseCoords["linestring"].apply(this, [array[i]]); |
---|
376 | } catch(err) { |
---|
377 | throw err; |
---|
378 | } |
---|
379 | r = new OpenLayers.Geometry.LinearRing(l.components); |
---|
380 | rings.push(r); |
---|
381 | } |
---|
382 | return new OpenLayers.Geometry.Polygon(rings); |
---|
383 | }, |
---|
384 | |
---|
385 | /** |
---|
386 | * Method: parseCoords.multipolygon |
---|
387 | * Convert a coordinate array from GeoJSON into an |
---|
388 | * <OpenLayers.Geometry>. |
---|
389 | * |
---|
390 | * Parameters: |
---|
391 | * array - {Object} The coordinates array from the GeoJSON fragment. |
---|
392 | * |
---|
393 | * Returns: |
---|
394 | * {<OpenLayers.Geometry>} A geometry. |
---|
395 | */ |
---|
396 | "multipolygon": function(array) { |
---|
397 | var polys = []; |
---|
398 | var p = null; |
---|
399 | for(var i=0, len=array.length; i<len; ++i) { |
---|
400 | try { |
---|
401 | p = this.parseCoords["polygon"].apply(this, [array[i]]); |
---|
402 | } catch(err) { |
---|
403 | throw err; |
---|
404 | } |
---|
405 | polys.push(p); |
---|
406 | } |
---|
407 | return new OpenLayers.Geometry.MultiPolygon(polys); |
---|
408 | }, |
---|
409 | |
---|
410 | /** |
---|
411 | * Method: parseCoords.box |
---|
412 | * Convert a coordinate array from GeoJSON into an |
---|
413 | * <OpenLayers.Geometry>. |
---|
414 | * |
---|
415 | * Parameters: |
---|
416 | * array - {Object} The coordinates array from the GeoJSON fragment. |
---|
417 | * |
---|
418 | * Returns: |
---|
419 | * {<OpenLayers.Geometry>} A geometry. |
---|
420 | */ |
---|
421 | "box": function(array) { |
---|
422 | if(array.length != 2) { |
---|
423 | throw "GeoJSON box coordinates must have 2 elements"; |
---|
424 | } |
---|
425 | return new OpenLayers.Geometry.Polygon([ |
---|
426 | new OpenLayers.Geometry.LinearRing([ |
---|
427 | new OpenLayers.Geometry.Point(array[0][0], array[0][1]), |
---|
428 | new OpenLayers.Geometry.Point(array[1][0], array[0][1]), |
---|
429 | new OpenLayers.Geometry.Point(array[1][0], array[1][1]), |
---|
430 | new OpenLayers.Geometry.Point(array[0][0], array[1][1]), |
---|
431 | new OpenLayers.Geometry.Point(array[0][0], array[0][1]) |
---|
432 | ]) |
---|
433 | ]); |
---|
434 | } |
---|
435 | |
---|
436 | }, |
---|
437 | |
---|
438 | /** |
---|
439 | * APIMethod: write |
---|
440 | * Serialize a feature, geometry, array of features into a GeoJSON string. |
---|
441 | * |
---|
442 | * Parameters: |
---|
443 | * obj - {Object} An <OpenLayers.Feature.Vector>, <OpenLayers.Geometry>, |
---|
444 | * or an array of features. |
---|
445 | * pretty - {Boolean} Structure the output with newlines and indentation. |
---|
446 | * Default is false. |
---|
447 | * |
---|
448 | * Returns: |
---|
449 | * {String} The GeoJSON string representation of the input geometry, |
---|
450 | * features, or array of features. |
---|
451 | */ |
---|
452 | write: function(obj, pretty) { |
---|
453 | var geojson = { |
---|
454 | "type": null |
---|
455 | }; |
---|
456 | if(obj instanceof Array) { |
---|
457 | geojson.type = "FeatureCollection"; |
---|
458 | var numFeatures = obj.length; |
---|
459 | geojson.features = new Array(numFeatures); |
---|
460 | for(var i=0; i<numFeatures; ++i) { |
---|
461 | var element = obj[i]; |
---|
462 | if(!element instanceof OpenLayers.Feature.Vector) { |
---|
463 | var msg = "FeatureCollection only supports collections " + |
---|
464 | "of features: " + element; |
---|
465 | throw msg; |
---|
466 | } |
---|
467 | geojson.features[i] = this.extract.feature.apply( |
---|
468 | this, [element] |
---|
469 | ); |
---|
470 | } |
---|
471 | } else if (obj.CLASS_NAME.indexOf("OpenLayers.Geometry") == 0) { |
---|
472 | geojson = this.extract.geometry.apply(this, [obj]); |
---|
473 | } else if (obj instanceof OpenLayers.Feature.Vector) { |
---|
474 | geojson = this.extract.feature.apply(this, [obj]); |
---|
475 | if(obj.layer && obj.layer.projection) { |
---|
476 | geojson.crs = this.createCRSObject(obj); |
---|
477 | } |
---|
478 | } |
---|
479 | return OpenLayers.Format.JSON.prototype.write.apply(this, |
---|
480 | [geojson, pretty]); |
---|
481 | }, |
---|
482 | |
---|
483 | /** |
---|
484 | * Method: createCRSObject |
---|
485 | * Create the CRS object for an object. |
---|
486 | * |
---|
487 | * Parameters: |
---|
488 | * object - {<OpenLayers.Feature.Vector>} |
---|
489 | * |
---|
490 | * Returns: |
---|
491 | * {Object} An object which can be assigned to the crs property |
---|
492 | * of a GeoJSON object. |
---|
493 | */ |
---|
494 | createCRSObject: function(object) { |
---|
495 | var proj = object.layer.projection.toString(); |
---|
496 | var crs = {}; |
---|
497 | if (proj.match(/epsg:/i)) { |
---|
498 | var code = parseInt(proj.substring(proj.indexOf(":") + 1)); |
---|
499 | if (code == 4326) { |
---|
500 | crs = { |
---|
501 | "type": "OGC", |
---|
502 | "properties": { |
---|
503 | "urn": "urn:ogc:def:crs:OGC:1.3:CRS84" |
---|
504 | } |
---|
505 | }; |
---|
506 | } else { |
---|
507 | crs = { |
---|
508 | "type": "EPSG", |
---|
509 | "properties": { |
---|
510 | "code": code |
---|
511 | } |
---|
512 | }; |
---|
513 | } |
---|
514 | } |
---|
515 | return crs; |
---|
516 | }, |
---|
517 | |
---|
518 | /** |
---|
519 | * Property: extract |
---|
520 | * Object with properties corresponding to the GeoJSON types. |
---|
521 | * Property values are functions that do the actual value extraction. |
---|
522 | */ |
---|
523 | extract: { |
---|
524 | /** |
---|
525 | * Method: extract.feature |
---|
526 | * Return a partial GeoJSON object representing a single feature. |
---|
527 | * |
---|
528 | * Parameters: |
---|
529 | * feature - {<OpenLayers.Feature.Vector>} |
---|
530 | * |
---|
531 | * Returns: |
---|
532 | * {Object} An object representing the point. |
---|
533 | */ |
---|
534 | 'feature': function(feature) { |
---|
535 | var geom = this.extract.geometry.apply(this, [feature.geometry]); |
---|
536 | return { |
---|
537 | "type": "Feature", |
---|
538 | "id": feature.fid == null ? feature.id : feature.fid, |
---|
539 | "properties": feature.attributes, |
---|
540 | "geometry": geom |
---|
541 | }; |
---|
542 | }, |
---|
543 | |
---|
544 | /** |
---|
545 | * Method: extract.geometry |
---|
546 | * Return a GeoJSON object representing a single geometry. |
---|
547 | * |
---|
548 | * Parameters: |
---|
549 | * geometry - {<OpenLayers.Geometry>} |
---|
550 | * |
---|
551 | * Returns: |
---|
552 | * {Object} An object representing the geometry. |
---|
553 | */ |
---|
554 | 'geometry': function(geometry) { |
---|
555 | if (geometry == null) { |
---|
556 | return null; |
---|
557 | } |
---|
558 | if (this.internalProjection && this.externalProjection) { |
---|
559 | geometry = geometry.clone(); |
---|
560 | geometry.transform(this.internalProjection, |
---|
561 | this.externalProjection); |
---|
562 | } |
---|
563 | var geometryType = geometry.CLASS_NAME.split('.')[2]; |
---|
564 | var data = this.extract[geometryType.toLowerCase()].apply(this, [geometry]); |
---|
565 | var json; |
---|
566 | if(geometryType == "Collection") { |
---|
567 | json = { |
---|
568 | "type": "GeometryCollection", |
---|
569 | "geometries": data |
---|
570 | }; |
---|
571 | } else { |
---|
572 | json = { |
---|
573 | "type": geometryType, |
---|
574 | "coordinates": data |
---|
575 | }; |
---|
576 | } |
---|
577 | |
---|
578 | return json; |
---|
579 | }, |
---|
580 | |
---|
581 | /** |
---|
582 | * Method: extract.point |
---|
583 | * Return an array of coordinates from a point. |
---|
584 | * |
---|
585 | * Parameters: |
---|
586 | * point - {<OpenLayers.Geometry.Point>} |
---|
587 | * |
---|
588 | * Returns: |
---|
589 | * {Array} An array of coordinates representing the point. |
---|
590 | */ |
---|
591 | 'point': function(point) { |
---|
592 | return [point.x, point.y]; |
---|
593 | }, |
---|
594 | |
---|
595 | /** |
---|
596 | * Method: extract.multipoint |
---|
597 | * Return an array of point coordinates from a multipoint. |
---|
598 | * |
---|
599 | * Parameters: |
---|
600 | * multipoint - {<OpenLayers.Geometry.MultiPoint>} |
---|
601 | * |
---|
602 | * Returns: |
---|
603 | * {Array} An array of point coordinate arrays representing |
---|
604 | * the multipoint. |
---|
605 | */ |
---|
606 | 'multipoint': function(multipoint) { |
---|
607 | var array = []; |
---|
608 | for(var i=0, len=multipoint.components.length; i<len; ++i) { |
---|
609 | array.push(this.extract.point.apply(this, [multipoint.components[i]])); |
---|
610 | } |
---|
611 | return array; |
---|
612 | }, |
---|
613 | |
---|
614 | /** |
---|
615 | * Method: extract.linestring |
---|
616 | * Return an array of coordinate arrays from a linestring. |
---|
617 | * |
---|
618 | * Parameters: |
---|
619 | * linestring - {<OpenLayers.Geometry.LineString>} |
---|
620 | * |
---|
621 | * Returns: |
---|
622 | * {Array} An array of coordinate arrays representing |
---|
623 | * the linestring. |
---|
624 | */ |
---|
625 | 'linestring': function(linestring) { |
---|
626 | var array = []; |
---|
627 | for(var i=0, len=linestring.components.length; i<len; ++i) { |
---|
628 | array.push(this.extract.point.apply(this, [linestring.components[i]])); |
---|
629 | } |
---|
630 | return array; |
---|
631 | }, |
---|
632 | |
---|
633 | /** |
---|
634 | * Method: extract.multilinestring |
---|
635 | * Return an array of linestring arrays from a linestring. |
---|
636 | * |
---|
637 | * Parameters: |
---|
638 | * linestring - {<OpenLayers.Geometry.MultiLineString>} |
---|
639 | * |
---|
640 | * Returns: |
---|
641 | * {Array} An array of linestring arrays representing |
---|
642 | * the multilinestring. |
---|
643 | */ |
---|
644 | 'multilinestring': function(multilinestring) { |
---|
645 | var array = []; |
---|
646 | for(var i=0, len=multilinestring.components.length; i<len; ++i) { |
---|
647 | array.push(this.extract.linestring.apply(this, [multilinestring.components[i]])); |
---|
648 | } |
---|
649 | return array; |
---|
650 | }, |
---|
651 | |
---|
652 | /** |
---|
653 | * Method: extract.polygon |
---|
654 | * Return an array of linear ring arrays from a polygon. |
---|
655 | * |
---|
656 | * Parameters: |
---|
657 | * polygon - {<OpenLayers.Geometry.Polygon>} |
---|
658 | * |
---|
659 | * Returns: |
---|
660 | * {Array} An array of linear ring arrays representing the polygon. |
---|
661 | */ |
---|
662 | 'polygon': function(polygon) { |
---|
663 | var array = []; |
---|
664 | for(var i=0, len=polygon.components.length; i<len; ++i) { |
---|
665 | array.push(this.extract.linestring.apply(this, [polygon.components[i]])); |
---|
666 | } |
---|
667 | return array; |
---|
668 | }, |
---|
669 | |
---|
670 | /** |
---|
671 | * Method: extract.multipolygon |
---|
672 | * Return an array of polygon arrays from a multipolygon. |
---|
673 | * |
---|
674 | * Parameters: |
---|
675 | * multipolygon - {<OpenLayers.Geometry.MultiPolygon>} |
---|
676 | * |
---|
677 | * Returns: |
---|
678 | * {Array} An array of polygon arrays representing |
---|
679 | * the multipolygon |
---|
680 | */ |
---|
681 | 'multipolygon': function(multipolygon) { |
---|
682 | var array = []; |
---|
683 | for(var i=0, len=multipolygon.components.length; i<len; ++i) { |
---|
684 | array.push(this.extract.polygon.apply(this, [multipolygon.components[i]])); |
---|
685 | } |
---|
686 | return array; |
---|
687 | }, |
---|
688 | |
---|
689 | /** |
---|
690 | * Method: extract.collection |
---|
691 | * Return an array of geometries from a geometry collection. |
---|
692 | * |
---|
693 | * Parameters: |
---|
694 | * collection - {<OpenLayers.Geometry.Collection>} |
---|
695 | * |
---|
696 | * Returns: |
---|
697 | * {Array} An array of geometry objects representing the geometry |
---|
698 | * collection. |
---|
699 | */ |
---|
700 | 'collection': function(collection) { |
---|
701 | var len = collection.components.length; |
---|
702 | var array = new Array(len); |
---|
703 | for(var i=0; i<len; ++i) { |
---|
704 | array[i] = this.extract.geometry.apply( |
---|
705 | this, [collection.components[i]] |
---|
706 | ); |
---|
707 | } |
---|
708 | return array; |
---|
709 | } |
---|
710 | |
---|
711 | |
---|
712 | }, |
---|
713 | |
---|
714 | CLASS_NAME: "OpenLayers.Format.GeoJSON" |
---|
715 | |
---|
716 | }); |
---|