0

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

Pedram
  • 828
  • 9
  • 24
  • These look like they may be EPSG:4326 and EPSG:900913 already. Are you trying to go the other way maybe? I.e., lat = 30.453789 , lon = 35.637485 to EPSG:900913 and x = 3667550.3453 , y = 2205578.3453 to EPSG:4326? On the first one: x = 30.453789 , y = 35.637485 - either could be a latitude value (and latitude goes in the 'y' direction don't forget.) – jwd630 Jun 05 '15 at 16:49
  • @jwd630 : thanks for your reply. I know that x and y values maybe similar, but I want to get type of point projection by y and x (lat,lon) values. – Pedram Jun 07 '15 at 06:03

2 Answers2

2

In OpenLayers 2 it is the base map that has an associated projection. If your base layer is a Google map, which inherits from SphericalMercator, the base layer will be EPSG:900913 aka EPSG:3857 . If your base map is from some other service the projection may be WGS84 aka EPSG:4326 or it may be some other projection.

Later on in your code you are likely to need to determine the projection of the points you are getting in response to events so you will know if you need to project them to another coordinate reference frame. One way to do that is:

WGS84 = new OpenLayers.Projection("EPSG:4326"),
...

// Register event handler
map_layers.point.events.on({
  beforefeatureadded: recordCoord,
  featuremodified: recordCoord,
  afterfeaturemodified: recordCoord,
  featureselected: recordCoord,
  featureunselected: recordCoord,
  vertexmodified: recordCoord
});

...
// Handler to capture map additions/modifications/etc.
function recordCoord(event) {
    var layer = this,
        geometry = event.feature.geometry,
        map_loc = new OpenLayers.LonLat(geometry.x, geometry.y);
    if (map.getProjection() !== WGS84.getCode()) {
        map_loc.transform(map.getProjectionObject(), WGS84);
    }
    ...

That way as recordCoord proceeds map_loc is now in WGS84, regardless of what it was before.

If you have some other issue then I suggest adding some code to your question to show what you are trying to accomplish.

Community
  • 1
  • 1
jwd630
  • 4,529
  • 1
  • 20
  • 22
  • thanks for reply,I solved it by a code that similar to your code, but I wanted to determine point projection by x,y values. my baseLayer has spherical mercator, and my objects in db have wgs84 projection. I used layer.preFeatureInsert() function on map.addLayers() function. when a feature added to layer, I manually create projection property for that. next time I checked it,and if needed, change feature projection... – Pedram Jun 09 '15 at 07:44
0

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.

Pedram
  • 828
  • 9
  • 24