Quickly find all polygons that overlap with two or more polygons in Shapely Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to find the intersection areas of overlapping buffer zones in single shapefile?Find and list all polygons that overlap with another polygonHow to convert small overlaping polygon features to inner ring in QGISShapely (geos) crashes during unary_unionDissolving polygons based on attributes with Python (shapely, fiona)?Summing attribute values for areas where multiple polygons overlap using QGIS?ArcGIS find all polygons that intersect with more that one polygon from another layerClipping algorithm that can make one layer with no overlapping polygons from N polygonsMultipolygon created from Scratch LayerWhen does Shapely's “polygonize_full” detect a dangle?Cannot find polygons that are inside a big polygon using GeoPandas
Is there a kind of relay only consumes power when switching?
Project Euler #1 in C++
Is there hard evidence that the grant peer review system performs significantly better than random?
Can the Great Weapon Master feat's "Power Attack" apply to attacks from the Spiritual Weapon spell?
Did Deadpool rescue all of the X-Force?
How fail-safe is nr as stop bytes?
How can I reduce the gap between left and right of cdot with a macro?
The code below, is it ill-formed NDR or is it well formed?
How do I use the new nonlinear finite element in Mathematica 12 for this equation?
Dating a Former Employee
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
What is the topology associated with the algebras for the ultrafilter monad?
How often does castling occur in grandmaster games?
Why wasn't DOSKEY integrated with COMMAND.COM?
Why do we need to use the builder design pattern when we can do the same thing with setters?
What's the meaning of "fortified infraction restraint"?
How come Sam didn't become Lord of Horn Hill?
AppleTVs create a chatty alternate WiFi network
Question about debouncing - delay of state change
What would you call this weird metallic apparatus that allows you to lift people?
What is a fractional matching?
Why is it faster to reheat something than it is to cook it?
How to react to hostile behavior from a senior developer?
Can anything be seen from the center of the Boötes void? How dark would it be?
Quickly find all polygons that overlap with two or more polygons in Shapely
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to find the intersection areas of overlapping buffer zones in single shapefile?Find and list all polygons that overlap with another polygonHow to convert small overlaping polygon features to inner ring in QGISShapely (geos) crashes during unary_unionDissolving polygons based on attributes with Python (shapely, fiona)?Summing attribute values for areas where multiple polygons overlap using QGIS?ArcGIS find all polygons that intersect with more that one polygon from another layerClipping algorithm that can make one layer with no overlapping polygons from N polygonsMultipolygon created from Scratch LayerWhen does Shapely's “polygonize_full” detect a dangle?Cannot find polygons that are inside a big polygon using GeoPandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a set of Polygons (in Shapely) that I wish to find all intersections that are comprised of overlaps of two or more polygons. I have been working based off the code found in this answer. I have a code so far that works in the if 1: statement below, but it takes a very long time to run. The code that is in the if 0: statement, runs quickly, but produces an output with some errors. See below for an example of the error.
from shapely.geometry import Point, LineString, shape, mapping
from shapely.ops import cascaded_union
import fiona
import shapely
from shapely.ops import unary_union, polygonize
from osgeo import ogr
from shapely.strtree import STRtree
with fiona.open("Untitled.shp") as layer:
subset=layer
spoof=[]
for n,pol in enumerate(subset):
if type(shape(pol['geometry'])) == shapely.geometry.polygon.Polygon:
spoof.append(shape(pol['geometry']))
elif type(shape(pol['geometry'])) == shapely.geometry.multipolygon.MultiPolygon:
print('theres one here')
tmp=list(shape(pol['geometry']))
for x in tmp:
spoof.append(x)
rings = [LineString(list(pol.exterior.coords)) for pol in spoof]
union = unary_union(rings)
tree = STRtree(spoof)
result = [geom for geom in polygonize(union)]
final=[]
if 1:
for k,item1 in enumerate(result):
count=0
for n,item2 in enumerate(spoof):
if item1.intersection(item2).area >1e-8:
count=count+1
if count>=2:
final.append(item1)
if 0:
for k,item in enumerate(result):
touch=tree.query(item.centroid)
if len(touch)>=2:
final.append(item)
Below, the orange represents all the shapes that I would like to check if they are produced (by unary union in Shapely) by two or more overlaps, the cyan represents the output of the if-statement that works (if 1 in above code), and the pink represents the incorrect selected polygons by the if-statement that doesn't work (if 0 in above code). The top left corners of the text boxes in the pictures represent that point being selected, and as you can see the second picture, the pink area only has one polygon from the STEREO_OBSERVATIONS (which is the set of polygons that are being checked for overlap) so it should not be selected. By contrast, the left adjacent cyan square to the pink square highlighted in the third picture has two polygons from STEREO_OBSERVATIONS and as such is correctly selected by the cyan result. The layering of the polygon layers is shown in the first picture.
What is the best way to query the STRtree structure so as to check to intersection or overlap? I have tried to use the centroid in the code, but that hasn't seemed to produce the correct output.
Fig. 1 (Legend/layering)
Fig 2. (Incorrect selection by fast algorithm [if 0 in code] in pink)
Fig 3. (Correct selection by slow algorithm [if 1 in code]
python polygon shapely fiona
add a comment |
I have a set of Polygons (in Shapely) that I wish to find all intersections that are comprised of overlaps of two or more polygons. I have been working based off the code found in this answer. I have a code so far that works in the if 1: statement below, but it takes a very long time to run. The code that is in the if 0: statement, runs quickly, but produces an output with some errors. See below for an example of the error.
from shapely.geometry import Point, LineString, shape, mapping
from shapely.ops import cascaded_union
import fiona
import shapely
from shapely.ops import unary_union, polygonize
from osgeo import ogr
from shapely.strtree import STRtree
with fiona.open("Untitled.shp") as layer:
subset=layer
spoof=[]
for n,pol in enumerate(subset):
if type(shape(pol['geometry'])) == shapely.geometry.polygon.Polygon:
spoof.append(shape(pol['geometry']))
elif type(shape(pol['geometry'])) == shapely.geometry.multipolygon.MultiPolygon:
print('theres one here')
tmp=list(shape(pol['geometry']))
for x in tmp:
spoof.append(x)
rings = [LineString(list(pol.exterior.coords)) for pol in spoof]
union = unary_union(rings)
tree = STRtree(spoof)
result = [geom for geom in polygonize(union)]
final=[]
if 1:
for k,item1 in enumerate(result):
count=0
for n,item2 in enumerate(spoof):
if item1.intersection(item2).area >1e-8:
count=count+1
if count>=2:
final.append(item1)
if 0:
for k,item in enumerate(result):
touch=tree.query(item.centroid)
if len(touch)>=2:
final.append(item)
Below, the orange represents all the shapes that I would like to check if they are produced (by unary union in Shapely) by two or more overlaps, the cyan represents the output of the if-statement that works (if 1 in above code), and the pink represents the incorrect selected polygons by the if-statement that doesn't work (if 0 in above code). The top left corners of the text boxes in the pictures represent that point being selected, and as you can see the second picture, the pink area only has one polygon from the STEREO_OBSERVATIONS (which is the set of polygons that are being checked for overlap) so it should not be selected. By contrast, the left adjacent cyan square to the pink square highlighted in the third picture has two polygons from STEREO_OBSERVATIONS and as such is correctly selected by the cyan result. The layering of the polygon layers is shown in the first picture.
What is the best way to query the STRtree structure so as to check to intersection or overlap? I have tried to use the centroid in the code, but that hasn't seemed to produce the correct output.
Fig. 1 (Legend/layering)
Fig 2. (Incorrect selection by fast algorithm [if 0 in code] in pink)
Fig 3. (Correct selection by slow algorithm [if 1 in code]
python polygon shapely fiona
I added the imports, what is the best way to share a simple dataset?
– Loonuh
Apr 11 at 17:39
Do you care when many times something intersected? If you only care that it intersected at least twice, you could check the count value in every indented for in where for n,item2 in enumerate(spoof):, then break out of the loop if the count >= 2, rather than continuing to check every shape
– saph_top
Apr 11 at 20:51
Select a small number of polys that demonstrate the issue (incorrect results with 0 and correct with 1), export selected to new shapefile (delete all uneeded fields) and convert to geojson (i.e. with mapshaper.org) and include (formatted as code) in your question.
– user2856
Apr 11 at 21:22
add a comment |
I have a set of Polygons (in Shapely) that I wish to find all intersections that are comprised of overlaps of two or more polygons. I have been working based off the code found in this answer. I have a code so far that works in the if 1: statement below, but it takes a very long time to run. The code that is in the if 0: statement, runs quickly, but produces an output with some errors. See below for an example of the error.
from shapely.geometry import Point, LineString, shape, mapping
from shapely.ops import cascaded_union
import fiona
import shapely
from shapely.ops import unary_union, polygonize
from osgeo import ogr
from shapely.strtree import STRtree
with fiona.open("Untitled.shp") as layer:
subset=layer
spoof=[]
for n,pol in enumerate(subset):
if type(shape(pol['geometry'])) == shapely.geometry.polygon.Polygon:
spoof.append(shape(pol['geometry']))
elif type(shape(pol['geometry'])) == shapely.geometry.multipolygon.MultiPolygon:
print('theres one here')
tmp=list(shape(pol['geometry']))
for x in tmp:
spoof.append(x)
rings = [LineString(list(pol.exterior.coords)) for pol in spoof]
union = unary_union(rings)
tree = STRtree(spoof)
result = [geom for geom in polygonize(union)]
final=[]
if 1:
for k,item1 in enumerate(result):
count=0
for n,item2 in enumerate(spoof):
if item1.intersection(item2).area >1e-8:
count=count+1
if count>=2:
final.append(item1)
if 0:
for k,item in enumerate(result):
touch=tree.query(item.centroid)
if len(touch)>=2:
final.append(item)
Below, the orange represents all the shapes that I would like to check if they are produced (by unary union in Shapely) by two or more overlaps, the cyan represents the output of the if-statement that works (if 1 in above code), and the pink represents the incorrect selected polygons by the if-statement that doesn't work (if 0 in above code). The top left corners of the text boxes in the pictures represent that point being selected, and as you can see the second picture, the pink area only has one polygon from the STEREO_OBSERVATIONS (which is the set of polygons that are being checked for overlap) so it should not be selected. By contrast, the left adjacent cyan square to the pink square highlighted in the third picture has two polygons from STEREO_OBSERVATIONS and as such is correctly selected by the cyan result. The layering of the polygon layers is shown in the first picture.
What is the best way to query the STRtree structure so as to check to intersection or overlap? I have tried to use the centroid in the code, but that hasn't seemed to produce the correct output.
Fig. 1 (Legend/layering)
Fig 2. (Incorrect selection by fast algorithm [if 0 in code] in pink)
Fig 3. (Correct selection by slow algorithm [if 1 in code]
python polygon shapely fiona
I have a set of Polygons (in Shapely) that I wish to find all intersections that are comprised of overlaps of two or more polygons. I have been working based off the code found in this answer. I have a code so far that works in the if 1: statement below, but it takes a very long time to run. The code that is in the if 0: statement, runs quickly, but produces an output with some errors. See below for an example of the error.
from shapely.geometry import Point, LineString, shape, mapping
from shapely.ops import cascaded_union
import fiona
import shapely
from shapely.ops import unary_union, polygonize
from osgeo import ogr
from shapely.strtree import STRtree
with fiona.open("Untitled.shp") as layer:
subset=layer
spoof=[]
for n,pol in enumerate(subset):
if type(shape(pol['geometry'])) == shapely.geometry.polygon.Polygon:
spoof.append(shape(pol['geometry']))
elif type(shape(pol['geometry'])) == shapely.geometry.multipolygon.MultiPolygon:
print('theres one here')
tmp=list(shape(pol['geometry']))
for x in tmp:
spoof.append(x)
rings = [LineString(list(pol.exterior.coords)) for pol in spoof]
union = unary_union(rings)
tree = STRtree(spoof)
result = [geom for geom in polygonize(union)]
final=[]
if 1:
for k,item1 in enumerate(result):
count=0
for n,item2 in enumerate(spoof):
if item1.intersection(item2).area >1e-8:
count=count+1
if count>=2:
final.append(item1)
if 0:
for k,item in enumerate(result):
touch=tree.query(item.centroid)
if len(touch)>=2:
final.append(item)
Below, the orange represents all the shapes that I would like to check if they are produced (by unary union in Shapely) by two or more overlaps, the cyan represents the output of the if-statement that works (if 1 in above code), and the pink represents the incorrect selected polygons by the if-statement that doesn't work (if 0 in above code). The top left corners of the text boxes in the pictures represent that point being selected, and as you can see the second picture, the pink area only has one polygon from the STEREO_OBSERVATIONS (which is the set of polygons that are being checked for overlap) so it should not be selected. By contrast, the left adjacent cyan square to the pink square highlighted in the third picture has two polygons from STEREO_OBSERVATIONS and as such is correctly selected by the cyan result. The layering of the polygon layers is shown in the first picture.
What is the best way to query the STRtree structure so as to check to intersection or overlap? I have tried to use the centroid in the code, but that hasn't seemed to produce the correct output.
Fig. 1 (Legend/layering)
Fig 2. (Incorrect selection by fast algorithm [if 0 in code] in pink)
Fig 3. (Correct selection by slow algorithm [if 1 in code]
python polygon shapely fiona
python polygon shapely fiona
edited Apr 11 at 17:39
Loonuh
asked Apr 10 at 21:44
LoonuhLoonuh
1164
1164
I added the imports, what is the best way to share a simple dataset?
– Loonuh
Apr 11 at 17:39
Do you care when many times something intersected? If you only care that it intersected at least twice, you could check the count value in every indented for in where for n,item2 in enumerate(spoof):, then break out of the loop if the count >= 2, rather than continuing to check every shape
– saph_top
Apr 11 at 20:51
Select a small number of polys that demonstrate the issue (incorrect results with 0 and correct with 1), export selected to new shapefile (delete all uneeded fields) and convert to geojson (i.e. with mapshaper.org) and include (formatted as code) in your question.
– user2856
Apr 11 at 21:22
add a comment |
I added the imports, what is the best way to share a simple dataset?
– Loonuh
Apr 11 at 17:39
Do you care when many times something intersected? If you only care that it intersected at least twice, you could check the count value in every indented for in where for n,item2 in enumerate(spoof):, then break out of the loop if the count >= 2, rather than continuing to check every shape
– saph_top
Apr 11 at 20:51
Select a small number of polys that demonstrate the issue (incorrect results with 0 and correct with 1), export selected to new shapefile (delete all uneeded fields) and convert to geojson (i.e. with mapshaper.org) and include (formatted as code) in your question.
– user2856
Apr 11 at 21:22
I added the imports, what is the best way to share a simple dataset?
– Loonuh
Apr 11 at 17:39
I added the imports, what is the best way to share a simple dataset?
– Loonuh
Apr 11 at 17:39
Do you care when many times something intersected? If you only care that it intersected at least twice, you could check the count value in every indented for in where for n,item2 in enumerate(spoof):, then break out of the loop if the count >= 2, rather than continuing to check every shape
– saph_top
Apr 11 at 20:51
Do you care when many times something intersected? If you only care that it intersected at least twice, you could check the count value in every indented for in where for n,item2 in enumerate(spoof):, then break out of the loop if the count >= 2, rather than continuing to check every shape
– saph_top
Apr 11 at 20:51
Select a small number of polys that demonstrate the issue (incorrect results with 0 and correct with 1), export selected to new shapefile (delete all uneeded fields) and convert to geojson (i.e. with mapshaper.org) and include (formatted as code) in your question.
– user2856
Apr 11 at 21:22
Select a small number of polys that demonstrate the issue (incorrect results with 0 and correct with 1), export selected to new shapefile (delete all uneeded fields) and convert to geojson (i.e. with mapshaper.org) and include (formatted as code) in your question.
– user2856
Apr 11 at 21:22
add a comment |
1 Answer
1
active
oldest
votes
The solution ended up being to change the if-statement to:
if 1:
for k,item in enumerate(result):
print(len(result),k)
touch=tree.query(item.centroid)
inside=[]
for x in touch:
if x.contains(item.centroid):
inside.append(item)
if len(inside)>=2:
final.append(item)
The reason for the original error was that the tree.query(<>) only returns the polygons that are likely to interact with the query <> object based on the bounds (top, bottom, left, right) of the objects inside of the tree. The solution was to check whether or not the centroid of the polygon was contained within each member of the list generated by the query. In some cases, the objects returned by the query were in the vicinity of the query object but did not in fact contain it.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f318431%2fquickly-find-all-polygons-that-overlap-with-two-or-more-polygons-in-shapely%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution ended up being to change the if-statement to:
if 1:
for k,item in enumerate(result):
print(len(result),k)
touch=tree.query(item.centroid)
inside=[]
for x in touch:
if x.contains(item.centroid):
inside.append(item)
if len(inside)>=2:
final.append(item)
The reason for the original error was that the tree.query(<>) only returns the polygons that are likely to interact with the query <> object based on the bounds (top, bottom, left, right) of the objects inside of the tree. The solution was to check whether or not the centroid of the polygon was contained within each member of the list generated by the query. In some cases, the objects returned by the query were in the vicinity of the query object but did not in fact contain it.
add a comment |
The solution ended up being to change the if-statement to:
if 1:
for k,item in enumerate(result):
print(len(result),k)
touch=tree.query(item.centroid)
inside=[]
for x in touch:
if x.contains(item.centroid):
inside.append(item)
if len(inside)>=2:
final.append(item)
The reason for the original error was that the tree.query(<>) only returns the polygons that are likely to interact with the query <> object based on the bounds (top, bottom, left, right) of the objects inside of the tree. The solution was to check whether or not the centroid of the polygon was contained within each member of the list generated by the query. In some cases, the objects returned by the query were in the vicinity of the query object but did not in fact contain it.
add a comment |
The solution ended up being to change the if-statement to:
if 1:
for k,item in enumerate(result):
print(len(result),k)
touch=tree.query(item.centroid)
inside=[]
for x in touch:
if x.contains(item.centroid):
inside.append(item)
if len(inside)>=2:
final.append(item)
The reason for the original error was that the tree.query(<>) only returns the polygons that are likely to interact with the query <> object based on the bounds (top, bottom, left, right) of the objects inside of the tree. The solution was to check whether or not the centroid of the polygon was contained within each member of the list generated by the query. In some cases, the objects returned by the query were in the vicinity of the query object but did not in fact contain it.
The solution ended up being to change the if-statement to:
if 1:
for k,item in enumerate(result):
print(len(result),k)
touch=tree.query(item.centroid)
inside=[]
for x in touch:
if x.contains(item.centroid):
inside.append(item)
if len(inside)>=2:
final.append(item)
The reason for the original error was that the tree.query(<>) only returns the polygons that are likely to interact with the query <> object based on the bounds (top, bottom, left, right) of the objects inside of the tree. The solution was to check whether or not the centroid of the polygon was contained within each member of the list generated by the query. In some cases, the objects returned by the query were in the vicinity of the query object but did not in fact contain it.
answered Apr 11 at 21:44
LoonuhLoonuh
1164
1164
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f318431%2fquickly-find-all-polygons-that-overlap-with-two-or-more-polygons-in-shapely%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I added the imports, what is the best way to share a simple dataset?
– Loonuh
Apr 11 at 17:39
Do you care when many times something intersected? If you only care that it intersected at least twice, you could check the count value in every indented for in where for n,item2 in enumerate(spoof):, then break out of the loop if the count >= 2, rather than continuing to check every shape
– saph_top
Apr 11 at 20:51
Select a small number of polys that demonstrate the issue (incorrect results with 0 and correct with 1), export selected to new shapefile (delete all uneeded fields) and convert to geojson (i.e. with mapshaper.org) and include (formatted as code) in your question.
– user2856
Apr 11 at 21:22