Skip to content

Instantly share code, notes, and snippets.

@isccarrasco
Last active April 28, 2020 07:22
Show Gist options
  • Save isccarrasco/31e245aeada8ab7dd5af885f8d7d3392 to your computer and use it in GitHub Desktop.
Save isccarrasco/31e245aeada8ab7dd5af885f8d7d3392 to your computer and use it in GitHub Desktop.
Looking for adjacency municipalities.
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