0

baby programmer here so apologies if this is a simple fix, however I am running a test to see where multiple polygons and multipolygons intersect, using two identical lists to loop through the whole dataset. The dataset is a geoseries of length 200, and I know for a fact more than one of these shapefiles intersect, however I keep getting the same output of just one multistring. I have been attempting this using the following code.

from geopandas import read_file, GeoSeries
from sys import exit
from matplotlib.pyplot import subplots, savefig, title
from pyproj import Geod
#import statements at the beginning

shapes = read_file("../data/ne_10m_admin_0.shp")

shapes_geo = shapes.geometry #retrieve the shapefile from the dataset
shape_borders = shapes_geo.iloc[range(len(world_geo))] #

bordersb = shape_geo #second list of countries


intborders = []

for b in bordersb:
    for a in shape_geo():
        if b.equals(a) == True:
            exit
        elif b.touches(a):
            intborders = b.intersection(a)

print(intborders)

This currently outputs a single Multistring of 33 coordinates, however I have been aiming for a list of Multistrings. I have been testing the output by printing the pure values, checking the number on variable explorer as well as creating an image of the intersections, code for which is not shown here as there has been no issues there!

The final intended output is a list named 'intborders' showing all the intersections as multistrings.

wodseaur
  • 1
  • 2
  • 1
    Welcome, @wodseaur. It looks like the loop needs to append each new element to the list rather than replace the list: `intborders.append(b.intersection(a))`. Also the variable `shapes` doesn't get set in the code snippet. BTW to exit the program, call the function: `exit()`. Just referring to the function itself won't do anything. Usually we'd define a function and make it return rather than exit the entire program. And FYI there's no need for `== True` if the `if` statement. – Jerry101 Nov 18 '22 at 01:52
  • Fantastic thank you so much that seems to have solved it! The shapes variable was a typo I'd made when translating it - fixed that and an if statement that was meant to be an elif. However using the exit function correctly results in no outputs? This is probably another rookie error but thank you very much for your help! – wodseaur Nov 18 '22 at 02:06
  • *edit* Just realised I'd completely misunderstood how the exit function works it cancels the entire loop so of course there would be no output- thank you for your help! – wodseaur Nov 18 '22 at 02:13
  • Glad that helped! To skip the rest of the current loop iteration, use a `continue` statement. To exit the loop, use a `break` statement, then it'll reach your `print()` function call. The `exit()` function exits the entire program and the Python interpreter. – Jerry101 Nov 18 '22 at 06:05

0 Answers0