I am working app which need to find nearby distributors of particular product. As of now I have current locations latitude and longitude. Besides that I also have list of all product distributors with their respective coordinates. I am running query which gives me nearest 10 locations but for that it goes thorough every record in DB calculate distance between current and that particular location. It takes way too much time. Is there any other alternative that can I take ?
Can you not first narrow down the data set by creating a max and min long and lat (say within 10 miles of the current location). You can then query the data set by lat > minLat and lat < maxLax, etc. You could then sort them as you are proposing by calculating the actual distances on the reduced subset if you need too.
To avoid calculating distance on every location you could create a lat long box (lets say for 10 miles) using the max top left lat long (10 miles up and 10 miles left) and the max bottom right lat long (10 miles down and 10 miles right). Then your query will find lat longs in that box using >= and <= and then calculate the distance for each of those to filter out the locations in the corners which exceed 10 miles.
Another other option is to look into spatial indexing for SQLite.
You can narrow down list of location by creating rectangular buffer around your location, to filter locations that are nearby.
SELECT * FROM table t
WHERE t.lat<(lat+buff) AND t.long<(long+buff) AND t.lat>(lat-buff) AND t.long>(long-buff)
lat, long - your location, buff - some value you can adjust to match your app need (e.g. 100
feet, 1 mile, etc)
Then you can ran your distance calculation on returned records.
Related
I have a question about the use of postgreSQL/postGIS.
I would like to display markers on a map (stored in a database) which are some distance away from the user (coordinates given to the request).
The type of the field of the markers is POINT (I store lat/long).
The user position is detetermined by the Google Map API.
Here is the actual request :
SELECT * FROM geo_points WHERE ST_distance(ST_SetSRID(geo_points.coords::geometry,4326),ST_GeomFromEWKT('SRID=4326;POINT(45.0653944 4.859764599999996)')) > 65
I know (after some research on internet) that the function ST_distance gives me the distance in degree between markers and the user position and that I test the distance in km.
I think I have to use the function ST_tranform to transform the points in metric coordinates.
So my questions are :
- what is the SRID for France
- how can I make this dynamically for the entire world according to the user position ?
I also kow that the function ST_within exists and that could do this. But I anticipate the fact that later, I could need the distance.
Any help would be greatly appreciated
ps: there are maybe solutions in other post, but all the answers I have found during my researches were not really meeting my needs.
Firstly, pay attention to the axis order of coordinates used by PostGIS, it should be long/lat. Currently you are searching in Somalia. Swapping to the coordinates, you would be searching in France.
You can use a geodesic calculation with the geography type, or use geodesic functions like ST_Distance_Spheroid. With the geography type, you may want to use ST_DWithin for higher performance.
Here are geo_points 65 m away or less from the point of interest in France (not Somalia):
SELECT * FROM geo_points
WHERE ST_Distance_Spheroid(
ST_Transform(geo_points.coords::geometry, 4326),
ST_SetSRID(ST_MakePoint(4.859764599999996, 45.0653944), 4326),
'SPHEROID["WGS 84",6378137,298.257223563]') < 65.0;
However, it will be very slow, since it needs to find the distance to every geo_points, so only do this if you don't care about performance and have less than a few thousand points.
If you change and transform geo_points.coords to store lon/lat (WGS84) as a geography type:
SELECT * FROM geo_points
WHERE ST_DWithin(
geo_points::geography,
ST_SetSRID(ST_MakePoint(4.859764599999996, 45.0653944), 4326)::geography,
65.0);
I have a mySQL table with user name, latitude and longitude of the user. I would like to get a list of user who are inside the circle of a given latitude and longitude with given distance. For example my input Lat= 78.3232 and Long = 65.3234 and distance = 5 km. I would like to get the list of users who are inside 5km distance from the point 78.3232 and 65.3234. Is it possible to solve this with single query? Or can you give me a hint start solving this query? I am new to the geo based information.
google gives me this :
http://www.movable-type.co.uk/scripts/latlong-db.html
it gives you a good idea to start your work.. based on the distance between points (your circle radius)
imo, you need to define all points (surface) that are into this "circle", and check if your users are into this surface... in fact, a good way to do this would be to define ranges... for example :
lat : 72, long between 65 & 67
lat : 73, long between 64 & 69
... ...
if you do it with your precision xx.yyyy that will increase consequently the possibilities... it's just a mathematical challenge... Good luck!
I think a good place to start is with the Pythagorean theorem a^2 + b^2 = c^2 where a would = the difference in lat from point to Origin and b would = the difference in long. and c would equal distance.
that solves the math point.
I would need some helping doing the conversion from distance in lat and distance in long.
is the going to be a local, state, or a world wide program? that will create changes as differences in longitude get small as you reach the poles and larger as your reach the equator.
I have a problem with morphia. I want to query locations, which are in a specific radius arround a location.
But I am not sure how to do so. If I am using "near(x,y,r)", I get lots of results (up to limit) in order of distance. But they're results out of my radius as well.
If I am using "within(x,y,r)" then I do get the results I want just in the wrong order.
What can I use to get alle locations in that radius AND in order of distance?
I have around 800 geo co-ordinates in my iPhone app as a flat file. I am searching for an effective way to find an algorithm which will take the current user location, loop through all these 800 coordinates and pull only the coordinates which are in 10 miles vicinity. How effectively this can be done? Also please share links which will get me the basic understanding on the maths behind this.
Here is a link for a question where the final code of OP may help you understand the how to create locations from coordinates and how to compute the distance between them.
Here is how to create a location:
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
And here is how to find the distance between two locations:
CLLocationDistance distance = [locationA distanceFromLocation:locationB]; //CLLocationDistance is a double
However you don't have to sort the locations. Just loop through them and add the near locations to an array.
First, I think everyone agrees that to compute distance, you need to use the Haversine function.
Finding the closest point to a given point
If the search time is an issue (iterating over the 800 data points you mentioned) then how about a 2D hash? Simply load the dataset into buckets or regions based on lat/long - then, you won't have to search through the whole data set - only the possible buckets that may contain matches.
Good hash function for a 2d index
I need to identify coordinates for the range selected by User. I can get users current coordinates by coreLocation but I need to pick places of interest in user selected value range say 1 mile within his current location. Essentially I want to circle around 1 mile coordinates.
Any help/lead would really be appreciated, I am new to iPhone programming.