Locations in a specific radius in order of distance - mongodb

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?

Related

How can I find all nodes around a point that are members of a way with a certain tag?

I would like to find all highway way member nodes in a certain radius. I cannot see how to do this without using intersection, however, that is not in the API. For example I have this:
[out:json];
way(around:25, 50.61193,-4.68711)["highway"];>->.a;
(node(around:25, 50.61193,-4.68711) - .a);
out;
Result set .a contains the nodes I want but also nodes outside the radius - potentially a large number if the ways are long. I can find all the nodes inside the radius I don't need, as returned by the complete query above. Now I can always perform a second around query and do the intersection of the two result sets outside of Overpass. Or I can do another difference:
[out:json];
way(around:25, 50.61193,-4.68711)["highway"];>->.a;
(node(around:25, 50.61193,-4.68711) - .a)->.b;
(node(around:25, 50.61193,-4.68711) - .b);
out;
This gives the result I want but can it be simplified? I'm certain I'm missing something here.
Indeed, your query can be simplified to an extent that we don't need any difference operator at all. I would recommend the following approach:
We first query for all nodes around a certain lat/lon position and a given radius.
Based on this set of nodes we determine all ways, which contain some of the previously found nodes (-> Hint: that's why we don't need any kind of intersection or difference!).
Using our set of highway ways we now look again for all nodes of those ways within a certain radius of our lat/lon position.
In Overpass QL this reads like:
[out:json];
node(around:25, 50.61193,-4.68711);
way(bn)[highway];
node(w)(around:25, 50.61193,-4.68711);
out;
Try it on Overpass Turbo

Get metric distance between two points via a PostgreSQL/PostGIS request

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);

Optimizing The Way To Know most nearby objects

I'm using Core Location with ios 6 for this.
Scenario:
I have the spacial coordinates of a sample of points. I save all those coords using core data.
As and when I am moving with my iphone, I need to detect if I am like 500m from any points in that sample.
Right now, I am looping through those points and calculating the distances of them from my current location. It does this frequently as the user's current location is changing.
But the thing is this will not be a good idea if I have like 100 points, 1000 points.etc
Question:
How can I optimize this, any hint?
Idea:
rasterize (grid) your objects and asign each object to a grid-object (clustering).
while moving, detect the grids intersecting your current position/radius.
get the most nearby object(s) inside those grids.
So you only need to calculate the distances of your grid-objects and the distances to the objects inside the grid-objects nearby your position.

Find coordinates nearest to my current GPS location

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

Need help with finding nearby location

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.