I am developing a web page where users can create activities and others find them via a search function. When you create an activity you must specify the exact location where it will take place, assisted by google maps I retrieve the latitude and longitude. Now, when doing a search I want to have the functionality to find all activities close to a specified location(also assisted by google maps).
So I have a set of activities with coordinates, the coordinates of a point I want to find activities nearby, and I want to return activities that are no more than, lets say, 5 km(or miles or whatever you prefer) away from this point.
I am having this idea in my head that this can be solved by calculating max/min latitude and longitude, and use these as parameters in an sql-query where I use a where clause for filtering...The problem I'm facing here is firstly calculating these max/min values, secondly in an circular area(with radius 5km), and not a rectangular
Would appreciate any input here!
Thanks!:)
Coordinates you get are probably not x and y but latitude and longitude; you will need spherical distance unless all your points are within rather small radius, e.g. few hundred miles.
If you have many points, direct exhaustive search becomes too slow, spherical or not. Fortunately, GIS extensions available both for MySQL and for Postgres. Commercial DBs also have spatial extensions. These make searches for nearby objects efficient.
Calculate the boundary latitudes and longitudes.
Use the inverse http://en.wikipedia.org/wiki/Haversine_formula
Select everything where the latitude is between your two values for that, and similarly for longitude. If you're not using a spatial index, beware of edge cases on your sphere (a most excellent pun!): crossing 0, 90, or 180 degrees may result in impossible criteria.
Either in your SQL server or your app, execute the Haversine formula against your results. You must have the rectangular bounding values to prevent a table scan, but results in the rectangle will include results outside of your circle.
If you actually stop to think about it, your rectangle and your circle are both misshapen... but that's not really relevant anyway.
Also, check out this, which will expand on distance measuring and mention some other ideas: http://www.movable-type.co.uk/scripts/gis-faq-5.1.html
Hope this can help you.
Get nearest places on Google Maps, using MySQL spatial data
However this is MySQl query. You can convert it as your requirement.
Related
I have access to a list of lat/long coordinates, and I want to know (roughly) the US State these coordinates are located in. I can do with loss of precision, but I can't rely on external libraries or API. I can also add a database of locations in my code.
What is a reasonable way to do this?
I thought about 3 possibilities:
Represent each state by a single point at its center, then do a nearest-neighbour search
Represent each state by points located at cities in the state, then do a nearest-neighbour search (with much more points)
Represent each state by a simple bounding box, then use some algorithm to query which bounding box my point belongs to
What do you think is best? I would tend to think about solution 3, but I can't find a list of coarse "bounding boxes" for US states
I made a little search and find out a proper solution for what you are looking for with a dataset of bounding box.
Answer on StackOverflow: LINK
Dataset: LINK
Algorithm to use(implement): LINK
So yes, the proper way to implement it's using the solution 3 with the given dataset.
Hope it helps :)
Will not work, consider
Has a high likelihood to not work for at least some states. Consider states with towns/cities more clustered to the middle, against states with towns/cities clustered to the edge.
Will not work (these were supposed to be 90 degree angles, perfect squares, but drawing with a mouse is hard :) )
If you want to do this even vaguely accurately you will need some shape data which defines the boundaries between states. You will then need an algorithm which can determine whether a point is within an irregular polygon
See List of the United States (US) state boundaries / borders as latitude/longitude pairs for geofence?
I am Android developer.
I am getting the longitude and latitude point of a place but for the same place these values changes and the third decimal varies.
My main purpose is to detect a place so it works only
when all these values of longitude and latitude matches so they must
be constant.
My questions are:
What is the reason for this variation?
Is there any method by which ti make these values remain constant for a specific place?
The phone's GPS isn't very accurate. That is, it is very accurate for navigate in your car, but missing by 20 meters is not unheard of.
To figure out if you're in the same place, you should calculate the distance between your current location and the place's coordinates. If it's lower than a threshold (try to find one that makes sense) - you're there.
Try using the GPS for more accurate data, but know that they will not be the same values everytime, but not very different either
You can look at the accuracy as well. The Location class has a getAccuracy() method -- if your accuracy decreased from the last method, it may be that the person went inside and lost line-of-sight to the sky.
As I have said before in other posts, I recommend using Google's Play Location service rather than the pure GPS. You can also use an isBetterLocation method as documented here.
As stated above, the GPS inherent accuracy isn't the best. For an idea of how the decimal places show accuracy levels, see the Wikipedia page
In short, the third decimal place results in an accuracy differential of 43-111 meters, depending on your position on the globe. So I would either look at some other method of refining the data, or implementing some sort of threshold processing.
I am building an Android application where users can find nearest places according to their current location and also, be able to add places into the database. I understand that I will have to use long and lat in my database but I really don't know how to compare the current location against the places in the database. How would I even search it because I would potentially have to go through the entire database systematically pulling out each geo point and then comparing it against the user location?
Example:
Say users have entered 10 places in London and 1000 other places all around the world and a person in London is using the application and wants to find places that are nearest to them based on their current location. How would I search for these 10 places among 1000s of other places in the database? Performing distanceTo() a 1000 times is unnecessary.
Any advice, guides, tutorials, references would be great.
Thanks in advance.
EDIT:
This is my idea so far.
Get the longitude and latitude of the location, which could either have been searched, the current location or the location that the user has tapped on the screen.
With Long/Lat of said location - get the place description of the place from reverse geocoding with the getFromLocation method.
With place description, search database based on address string.
This is what I have right now but again this doesn't seem to be reliable. There must be a simpler way to query the database given the long/lat of a location and return back a complete list of the nearest spots.
If you are using latitude and longitude, this should be pretty simple. Let's say the coordinates of the individual are roughly: 49°N, 2°E.
To find all locations with 1 square degree of lat/long, you would just select all locations that satisfied the following criteria:
Latitude >= 48°N and <= 50°N
.. and ..
Longitude >= 1°E and <= 3°E
It's pretty simple.
There's a table on Wikipedia that explains how much decimal degrees are 1 kilometer etc. And the coordinates that you receive from Google are in decimal degrees.
https://en.wikipedia.org/wiki/Decimal_degrees
If you have any more questions on how to calculate etc, just ask :)
Basically you just need to check if the places latitude is higher than
currentLocationLatitude - rangeInDecimalDegrees
and smaller than
currentLocationLatitude + rangeInDecimalDegrees
I a working on GPS location where i need to convert latitude and longitude in corresponding address. I have a database of all latitude and longitude(more than 1,50,000 data) with its actual landmark i.e. address in one master table.
We have installed GPS device on many vehicles which is moving across all geographic location. Every 2 minute this device sends the actual position in term of latitude and longitude, i get this latitude and longitude and maps to our Master table and check near by which landmark from our master table. For doing this calculation i do some mathematical operation to add some delta value to latitude and longitude get from vehicle and then find the nearest search from my master table, once we find we show vehicle is 5 KM from XYZ location, we get XYZ location from master table which is nearest point.
This program basically takes lot of time to calculate the location, since we have 10,000 GPS devices installed on Vehicle and every device send GPS data in 2 minute, so you may imagine how much data we do get.
Could you please help me to fix this issue and make it very scalable and fast.
Thanks in advance.
How about using a GIS layer on your db? Something like PostGIS adds a new layer to sql with just that kind of functinality. From an FAQ:
3.7. What is the best way to find all objects within a radius of another object?
To use the database most efficiently, it is best to do radius queries
which combine the radius test with a bounding box test: the bounding
box test uses the spatial index, giving fast access to a subset of
data which the radius test is then applied to.
The ST_DWithin(geometry, geometry, distance) function is a handy way
of performing an indexed distance search. It works by creating a
search rectangle large enough to enclose the distance radius, then
performing an exact distance search on the indexed subset of results.
For example, to find all objects with 100 meters of POINT(1000 1000)
the following query would work well:
SELECT * FROM geotable WHERE ST_DWithin(geocolumn, 'POINT(1000
1000)', 100.0);
I have a set of points that i want to turn into a closed polygon in Java. I'm currently trying to use java.awt.geom.Point2D and java.awt.geom.Area but can't figure out how to turn a group of the points into an Area.
I think I can define a set of Line2Ds based on the points and then add those to the Areas, but that's a lot of work and I'm lazy. So is there an easier way to go.
The problem is I have a list of lat/lon coordinates and want to build up an area that I can use for hit testing.
Non-core Java libraries are a possibility as well.
Update, I looked at using java.awt.Polygon but it only supports ints and I'm operating with doubles for the coordinates.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4077518
Hear that, "customer"? You should be using GeneralPath, even though the absence of Polygon2D since the late 1990s is an obvious monster-truck-sized hole in the API.
If you are actually working with Geodetic lat/lon values, you can actually use OpenMap to do some of this work. I just spent some time using the Geo class in that API to bounce an object around an area defined by a polygon of lat/lon points. There are intersection calls and everything and all of the math is done spherically so that the points are more correct as far as projections go.
The simplest (and laziest) thing to do is to create a bounding box for the points from the maximum and minimum of the X, Y ordinate values.
If you need a closer fit then rather than devise your own algorithm, this might be a good place to start: