I have a large .geojson file (~ 1 GB) of made up of thousands of polylines. My objective is to count the number of vertices (not end points) in the entire file. Is there a python module that is suited for this type of analysis?
-
Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq.– underdarkCommented Jun 16, 2016 at 19:05
-
Can we use R rather than Python? ;)– mdsumnerCommented Jun 16, 2016 at 21:58
-
yes of course, R may even be preferable– iskandarblueCommented Jun 17, 2016 at 11:37
-
Are you open to using a module that is tied to an open source GIS app like pyQGIS?– artwork21Commented Jun 17, 2016 at 13:28
Add a comment
|
1 Answer
You can use the json or simplejson library to convert the geojson to a Python object, then loop through each LineString. For each LineString get the length of the coordinates list, subtract 2 for the end points, and add it to a running sum.
Or use the JQ json processor:
cat my.geojson | jq "[.geometries[].coordinates | length-2] | add"
assuming you have a FeatureCollection of LineStrings, you may need to tweak for your actual geometry.
-
I've tried this command but I receive the error
jq: error (at <stdin>:0): Cannot iterate over null (null)
Commented Jun 17, 2016 at 11:53 -
I misread your original question. Updated the JQ filter query, but I'm guessing without an example of your GeoJSON data. Commented Jun 17, 2016 at 13:20
-
Still getting an error
Alexanders-MacBook-Pro:Pune alexander$ cat routes.geojson| jq '[.geometries[].coordinates| length -2] add' jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1: [.geometries[].coordinates| length -2] add jq: 1 compile error
Commented Jun 17, 2016 at 16:41 -
Since it says
Unix shell quoting issues?
you probably need the query in double quotes. Commented Jun 17, 2016 at 19:09 -
In my case
cat my.geojson | jq "[.features[].geometry.coordinates[] | length-2] | add"
worked like a charm.– sroeckerCommented Mar 11, 2021 at 19:06