2

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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
iskandarblue
  • 2,092
  • 2
  • 17
  • 34

1 Answers1

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