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.js |
---|
8 | * @requires OpenLayers/Feature/Vector.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Format.WKT |
---|
13 | * Class for reading and writing Well-Known Text. Create a new instance |
---|
14 | * with the <OpenLayers.Format.WKT> constructor. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Format> |
---|
18 | */ |
---|
19 | OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, { |
---|
20 | |
---|
21 | /** |
---|
22 | * Constructor: OpenLayers.Format.WKT |
---|
23 | * Create a new parser for WKT |
---|
24 | * |
---|
25 | * Parameters: |
---|
26 | * options - {Object} An optional object whose properties will be set on |
---|
27 | * this instance |
---|
28 | * |
---|
29 | * Returns: |
---|
30 | * {<OpenLayers.Format.WKT>} A new WKT parser. |
---|
31 | */ |
---|
32 | initialize: function(options) { |
---|
33 | this.regExes = { |
---|
34 | 'typeStr': /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/, |
---|
35 | 'spaces': /\s+/, |
---|
36 | 'parenComma': /\)\s*,\s*\(/, |
---|
37 | 'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/, // can't use {2} here |
---|
38 | 'trimParens': /^\s*\(?(.*?)\)?\s*$/ |
---|
39 | }; |
---|
40 | OpenLayers.Format.prototype.initialize.apply(this, [options]); |
---|
41 | }, |
---|
42 | |
---|
43 | /** |
---|
44 | * Method: read |
---|
45 | * Deserialize a WKT string and return a vector feature or an |
---|
46 | * array of vector features. Supports WKT for POINT, MULTIPOINT, |
---|
47 | * LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, and |
---|
48 | * GEOMETRYCOLLECTION. |
---|
49 | * |
---|
50 | * Parameters: |
---|
51 | * wkt - {String} A WKT string |
---|
52 | * |
---|
53 | * Returns: |
---|
54 | * {<OpenLayers.Feature.Vector>|Array} A feature or array of features for |
---|
55 | * GEOMETRYCOLLECTION WKT. |
---|
56 | */ |
---|
57 | read: function(wkt) { |
---|
58 | var features, type, str; |
---|
59 | var matches = this.regExes.typeStr.exec(wkt); |
---|
60 | if(matches) { |
---|
61 | type = matches[1].toLowerCase(); |
---|
62 | str = matches[2]; |
---|
63 | if(this.parse[type]) { |
---|
64 | features = this.parse[type].apply(this, [str]); |
---|
65 | } |
---|
66 | if (this.internalProjection && this.externalProjection) { |
---|
67 | if (features && |
---|
68 | features.CLASS_NAME == "OpenLayers.Feature.Vector") { |
---|
69 | features.geometry.transform(this.externalProjection, |
---|
70 | this.internalProjection); |
---|
71 | } else if (features && |
---|
72 | type != "geometrycollection" && |
---|
73 | typeof features == "object") { |
---|
74 | for (var i=0, len=features.length; i<len; i++) { |
---|
75 | var component = features[i]; |
---|
76 | component.geometry.transform(this.externalProjection, |
---|
77 | this.internalProjection); |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | return features; |
---|
83 | }, |
---|
84 | |
---|
85 | /** |
---|
86 | * Method: write |
---|
87 | * Serialize a feature or array of features into a WKT string. |
---|
88 | * |
---|
89 | * Parameters: |
---|
90 | * features - {<OpenLayers.Feature.Vector>|Array} A feature or array of |
---|
91 | * features |
---|
92 | * |
---|
93 | * Returns: |
---|
94 | * {String} The WKT string representation of the input geometries |
---|
95 | */ |
---|
96 | write: function(features) { |
---|
97 | var collection, geometry, type, data, isCollection; |
---|
98 | if(features.constructor == Array) { |
---|
99 | collection = features; |
---|
100 | isCollection = true; |
---|
101 | } else { |
---|
102 | collection = [features]; |
---|
103 | isCollection = false; |
---|
104 | } |
---|
105 | var pieces = []; |
---|
106 | if(isCollection) { |
---|
107 | pieces.push('GEOMETRYCOLLECTION('); |
---|
108 | } |
---|
109 | for(var i=0, len=collection.length; i<len; ++i) { |
---|
110 | if(isCollection && i>0) { |
---|
111 | pieces.push(','); |
---|
112 | } |
---|
113 | geometry = collection[i].geometry; |
---|
114 | type = geometry.CLASS_NAME.split('.')[2].toLowerCase(); |
---|
115 | if(!this.extract[type]) { |
---|
116 | return null; |
---|
117 | } |
---|
118 | if (this.internalProjection && this.externalProjection) { |
---|
119 | geometry = geometry.clone(); |
---|
120 | geometry.transform(this.internalProjection, |
---|
121 | this.externalProjection); |
---|
122 | } |
---|
123 | data = this.extract[type].apply(this, [geometry]); |
---|
124 | pieces.push(type.toUpperCase() + '(' + data + ')'); |
---|
125 | } |
---|
126 | if(isCollection) { |
---|
127 | pieces.push(')'); |
---|
128 | } |
---|
129 | return pieces.join(''); |
---|
130 | }, |
---|
131 | |
---|
132 | /** |
---|
133 | * Object with properties corresponding to the geometry types. |
---|
134 | * Property values are functions that do the actual data extraction. |
---|
135 | */ |
---|
136 | extract: { |
---|
137 | /** |
---|
138 | * Return a space delimited string of point coordinates. |
---|
139 | * @param {<OpenLayers.Geometry.Point>} point |
---|
140 | * @returns {String} A string of coordinates representing the point |
---|
141 | */ |
---|
142 | 'point': function(point) { |
---|
143 | return point.x + ' ' + point.y; |
---|
144 | }, |
---|
145 | |
---|
146 | /** |
---|
147 | * Return a comma delimited string of point coordinates from a multipoint. |
---|
148 | * @param {<OpenLayers.Geometry.MultiPoint>} multipoint |
---|
149 | * @returns {String} A string of point coordinate strings representing |
---|
150 | * the multipoint |
---|
151 | */ |
---|
152 | 'multipoint': function(multipoint) { |
---|
153 | var array = []; |
---|
154 | for(var i=0, len=multipoint.components.length; i<len; ++i) { |
---|
155 | array.push('(' + |
---|
156 | this.extract.point.apply(this, [multipoint.components[i]]) + |
---|
157 | ')'); |
---|
158 | } |
---|
159 | return array.join(','); |
---|
160 | }, |
---|
161 | |
---|
162 | /** |
---|
163 | * Return a comma delimited string of point coordinates from a line. |
---|
164 | * @param {<OpenLayers.Geometry.LineString>} linestring |
---|
165 | * @returns {String} A string of point coordinate strings representing |
---|
166 | * the linestring |
---|
167 | */ |
---|
168 | 'linestring': function(linestring) { |
---|
169 | var array = []; |
---|
170 | for(var i=0, len=linestring.components.length; i<len; ++i) { |
---|
171 | array.push(this.extract.point.apply(this, [linestring.components[i]])); |
---|
172 | } |
---|
173 | return array.join(','); |
---|
174 | }, |
---|
175 | |
---|
176 | /** |
---|
177 | * Return a comma delimited string of linestring strings from a multilinestring. |
---|
178 | * @param {<OpenLayers.Geometry.MultiLineString>} multilinestring |
---|
179 | * @returns {String} A string of of linestring strings representing |
---|
180 | * the multilinestring |
---|
181 | */ |
---|
182 | 'multilinestring': function(multilinestring) { |
---|
183 | var array = []; |
---|
184 | for(var i=0, len=multilinestring.components.length; i<len; ++i) { |
---|
185 | array.push('(' + |
---|
186 | this.extract.linestring.apply(this, [multilinestring.components[i]]) + |
---|
187 | ')'); |
---|
188 | } |
---|
189 | return array.join(','); |
---|
190 | }, |
---|
191 | |
---|
192 | /** |
---|
193 | * Return a comma delimited string of linear ring arrays from a polygon. |
---|
194 | * @param {<OpenLayers.Geometry.Polygon>} polygon |
---|
195 | * @returns {String} An array of linear ring arrays representing the polygon |
---|
196 | */ |
---|
197 | 'polygon': function(polygon) { |
---|
198 | var array = []; |
---|
199 | for(var i=0, len=polygon.components.length; i<len; ++i) { |
---|
200 | array.push('(' + |
---|
201 | this.extract.linestring.apply(this, [polygon.components[i]]) + |
---|
202 | ')'); |
---|
203 | } |
---|
204 | return array.join(','); |
---|
205 | }, |
---|
206 | |
---|
207 | /** |
---|
208 | * Return an array of polygon arrays from a multipolygon. |
---|
209 | * @param {<OpenLayers.Geometry.MultiPolygon>} multipolygon |
---|
210 | * @returns {Array} An array of polygon arrays representing |
---|
211 | * the multipolygon |
---|
212 | */ |
---|
213 | 'multipolygon': function(multipolygon) { |
---|
214 | var array = []; |
---|
215 | for(var i=0, len=multipolygon.components.length; i<len; ++i) { |
---|
216 | array.push('(' + |
---|
217 | this.extract.polygon.apply(this, [multipolygon.components[i]]) + |
---|
218 | ')'); |
---|
219 | } |
---|
220 | return array.join(','); |
---|
221 | } |
---|
222 | |
---|
223 | }, |
---|
224 | |
---|
225 | /** |
---|
226 | * Object with properties corresponding to the geometry types. |
---|
227 | * Property values are functions that do the actual parsing. |
---|
228 | */ |
---|
229 | parse: { |
---|
230 | /** |
---|
231 | * Return point feature given a point WKT fragment. |
---|
232 | * @param {String} str A WKT fragment representing the point |
---|
233 | * @returns {<OpenLayers.Feature.Vector>} A point feature |
---|
234 | * @private |
---|
235 | */ |
---|
236 | 'point': function(str) { |
---|
237 | var coords = OpenLayers.String.trim(str).split(this.regExes.spaces); |
---|
238 | return new OpenLayers.Feature.Vector( |
---|
239 | new OpenLayers.Geometry.Point(coords[0], coords[1]) |
---|
240 | ); |
---|
241 | }, |
---|
242 | |
---|
243 | /** |
---|
244 | * Return a multipoint feature given a multipoint WKT fragment. |
---|
245 | * @param {String} A WKT fragment representing the multipoint |
---|
246 | * @returns {<OpenLayers.Feature.Vector>} A multipoint feature |
---|
247 | * @private |
---|
248 | */ |
---|
249 | 'multipoint': function(str) { |
---|
250 | var point; |
---|
251 | var points = OpenLayers.String.trim(str).split(this.regExes.parenComma); |
---|
252 | var components = []; |
---|
253 | for(var i=0, len=points.length; i<len; ++i) { |
---|
254 | point = points[i].replace(this.regExes.trimParens, '$1'); |
---|
255 | components.push(this.parse.point.apply(this, [point]).geometry); |
---|
256 | } |
---|
257 | return new OpenLayers.Feature.Vector( |
---|
258 | new OpenLayers.Geometry.MultiPoint(components) |
---|
259 | ); |
---|
260 | }, |
---|
261 | |
---|
262 | /** |
---|
263 | * Return a linestring feature given a linestring WKT fragment. |
---|
264 | * @param {String} A WKT fragment representing the linestring |
---|
265 | * @returns {<OpenLayers.Feature.Vector>} A linestring feature |
---|
266 | * @private |
---|
267 | */ |
---|
268 | 'linestring': function(str) { |
---|
269 | var points = OpenLayers.String.trim(str).split(','); |
---|
270 | var components = []; |
---|
271 | for(var i=0, len=points.length; i<len; ++i) { |
---|
272 | components.push(this.parse.point.apply(this, [points[i]]).geometry); |
---|
273 | } |
---|
274 | return new OpenLayers.Feature.Vector( |
---|
275 | new OpenLayers.Geometry.LineString(components) |
---|
276 | ); |
---|
277 | }, |
---|
278 | |
---|
279 | /** |
---|
280 | * Return a multilinestring feature given a multilinestring WKT fragment. |
---|
281 | * @param {String} A WKT fragment representing the multilinestring |
---|
282 | * @returns {<OpenLayers.Feature.Vector>} A multilinestring feature |
---|
283 | * @private |
---|
284 | */ |
---|
285 | 'multilinestring': function(str) { |
---|
286 | var line; |
---|
287 | var lines = OpenLayers.String.trim(str).split(this.regExes.parenComma); |
---|
288 | var components = []; |
---|
289 | for(var i=0, len=lines.length; i<len; ++i) { |
---|
290 | line = lines[i].replace(this.regExes.trimParens, '$1'); |
---|
291 | components.push(this.parse.linestring.apply(this, [line]).geometry); |
---|
292 | } |
---|
293 | return new OpenLayers.Feature.Vector( |
---|
294 | new OpenLayers.Geometry.MultiLineString(components) |
---|
295 | ); |
---|
296 | }, |
---|
297 | |
---|
298 | /** |
---|
299 | * Return a polygon feature given a polygon WKT fragment. |
---|
300 | * @param {String} A WKT fragment representing the polygon |
---|
301 | * @returns {<OpenLayers.Feature.Vector>} A polygon feature |
---|
302 | * @private |
---|
303 | */ |
---|
304 | 'polygon': function(str) { |
---|
305 | var ring, linestring, linearring; |
---|
306 | var rings = OpenLayers.String.trim(str).split(this.regExes.parenComma); |
---|
307 | var components = []; |
---|
308 | for(var i=0, len=rings.length; i<len; ++i) { |
---|
309 | ring = rings[i].replace(this.regExes.trimParens, '$1'); |
---|
310 | linestring = this.parse.linestring.apply(this, [ring]).geometry; |
---|
311 | linearring = new OpenLayers.Geometry.LinearRing(linestring.components); |
---|
312 | components.push(linearring); |
---|
313 | } |
---|
314 | return new OpenLayers.Feature.Vector( |
---|
315 | new OpenLayers.Geometry.Polygon(components) |
---|
316 | ); |
---|
317 | }, |
---|
318 | |
---|
319 | /** |
---|
320 | * Return a multipolygon feature given a multipolygon WKT fragment. |
---|
321 | * @param {String} A WKT fragment representing the multipolygon |
---|
322 | * @returns {<OpenLayers.Feature.Vector>} A multipolygon feature |
---|
323 | * @private |
---|
324 | */ |
---|
325 | 'multipolygon': function(str) { |
---|
326 | var polygon; |
---|
327 | var polygons = OpenLayers.String.trim(str).split(this.regExes.doubleParenComma); |
---|
328 | var components = []; |
---|
329 | for(var i=0, len=polygons.length; i<len; ++i) { |
---|
330 | polygon = polygons[i].replace(this.regExes.trimParens, '$1'); |
---|
331 | components.push(this.parse.polygon.apply(this, [polygon]).geometry); |
---|
332 | } |
---|
333 | return new OpenLayers.Feature.Vector( |
---|
334 | new OpenLayers.Geometry.MultiPolygon(components) |
---|
335 | ); |
---|
336 | }, |
---|
337 | |
---|
338 | /** |
---|
339 | * Return an array of features given a geometrycollection WKT fragment. |
---|
340 | * @param {String} A WKT fragment representing the geometrycollection |
---|
341 | * @returns {Array} An array of OpenLayers.Feature.Vector |
---|
342 | * @private |
---|
343 | */ |
---|
344 | 'geometrycollection': function(str) { |
---|
345 | // separate components of the collection with | |
---|
346 | str = str.replace(/,\s*([A-Za-z])/g, '|$1'); |
---|
347 | var wktArray = OpenLayers.String.trim(str).split('|'); |
---|
348 | var components = []; |
---|
349 | for(var i=0, len=wktArray.length; i<len; ++i) { |
---|
350 | components.push(OpenLayers.Format.WKT.prototype.read.apply(this,[wktArray[i]])); |
---|
351 | } |
---|
352 | return components; |
---|
353 | } |
---|
354 | |
---|
355 | }, |
---|
356 | |
---|
357 | CLASS_NAME: "OpenLayers.Format.WKT" |
---|
358 | }); |
---|