Search code examples
c#mapboxcoordinate-transformation

Translating between Lat/Lon to a local coordinate System? When knowing the bounds


So i have 4 bounding points in Lat/Lon, and i also have 4 bounding points in an (x,y) coordinate system that directly relate to the bounding points of the lat/lon box. (it's a tile loaded through an API in c#, but no function exists to get the local coordinates)

This tile is 305.5 units wide & tall, (it's a vector tile) How would i setup a linear algebra problem so that when i have a Lat/Lon within the bounds, i can find the x,y coordinate it relates to in the other coordinate system.

I've read multiple projection questions and none of them deal with the image being a vector image, but i do have an absolute distance that the map is being represented onto (305.5 local units).

Here is some data i have, but im trying to make this into an algorithm that i could use for different bounds etc in c# (the data is obtained through an API where i can easily get the bounds and lat/lon values:

                              (lat,lon) -> (x,y)
Top-Left Bound:  (38.79691,-90)         -> (-305.5,305.5)
Top-Right Bound: (38.79691,-89.99451)   -> (305.5,305.5)
Bot-Left Bound:  (38.79263,-90)         -> (-305.5,-305.5)
Bot-Right Bound: (38.79263,-89.99451)   -> (305.5,-305.5)
Center-Bound:    (38.79477,-89.99725)   -> (0,0)

Solution

  • In your specific case, this is what you're looking for

    X = -305.5 + (305.5 - -305.5) * (lon - -90) / (-89.99451 - -90) Y = -305.5 + (305.5 - -305.5) * (lat - 38.79691) / (38.79691 - 38.79477)

    The general form is:

    X = ImageLeft + ImageWidth * (lon - LonLeft) / (LonWidth)

    You may need to tweak this slightly depending if min values are at the top / left or bottom / left.