Changeset 3158
- Timestamp:
- 05/21/07 10:24:55 (2 years ago)
- Files:
-
- trunk/openlayers/examples/wkt.html (modified) (2 diffs)
- trunk/openlayers/lib/OpenLayers/Format/WKT.js (modified) (9 diffs)
- trunk/openlayers/lib/OpenLayers/Geometry.js (modified) (2 diffs)
- trunk/openlayers/tests/Format/test_WKT.html (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/examples/wkt.html
r3043 r3158 79 79 80 80 function displayWKT(feature) { 81 var str = wkt.write(feature .geometry);81 var str = wkt.write(feature); 82 82 // not a good idea in general, just for this demo 83 83 str = str.replace(/,/g, ', '); … … 87 87 function parseWKT() { 88 88 var element = document.getElementById('wkt'); 89 var collection= wkt.read(element.value);89 var features = wkt.read(element.value); 90 90 var bounds; 91 if( collection) {92 if( collection.constructor != Array) {93 collection = [collection];91 if(features) { 92 if(features.constructor != Array) { 93 features = [features]; 94 94 } 95 var features = []; 96 for(var i=0; i<collection.length; ++i) { 97 features.push(new OpenLayers.Feature.Vector(collection[i])); 95 for(var i=0; i<features.length; ++i) { 98 96 if (!bounds) { 99 bounds = collection[i].getBounds(); 97 bounds = features[i].geometry.getBounds(); 98 } else { 99 bounds.extend(features[i].geometry.getBounds()); 100 100 } 101 bounds.extend(collection[i].getBounds());102 101 103 102 } trunk/openlayers/lib/OpenLayers/Format/WKT.js
r2978 r3158 26 26 27 27 /** 28 * Deserialize a WKT string and return an OpenLayers.Geometry or an array 29 * of OpenLayers.Geometry. Supports WKT for POINT, MULTIPOINT, LINESTRING, 30 * MULTILINESTRING, POLYGON, MULTIPOLYGON, and GEOMETRYCOLLECTION. 28 * Deserialize a WKT string and return an OpenLayers.Feature.Vector or an 29 * array of OpenLayers.Feature.Vector. Supports WKT for POINT, MULTIPOINT, 30 * LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, and 31 * GEOMETRYCOLLECTION. 31 32 * @param {String} wkt A WKT string 32 * @returns {OpenLayers.Geometry|Array} A geometry or array of geometries 33 * for GEOMETRYCOLLECTION WKT. 33 * @returns {OpenLayers.Feature.Vector|Array} A feature or array of 34 * features for 35 * GEOMETRYCOLLECTION WKT. 34 36 */ 35 37 read: function(wkt) { 36 var geometry, type, str;38 var features, type, str; 37 39 var matches = this.regExes.typeStr.exec(wkt); 38 40 if(matches) { … … 40 42 str = matches[2]; 41 43 if(this.parse[type]) { 42 geometry = this.parse[type].apply(this, [str]); 43 } 44 } 45 return geometry; 46 }, 47 48 /** 49 * Serialize a geometry or array of geometries into a WKT string. 50 * @param {OpenLayers.Geometry|Array} geom A geometry or array of geometries 44 features = this.parse[type].apply(this, [str]); 45 } 46 } 47 return features; 48 }, 49 50 /** 51 * Serialize a feature or array of features into a WKT string. 52 * @param {OpenLayers.Feature.Vector|Array} features A feature or array of 53 * features 51 54 * @returns {String} The WKT string representation of the input geometries 52 55 */ 53 write: function( geom) {56 write: function(features) { 54 57 var collection, geometry, type, data, isCollection; 55 if( geom.constructor == Array) {56 collection = geom;58 if(features.constructor == Array) { 59 collection = features; 57 60 isCollection = true; 58 61 } else { 59 collection = [ geom];62 collection = [features]; 60 63 isCollection = false; 61 64 } … … 68 71 pieces.push(','); 69 72 } 70 geometry = collection[i] ;73 geometry = collection[i].geometry; 71 74 type = geometry.CLASS_NAME.split('.')[2].toLowerCase(); 72 75 if(!this.extract[type]) { … … 179 182 parse: { 180 183 /** 181 * Return point geometrygiven a point WKT fragment.184 * Return point feature given a point WKT fragment. 182 185 * @param {String} str A WKT fragment representing the point 183 * @returns {OpenLayers.Geometry.Point} A point geometry 186 * @returns {OpenLayers.Feature.Vector} A point feature 187 * @private 184 188 */ 185 189 'point': function(str) { 186 190 var coords = str.trim().split(this.regExes.spaces); 187 return new OpenLayers.Geometry.Point(coords[0], coords[1]); 188 }, 189 190 /** 191 * Return a multipoint geometry given a multipoint WKT fragment. 191 return new OpenLayers.Feature.Vector( 192 new OpenLayers.Geometry.Point(coords[0], coords[1]) 193 ); 194 }, 195 196 /** 197 * Return a multipoint feature given a multipoint WKT fragment. 192 198 * @param {String} A WKT fragment representing the multipoint 193 * @returns {OpenLayers.Geometry.MultiPoint} A multipoint geometry 199 * @returns {OpenLayers.Feature.Vector} A multipoint feature 200 * @private 194 201 */ 195 202 'multipoint': function(str) { … … 197 204 var components = []; 198 205 for(var i=0; i<points.length; ++i) { 199 components.push(this.parse.point.apply(this, [points[i]])); 200 } 201 return new OpenLayers.Geometry.MultiPoint(components); 206 components.push(this.parse.point.apply(this, [points[i]]).geometry); 207 } 208 return new OpenLayers.Feature.Vector( 209 new OpenLayers.Geometry.MultiPoint(components) 210 ); 202 211 }, 203 212 204 213 /** 205 * Return a linestring geometrygiven a linestring WKT fragment.214 * Return a linestring feature given a linestring WKT fragment. 206 215 * @param {String} A WKT fragment representing the linestring 207 * @returns {OpenLayers.Geometry.LineString} A linestring geometry 216 * @returns {OpenLayers.Feature.Vector} A linestring feature 217 * @private 208 218 */ 209 219 'linestring': function(str) { … … 211 221 var components = []; 212 222 for(var i=0; i<points.length; ++i) { 213 components.push(this.parse.point.apply(this, [points[i]])); 214 } 215 return new OpenLayers.Geometry.LineString(components); 216 }, 217 218 /** 219 * Return a multilinestring geometry given a multilinestring WKT fragment. 223 components.push(this.parse.point.apply(this, [points[i]]).geometry); 224 } 225 console.log(components); 226 return new OpenLayers.Feature.Vector( 227 new OpenLayers.Geometry.LineString(components) 228 ); 229 }, 230 231 /** 232 * Return a multilinestring feature given a multilinestring WKT fragment. 220 233 * @param {String} A WKT fragment representing the multilinestring 221 * @returns {OpenLayers.Geometry.LineString} A multilinestring geometry 234 * @returns {OpenLayers.Feature.Vector} A multilinestring feature 235 * @private 222 236 */ 223 237 'multilinestring': function(str) { … … 227 241 for(var i=0; i<lines.length; ++i) { 228 242 line = lines[i].replace(this.regExes.trimParens, '$1'); 229 components.push(this.parse.linestring.apply(this, [line])); 230 } 231 return new OpenLayers.Geometry.MultiLineString(components); 243 components.push(this.parse.linestring.apply(this, [line]).geometry); 244 } 245 return new OpenLayers.Feature.Vector( 246 new OpenLayers.Geometry.MultiLineString(components) 247 ); 232 248 }, 233 249 234 250 /** 235 * Return a polygon geometrygiven a polygon WKT fragment.251 * Return a polygon feature given a polygon WKT fragment. 236 252 * @param {String} A WKT fragment representing the polygon 237 * @returns {OpenLayers.Geometry.Polygon} A polygon geometry 253 * @returns {OpenLayers.Feature.Vector} A polygon feature 254 * @private 238 255 */ 239 256 'polygon': function(str) { … … 243 260 for(var i=0; i<rings.length; ++i) { 244 261 ring = rings[i].replace(this.regExes.trimParens, '$1'); 245 linestring = this.parse.linestring.apply(this, [ring]) ;246 linearring = new OpenLayers.Geometry.LinearRing(linestring.components);262 linestring = this.parse.linestring.apply(this, [ring]).geometry; 263 linearring = new OpenLayers.Geometry.LinearRing(linestring.components) 247 264 components.push(linearring); 248 265 } 249 return new OpenLayers.Geometry.Polygon(components); 250 }, 251 252 /** 253 * Return a multipolygon geometry given a multipolygon WKT fragment. 266 return new OpenLayers.Feature.Vector( 267 new OpenLayers.Geometry.Polygon(components) 268 ); 269 }, 270 271 /** 272 * Return a multipolygon feature given a multipolygon WKT fragment. 254 273 * @param {String} A WKT fragment representing the multipolygon 255 * @returns {OpenLayers.Geometry.MultiPolygon} A multipolygon geometry 274 * @returns {OpenLayers.Feature.Vector} A multipolygon feature 275 * @private 256 276 */ 257 277 'multipolygon': function(str) { … … 261 281 for(var i=0; i<polygons.length; ++i) { 262 282 polygon = polygons[i].replace(this.regExes.trimParens, '$1'); 263 components.push(this.parse.polygon.apply(this, [polygon])); 264 } 265 return new OpenLayers.Geometry.MultiPolygon(components); 266 }, 267 268 /** 269 * Return an array of geometries given a geometrycollection WKT fragment. 283 components.push(this.parse.polygon.apply(this, [polygon]).geometry); 284 } 285 return new OpenLayers.Feature.Vector( 286 new OpenLayers.Geometry.MultiPolygon(components) 287 ); 288 }, 289 290 /** 291 * Return an array of features given a geometrycollection WKT fragment. 270 292 * @param {String} A WKT fragment representing the geometrycollection 271 * @returns {Array} An array of OpenLayers.Geometry 293 * @returns {Array} An array of OpenLayers.Feature.Vector 294 * @private 272 295 */ 273 296 'geometrycollection': function(str) { trunk/openlayers/lib/OpenLayers/Geometry.js
r3043 r3158 6 6 * @class 7 7 * @requires OpenLayers/Format/WKT.js 8 * @requires OpenLayers/Feature/Vector.js 8 9 */ 9 10 OpenLayers.Geometry = OpenLayers.Class.create(); … … 151 152 */ 152 153 toString: function() { 153 return OpenLayers.Format.WKT.prototype.write(this); 154 return OpenLayers.Format.WKT.prototype.write( 155 new OpenLayers.Feature.Vector(this) 156 ); 154 157 }, 155 158 trunk/openlayers/tests/Format/test_WKT.html
r2978 r3158 6 6 var points = []; 7 7 for(var i=0; i<12; ++i) { 8 points.push(new OpenLayers.Geometry.Point(Math.random() * 100, 9 Math.random() * 100)); 8 points.push(new OpenLayers.Feature.Vector( 9 new OpenLayers.Geometry.Point(Math.random() * 100, 10 Math.random() * 100)) 11 ); 10 12 } 11 var multipoint = new OpenLayers.Geometry.MultiPoint([ 12 points[0], points[1], points[2] 13 ]); 13 var multipoint = new OpenLayers.Feature.Vector( 14 new OpenLayers.Geometry.MultiPoint([ 15 points[0].geometry, points[1].geometry, points[2].geometry 16 ]) 17 ); 14 18 15 19 var linestrings = [ 16 new OpenLayers.Geometry.LineString([points[0], points[1], points[2]]), 17 new OpenLayers.Geometry.LineString([points[3], points[4], points[5]]) 20 new OpenLayers.Feature.Vector( 21 new OpenLayers.Geometry.LineString([points[0].geometry, points[1].geometry, points[2].geometry]) 22 ), 23 new OpenLayers.Feature.Vector( 24 new OpenLayers.Geometry.LineString([points[3].geometry, points[4].geometry, points[5].geometry]) 25 ) 18 26 ]; 19 27 20 var multilinestring = new OpenLayers.Geometry.MultiLineString([ 21 linestrings[0], linestrings[1] 22 ]); 28 var multilinestring = new OpenLayers.Feature.Vector( 29 new OpenLayers.Geometry.MultiLineString([ 30 linestrings[0].geometry, linestrings[1].geometry 31 ]) 32 ); 23 33 24 34 var rings = [ 25 new OpenLayers.Geometry.LinearRing([points[0] , points[1], points[2]]),26 new OpenLayers.Geometry.LinearRing([points[3] , points[4], points[5]]),27 new OpenLayers.Geometry.LinearRing([points[6] , points[7], points[8]]),28 new OpenLayers.Geometry.LinearRing([points[9] , points[10], points[11]])35 new OpenLayers.Geometry.LinearRing([points[0].geometry, points[1].geometry, points[2].geometry]), 36 new OpenLayers.Geometry.LinearRing([points[3].geometry, points[4].geometry, points[5].geometry]), 37 new OpenLayers.Geometry.LinearRing([points[6].geometry, points[7].geometry, points[8].geometry]), 38 new OpenLayers.Geometry.LinearRing([points[9].geometry, points[10].geometry, points[11].geometry]) 29 39 ]; 30 40 31 41 var polygons = [ 32 new OpenLayers.Geometry.Polygon([rings[0], rings[1]]), 33 new OpenLayers.Geometry.Polygon([rings[2], rings[3]]) 42 new OpenLayers.Feature.Vector( 43 new OpenLayers.Geometry.Polygon([rings[0], rings[1]]) 44 ), 45 new OpenLayers.Feature.Vector( 46 new OpenLayers.Geometry.Polygon([rings[2], rings[3]]) 47 ) 34 48 ]; 35 49 36 var multipolygon = new OpenLayers.Geometry.MultiPolygon([ 37 polygons[0], polygons[1] 38 ]); 50 var multipolygon = new OpenLayers.Feature.Vector( 51 new OpenLayers.Geometry.MultiPolygon([ 52 polygons[0].geometry, polygons[1].geometry 53 ]) 54 ); 39 55 40 56 var collection = [points[0], linestrings[0]]; … … 54 70 t.plan(7); 55 71 var format = new OpenLayers.Format.WKT(); 56 72 57 73 // test a point 58 74 t.eq(format.write(points[0]), 59 "POINT(" + points[0]. x + " " + points[0].y + ")",75 "POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")", 60 76 "format correctly writes Point WKT"); 61 77 62 78 // test a multipoint 63 79 t.eq(format.write(multipoint), 64 "MULTIPOINT(" + points[0]. x + " " + points[0].y + "," +65 points[1]. x + " " + points[1].y + "," +66 points[2]. x + " " + points[2].y + ")",80 "MULTIPOINT(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 81 points[1].geometry.x + " " + points[1].geometry.y + "," + 82 points[2].geometry.x + " " + points[2].geometry.y + ")", 67 83 "format correctly writes MultiPoint WKT"); 68 84 69 85 // test a linestring 70 86 t.eq(format.write(linestrings[0]), 71 "LINESTRING(" + points[0]. x + " " + points[0].y + "," +72 points[1]. x + " " + points[1].y + "," +73 points[2]. x + " " + points[2].y + ")",87 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 88 points[1].geometry.x + " " + points[1].geometry.y + "," + 89 points[2].geometry.x + " " + points[2].geometry.y + ")", 74 90 "format correctly writes LineString WKT"); 75 91 76 92 // test a multilinestring 77 93 t.eq(format.write(multilinestring), 78 "MULTILINESTRING((" + points[0]. x + " " + points[0].y + "," +79 points[1]. x + " " + points[1].y + "," +80 points[2]. x + " " + points[2].y + ")," +81 "(" + points[3]. x + " " + points[3].y + "," +82 points[4]. x + " " + points[4].y + "," +83 points[5]. x + " " + points[5].y + "))",94 "MULTILINESTRING((" + points[0].geometry.x + " " + points[0].geometry.y + "," + 95 points[1].geometry.x + " " + points[1].geometry.y + "," + 96 points[2].geometry.x + " " + points[2].geometry.y + ")," + 97 "(" + points[3].geometry.x + " " + points[3].geometry.y + "," + 98 points[4].geometry.x + " " + points[4].geometry.y + "," + 99 points[5].geometry.x + " " + points[5].geometry.y + "))", 84 100 "format correctly writes MultiLineString WKT"); 85 101 86 102 // test a polygon 87 103 t.eq(format.write(polygons[0]), 88 "POLYGON((" + points[0]. x + " " + points[0].y + "," +89 points[1]. x + " " + points[1].y + "," +90 points[2]. x + " " + points[2].y + "," +91 points[0]. x + " " + points[0].y + ")," +92 "(" + points[3]. x + " " + points[3].y + "," +93 points[4]. x + " " + points[4].y + "," +94 points[5]. x + " " + points[5].y + "," +95 points[3]. x + " " + points[3].y + "))",104 "POLYGON((" + points[0].geometry.x + " " + points[0].geometry.y + "," + 105 points[1].geometry.x + " " + points[1].geometry.y + "," + 106 points[2].geometry.x + " " + points[2].geometry.y + "," + 107 points[0].geometry.x + " " + points[0].geometry.y + ")," + 108 "(" + points[3].geometry.x + " " + points[3].geometry.y + "," + 109 points[4].geometry.x + " " + points[4].geometry.y + "," + 110 points[5].geometry.x + " " + points[5].geometry.y + "," + 111 points[3].geometry.x + " " + points[3].geometry.y + "))", 96 112 "format correctly writes Polygon WKT"); 97 113 98 114 // test a multipolygon 99 115 t.eq(format.write(multipolygon), 100 "MULTIPOLYGON(((" + points[0]. x + " " + points[0].y + "," +101 points[1]. x + " " + points[1].y + "," +102 points[2]. x + " " + points[2].y + "," +103 points[0]. x + " " + points[0].y + ")," +104 "(" + points[3]. x + " " + points[3].y + "," +105 points[4]. x + " " + points[4].y + "," +106 points[5]. x + " " + points[5].y + "," +107 points[3]. x + " " + points[3].y + "))," +108 "((" + points[6]. x + " " + points[6].y + "," +109 points[7]. x + " " + points[7].y + "," +110 points[8]. x + " " + points[8].y + "," +111 points[6]. x + " " + points[6].y + ")," +112 "(" + points[9]. x + " " + points[9].y + "," +113 points[10]. x + " " + points[10].y + "," +114 points[11]. x + " " + points[11].y + "," +115 points[9]. x + " " + points[9].y + ")))",116 "MULTIPOLYGON(((" + points[0].geometry.x + " " + points[0].geometry.y + "," + 117 points[1].geometry.x + " " + points[1].geometry.y + "," + 118 points[2].geometry.x + " " + points[2].geometry.y + "," + 119 points[0].geometry.x + " " + points[0].geometry.y + ")," + 120 "(" + points[3].geometry.x + " " + points[3].geometry.y + "," + 121 points[4].geometry.x + " " + points[4].geometry.y + "," + 122 points[5].geometry.x + " " + points[5].geometry.y + "," + 123 points[3].geometry.x + " " + points[3].geometry.y + "))," + 124 "((" + points[6].geometry.x + " " + points[6].geometry.y + "," + 125 points[7].geometry.x + " " + points[7].geometry.y + "," + 126 points[8].geometry.x + " " + points[8].geometry.y + "," + 127 points[6].geometry.x + " " + points[6].geometry.y + ")," + 128 "(" + points[9].geometry.x + " " + points[9].geometry.y + "," + 129 points[10].geometry.x + " " + points[10].geometry.y + "," + 130 points[11].geometry.x + " " + points[11].geometry.y + "," + 131 points[9].geometry.x + " " + points[9].geometry.y + ")))", 116 132 "format correctly writes MultiPolygon WKT"); 117 133 118 134 // test a geometrycollection 119 135 t.eq(format.write(collection), 120 "GEOMETRYCOLLECTION(POINT(" + points[0]. x + " " + points[0].y + ")," +121 "LINESTRING(" + points[0]. x + " " + points[0].y + "," +122 points[1]. x + " " + points[1].y + "," +123 points[2]. x + " " + points[2].y + "))",136 "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," + 137 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 138 points[1].geometry.x + " " + points[1].geometry.y + "," + 139 points[2].geometry.x + " " + points[2].geometry.y + "))", 124 140 "format correctly writes GeometryCollection WKT"); 125 141 … … 135 151 136 152 // test a point 137 t.ok(points[0]. equals(format.read(format.write(points[0]))),153 t.ok(points[0].geometry.equals(format.read(format.write(points[0])).geometry), 138 154 "format correctly reads Point WKT"); 139 155 140 156 // test a multipoint 141 t.ok(multipoint. equals(format.read(format.write(multipoint))),157 t.ok(multipoint.geometry.equals(format.read(format.write(multipoint)).geometry), 142 158 "format correctly reads MultiPoint WKT"); 143 159 144 160 // test a linestring 145 t.ok(linestrings[0]. equals(format.read(format.write(linestrings[0]))),161 t.ok(linestrings[0].geometry.equals(format.read(format.write(linestrings[0])).geometry), 146 162 "format correctly reads LineString WKT"); 147 163 148 164 // test a multilinestring 149 t.ok(multilinestring. equals(format.read(format.write(multilinestring))),165 t.ok(multilinestring.geometry.equals(format.read(format.write(multilinestring)).geometry), 150 166 "format correctly reads MultiLineString WKT"); 151 167 152 168 // test a polygon 153 t.ok(polygons[0]. equals(format.read(format.write(polygons[0]))),169 t.ok(polygons[0].geometry.equals(format.read(format.write(polygons[0])).geometry), 154 170 "format correctly reads Polygon WKT"); 155 171 156 172 // test a multipolygon 157 t.ok(multipolygon. equals(format.read(format.write(multipolygon))),173 t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry), 158 174 "format correctly reads MultiPolygon WKT"); 159 175 160 176 // test a geometrycollection 161 177 t.eq(format.write(collection), 162 "GEOMETRYCOLLECTION(POINT(" + points[0]. x + " " + points[0].y + ")," +163 "LINESTRING(" + points[0]. x + " " + points[0].y + "," +164 points[1]. x + " " + points[1].y + "," +165 points[2]. x + " " + points[2].y + "))",178 "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," + 179 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 180 points[1].geometry.x + " " + points[1].geometry.y + "," + 181 points[2].geometry.x + " " + points[2].geometry.y + "))", 166 182 "format correctly writes GeometryCollection WKT"); 167 183
