Last active
April 28, 2020 07:22
-
-
Save isccarrasco/31e245aeada8ab7dd5af885f8d7d3392 to your computer and use it in GitHub Desktop.
Looking for adjacency municipalities.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import geopandas as gpd | |
df = gpd.read_file('municipalities.shp') | |
df["adjacents"] = None | |
for index, municipality in df.iterrows(): | |
# get 'not disjoint' countries | |
# CVE_ENT is the ID of the Municipality | |
adjacents = df[~df.geometry.disjoint(municipality.geometry)].CVE_ENT.tolist() | |
# remove own name from the list | |
adjacents = [name for name in neighbors if municipality.CVE_ENT != name] | |
# add names of adjacents as adjacents value | |
df.at[index, "adjacents"] = ", ".join(adjacents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment