Python Convert GeoJSON to CSV

5/5 - (2 votes)

What is GeoJSON?

πŸ’‘ GeoJSON is an RFC standardized data format to encode geographic data structures such as Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. GeoJSON is based on the JavaScript Object Notation (JSON).

Example GeoJSON to CSV

Say, you have the following GeoJSON snippet:

{ 
    "type": "FeatureCollection",
    "features": [
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]},
        "properties": { 
          "name": "Location A",
          "category": "Store"
        }
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-80.24, 40.12]},
        "properties": { 
          "name": "Location B",
          "category": "House"
        }
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [ -77.2, 41.427]},
        "properties": { 
          "name": "Location C",
          "category": "Office"
        }
      }
    ]
  }

You want to convert it to the following CSV format:

latitude,longitude,altitude,geometry,name,category
39.984,-75.343,,Point,Location A,Store
40.12,-80.24,,Point,Location B,House
41.427,-77.2,,Point,Location C,Office

Python GeoJSON to CSV Conversion

The Python code to convert a GeoJSON to a CSV in Python uses a combination of the json and csv packages.

import json
import csv


geo_filename = 'my_file.json'
csv_filename = 'my_file.csv'


def feature_to_row(feature, header):
    l = []
    for k in header:
        l.append(feature['properties'][k])
    coords = feature['geometry']['coordinates']
    assert(len(coords)==2)
    l.extend(coords)
    return l


with open(geo_filename, 'r') as geo_file:
    with open(csv_filename, 'w', newline='') as csv_file:
        geojson_data = json.load(geo_file)
        features = geojson_data['features']
        
        csv_writer = csv.writer(csv_file)

        is_header = True
        header = []

        for feature in features:
            if is_header:
                is_header = False
                header = list(feature['properties'].keys())
                header.extend(['px','py'])
                csv_writer.writerow(header)
                
            csv_writer.writerow(feature_to_row(feature, feature['properties'].keys()))

You can either copy&paste this code and run it in the same folder as your GeoJSON (of course, after renaming the input and output filenames.

Or you can check out this excellent GitHub to get a more “scriptable” variant to be used in the command line. This code is inspired by the GitHub but simplified significantly.

Example input:

{ 
    "type": "FeatureCollection",
    "features": [
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]},
        "properties": { 
          "name": "Location A",
          "category": "Store"
        }
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-80.24, 40.12]},
        "properties": { 
          "name": "Location B",
          "category": "House"
        }
      },
      { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [ -77.2, 41.427]},
        "properties": { 
          "name": "Location C",
          "category": "Office"
        }
      }
    ]
  }

Example output:

GeoJSON to CSV in QGIS

In QGIS, if you have a map like this one (source):

You can convert GEOJSON to CSV right within QGIS by clicking Export, then Save Feature As and select the Comma Separated Value [CSV] selector in the first dropdown menu.

GeoJSON to CSV Online Converter

You can easily convert specific GeoJSON snippets to CSV using the following online converter: