Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid geometries #19

Open
eric-g-97477 opened this issue Jan 30, 2023 · 0 comments
Open

Invalid geometries #19

eric-g-97477 opened this issue Jan 30, 2023 · 0 comments

Comments

@eric-g-97477
Copy link

eric-g-97477 commented Jan 30, 2023

I need to be able to compute a "center" point for the largest landmasses in the geometries for the countries. The shapely.ops.polylabel function implements a good algorithm for doing that.

Note that if I use the 50m (https://github.com/AshKyd/geojson-regions/blob/master/countries/50m/SDN.geojson) version of the file, polylabel works.

Unfortunately, the geometry defined by SDN.geojson is invalid. Shapely's explain_validity function returns the following information:

0    Self-intersection[33.9633927949711 9.464285229...
Name: geometry, dtype: object

make_valid generates the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [5], in <cell line: 355>()
    353 gdf = geopandas.GeoDataFrame.from_features( features, crs = 4326 )   
    354 print( explain_validity( gdf.geometry ) )
--> 355 print( make_valid( gdf.geometry ) )
    356 gdf.plot( figsize = ( 20, 20 ) )
    357 center = polylabel( gdf.geometry.values[0], tolerance = .1 ) 

File shapely/validation.py:53, in make_valid(ob)
     29 def make_valid(ob):
     30     """
     31     Make the input geometry valid according to the GEOS MakeValid algorithm.
     32 
   (...)
     51 
     52     """
---> 53     if ob.is_valid:
     54         return ob
     55     return shapely.make_valid(ob)

File pandas/core/generic.py:1527, in NDFrame.__nonzero__(self)
   1525 @final
   1526 def __nonzero__(self) -> NoReturn:
-> 1527     raise ValueError(
   1528         f"The truth value of a {type(self).__name__} is ambiguous. "
   1529         "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1530     )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I suggest updating how the geometries are computed so they are guaranteed to be valid. There are several other invalid geometries in the dataset.

A script that generates the error is:

The data is the SDN.geojson file located at https://github.com/AshKyd/geojson-regions/blob/master/countries/110m/SDN.geojson
(removed some of the "properties" for simplicity). It will draw correctly.

import json
from shapely.ops import polylabel
import geopandas
import os

data = {
  "type": "Feature",
  "properties": {
    "featurecla": "Admin-0 country",
    "filename": "SDN.geojson"
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          24.567369012152085,
          8.229187933785468
        ],
        [
          23.805813429466752,
          8.666318874542526
        ],
        [
          23.459012892355986,
          8.954285793488893
        ],
        [
          23.394779087017184,
          9.265067857292223
        ],
        [
          23.55724979014283,
          9.681218166538684
        ],
        [
          23.554304233502194,
          10.089255275915308
        ],
        [
          22.97754357269261,
          10.71446259199854
        ],
        [
          22.864165480244225,
          11.142395127807546
        ],
        [
          22.87622,
          11.384610000000123
        ],
        [
          22.50869,
          11.67936
        ],
        [
          22.49762,
          12.26024
        ],
        [
          22.28801,
          12.64605
        ],
        [
          21.93681,
          12.588180000000136
        ],
        [
          22.03759,
          12.95546
        ],
        [
          22.29658,
          13.37232
        ],
        [
          22.18329,
          13.78648
        ],
        [
          22.51202,
          14.09318
        ],
        [
          22.30351,
          14.32682
        ],
        [
          22.56795000000011,
          14.944290000000137
        ],
        [
          23.024590000000103,
          15.68072
        ],
        [
          23.886890000000108,
          15.61084
        ],
        [
          23.83766000000014,
          19.580470000000105
        ],
        [
          23.850000000000136,
          20
        ],
        [
          25.000000000000114,
          20.00304
        ],
        [
          25,
          22
        ],
        [
          29.02,
          22
        ],
        [
          32.9,
          22
        ],
        [
          36.86623,
          22
        ],
        [
          37.1887200000001,
          21.01885
        ],
        [
          36.96941,
          20.83744000000013
        ],
        [
          37.11470000000014,
          19.80796
        ],
        [
          37.4817900000001,
          18.61409
        ],
        [
          37.86276,
          18.36786
        ],
        [
          38.410089959473225,
          17.998307399970315
        ],
        [
          37.90400000000011,
          17.42754
        ],
        [
          37.16747,
          17.263140000000135
        ],
        [
          36.852530000000115,
          16.95655
        ],
        [
          36.75389,
          16.29186
        ],
        [
          36.32322,
          14.82249
        ],
        [
          36.42951,
          14.42211
        ],
        [
          36.27022,
          13.563330000000121
        ],
        [
          35.86363,
          12.57828
        ],
        [
          35.26049,
          12.08286
        ],
        [
          34.83163000000013,
          11.318960000000118
        ],
        [
          34.73115000000013,
          10.910170000000107
        ],
        [
          34.25745,
          10.63009
        ],
        [
          33.96162,
          9.58358
        ],
        [
          33.97498,
          8.68456
        ],
        [
          33.963392794971185,
          9.464285229420625
        ],
        [
          33.82496348090751,
          9.484060845715362
        ],
        [
          33.84213085302815,
          9.981914637215993
        ],
        [
          33.72195924818311,
          10.325262079630193
        ],
        [
          33.206938084561784,
          10.720111638406593
        ],
        [
          33.086766479716744,
          11.441141267476496
        ],
        [
          33.206938084561784,
          12.179338268667095
        ],
        [
          32.743419037302544,
          12.248007757149992
        ],
        [
          32.67474954881965,
          12.02483191958072
        ],
        [
          32.073891524594785,
          11.973329803218519
        ],
        [
          32.31423473428475,
          11.68148447716652
        ],
        [
          32.400071594888345,
          11.080626452941488
        ],
        [
          31.850715687025513,
          10.531270545078826
        ],
        [
          31.35286189552488,
          9.810240916008695
        ],
        [
          30.837840731903384,
          9.70723668328452
        ],
        [
          29.996639497988554,
          10.290927335388687
        ],
        [
          29.61895731133285,
          10.084918869940225
        ],
        [
          29.515953078608618,
          9.793073543888056
        ],
        [
          29.000931914987177,
          9.60423245056029
        ],
        [
          28.966597170745786,
          9.398223985111656
        ],
        [
          27.970889587744352,
          9.398223985111656
        ],
        [
          27.833550610778786,
          9.60423245056029
        ],
        [
          27.112520981708883,
          9.638567194801624
        ],
        [
          26.752006167173818,
          9.466893473594496
        ],
        [
          26.477328213242515,
          9.552730334198088
        ],
        [
          25.962307049621018,
          10.136420986302426
        ],
        [
          25.790633328413946,
          10.411098940233728
        ],
        [
          25.069603699343986,
          10.273759963267992
        ],
        [
          24.794925745412684,
          9.810240916008695
        ],
        [
          24.53741516360202,
          8.91753756573172
        ],
        [
          24.19406772118765,
          8.728696472403897
        ],
        [
          23.886979580860668,
          8.619729712933065
        ],
        [
          24.567369012152085,
          8.229187933785468
        ]
      ]
    ]
  }
}

features = {
    "type": "FeatureCollection",
    "features": [data]
}

gdf = geopandas.GeoDataFrame.from_features( features, crs = 4326 )   
print( explain_validity( gdf.geometry ) )
gdf.plot( figsize = ( 20, 20 ) )
center = polylabel( gdf.geometry.values[0], tolerance = .1 ) 
print( center )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant