How do I find the x and y position given a latitude and longitude coordinate?

1.3k Views Asked by At

I am building an app that allows me to track the user's position inside a building. For this I use GPS and an image of the floor plan.

I have the latitude and longitude of each of the four corners and I now want to work out what the x and y position is in the image based on the GPS coordinate I receive.

I have provided an image to make this a lot clearer:

Click here - don't have enough rep yet :(

I've tried many things so far, but this one came the closest:

Find the range between top left (min lat/lon) and bottom right (max lat/lon):

  • lonrange = |minLon - maxLon|
  • latrange = |minLat - maxLat|

Calculate the scale factors for pixel per degree:

  • scalex = width / lonrange
  • scaley = height / latrange

Now get pixel positions:

  • x = (currentLon - minLon) * scalex
  • y = (currentLat - minLat) * scaly

What I've done above is kinda accurate when the lat/lon point I'm trying to convert is somewhere directly between the topLeft and bottomRight of the image but gets wildly inaccurate the more I deviate towards the other corners.

I think it's probably because of how rotated my image is compared to the map but I don't know how to account for it (I don't even know how much it's rotated angle wise).

Any help would be appreciated!

Note: also I apologise if my tags are inaccurate! I'm not very well versed in terms of what is what in maths.

2

There are 2 best solutions below

0
On BEST ANSWER

In the end, I've found my own solution with the help of my university professor.

I've used this solution for the building I've described above, and as the distances I am working with are quite small, I'm not taking the Earth's curvature into account.

First, we define:

  • the top left corner of the image as $c(0,0)$, the top right as $c(width,0)$, bottom left as $c(0,height)$ and the user’s position $u(x,y)$;

  • the 3D points of each of the previously defined corners $C(0,0)$, $C(width,0)$ and $C(0,height)$, as well as the 3D point for the user’s location $U(x,y)$;

  • three 3D vectors $U$, $V$ and $W$. The vector $U$ is the vector from $C(0,0)$ to $C(0,height)$, the vector $V$ is the vector from $C(0,0)$ to $C(width,0)$ and the vector $W$ is the vector from $C(0,0)$ to the user’s position $U(x,y)$;

  • the distances $a1$ and $a2$ we multiply by the width and height of the image respectively to get our pixel coordinates.

Then, we outline the translation process:

  1. measure the latitude and longitude of each of the defined corners;

  2. convert each latitude and longitude into radians and calculate the 3D points $C(0,0)$, $C(width,0)$, $C(0,height)$ and $U(x,y)$, where $p$ and $P$ represent any points and 3D points respectively: $$ \begin{align*} P_x &= \cos Lat(p) \cdot \cos Lon(p)\\ P_y &= \cos Lat(p) \cdot \sin Lon(p)\\ P_z &= \sin Lat(p) \end{align*} $$

  3. calculate the vectors $U$, $V$ and $W$: $$ \begin{align*} U &= C_{(0, height)} - C_{(0, 0)}\\ V &= C_{(width, 0)} - C_{(0, 0)}\\ W &= U_{(x, y)} - C_{(0, 0)} \end{align*} $$ which leaves us with the following simultaneous equations to solve (as we are not using altitude, $z$ can be ignored): $$ \begin{align*} W_x &= a_1 \cdot U_x + a_2 \cdot V_x \\ W_y &= a_1 \cdot U_y + a_2 \cdot V_y \end{align*} $$

  4. solve the simultaneous equations using Cramer's rule with matrices and their determinants: $$ \begin{align*} D &= {\det \begin{bmatrix} U_x & V_x \\ U_y & V_y\end{bmatrix}} \qquad D_x &= \det \begin{bmatrix} W_x & V_x \\ W_y & V_y\end{bmatrix} \qquad D_y &= {\det \begin{bmatrix} U_x & W_x \\ U_y & W_y\end{bmatrix}} \end{align*} $$

  5. calculate your pixel coordinates: $$ \begin{align*} x &= width \cdot {D_x \over D} \\ y &= height \cdot {D_y \over D} \end{align*} $$

This is the solution I have used in my project to turn latitude and longitude into pixel coordinates.

Source: My dissertation.

3
On

Using $x$ and $y$ coordinates is a method to describe a position in a plain. However, the earth is not a plane. Therefore, you need to use a projection to project the points, described as lat-lng-values on a reference ellipsoid, to points on a map. Many different map projections are known, each of them with different properties. The first step is to choose one projection that suits your need. For example, you could use UTM projections.

After you projected your coordinates onto the map, all you have to do is to perform a rotation (which can be achieved by applying a $2\times 2$ rotation matrix to a point) and translation (which can be achieved by adding a vector).