Yet another GeoJson to Ndjson converter

You might have already seen many ways to convert GeoJson files to something BigQuery can understand. Let’s invent one more wheel!

E.g. this python script from Lak Lakshmanan:

Or using org2org tools to produce CSV file:

I also have a few node.js scripts to convert both ways between GeoJson or new-line-delimited GeoJson to New-line-delimited Json (ndjson) that BigQuery uses at https://github.com/mentin/geoscripts

But not everyone knows how to run Python and installing Node.js might be a mess, so here is a simpler way to do it, using jq command line tool. Jq (https://stedolan.github.io/jq/) is single-executable command line tool for processing json files. Install it following instructions at https://stedolan.github.io/jq/download/.

You can convert geojson files to ndjson files using this script:

You do need to replace input_file and output_file with proper file names.

— —

OK, if you want to know how it works, it does the following (node.js scripts mentioned above use the same logic):

  • .features[] selects individual feature in top level feature collection
  • .geometry | tojson saves the geometry field as quoted string to variable $g
  • we then create object based on feature’s .properties
  • and finally update this object’s .geometry field with saved variable $g.
  • — compact-output makes jq output every feature on its own line, which is exactly what BigQuery needs.

Some limitations of this approach:

  • this only supports GeoJson with default coordinate reference system (other CRS were deprecated by standard, but are still used sometimes)
  • this only supports GeoJson that contains FeatureCollection object, would not work if file has a single top level Feature.

Hi, I'm TL of BigQuery GIS project. Posting small recipes and various notes for BQ GIS users.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store