Geopandas polygon to line
Asked Answered
B

2

8

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

Bornie answered 10/12, 2020 at 7:4 Comment(0)
G
9

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.

Glasgow answered 10/12, 2020 at 9:27 Comment(0)
O
0

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'.

Olympe answered 25/8, 2022 at 14:23 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Punke
Best to add this as a comment to the accepted answer. This is not a solution.Childers

© 2022 - 2024 — McMap. All rights reserved.