OpenLayers OpenLayers

Changeset 5516

Show
Ignore:
Timestamp:
12/19/07 17:07:12 (1 year ago)
Author:
crschmidt
Message:

Formats now support reprojection using internalProjection and
externalProjection properties. These allow for the reprojection of data --
OpenLayers users with SphericalMercator get this built in for EPSG:900913, and
other users can use the external proj4js library available from MapBuilder SVN
to add support for any number of projections. This means that featres can be,
for example, transformed from a KML doc in 4326 to Spherical Mercator before
being added to a layer, making using SphericalMercator slightly more enticing.
r=elemoine
(Closes #1039)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/vector-formats.html

    r5362 r5516  
    5656    <script type="text/javascript"> 
    5757        var map, vectors, formats; 
     58        function updateFormats() { 
     59            var in_options = { 
     60                'internalProjection': map.baseLayer.projection, 
     61                'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("inproj").value) 
     62            }     
     63            var out_options = { 
     64                'internalProjection': map.baseLayer.projection, 
     65                'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("outproj").value) 
     66            }     
     67            formats = { 
     68              'in': { 
     69                wkt: new OpenLayers.Format.WKT(in_options), 
     70                geojson: new OpenLayers.Format.GeoJSON(in_options), 
     71                georss: new OpenLayers.Format.GeoRSS(in_options), 
     72                gml: new OpenLayers.Format.GML(in_options), 
     73                kml: new OpenLayers.Format.KML(in_options) 
     74              },  
     75              'out': { 
     76                wkt: new OpenLayers.Format.WKT(out_options), 
     77                geojson: new OpenLayers.Format.GeoJSON(out_options), 
     78                georss: new OpenLayers.Format.GeoRSS(out_options), 
     79                gml: new OpenLayers.Format.GML(out_options), 
     80                kml: new OpenLayers.Format.KML(out_options) 
     81              }  
     82            }; 
     83        } 
    5884        function init(){ 
    5985            map = new OpenLayers.Map('map'); 
     
    74100            map.addControl(select); 
    75101            select.activate(); 
    76  
    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             }; 
     102             
     103            updateFormats(); 
    84104 
    85105            map.setCenter(new OpenLayers.LonLat(0, 0), 1); 
     
    90110            // second argument for pretty printing (geojson only) 
    91111            var pretty = document.getElementById("prettyPrint").checked; 
    92             var str = formats[type].write(feature, pretty); 
     112            var str = formats['out'][type].write(feature, pretty); 
    93113            // not a good idea in general, just for this demo 
    94114            str = str.replace(/,/g, ', '); 
     
    99119            var element = document.getElementById('text'); 
    100120            var type = document.getElementById("formatType").value; 
    101             var features = formats[type].read(element.value); 
     121            var features = formats['in'][type].read(element.value); 
    102122            var bounds; 
    103123            if(features) { 
     
    165185                   name="prettyPrint" value="1" /> 
    166186            <br /> 
     187            Input Projection: <select id="inproj" onchange='updateFormats()'> 
     188              <option value="EPSG:4326" selected="selected">EPSG:4326</option> 
     189              <option value="EPSG:900913">Spherical Mercator</option> 
     190            </select> <br />  
     191            Output Projection: <select id="outproj" onchange='updateFormats()'> 
     192              <option value="EPSG:4326" selected="selected">EPSG:4326</option> 
     193              <option value="EPSG:900913">Spherical Mercator</option> 
     194            </select>  
     195            <br />  
    167196            <textarea id="text">paste text here...</textarea> 
    168197            <br /> 
  • trunk/openlayers/lib/OpenLayers/Format.js

    r4985 r5516  
    1212OpenLayers.Format = OpenLayers.Class({ 
    1313     
     14    /** 
     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 
    1442    /** 
    1543     * Constructor: OpenLayers.Format 
  • trunk/openlayers/lib/OpenLayers/Format/GML.js

    r5514 r5516  
    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: " + 
     
    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); 
  • trunk/openlayers/lib/OpenLayers/Format/GeoJSON.js

    r5436 r5516  
    232232            } 
    233233        } 
     234        if (this.internalProjection && this.externalProjection) { 
     235            geometry.transform(this.externalProjection,  
     236                               this.internalProjection);  
     237        }                        
    234238        return geometry; 
    235239    }, 
     
    527531         */ 
    528532        'geometry': function(geometry) { 
     533            if (this.internalProjection && this.externalProjection) { 
     534                geometry = geometry.clone(); 
     535                geometry.transform(this.internalProjection,  
     536                                   this.externalProjection); 
     537            }                        
    529538            var geometryType = geometry.CLASS_NAME.split('.')[2]; 
    530539            var data = this.extract[geometryType.toLowerCase()].apply(this, [geometry]); 
  • trunk/openlayers/lib/OpenLayers/Format/GeoRSS.js

    r5238 r5516  
    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    },         
     
    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 
  • trunk/openlayers/lib/OpenLayers/Format/KML.js

    r5402 r5516  
    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: " + 
     
    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); 
  • trunk/openlayers/lib/OpenLayers/Format/Text.js

    r5412 r5516  
    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); 
  • trunk/openlayers/lib/OpenLayers/Format/WKT.js

    r5002 r5516  
    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    }, 
     
    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 + ')');