OpenLayers OpenLayers

Changeset 3156

Show
Ignore:
Timestamp:
05/17/07 14:01:17 (2 years ago)
Author:
tschaub
Message:

add write support for multi-point, linestring, and polygon

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js

    r3140 r3156  
    7373            throw "Geometry must have coordinates array: " + obj; 
    7474        } 
    75         if(!this.parseCoords[obj.type]) { 
     75        if(!this.parseCoords[obj.type.toLowerCase()]) { 
    7676            throw "Unsupported geometry type: " + obj.type; 
    7777        } 
    7878        try { 
    79             geometry = this.parseCoords[obj.type].apply(this, [obj.coordinates]); 
     79            geometry = this.parseCoords[obj.type.toLowerCase()].apply(this, [obj.coordinates]); 
    8080        } catch(err) { 
    8181            // deal with bad coordinates 
     
    9797         * @private 
    9898         */ 
    99         "Point": function(array) { 
     99        "point": function(array) { 
    100100            if(array.length != 1) { 
    101101                throw "Bad point coordinates: " + array; 
     
    104104                throw "Only 2D points are supported: " + array[0]; 
    105105            } 
     106            // let this [[x, y]] be gone please 
    106107            return new OpenLayers.Geometry.Point(array[0][0], array[0][1]); 
    107108        }, 
     
    113114         * @private 
    114115         */ 
    115         "Line": function(array) { 
     116        "multipoint": function(array) { 
    116117            var points = []; 
    117118            var p = null; 
    118119            for(var i=0; i<array.length; ++i) { 
    119                 p = this.parseCoords["Point"].apply(this, [[array[i]]]); 
     120                p = this.parseCoords["point"].apply(this, [[array[i]]]); 
    120121                points.push(p); 
    121122            } 
     123            return new OpenLayers.Geometry.MultiPoint(points); 
     124        }, 
     125 
     126        /** 
     127         * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry. 
     128         * @param {Object} array The coordinates array from the GeoJSON fragment 
     129         * @returns {OpenLayers.Geometry} A geometry 
     130         * @private 
     131         */ 
     132        "linestring": function(array) { 
     133            var points = []; 
     134            var p = null; 
     135            for(var i=0; i<array.length; ++i) { 
     136                p = this.parseCoords["point"].apply(this, [[array[i]]]); 
     137                points.push(p); 
     138            } 
    122139            return new OpenLayers.Geometry.LineString(points); 
    123140        }, 
     
    129146         * @private 
    130147         */ 
    131         "Polygon": function(array) { 
     148        "multilinestring": function(array) { 
     149            var lines = []; 
     150            var l = null; 
     151            for(var i=0; i<array.length; ++i) { 
     152                l = this.parseCoords["linestring"].apply(this, [array[i]]); 
     153                lines.push(l); 
     154            } 
     155            return new OpenLayers.Geometry.MultiLineString(lines); 
     156        }, 
     157         
     158        /** 
     159         * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry. 
     160         * @param {Object} array The coordinates array from the GeoJSON fragment 
     161         * @returns {OpenLayers.Geometry} A geometry 
     162         * @private 
     163         */ 
     164        "polygon": function(array) { 
    132165            var rings = []; 
    133166            var r, l; 
    134167            for(var i=0; i<array.length; ++i) { 
    135                 l = this.parseCoords["Line"].apply(this, [array[i]]); 
     168                l = this.parseCoords["linestring"].apply(this, [array[i]]); 
    136169                r = new OpenLayers.Geometry.LinearRing(l.components); 
    137170                rings.push(r); 
    138171            } 
    139172            return new OpenLayers.Geometry.Polygon(rings); 
    140         } 
     173        }, 
     174         
     175        /** 
     176         * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry. 
     177         * @param {Object} array The coordinates array from the GeoJSON fragment 
     178         * @returns {OpenLayers.Geometry} A geometry 
     179         * @private 
     180         */ 
     181        "multipolygon": function(array) { 
     182            var polys = []; 
     183            var p = null; 
     184            for(var i=0; i<array.length; ++i) { 
     185                p = this.parseCoords["polygon"].apply(this, [array[i]]); 
     186                polys.push(p); 
     187            } 
     188            return new OpenLayers.Geometry.MultiPolygon(polys); 
     189        } 
     190 
    141191    }, 
    142192 
     
    159209            type = geometry.CLASS_NAME.split('.')[2]; 
    160210            data = this.extract[type.toLowerCase()].apply(this, [geometry]); 
    161             if (type == "LineString") { 
    162                 type = "Line"; 
    163             } 
    164             if (type == "Point") { 
     211            // please let this go away 
     212            if(type == "Point") { 
    165213                data = [data]; 
    166214            }     
     
    209257         
    210258        /** 
    211          * Return an array of coordinate arrays from a line
     259         * Return an array of coordinate arrays from a linestring
    212260         * @param {OpenLayers.Geometry.LineString} linestring 
    213261         * @returns {Array} An array of coordinate arrays representing 
     
    223271 
    224272        /** 
    225          * Return an array of linestring arrays from a line
    226          * @param {OpenLayers.Geometry.MultiLineString} line 
     273         * Return an array of linestring arrays from a linestring
     274         * @param {OpenLayers.Geometry.MultiLineString} linestring 
    227275         * @returns {Array} An array of linestring arrays representing 
    228276         *                  the multilinestring