CartoDB / carto-vl

CARTO VL: a Javascript library to create vector-based visualizations
BSD 3-Clause "New" or "Revised" License
129 stars 26 forks source link

MVTs with bottom-left corner as the origin of coordinates #1393

Open antoniocarlon opened 5 years ago

antoniocarlon commented 5 years ago

We've been using CARTO-VL to draw geometries on a map using our own MVT server and it seems that the current implementation takes the bottom-left corner of the tile as the origin of the tile.

In particular, we've been diving into this function that transforms the tile's coordinates space (0 to 4096 for example) to a new coordinates space from -1 to 1: https://github.com/CartoDB/carto-vl/blob/c374e0ed80183e12353fdf5808b4031a4bf99dc8/src/client/mvt/feature-decoder.js#L189-L199

It seems that this function is inverting the y coordinate, making the bottom edge of the tile the origin of the y axis. We have written a quick Python test to prove our point:

>>> # Original CARTO-VL definition
... def transform(x, y, mvtExtent=4096):
...     x = 2 * x / mvtExtent - 1
...     y = 2 * (1 - y / mvtExtent) - 1
...     return [x, y]
... 
>>> # "Fixed"
... def transform_fix(x, y, mvtExtent=4096):
...     x = 2 * x / mvtExtent - 1
...     y = 2 * y / mvtExtent - 1
...     return [x, y]
... 
>>> transform(0, 0)
[-1.0, 1.0]
>>> transform_fix(0, 0)
[-1.0, -1.0]
>>> transform(4096, 4096)
[1.0, -1.0]
>>> transform_fix(4096, 4096)
[1.0, 1.0]

Taking a look at the Mapbox Vector Tile Specification, it seems that from version 2.0, Geometry data in a Vector Tile is defined in a screen coordinate system. The upper left corner of the tile (as displayed by default) is the origin of the coordinate system. The X axis is positive to the right, and the Y axis is positive downward. (https://github.com/mapbox/vector-tile-spec/tree/master/2.0). We haven't found anything similar for versions 1.0.0 and 1.0.1.

Is it a bug or is this the intended behavior and we need to fix it in our server?

cc @VictorVelarde @juanignaciosl

elenatorro commented 5 years ago

I've just done a very quick review, and I think the VL function is the correct one:

Comparing both coordinate systems:

  0,0     |1    1,0
          |
          |
-1 -------+------- 1
          |
          |
  0,1   -1|      1,1

Will result into:

0, 0 => -1, 1 
1, 0 => 1, 1 
0, 1 => -1, -1
1, 1 => 1, -1