Finding all points a distance from particular lat long java - java

I have a csv file with a bunch of lat long coordinates. I also have a csv file with a bunch of positions that a particular person will be standing at. For each of the points in the second file, I need to figure out whether they are near (under 1 mile) any of the points in the first file. I have about 500 points in each of the files.
I'm trying to solve this in Java, and I thought I would use something along the lines of read the first file in and putting it in some sort of structure that is easily searchable, this way I do not need to keep doing IO operations. I'm unclear as to what type of data structure I should hold the points in so that I can easily search for ones that are within a radius of a given point. Could someone point me in the right direction? Is there any way that I can organize this so that I don't need to make n^2 comparisons?

Sounds like you want to store your points in a k-d tree based on latitude and longitude.
If we know that we want all points within some set distance D of some point (lat, lon), it's simple to calculate the difference in latitude d_lat corresponding to D units of distance due north/south, and the difference in longitude d_lon corresponding to D units of distance due east/west at whichever of latitudes lat-d_lat or lat+d_lat is closest to a pole. Using this we perform an orthogonal range search in the tree for all points with latitude between lat-d_lat and lat+d_lat and longitude between lon-d_lon and lon+d_lon. We then need to calculate the distance for each of these, and reject ones over D away from (lat, lon) - but we won't need to do as many calculations as without the tree (we should only end up rejecting roughly 1-pi/4 = 21.5% of points that get to this stage).
Of course you'll need to account for edge cases, if they're relevant to you:
If you're within d_lon of 180 degrees longitude, you'll need to do two different searches in the tree (either side of 180 degrees).
If (lat, lon) is within d_lat latitude of a pole, just look for everything north/south of whichever of lat-d_lat or lat+d_lat is farthest from the pole.

Here's what I'd do.
Sort all the points in both files in order of latitude. Then iterate through both lists at the same time, so that for each point in file 1, you get a list of points in file 2 whose latitude circle is within one mile of the point from file 1. You can probably use the subList method of List somewhere along the way here.
Still within the context of the point from file 1, filter out the points from that sublist whose longitude differs from point 1 by more than one mile. You'll then have pairs of points that are both within a mile's longitude and within a mile's latitude of each other.
For each such pair, do the exact calculation to see if they really are within a mile "real distance" of each other.

The easiest way is to define a coarse grid and bucketize your points from the first list into the grid cells. You need to compute a cell "id" for each point and put in into a hash table based on that id.
Once you have that, you can easily lookup nearby points for a given lat/long by finding the right cell and enumerating its content (and the content of the neighboring cells).
The trick is to convert a lat/long into a cell id. One way is to round up the lat/long. So, for example, convert (47.43402067, -121.89068567) pair into a "47_-121" string. This can be too rough because one degree is approximately 70 mile at equator. You can tighten it up by rounding to a certain decimal point:
e.g. "47.43_-122.89".
Note that cells width is going to narrow down as you going further north or south. For example at 60 degrees North the cell is going to be two times more narrow than at equator (it will only cover 35 miles).
You can also use existing geospatial indexes from libraries like JTS Topology Suite which allow much more flexibility.

Related

create polygon for a route using latitude and longitude

I am having a predefined route as a set of locations with latitude and longitude. I want to create a polygon by using these co-ordinates and want to know when the user deviates from it.
Anyone have any tips or sample code to draw a virtual fence across the points A, B and C ?
I don't know if polygon is the way to do it...
I can suggest a more basic approach, where you compute the distance of point to a line segment and check for distance < fence radius
you can compute line segments of your route, in your case the segments are (A,B) (B,C)
when you got a new position and want to know if it reside within the fence, you just compute the distance of that position to each line segments
the calculation of this is explained (very clearly, with code examples) in here
the math of geo position is pretty straight forward when dealing with small areas (don't need to take the earth curvature into consideration) but even if you do, it's a small change and there a lot of code examples for that either

How to select a region of GPS coordinates from multiple tracks?

I have multiple GPS tracks(vector of 2d latitude, longitude coordinates) that I have created using my mobile device. They have different lengths and directions. I want to average this tracks and create just a single one. As a first step I would like to select only the points that are in a certain area. For example in the image bellow I want to select only the points that are between the grey lines.
Given the fact that the tracks might have different shapes and positioning would a bounding rectangle approach make sense? Are there better algorithms to do this?
I would suggest taking a look into these classes for practical use :
https://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/stat/clustering/DBSCANClusterer.html
http://commons.apache.org/proper/commons-math/userguide/filter.html
I would try clustering points from multiple tracks. After that, I would use the center point of each cluster to get my average path.
For clustering, you can use kNN or any other principle where you group points that are near each other.
After getting your average path, you can apply bounds to it (you could also filter your points before clustering).

How to calculate distance between 2 longitudes at any coordinate? Can we assume earth to be a sphere?

I thought that this one would be easy to find on net but seems its not.
I want to know the distance between 2 consecutive longitudes at a given latitude value. That is to measure with a rope keeping it parallel to equator. I can calculate it by simple geometry assuming earth to be a proper sphere. But will that assumption be acceptable?Please give an idea about the error margin in that assumption. Or,
Please let me know a mathematical formula or a Java/Android code to calculate it.
Thanks.
EDIT: Thanks for your responses, it seems to have erupted a mini storm . Please read my comment to David below for clarity
EDIT2: The debate is will the great circle distance (found by haversine formula) and the "rope" distance be the same? According to me it should be. As the great circle distance is the shortest distance between to 2 points, and in the case of rope distance i restrict myself to move along the same latitude from one point to another, which I think is the shortest distance. Isnt it?
EDIT3: I was wrong. After visualizing it a bit I realize that the great circle distance will not be the same as the longitudinal separation at a latitude ("rope" distance"). Both would be same only if the latitude happens to be the equator. What David has been saying. So yes no correct answers yet. For my case I would assume the earth to be a sphere, I would accept Laune's answer in some time as he/she has partially answered my question. Still would be really keen to know if there is way to get a correct calculation.Also, please go through the link given by Byzantine Failure (wats with the name??) It talks about how to create a stored procedure and and query for such problems, really helpful!! Thanks for all your responses!
Have a look at this: http://en.wikipedia.org/wiki/Geoid
For everyday purposes, you can use the spheric model. Geoid deformation starts in the higher latitudes, so unless you are into something very accurate, scientific or polar travel, you should be fine.
quite simple with android actually:
Location locationA = new Location();
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location();
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
The distance usually used in situation of geolocations is haversine distance. This is basically an approach of the real distance in earth's surface since it is the distance of two points lying on a sphere having the average radius of earth.
Anyway there are many implementations in Java if you search for them.
Edit:
In this case there isn't any real difference if the two points have some of their coordinates the same or not (if they have them both different).
You can check the following link for a detailed algorithm
http://www.arubin.org/files/geo_search.pdf
Although this is based on JavaScript, but there is actually a function of the API to calculate the distance between the two given lat long points: https://developers.google.com/maps/documentation/javascript/reference#spherical

Geo-fencing in java web application

I am writing a server side java application related to Geo-fencing.
I have a store with their latitude,longitude and radius
I also have Person's current position(in terms of latitude & longitude).
If he arrives within the geo fence i.e., within the store's radius i have to give an alert. I googled for the solution & there are some methods to identify person is inside the fence(circle) or outside the fence.
Calculate distance between store latitude,longitude and person's current latitude & longitude and if Distance < radius then person is inside the fence else outside the fence.
Using Polygon Geofencing
Can anyone suggest me which method is best from the above.
Thanks in advance
It depends on your needs. If you want to know whether a Person is inside 1km radius of store, then you need a circle search. Here, location of the store is the pivot. You might change the radius based on your need.
Polygon search is used when you need to search people in a particular area regardless of how much distance it is away from anywhere. People in Paris, New York or Istanbul area, not people inside 1km radius of a store.
In your case, where only radius (i.e. distance to the shop) is needed, I would definitely choose the radius search (your first proposition).
The Polygon Geofencing, I think, is using the Ray-Casting Algrithm which iterates over each segment of the polygon. This will take more time and could lead to some approximation in the case of a circle (depending on how the algorithm is applied).
I would have thought that the best method to use here would be a radius distance calculation, presuming you need to know the distance 'as the crow flies' so to speak. So geographical distance rather than travelling distance. This will be the most performant and simplest to implement. Examples available here and here.
If the latter is the case (which I hope for your sake it isn't), then I would imagine you would need to use polygon geofencing or perhaps Google's distance Matrix API.
Thanks for all your answers. In my project i have integrated both methods(Polygon & Distance). If a user enters into the polygon he ll get a notification and same like radius. I have stored the polygon co-ordinates and radius into my database.

Mapping real world cities and calculating distance Java

I need to be able to map out in coordinates real world cities and then have a method that calculated the distance between two cities that I give it... What would be the best way to do this? just citie object that has coordinates and then ill figure out the fomula for calculating the distance my self or a hash map or something... another thing is how am i gonna figure out the coordinates of the cities? Im talking about real world cities so yeah...
i had an idea of getting the latitude and longitude of the cities and then somehow when calculating distance turn it into kilo meters... If anyone can help my if this I would be glad!
Thank you very much!
If you need more info just tell me and ill add it.
What I mainly need: A suggestion to based on what map the cities and a lead on implementing distance calculations.. I would not like to check out the distance between each city i make to all the rest online then put it in because it seams to be a lot of work and if i manage to figure out a algorithm then it would be much faster and easier to add cities to the game...
First you need to translate city name to geographical coordinates. GeoNames is a big database of geographical locations including coordinates out of many others available.
When having geographical coordinates of two cities, you can use simple equation to calculate the distance, see How to find distance from the latitude and longitude of two locations? and Great-circle distance.
one part is GREAT CIRCLE DISTANCE - there are lots of examples.
finding a data set of city coordinates may be trickier.

Categories

Resources