6

I'm loving the ndjson-cli tools for working with newline-delimited GeoJSON files. But I can't seem to find a tool that converts a standard GeoJSON (whether minified or prettified) ond converts it to newline-delimited. Is there one?

4 Answers 4

12

Borrowing from this StackOverflow answer and using the jq command line tool you can filter GeoJSON feture objects and dump them as new-line delimiter with:

cat my.geojson | jq -c ".features[]" > nld_features.json

The important parts here are the -c flag which prints compact JSON objects on a single line, and the filter ".features[]" which selects the features object from the top level of the object, and then calls an Array/Object iterator over the values to break the features into separate JSON objects. If you want to extract coordinates as well, you can use the different filters in jq to extract the values as well.

2
  • 1
    Ah yeah. I used to use jq, but in the end I found its syntax too hard to remember, and scripts quickly became incomprehensible. Commented Feb 11, 2018 at 3:10
  • 1
    This is extremely slow for larger file sizes (I'm guessing b/c jq reads the entire file into memory ... you can the --stream option to improve this...
    – dsummersl
    Commented Dec 18, 2020 at 15:29
6

You can use ogr2ogr:

ogr2ogr -f GeoJSONSeq ~/Downloads/data.geojson.ld ~/Downloads/data.geojson
1
  • 1
    This helped me a lot, I was using ogr2ogr to convert PostGIS geometries into geojson but I needed line delimited version, now I use ogr2ogr -f GeoJSONSeq regions.json.ld "PG:host=localhost dbname=mydb user=postgres password=passwd" -sql "select name,wkb_geometry from regions" Commented Mar 31, 2021 at 11:49
5

I wrote a dedicated command-line tool on NPM to do this, now:

https://www.npmjs.com/package/geojson2ndjson

Usage

npm install -g geojson2ndjson geojson2ndjson myfile.geojson | ...

Or read from standard input:

cat myfile.geojson | geojson2ndjson | ...

Examples

List the properties of every feature, without the geometries:

geojson2ndjson myfile.geojson | ndjson-map d.properties

4

Tippecanoe also has this functionality built in:

tippecanoe-json-tool source.json > out.nd.json

I've only tested with small examples, but given that tippecanoe is blazing fast and built for giant geojsons, this should be a robust option. Source here.

2
  • Huh, thanks. The documentation is a bit obscure, and there's no command-line help. Commented Oct 30, 2018 at 23:06
  • 1
    Note from a few years later — the Tippecanoe docs now live here: github.com/felt/tippecanoe#usage
    – bertday
    Commented May 8, 2023 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.