Skip to content

Instantly share code, notes, and snippets.

@mhweber
Created June 17, 2016 17:16
Show Gist options
  • Select an option

  • Save mhweber/cdb80fb03144fde8758d8e5b20a9b417 to your computer and use it in GitHub Desktop.

Select an option

Save mhweber/cdb80fb03144fde8758d8e5b20a9b417 to your computer and use it in GitHub Desktop.
A quick and dirty poor man's clip using geopandas. First does a union between a plot shapefile and an overlapping features shapefilie, then subsets where attributes of the features shapefile are not null to get the features clipped to boundaries of plots.
Plots = gpd.GeoDataFrame.from_file('Plots.shp')
Plots.plot()
Features = gpd.GeoDataFrame.from_file('Features.shp')
Features.plot()
# union features
Both = overlay(Plots, Features, how="union")
Both.head()
# 'clip' by getting rid of union features where attributes for plot are null in the union
Clips = Both[pd.notnull(Both['ORIG_FID'])]
Clips.plot()
Clips.to_file('Clips.shp', driver = 'ESRI Shapefile')
@candidia

Copy link
Copy Markdown

I think overlay is wrong. Should it be gpd.overlay or from some other package because I get an error?

@Perkov

Perkov commented Aug 15, 2019

Copy link
Copy Markdown

True. Change to geopandas.overlay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment