Inverting the Y Axis

Viewed 440

I'm new to OpenLayers and trying to figure out the best approach to inverting the Y axis so the Y origin is at the top of the extent instead of the bottom (plotting [0, 0] would be at the top left instead of the bottom left). I figured that I could update the extent to be something like [0, {-1 * map height}, {map width}, 0], but now I need to apply a transformation to the Y coordinates of all coordinate data going into the map to be the inverse (having coordinate [50, 100] transformed to [50, -100].

I'm trying to find a way that applies this transformation inside of OL instead of mutating the data going into it because I'd like to keep track of transformations within the OL code itself. So I was checking out the transform method on features, but wasn't able to hook into any transformative function in OL.

So my questions are, 1) Is this this best approach to inverse the Y axis in OpenLayers; and 2) if it is, how can I hook into OL to apply a transformation on all coordinates of a feature?

1 Answers

You can define a simple transform between data projection and view projection

addCoordinateTransforms(
  dataProjection,
  viewProjection,
  function (coordinate) {
    return [coordinate[0], -coordinate[1]];
  },
  function (coordinate) {
    return [coordinate[0], -coordinate[1]];
  }
);

This https://codesandbox.io/s/dazzling-mendeleev-w8cf8?file=/main.js flips an image layer, but the projections should work equally well as the dataProjection and featureProjection in a vector source.

Related