mbloch / mapshaper

Tools for editing Shapefile, GeoJSON, TopoJSON and CSV files
http://mapshaper.org
Other
3.74k stars 532 forks source link

producing bad output #579

Closed eric-g-97477 closed 1 year ago

eric-g-97477 commented 1 year ago

I am using Admin 0 – Details > map units 10m NaturalEarthData ( file )

I figure there is a multitude of tools out there that would do the job.

I ran a couple of the commands mentioned in the documentation and geopandas was unable to load the simplified data successfully. It can load the original data without issue.

The commands were:

mapshaper ne_10m_admin_0_map_units.geo.json -simplify 10% -o simplified.geo.json
mapshaper ne_10m_admin_0_map_units.geo.json -simplify interval=100 -o simplified.geo.json

The geopandas code was:

gdf = geopandas.GeoDataFrame.from_features( simplifiedData, crs = 4326 )
gdf.explore()

and geopandas generated the error:

folium/features.py:1099, in <listcomp>(.0)
   1094 def warn_for_geometry_collections(self):
   1095     """Checks for GeoJson GeometryCollection features to warn user about incompatibility."""
   1096     geom_collections = [
   1097         feature.get("properties") if feature.get("properties") is not None else key
   1098         for key, feature in enumerate(self._parent.data["features"])
-> 1099         if feature["geometry"]["type"] == "GeometryCollection"
   1100     ]
   1101     if any(geom_collections):
   1102         warnings.warn(
   1103             "{} is not configured to render for GeoJson GeometryCollection geometries. "
   1104             "Please consider reworking these features: {} to MultiPolygon for full functionality.\n"
   (...)
   1108             UserWarning,
   1109         )

TypeError: 'NoneType' object is not subscriptable

Is this a bug in mapshaper? Is there a correct way I should be using mapshaper? Is there a problem with the NaturalEarthData?

mbloch commented 1 year ago

I suspect that the file you ran through mapshaper (ne_10m_admin_0_map_units.geo.json) has no data properties. I can't confirm that, because the zip file you provided above contains a Shapefile, not a GeoJSON file named ne_10m_admin_0_map_units.geo.json.

When a layer is missing attribute data, mapshaper exports it as a GeoJSON GeometryCollection (rather than as a FeatureCollection, which is probably what geopandas expects). You can tell mapshaper to use a FeatureCollection by adding a parameter to the -o command, like this: -o geojson-type=FeatureCollection

eric-g-97477 commented 1 year ago

Playing around with it more, I believe the reason is that during the simplification process, some of the features were simplified out of existence. When I added keep-shapes to the command, the output mapshaper produced was correct.

I do believe there is a bug here because mapshaper should not be outputting bad data.

The shape file I pointed to is trivial to convert to json using geopandas.

    shapefile = geopandas.read_file( shapeFilePath )
    shapefile.to_file( outPath, driver="GeoJSON" )  
mbloch commented 1 year ago

I see... my hunch was based on the error message that you pasted, which seemed to be related to the "GeometryCollection" GeoJSON type...

Mapshaper does not remove features that have null geometries post-simplification. I don't consider null-geometry features to be "bad data" -- they are valid GeoJSON according to the spec (see https://datatracker.ietf.org/doc/html/rfc7946#section-3.2 ). If you need to remove the null geometries, you can use the mapshaper command -filter remove-empty.

eric-g-97477 commented 1 year ago

That make sense. Thank you for the clarification.