Make a condition if a polygon contains a point - python

I need to set up a condition using shapely that accuses whether or not a point belongs to a polygon.
If the value is true, a message "point belongs to polygon" should be displayed. If the point does not belong, then the message "the point does not belong to the polygon" should be displayed.
Also, if the point belongs to the polygon, it must be shown which polygon the point belongs to.
I tried to set up a condition using the "if" inside the "for" but it didn't work.

Related

How to find any point lying inside polyhedron

I am trying to identify whether a point lies inside or outside of a polyhedron. How would I do this in Python?

Distance outside shapely.Polygon

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

Identify which polygon contains a point with Django?

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)

Indexing in python quadtree

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:

obtaining boundary of polygon embedded in a matrix and other post-processing techniques

I have a 100x100 Boolean matrix called mat. All cells have false value except a continuous patch of polygonal area. I can read the cells belonging to this polygon by running through each cell of the matrix and finding true cells.
region_of_interest=false(size(mat));
for i=1:size(mat,1)
for j=1:size(mat,2)
if mat(i,j)
region_of_interest(i,j)=true;
end
end
end
Now I want to do further processing of this polygon like store only the boundary cells. How to do this? I tried visiting each polygonal cell and seeing if all its four neighbors are in the polygon or not. But this did not seem very efficient. Are there better algorithms out there?
If there are other post-processing methods that can be run in this scenario please suggest. Suggestions outside of Matlab are also welcome.
This is neither Python nor a convex-hull problem.
However, I would point out that boundary cells inside the polygon are true and have at least one neighbor which is false, and boundary cells outside the polygon are false and have at least one neighbor which is true.
It's up to you to decide whether you want inner or outer boundary cells, and what a "neighbor" is. For example, are the neighbors of a cell the four neighbors in cardinal directions, or the eight neighbors in diagonal directions too? If it's the former, then you'll still have to search through the eight neighbors to use the algorithm I describe below to get from cell 1 to cell 2 in cases like the following:
...XX
...2X
XX1XX
XXXXX
That being said, it would depend on what your data is like as to how you'd want to write the algorithm. If you know that there is a single contiguous block with no holes in it, which seems to be what your question implies, then once you find a boundary cell, you simply need to walk along the boundary until you find the first cell again. So search neighbors until you find the next boundary cell, and then do it again.
The problem is finding that first boundary cell.
One method would be just searching randomly until you find a cell within the patch, and, then walking in any direction until you find a boundary.
Another would be to do some sort of uniform search, perhaps in a grid, until you find that same first cell as described above. If you know that your patch size is always at least x-cells large, you could adjust your grid size based on that.
Without more specific information, these ideas should at least help you reach a solution. Good luck!

Categories