OpenLayers OpenLayers

Ticket #1039: format.3.patch

File format.3.patch, 11.2 kB (added by crschmidt, 1 year ago)
  • lib/OpenLayers/Format/GeoJSON.js

    old new  
    238238            // deal with bad coordinates 
    239239            throw err; 
    240240        } 
     241        if (this.internalProjection && this.externalProjection) { 
     242            geometry.transform(this.externalProjection,  
     243                               this.internalProjection);  
     244        }                        
    241245        return geometry; 
    242246    }, 
    243247     
     
    548552         * {Object} An object representing the geometry. 
    549553         */ 
    550554        'geometry': function(geometry) { 
     555            if (this.internalProjection && this.externalProjection) { 
     556                geometry = geometry.clone(); 
     557                geometry.transform(this.internalProjection,  
     558                                   this.externalProjection); 
     559            }                        
    551560            var geometryType = geometry.CLASS_NAME.split('.')[2]; 
    552561            var data = this.extract[geometryType.toLowerCase()].apply(this, [geometry]); 
    553562            return { 
  • lib/OpenLayers/Format/Text.js

    old new  
    9494                        } 
    9595                    } 
    9696                    if (set) { 
     97                      if (this.internalProjection && this.externalProjection) { 
     98                          geometry.transform(this.externalProjection,  
     99                                             this.internalProjection);  
     100                      }                        
    97101                      var feature = new OpenLayers.Feature.Vector(geometry, attributes, style); 
    98102                      features.push(feature); 
    99103                    } 
  • lib/OpenLayers/Format/WKT.js

    old new  
    6060            if(this.parse[type]) { 
    6161                features = this.parse[type].apply(this, [str]); 
    6262            } 
    63         } 
     63            if (this.internalProjection && this.externalProjection) { 
     64                if (features &&  
     65                    features.CLASS_NAME == "OpenLayers.Feature.Vector") { 
     66                    features.geometry.transform(this.externalProjection, 
     67                                                this.internalProjection); 
     68                } else if (features && typeof features == "object") { 
     69                    for (var i = 0; i < features.length; i++) { 
     70                        var component = features[i]; 
     71                        component.geometry.transform(this.externalProjection, 
     72                                                     this.internalProjection); 
     73                    } 
     74                } 
     75            } 
     76        }     
    6477        return features; 
    6578    }, 
    6679 
     
    97110            if(!this.extract[type]) { 
    98111                return null; 
    99112            } 
     113            if (this.internalProjection && this.externalProjection) { 
     114                geometry = geometry.clone(); 
     115                geometry.transform(this.internalProjection,  
     116                                   this.externalProjection); 
     117            }                        
    100118            data = this.extract[type].apply(this, [geometry]); 
    101119            pieces.push(type.toUpperCase() + '(' + data + ')'); 
    102120        } 
  • lib/OpenLayers/Format/GML.js

    old new  
    147147                var parser = this.parseGeometry[type.toLowerCase()]; 
    148148                if(parser) { 
    149149                    geometry = parser.apply(this, [nodeList[0]]); 
     150                    if (this.internalProjection && this.externalProjection) { 
     151                        geometry.transform(this.externalProjection,  
     152                                           this.internalProjection);  
     153                    }                        
    150154                } else { 
    151155                    OpenLayers.Console.error("Unsupported geometry type: " + 
    152156                                             type); 
     
    619623     * APIMethod: buildGeometryNode 
    620624     */ 
    621625    buildGeometryNode: function(geometry) { 
     626        if (this.externalProjection && this.internalProjection) { 
     627            geometry = geometry.clone(); 
     628            geometry.transform(this.internalProjection,  
     629                               this.externalProjection); 
     630        }     
    622631        var className = geometry.CLASS_NAME; 
    623632        var type = className.substring(className.lastIndexOf(".") + 1); 
    624633        var builder = this.buildGeometry[type.toLowerCase()]; 
  • lib/OpenLayers/Format/GeoRSS.js

    old new  
    152152            var feature = this.gmlParser.parseFeature(where[0]); 
    153153            geometry = feature.geometry; 
    154154        } 
     155         
     156        if (this.internalProjection && this.externalProjection) { 
     157            geometry.transform(this.externalProjection,  
     158                               this.internalProjection); 
     159        } 
     160 
    155161        return geometry; 
    156162    },         
    157163 
     
    324330     * {DOMElement} A gml node. 
    325331     */ 
    326332    buildGeometryNode: function(geometry) { 
     333        if (this.internalProjection && this.externalProjection) { 
     334            geometry = geometry.clone(); 
     335            geometry.transform(this.internalProjection,  
     336                               this.externalProjection); 
     337        } 
    327338        var node; 
    328339        // match Polygon 
    329340        if (geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") { 
  • lib/OpenLayers/Format/KML.js

    old new  
    132132                var parser = this.parseGeometry[type.toLowerCase()]; 
    133133                if(parser) { 
    134134                    geometry = parser.apply(this, [nodeList[0]]); 
     135                    if (this.internalProjection && this.externalProjection) { 
     136                        geometry.transform(this.externalProjection,  
     137                                           this.internalProjection);  
     138                    }                        
    135139                } else { 
    136140                    OpenLayers.Console.error("Unsupported geometry type: " + 
    137141                                             type); 
     
    450454     * {DOMElement} 
    451455     */ 
    452456    buildGeometryNode: function(geometry) { 
     457        if (this.internalProjection && this.externalProjection) { 
     458            geometry = geometry.clone(); 
     459            geometry.transform(this.internalProjection,  
     460                               this.externalProjection); 
     461        }                        
    453462        var className = geometry.CLASS_NAME; 
    454463        var type = className.substring(className.lastIndexOf(".") + 1); 
    455464        var builder = this.buildGeometry[type.toLowerCase()]; 
  • lib/OpenLayers/Format.js

    old new  
    1212OpenLayers.Format = OpenLayers.Class({ 
    1313     
    1414    /** 
     15     * APIProperty: externalProjection 
     16     * {<OpenLayers.Projection>} When passed a externalProjection and 
     17     *     internalProjection, the format will reproject the geometries it 
     18     *     reads or writes. The externalProjection is the projection used by 
     19     *     the content which is passed into read or which comes out of write. 
     20     *     In order to reproject, a projection transformation function for the 
     21     *     specified projections must be available. This support may be  
     22     *     provided via proj4js or via a custom transformation function. See 
     23     *     {<OpenLayers.Projection.addTransform>} for more information on 
     24     *     custom transformations. 
     25     */ 
     26    externalProjection: null, 
     27 
     28    /** 
     29     * APIProperty: internalProjection 
     30     * {<OpenLayers.Projection>} When passed a externalProjection and 
     31     *     internalProjection, the format will reproject the geometries it 
     32     *     reads or writes. The internalProjection is the projection used by 
     33     *     the geometries which are returned by read or which are passed into 
     34     *     write.  In order to reproject, a projection transformation function 
     35     *     for the specified projections must be available. This support may be 
     36     *     provided via proj4js or via a custom transformation function. See 
     37     *     {<OpenLayers.Projection.addTransform>} for more information on 
     38     *     custom transformations. 
     39     */ 
     40    internalProjection: null, 
     41 
     42    /** 
    1543     * Constructor: OpenLayers.Format 
    1644     * Instances of this class are not useful.  See one of the subclasses. 
    1745     * 
  • examples/vector-formats.html

    old new  
    5555    <script src="../lib/OpenLayers.js"></script> 
    5656    <script type="text/javascript"> 
    5757        var map, vectors, formats; 
     58        function updateFormats() { 
     59            var options = { 
     60                'internalProjection': map.baseLayer.projection, 
     61                'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("proj").value) 
     62            }     
     63            formats = { 
     64                wkt: new OpenLayers.Format.WKT(options), 
     65                geojson: new OpenLayers.Format.GeoJSON(options), 
     66                georss: new OpenLayers.Format.GeoRSS(options), 
     67                gml: new OpenLayers.Format.GML(options), 
     68                kml: new OpenLayers.Format.KML(options) 
     69            }; 
     70        } 
    5871        function init(){ 
    5972            map = new OpenLayers.Map('map'); 
    6073            var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS", 
     
    7386            var select = new OpenLayers.Control.SelectFeature(vectors, options); 
    7487            map.addControl(select); 
    7588            select.activate(); 
     89             
     90            updateFormats(); 
    7691 
    77             formats = { 
    78                 wkt: new OpenLayers.Format.WKT(), 
    79                 geojson: new OpenLayers.Format.GeoJSON(), 
    80                 georss: new OpenLayers.Format.GeoRSS(), 
    81                 gml: new OpenLayers.Format.GML(), 
    82                 kml: new OpenLayers.Format.KML() 
    83             }; 
    84  
    8592            map.setCenter(new OpenLayers.LonLat(0, 0), 1); 
    8693        } 
    8794 
     
    164171            <input id="prettyPrint" type="checkbox" 
    165172                   name="prettyPrint" value="1" /> 
    166173            <br /> 
     174            Projection: <select id="proj" onchange='updateFormats()'> 
     175              <option value="EPSG:4326" selected="selected">EPSG:4326</option> 
     176              <option value="EPSG:900913">Spherical Mercator</option> 
     177            </select> <br />  
    167178            <textarea id="text">paste text here...</textarea> 
    168179            <br /> 
    169180            <input type="button" value="add feature" onclick="deserialize();" />