How to get minimum distance from point to polygon using openmap? - java

Given a point coordinates(latitude/longitude) and an array of latitude/longitude representing a polygon. How do I get the distance from the point to the polygon, using OpenMap API?

Related

Finding geopoints within radius in Java

There is geopoint array, one point with longitude and latitude as the center and radius. It takes to choose only those ones which are within the radius. One more thing is that the points are close to prime meridian. Can you advise the solution for it or some java libraries?

Calculate meter offset based on decimal degree position

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
I have a position given in LatLon format with two two values: latitude and longitude. We will assume this location is precise.
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.
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
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.
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:
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.
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.
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.
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
At https://www.movable-type.co.uk/scripts/latlong-vectors.html you can experiment online to determine a destination point along a great-circle given the distance and bearing from a start point (in our case: the center of the rectangle).
Old, but amazingly documented and well tested tool kit for geo-applications in general are the https://www.generic-mapping-tools.org/ - you might want to look at the command gmtvector.
If you are looking for java implementations, I found e.g.
https://introcs.cs.princeton.edu/java/12types/GreatCircle.java.html on of many implementations for calculating great circle distances
Need a standalone Java library for performing spatial calculations on lat/lon data
Calculate point based on distance and direction

Calculating Haversine distance of two coordinates taking into consideration the altitude

Today in class wev'e been introduced to the haversine function. the describtion of the function goes like this : "This uses the ‘haversine’ formula to calculate the great-circle distance between two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills they fly over, of course!)."
The goal is to load a csv file that inclued lat,lon and alt of a point and calculate the distance of 2 coordinates on earth.
My question is: how do I integrate the haversine function with the altitude factor?

GPS coordinates to pixel

I need to create image (polygon) from GPS coordinates. I have coordinates like this:
(49.274633220,17.160206083),(49.276968797,17.162732143),(49.278188519,17.162391767),(49.279761626,17.161087954), ......
And I need to transform them to XY pixel points. Each pair of coordinates are vertex of created polygon.
File with all coordinates:
GPS.txt
and how the created polygon should look like:
Any idea how can I transform the coordinates? Thanks for all reply.
In all cases you need a transformation form lat,lon (spherical) to cartesian (x,y) coordinated.
If the polygon is not bigger than 100km you can use a simple Cyclindrical Equidistant Projection.
Otherwise, you may use a Mercator Projection. (Google Maps uses that too)
Are you sure the assignment says to create a graphic? Or is it just to read the text file and extract the pairs of coordinates? Because you can't create a graphic without defining a transformation. I would start by finding the max and min latitude and longitude values (making sure which is which!). Then just use a linear scale for the longitude so that your minimum longitude goes to px=0 and the maximum longitude goes to however wide you want your image to be. Then do the same for latitude - it'll look distorted but at least you'll see something to start with.
By the way, the graphic you pasted doesn't seem to correspond to the coordinates you gave. If it helps, yours look more like this red area.

How can I determine the perimeter of a polygon based on a collection of X,Y

I have a boolean[][] where the true indexes make up a solid polygon. With this information how can I
A. determine which blocks make up the perimeter, and
B. which points (in order) can draw the polygon using the polygon class.
I intend to use Polygon class to call .contains but the code I have currently gives me the polygon points out of order (as i am simply scanning from left to right, top to bottom). Any help on the subject is appreciated.
I should think that you would just need to determine the corner points and connect them. A convex hull algorithm may not work because the polygon may not be convex. For instance, the third polygon from the left (the green one) on the wikipedia page for polygons is not convex. Also for polygons that are already convex a convex hull algorithm will likely be overkill.
Unless I am mistaken there should be four types of points in your list of Boolean values based on the status of the point's neighbors. In a regular grid an element at index i,j should have 8 neighbors. Based on these 8 neighbors you should have four different types of points assuming that your polygon does not touch any of the edges of the region:
Interior Points - Points which have all 8 neighboring values set to true
Interior Edge Points - Points which have a value of true and have 5 out of 8 neighbors set to true. These are points which are on the far edge of a polygon but not at the intersection of two polygon edges
Polygon Edge Points - Points which have a value of true and have fewer than 5 out of 8 neighbors set to true. These points occurs are the corner points of the polygon.
Outside Points - Points which have a value of false
You can go through your Boolean values and pick out all of the edge points for the polygon (you will have to take extra care in the case when the polygon is touching an edge of the region, for instance there is a true value at the smallest row and the smallest column). Once you have the edge points you determine which points connect to which by trial and error. Attempt to connect two points and see if the edge is valid. If the edge is valid then it will have all true values on one side and all false values on the other up to a reasonable margin or error. If it does not have this property then it must not be an edge of the polygon.
You're looking for a Convex Hull algorithm.
You need a couple of algorithms
First: a way to translate your boolean[][] into a set of X-Y coordinates.
Second: a way to convert those points into a polygon (read up on Convex Hull algorithm)
Third: a way to calculate the perimeter of that polygon.
Now go fill in the blanks!
It doesn't sound like a convex hull algorithm will be sufficient since the polygon might be concave. What you want is a bitmap to vector algorithm. Check out this link for a description of such an algorithm: http://cardhouse.com/computer/vector.htm
Once you've got the polygon points you can just do a distance calculation on each segment.

Categories