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)
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)
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:
I have a list of several polygons.
Some are completely separated and non-intersecting with other polygons.
And some are fully intersecting and enclosed within other polygons:
I want to tesselate it to a set of triangles so I can draw them.
I think that I have a code that works - I'm doing something similar to that:
tess = gluNewTess()
gluTessBeginPolygon (tess)
for polygon in polygons:
gluTessBeginContour(tess)
for point in polygon:
gluTessVertex(tess, point, point)
gluTessEndPolygon(tess)
gluDeleteTess(tess)
I'm wondering if that is the expected way to go?
The main reason I'm asking the question is that as you can see - the entire code is wrapped in a single gluTessBeginPolygon-gluTessEndPolygon. But it is actually a set of different polygons, which seems a bit odd (although it seems to work...)
This is actually the way to go (see also this example).
The "polygon" defined is actually a multi-polygon here, so all that matter are the contours, which you are defining properly.
OpenGL then checks internally whether some contours are self-contained or otherwise complicated and generates the proper polygon/set of polygons --- you can have a look at the code in PolygonTesselator or at this page for a walkthrough.
I have a set of polygons and they can overlap with each other, like this:
I want to modify them in such a way that they don't overlap and the resulting surface area stays the same. Something like this:
It is okay if the shape or the position changes. The main thing is that they should not overlap with each other and the area should not change much (I know the area changed a little in the second image but I drew it manually thus let's just assume that the areas did not change).
I am trying to do it programmatically with the help of Python. Basically I stored polygons in a PostGIS database and with the help of a script I want to retrieve them and modify them.
I am very new to GIS and thus this seems like a difficult task.
What is the correct way of doing it? Is there an algorithm that solves this kind of problems?
Take a look at ST_buffer and try passing a signed float as the second argument (degrees to reduce radius by)
SELECT buffer(the_geom,-0.01) as geom
Be careful with negative buffers as you could run into issues if the buffer size exceeds the radius, see here.
Here is what I did:
Iterated over all the polygons and found overlapping polygons. Then, I moved the polygon in different directions and found the best moving direction by calculating the minimum resulting overlapping area. Then I simply moved the polygon in that best direction until there is no overlapping area.
I have a set of points with their coordinates (latitudes, longigutes), and I also have a region (a box). All the points are inside the box.
Now I want to distribute the points into small cells (rectangles) considering the density of points. Basically, a cell containing many points should be subdivided into smaller ones, while keeping larger cells with small number of points.
I have check this question, which has almost the same problem with me but I couldn't find a good answer. I think I should use a quad tree, but all implementations I found doesn't provide that.
For example, this library allows making a tree associated with a box, and then we can insert boxes as follows:
spindex = Index(bbox=(0, 0, 100, 100))
for item in items:
spindex.insert(item, item.bbox)
But it doens't allow inserting points. Moreover, I'll need to get the IDs of cells (or names), and check if a point belongs to a given cell.
Here is another lib I found. It does allow inserting points, but it doens't give me the cell's ID (or name), so I cannot check if a point belongs to a certain cell. Notice that the tree should do the decomposition automatically.
Could you please suggest me a solution?
Thanks.
Finally, I ended up using Google's s2-geometry-library with Python wrapper. In fact, each cell created by this lib is not a rectangle (it's a projection), but it satisfies my need. The lib already divided the earth surface into cells at different levels (quad tree). Given an point (lat,lng), I can easily get a corresponding cell at leaf level. From these leaf nodes, I go up and merge cells based on what I need (number of points in a cell).
This tutorial explains everything in details.
Here is my result: