Check if geographical polygon is valid

Question:

I have a df that look like this

coordinates={"type":"zone","bound":"POLYGON ((11.31767 43.32289, 11.32205 43.32467, 11.3235 43.32458, 11.32395 43.32474, 11.32411 43.32522, 11.32623 43.32516, 11.32647 43.32459, 11.32576 43.32435, 11.32581 43.32384, 11.32438 43.32332, 11.32803 43.32171, 11.32573 43.32016, 11.32571 43.31896, 11.32588 43.31844, 11.32319 43.31699, 11.32058 43.31589, 11.31782 43.31419, 11.3171 43.31093, 11.3166 43.31046, 11.31569 43.31045, 11.31344 43.31128, 11.31158 43.31121, 11.3097 43.31289, 11.30727 43.31445, 11.30414 43.31606, 11.3027 43.31726, 11.30154 43.31853, 11.29848 43.32291, 11.29457 43.3281, 11.29194 43.3313, 11.29289 43.33069, 11.29388 43.33036, 11.29505 43.33021, 11.29745 43.33008, 11.30058 43.33046, 11.3029 43.33021, 11.30485 43.33054, 11.30569 43.33197, 11.30626 43.33223, 11.30809 43.3325, 11.30907 43.33198, 11.31024 43.33192, 11.312 43.33134, 11.31369 43.32529, 11.31767 43.32289))"}

df=pd.DataFrame([coordinates])

I would love to know if the column "bound" is a valid polygon and if it is not I want to fix it

I tries .is_valid but it doesn’t look working

Asked By: arrabattapp man

||

Answers:

for that you can use geopandas:

import geopandas as gpd

coordinates = {"type":"zone", "bound":"POLYGON ((11.31767 43.32289, 11.32205 43.32467, 11.3235 43.32458, 11.32395 43.32474, 11.32411 43.32522, 11.32623 43.32516, 11.32647 43.32459, 11.32576 43.32435, 11.32581 43.32384, 11.32438 43.32332, 11.32803 43.32171, 11.32573 43.32016, 11.32571 43.31896, 11.32588 43.31844, 11.32319 43.31699, 11.32058 43.31589, 11.31782 43.31419, 11.3171 43.31093, 11.3166 43.31046, 11.31569 43.31045, 11.31344 43.31128, 11.31158 43.31121, 11.3097 43.31289, 11.30727 43.31445, 11.30414 43.31606, 11.3027 43.31726, 11.30154 43.31853, 11.29848 43.32291, 11.29457 43.3281, 11.29194 43.3313, 11.29289 43.33069, 11.29388 43.33036, 11.29505 43.33021, 11.29745 43.33008, 11.30058 43.33046, 11.3029 43.33021, 11.30485 43.33054, 11.30569 43.33197, 11.30626 43.33223, 11.30809 43.3325, 11.30907 43.33198, 11.31024 43.33192, 11.312 43.33134, 11.31369 43.32529, 11.31767 43.32289))"}

foo = gpd.GeoDataFrame([coordinates])
foo['geometry'] = gpd.GeoSeries.from_wkt(foo['bound'])

foo.is_valid

0    True
dtype: bool
Answered By: AlexWach