Basically, I want to know if there is a way to query all objects with a geographic field that are near a linestring in django using postgis. I am calculating a route and want to find objects that are approximately on that route (say max distance from the linestring 2m - 10m). I cannot find a way online that seems to solve this.
I can, of course, create an interval around the route of say 2m, and then create a polygon using these intervals and check which points fall within this created surface, but I wonder if there is a more direct approach (the way I described above).
This is some psu-code for my first described method (this i want)
def get_objects_on_route():
Model.objects.filter(geo_location__some_lookup=all_points_in_route_route, max_distance=2m)
other method which I could implement
def get_objects_on_route():
points_in_surface = []
for each element in route:
points_in_surface.append(two_corrected_points)
poly= Polygon(all_points_on_route)
Model.objects.filter(geo_location__covered_by=poly)
As it turns out, you can easily make this kind of query using Django's PostGIS filtering by creating a LineString object instead of a Point.
from django.contrib.gis.geos import LineString
line = LineString(*coords_in_route)
Model.object.filter(geo_location__distance_lte=(line, D(m=2)))
I tested it and seems to be working correctly:
Related
Say that we have the following polygon:
Now I'm looking for one of two results, where either problem will help me continue my current tasks. So I would just need one of the following two problems to be solved:
Calculate the center line of the polygon:
Now I have looked at this post already, however the library used is outdated and I haven't managed to make it work properly.
Split the polygon into separate rectangles that fill up the entire polygon, non-overlapping.
Note that this is a very simple example of the kind of polygon whose solution I would need. I would need the exact same for a much more complex polygon, say the following:
So as I said, I need one of these 2 problems solved. Problem 1 is just a way to generate a solution for problem 2. Having the polygon split into distinct rectangles is the final goal.
Are there any resources that could help with this?
(Also pardon my paint-skills)
I'm using GeoDjango to find all points within a Polygon but it seems to be using the Boundaries (NMW,NME,SME,SMW) to find points. So it brings back results that are outside of the primary shape.
polygon = Polygon((18.3825363358424 -33.97219070578159,...))
Order.objects.filter(location__within=polygon)
I would like the query to bring points inside the shape and not inside its bounds.
i.e if * was my shape; I'm getting points in # that seems to suggest that it's querying on the bounds instead of the actual shape.
**************
**************
******########
******########
******########
Please tell if I'm doing some wrong?
Ok looks like this is a MySQL Limitations
https://docs.djangoproject.com/en/2.2/ref/contrib/gis/db-api/#mysql-spatial-limitations
In other words, while spatial lookups such as contains are available in GeoDjango when using MySQL, the results returned are really equivalent to what would be returned when using bbcontains on a different spatial backend.
:( any other way of solving this problem
How do I establish the distance a point is from a polygon? If the point is within the polygon the result should be 0.
I'm guessing something like:
def distance_from(poly,point):
if poly.contains(point): return 0
return poly.exterior.distance(point)
I'd like to clarify things up a bit:
The instances which are passed to the distance_from function are made of the classes shapely.geometry.polygon.Polygon and shapely.geometry.point.Point from the shapely.geometry module of the Shapely Python package for computational geometry. (For more info on shapely and geometric objects have a look in the manual : Shapely Python Package, Geometric Objects).
The distance method of a geometric object returns the minimum float distance to another geometric object as described here in the manual. The exterior of a polygon is an instance of shapely.geometry.polygon.LinearRing which makes up the boundary of a polygon (although it is not to be mixed up with the boundary attribute of a polygon which returns a LineString). So for the case above you don't need to to explicitly use poly.exterior.distance(point) because the minimum distance is the same for the whole polygon as it is for its boundary. They have exactly the same shape only a different geometry type.
It would be less error prone if you would just use poly.distance(point) because that way if the point of interest lies within the interior (more info here) of the polygon it is directly clear that it contains it, so you do not need an explicit check and also the distance from a point within the interior to itself is then 0. It would be different if you would try to get the distance from the exterior (which, as explained above, is a LinearRing, i.e. a closed LineString) to a point which lies in the "inner" part. In this case you would get the minimum distance between the point in the "inner" part and the surrounding linear ring.
For further info about binary relationships (i.e between a geometric object and another, which covers contains, within, touches etc.) refer to this part of the manual and for those interested in the underlying DE-9IM relationships (which are a way to clearly identify the way a geometric objects relates to another) refer to this part.
Shapely is a python wrapper for the GEOS suite which itself is a C++ port of JTS. On the JTS page under the link Feature sheet more information can be found about how spatial relationships are checked and computed
I need to identify quickly to which polygons a set of points belong in Django 1.9.
First option is to loop through all polygons and check which points they contain:
for countrypolygon in countrypolygons:
placesinthecountry = Place.objects.filter(lnglat__intersects=countrypolygon.geom)
This takes a lot of time as I need to loop through a lot of polygons.
Is it possible to do the opposite, i.e. loop through each point and immediately get the polygons in which it is contained?
Yes, you can use contains:
for point in my_points:
polygons = MyModel.objects.filter(geom__contains=point.geom)
I create an android app which connect runners in closed proximity. I used a Tornado ServerWeb (Python) and a No-SQL database.
My solution:
Store all the {lon,lat} (regularly updated) of users in a DataLocation.
When a user want to see users around him, it calls specific function to my server which make a bounding box from his current position. The next step is to return the users of my DataLocation who are in his bounding box.
Is that a good way? Any advices? Is GeoJSON useful for me? How can I do that in Python?
If you don't have a library and want to do it yourself, you can calculate distance using the Great Circle Distance. The formula is not too complex. To find a group of points that are within a certain radius, you will need to query the database for points whose distance(as calculated by great circle distance) is less than your radius.
In addition, if you are wanting to get better speed, you will want to cache intermediate calculations, such as sines and cosines in another column, as that will speed things up, especially if you want to do the query at the database level.