Geopandas polygon to line

Viewed 2361

I am new to geopandas and would like to plot only the outline of a polygon, similar to the function ST_Boundary() in PostGIS

I have a geodataframe states containing polygons for each state

states = counties.dissolve(by='STATEFP')

When I subset by one state, I am able to plot that state:

states.loc[states.index.isin(['06'])]['geometry']

I am only interested in the outline but it is not clear in the documentation how to convert a polygon to line geometry however. Is there a useful method in geopandas or another spatial library that might help in converting a polygon to a linestring?

enter image description here

2 Answers

You can get boundary as

states.boundary

Alternatively, if you want only exterior boundary you get it as

states.exterior

Those give you new GeoSeries with line geometry.

It seems like .exterior returns a geometry of type 'linear ring', whereas .boundary returns a geometry of type 'linestring' - this may be important when it comes to writing the line geometries to file - I had issues writing 'linear ring' to GPKG and SHP but it worked for 'linestring'.

Related