Search Unity

[Solved]OpenStreetMap coordinates to tiles

Discussion in 'Scripting' started by beanie4now, May 10, 2019.

  1. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#C.23

    Using the docs I have
    Code (CSharp):
    1. public PointF WorldToTilePos(double lon, double lat, int zoom)
    2.     {
    3.         PointF p = new Point();
    4.         p.X = (float)((lon + 180.0) / 360.0 * (1 << zoom));
    5.         p.Y = (float)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) +
    6.             1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoom));
    7.  
    8.         return p;
    9.     }
    this gives me a very long float that I somehow need to use in a webrequest.

    Code (CSharp):
    1. string url = "http://d.tile.stamen.com/watercolor/"+zoom+"/"+x+"/"+y+".jpg";
    kind of stuck however. Not sure the step inbetween to get the correct index/ int of the tile from the PointF value
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Was trying
    Code (CSharp):
    1. string url = "http://d.tile.stamen.com/watercolor/"+zoom+"/"+(int)x+"/"+(int)y+".jpg";
    but it gave me weird offsets.

    subtiles.PNG
    Maybe this is the explanation... still a bit confusing tho.
     
  3. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Oh, duh. I was using the https://docs.mapbox.com/help/interactive-tools/static-api-playground/ and it was centering created maps on the Coordinates.
    In open street the tiles are pre rendered so each tile has 4 "subtiles" per zoom level. So when I zoom in a level I need to recenter the map on my coordinates since the map at any scale will be chosen simply if the coordinates are contained within it (thanks to my rounding).
    Code (CSharp):
    1. string url = "http://d.tile.stamen.com/watercolor/"+zoom+"/"+(int)x+"/"+(int)y+".jpg";