3

A have an openlayers map with with two layers: a Tiled image layer (served through zoomify protocol) and a Vector layer. I draw boxes/rectangles on the image then get their geojson coordinates and save them in my DB.

After upgrading from OpenLayers 2 to 3 on my site, all my rectangular features are displayed in the wrong place. It turns out that with my OL2 implementation the Y axis is centered at the bottom of the map and points upward whereas in my OL 3 implementation the Y axis is centered at the top of the map and also points upward.

For backward compatibility reasons I don't want to update the coordinates in the DB. So I want the existing coordinates to show in the right place in my OL 3 image viewer.

How should I define the layers/views/map in OL3 to centre the Y axis of my features at the bottom of the image?

I've tried to reproduce the issue here:

https://jsfiddle.net/zjee9sdw/

You'll see that the box appears above the image. I want this box to appear in the bottom left corner of the image without changing it's input coordinates (or transforming them).

1

The Zoomify source in OpenLayers 3 has indeed a different coordinate system than in OpenLayers 2. However, both coordinate systems do not make much sense. Ideally, the origin would be in the top left corner, and y coordinates would increase from top to bottom.

For now, all you can do is configure OpenLayers 3 to do an automatic transform of your coordinates. The following projection definition will do the trick:

var proj = new ol.proj.Projection({code: 'ZOOMIFY'});
ol.proj.addProjection(proj);
ol.proj.addCoordinateTransforms('EPSG:4326', proj,
    function(coord) {
      return [coord[0], -options.image_height + coord[1]];
    } /*, inverse transform here */);

To apply this transform, you also have to change the line that reads the features:

var features = (new ol.format.GeoJSON()).readFeatures(geojsonObject,
    {featureProjection: proj});

Note that this will not be necessary when you configure your vector source with a url.

Updated JSFiddle: https://jsfiddle.net/zjee9sdw/2/

  • Thanks for the solution and updating the fiddle. Do you know why the default Zoomify coordinate system was modified from OL 2 to OL 3? – user3748764 Jul 21 '16 at 20:58
  • I don't know, I was not involved in the development of Zoomify support. Neither in OpenLayers 2 nor 3. – ahocevar Jul 21 '16 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.