Search code examples
javascriptopenlayersgismap-projections

OpenLayers - Get Geometry Projection


How can I get projection of a point or geometry in openlayers (2.12)?

for example:

x = 30.453789 , y = 35.637485 ==> EPSG:4326

and

x = 3667550.3453 , y = 2205578.3453 ==> EPSG:900913

appreciate any help


Solution

  • I can't get point projection by lat lon values, but solved it by adding projection property for each feature that will be added to the layer. my code is something like this:

    var mapProjection = new OpenLayers.Projection("EPSG:900913");
    var dbProjection = new OpenLayers.Projection("EPSG:4326");
    
    layer.preFeatureInsert = function (feature) {
        if (!feature.projection)
            feature.projection = dbProjection;
    
        if (feature.projection != mapProjection)
            feature.geometry.transform(feature.projection, mapProjection);
    
        //do something...
    }
    map.addLayer(layer);
    

    in first use, features projection set to wgs84, and then transform to spherical mercator. for the next time use, does not change anything.