 # Converting coordinates in Vectortiles

Hi,

I have been able to download and parse vector tiles ( /14/10708/7006 in this case) in Java.

I use the no.ecc.vectortile package to handle the decoding, and am confident that so far everything is working.

Two questions:

• all the x, y coordinates are doubles between 0 and 256: my understanding is that it should be integers between 0 and 4096? Multiplying by 16 achieves that goal, so is there are quirk with the decoder or should it be that way?
• more importantly, how do I then convert these values to lat and lon?

Thanks!

OpenStreetMap wiki has good explanation with code examples:
https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

Thank you, I am aware and that page was indeed very helpful in getting me from LatLon to a tile, and then downloading that tile and parsing it.

I could not find any examples, however, in then later mapping the coordinates of the points in the vector file, which all are from 0-256 on each axis (or 0-4096, see my question above) to LatLon.

Should I calculate the bounding box of the tile and then it’s length (roughly 2km in each direction where I am living) and then treat a coordinate of (5, 7) as (“move (5/256)*2km East from the North-West corner, then move (7/256)*2km South”)?

Thanks!

I used vt2geojson · PyPI myself. Maybe the sources help.

1 Like

Oooooooh, that looks very promising. Sources are helping indeed.

Will update later!

• disable “autoscale” in VectorTileDecoder to prevent it from scaling everything to 256x256 and keep it sized “extend x extend” instead
• java code to calculate projection below
``````
// assume tileNo is a string in the format z/x/y
String [] nOc=tileNo.split("/");
int tileX=Integer.parseInt(nOc);
int tileY=Integer.parseInt(nOc);
int tileZ=Integer.parseInt(nOc);

// each picture f in the image layer is encoded as Point in the vector tile
int extent=f.getExtent();
double featX=f.getGeometry().getCoordinate().getX();
double featY=f.getGeometry().getCoordinate().getY();

double size=(double) extent*Math.pow(2, tileZ);
double x0=extent*tileX;
double y0=extent*tileY;

double y2   = 180.0d-(featY+y0) * 360.0d / size;
double lon  = (featX+x0) * 360.0d / size - 180.0d;
double lat  = 360.0d / Math.PI * Math.atan(Math.exp(y2*Math.PI/180.0d)) - 90.0d;
``````