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.
Related
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
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).
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
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.
I am doing a mashup using Google Maps under Grails where users can create geofences by selecting a point on the map and a radius. This get stored on my database and the application receives constantly a set of coordinates from a GPS device.
I would like to compare the received coordinates with the area stored in the circles. If the point is inside (or outside) the circle the program will fire an action. However, I would like to know how I can find out if the coordinates are located inside/outside the circle. There is a Javascript library which allows doing this but I need to do this on the server.
Is there a Java (or even Groovy) library for this?
How would you implement it?
if distance from point to center of circle is <= radius of circle then it is inside the circle.
if the area is made of more than one circle than compare to all the circles... it won't take that long.
java.awt.geom.Point2D.Double is perfect for this.
Well, if it doesn't need to be "perfect", you don't need to worry about plotting circles or anything like that. You can just take the two locations (the location you want to test, and the center of the circle) and use Pythagorus to find the distance. If that distance is less than the radius of the circle, it's inside.
There is a caveat to take into consideration, however: the reason this wouldn't be perfect is that that for your points, you're probably going to get a latitude and longitude...and the Earth is a sphere. So near the poles of the Earth this will kind of fall apart. But it may well be good enough for what you're doing.
Sadly, most of the responses here won't work for you conveniently, because GPS coordinates are in units of degrees. You will need something to convert from two points in Degrees of latitude and longitude to a great circle distance, which simple Pythagorean theorem falls short of.
If you're using Google maps API, you can probably do everything you need using GLatLng. As other posters have noted, You can determine the distance between two points is less than the radius of the specified circle. Specifically GLatLng.distance(other:GLatLng) returns the meters distance between too GPS locations.
To actually display the circles requires a bit more finesse. You will need to create a GPolygon to draw the circumference of the circle. You can find a number of free JavaScript functions that can do this for you.
Victor and Beska have the correct answer. That is, if the distance between the point and the center is less than the radius, then it's in the circle.
For the great circle distance between two points, you can use GeoTools' GeodeticCalculator. In particular you set the point and radius using setStartingGeographicPoint and setDestinationGeographicPoint followed by calling getOrthodromicDistance which will return the distance.
You want to find the vector that is the distance between the selected coordinate and the center of the circle, then compute the square distance between the selected coordinate and the center of the circle by squaring the components of the vector and adding them together; if that scalar (the squared distance) is less than the square of the radius, the point is within the circle.
This method avoids having to take a square root, and is just as accurate as normal distance comparison.
One possibility is to calculate the distance from the centerpoint and compare it to the radius.
Depending on you application you may be have to take into account that the world is a sphere and not 2Dimensional. To calcualte a distance on earth you can use this formula.
Since you are using Google Maps and for geographical distances spherical geometry holds rather than euclidean geometry. However if it is relativley smaller distance like a parking lot etc. then you can use euclidean distance formula (http://en.wikipedia.org/wiki/Distance) to find out whether the point is inside or outside the circle.
I presume you know the coordinates of the circle's center C(xc, yc) and its radius, R. Then for a given point P(x1, y1) find the euclidean distance, D as
square-root((x1-xc)^2 + (y1-yc)^2)). If D > R, the point lies outside the circle. If D < R, the point lies inside the circle. If D = R, the point lies on the circumference of the circle.
In case you are doing your measurements over larger distances then you should rather look for Geodesics (please check this http://en.wikipedia.org/wiki/Great-circle_distance).
I hope it helps.
cheers