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?
Asked
Active
Viewed 738 times
2
-
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]. – underdark Jun 16 '16 at 19:05
-
Can we use R rather than Python? ;) – mdsumner Jun 16 '16 at 21:58
-
yes of course, R may even be preferable – iskandarblue Jun 17 '16 at 11:37
-
Are you open to using a module that is tied to an open source GIS app like pyQGIS? – artwork21 Jun 17 '16 at 13:28
1 Answers
2
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.
Marc Pfister
- 4,097
- 17
- 11
-
I've tried this command but I receive the error
jq: error (at <stdin>:0): Cannot iterate over null (null)– iskandarblue Jun 17 '16 at 11:53 -
I misread your original question. Updated the JQ filter query, but I'm guessing without an example of your GeoJSON data. – Marc Pfister Jun 17 '16 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– iskandarblue Jun 17 '16 at 16:41 -
Since it says
Unix shell quoting issues?you probably need the query in double quotes. – Marc Pfister Jun 17 '16 at 19:09 -
In my case
cat my.geojson | jq "[.features[].geometry.coordinates[] | length-2] | add"worked like a charm. – sroecker Mar 11 '21 at 19:06