mbloch / mapshaper

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

Uncaught (in promise) Error: Each LinearRing of a Polygon must have 4 or more Positions. #605

Closed ralyodio closed 10 months ago

ralyodio commented 11 months ago

I'm getting this with turf/leaflet when using mapshaper to generate a geojson from a .shp file.

Is it possible to fix with mapshaper? If so how?

I'm using node mapshaper-cli

I tried this command but it filters out everything:

npx mapshaper -i ne_110m_admin_0_countries.shp -filter 'Boolean(this.properties.NAME_EN && this.geometry && ((this.geometry.type === "Polygon" && this.geometry.coordinates[0].length >= 4)) || (this.geometry.type === "MultiPolygon" && this.geometry.coordinates.every(polygon => polygon.every(ring => ring.length >= 4)))))' -o format=geojson countries.geojson

this.geometry is undefined so all fail. anyone know where geometry is stored?

mbloch commented 11 months ago

Hi! If mapshaper is generating invalid GeoJSON, I want to investigate and fix that. Can you submit a sample data file along with the mapshaper command that generated the invalid GeoJSON, so I can try to reproduce the error?

In an -each command, this.geojson gives you a GeoJSON Feature. This converts mapshaper's internal representation of a feature to GeoJSON (mapshaper doesn't use GeoJSON internally), so it's not the most efficient thing to do.

ralyodio commented 11 months ago

http://sprunge.us/hy2A0C has the shapefile i used .shp

Here's the command I used to create the geojson file:

$ npx mapshaper -i ne_110m_admin_0_countries.shp  -filter 'Boolean(this.properties && this.properties.NAME_EN)' -o countries.geojson

[filter] Retained 177 of 177 features
[o] Wrote countries.geojson

Can you give me an example command to remove invalid polygons?

ralyodio commented 10 months ago

I have tried two different shape files, neither work properly. I'm still getting the invalid polygons because less than 4 points.

How do I fix?

I can email you the files I guess.

ralyodio commented 10 months ago

$ mapshaper World_Countries_Generalized.shp -o format=geojson countries.geojson

https://sprunge.us/AiZ5Bt

mbloch commented 10 months ago

Hi! I'm not sure how to use the sprunge.us URLs that you provided, clicking them downloaded binary files in an unknown format. Can you help me out?

World_Countries_Generalized.shp came from here, yes? To test, I downloaded the Shapefile and converted to GeoJSON using mapshaper, the same way that you did above. The output appears to be correct. It loaded without errors in QGIS and https://geojson.io/, and tested valid using this script: https://www.npmjs.com/package/geojson-validation

Could the issue you're having be specific to turf/leaflet? If you download the GeoJSON version of the world countries from here, does the data behave as you expect in turf/leaflet?

ralyodio commented 10 months ago

https://gis.stackexchange.com/questions/471288/error-when-creating-polygons-with-turf-js-from-existing-geojson-polygon-rings

mbloch commented 10 months ago

In the stackexchange comments, you say, "Even if I download the geojson file instead of shp file i still get the same error." This seems to confirm that the problem is related to how you're using Turf, not to mapshaper. That forum is a better place than here to get help writing your script.

mbloch commented 10 months ago

Just a quick follow-up... my hunch is that the problem here is not invalid GeoJSON, but rather that your script is not handling the various kinds of GeoJSON geometry correctly. In a file containing polygon data, you are likely to encounter both type Polygon and type MultiPolygon geometries. (The coordinates property of these types is different). You may also encounter GeoJSON Feature objects with null geometries.