0

I have a position given in decimal degrees (x.xxxxxxxx and y.yyyyyyyy). I need to draw a rectangle around it. The center of the rectangle matches the position. The dimensions of the rectangle is given in meters and it has a rotation ranging from 0-360 degrees.

Question How can I calculate the four corners of the rectangle and return the result as four decimal degree values? Like arrayOf<LatLon> getRectangle(LatLon position, int rectWidthCm, int rectLengthCm, double rectRotation).

Example

  1. I have a position given in LatLon format with two two values: latitude and longitude. We will assume this location is precise.
  2. The main task is to draw a rectangle based on this position in a Google Maps chart. The rectangle can have any dimentions but let's use these in this example: Width = 0.9 meter and Length = 1.2 meters. Any heading may also be given so lets use this heading: 45. 0 Is north and going clockwise round (east = 90, south = 180 and west = 270). When the rectangle is pointing north it has the length in the north/south direction. Finally, the rectangle center should be equal to the given position.

Note: The project setup is an Android application with Kotlin support and a google maps chart. I am interested in a modern approach to this problem. Regarding precision loss it should at most be within centimeters.

B--rian
  • 5,578
  • 10
  • 38
  • 89
7heViking
  • 7,137
  • 11
  • 50
  • 94
  • Well, I don't really know how to start. I do ofcourse have the two coordinates - but from there on, no clue. – 7heViking May 13 '20 at 16:05
  • @B--rian I am not really sure about the map dates question. The position is received from a GPS device like the one in a phone or tablet. – 7heViking May 14 '20 at 07:40

1 Answers1

6

I understand that you are looking for a function geo_rect(x,y,w,h,a) with the following parameters

  • x is the longitude according to WGS84
  • y is the latitude
  • w is the width of the rectangle in meters
  • h is the height of the rectangle in meters
  • a is the angle to which the rectangle is turned from w being horizontal (meaning pointing exactly West to East). I suggest to allow values ranging within the open interval (-90°,90°) as this makes the math either to understand.

Your function getRectangle(LatLon position, int rectWidthCm, int rectLengthCm, double rectRotation) deliver all the required information, you need a small wrapper function which determines w, h, and a from rectWidthCm, rectLengthCm and rectRotation, with the latter being within [0°,360°).

The function geo_rect() will return an arrayOf<LatLon> of length four, namely the coordinates of all four corners, starting on the top left and then going clockwise. We will refer to the points as P_NE,P_NW,P_SE, and P_SW respectively.

Assumptions

In order to keep things mathematically feasible, we make some assumptions

  1. We assume that we can use as approximation that the rectangle is a plane, which is okay if w ~ h << r with r = 6378 km being the radius of the Earth.
  2. We further assume that the Earth is a ideal sphere rather than an ellipsoid or even more bumpy. For an accessible article on that issue, see e.g. Zachary C. Eilon's blog

Basic structure of the algorithm

The algorithm could be structured as follows:

  1. Determine the distance d from (x,y) to all four end points. Because of our first assumption we can use simple Euclidian geometry rather than intricate Spherical geometry. Pythagoras holds: d^2 = (w/2)^2 + (h/2)^2.

  2. We also need the four bearings, e.g. b_NW for the angle between the vector pointing to the North Pole and the vector pointing from (x,y) to point P_NW.

  3. Given the information (x,y,d,b_NW, b_NE, b_SW, b_SE) from the previous steps, we can now follow Get lat/long given current point, distance and bearing to calculate the position of all four points. This is the mathematically hard part where I suggest to use a well-established and tested library for.

  4. Last but not least, let us double-check whether the calculation went well by evaluating Great circle distances between some or all pairs of points. For instance d(P_NE,P_NW) should approximately be w, d(P_NW,P_SW) should approximately be h. Don't be surprised if there is actually a difference - this errors are due the assumptions we made. Normal GPS under usual conditions will anyhow not allow you to determine your position up to the centimeter, you will need DPGS for that.

Further reading

If you are looking for java implementations, I found e.g.

B--rian
  • 5,578
  • 10
  • 38
  • 89
  • Thanks for this. The assumptions are right but there is one problem though: Basic structure point 1. If I estimate that you have to add e.g 0.0001 per meter - it will only work when located in the same area, If you move further north or south the constant will be different as the coordinate system changes dimenstions around the globe. – 7heViking May 14 '20 at 08:59
  • Are you saying you need have to drop the local-plane-assumption? Well, then you have to go for full spherical geometry... What *exactly* do you mean by "located in the same area"? What range of `x` and `y` do you want to accept? I suggest something < 5km or so should be fine. – B--rian May 14 '20 at 09:08
  • 1
    It is fine to have the rectangle as a local plane as it never will be larger than 3 times 3 meters. But, one meter converted to decimal degrees is not the sam e at the equator as at the poles. It might be that there are a good example by using a library for this. It might also be that it is easier to convert the location to UTM locations, get the four points and convert back to LatLon. – 7heViking May 14 '20 at 09:11
  • @7heViking I restructured the algorithm by splitting up the first step into two, I hope it is clearer now. – B--rian May 14 '20 at 13:38
  • @7theViking. Please let me know what you think. I did update my answer, but I have not heard back from you whether it helped you. – B--rian May 19 '20 at 07:34
  • Thanks. It is more clear now. I will see if I can manage to solve it :) – 7heViking May 19 '20 at 07:40