Morey
Morey

Reputation: 629

OpenLayers 3 Convert Pixel Coordinates to Lat Long

I am new to OpenLayers and would appreciate any help I can get. How do you convert from pixel-based-coordinates to lat/lon? I'm using OL3 to view and draw features on a static image (6494 x 7687 jpg) using projection:

  var projection = new ol.proj.Projection({
    code: 'xkcd-image',
    units: 'pixels',
    extent: [0, 0, 6494, 7687]
  });

At the end of a polygan draw I have this, which works fine:

draw.on('drawend', function (event) {
  var coord = event.feature.getGeometry().getCoordinates();
  console.log("YOU DREW A Polygon with coord="+coord);
});

Is there an easy way to convert the above pixel-based coordinates of the polygon to Lat/Lon coordinates? I do have the lat/lons of the four corners of the image.

Upvotes: 3

Views: 4317

Answers (1)

ahocevar
ahocevar

Reputation: 5647

Instead of specifying a custom pixel projection, configure your static image source with the imageExtent set to your corner coordinates, and set its projection to 'EPSG:4326':

new ol.source.ImageStatic({
  // ...
  imageExtent: [minLon, minLat, maxLon, maxLat],
  projection: 'EPSG:4326'
})

If you want to show your image without being distorted, you have to configure your view with projection: 'EPSG:4326' as well. You'll then be working with geographic coordinates throughout.

Something similar is also shown in one of the official examples: http://openlayers.org/en/latest/examples/reprojection-image.html. The difference is that raster reprojection is used there, because image and view are in different projection.

Upvotes: 3

Related Questions