Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Geojson to CSV

3 ways of converting Geojson to CSV for free

Why would you consider converting the, spatially-aware GeoJSON into a seemingly simpler CSV format? The answer lies in the versatility and accessibility of CSV files. They are not just about simplicity; they represent compatibility with a broad spectrum of software, ease of use, and the democratization of data access. So here is how to do it!

Converting GeoJSON to CSV using QGIS

Converting GeoJSON to CSV using QGIS is a straightforward process that involves a few steps. QGIS is a powerful open-source geographic information system that supports various file formats, including GeoJSON and CSV. Here are your options for converting GeoJSON to CSV within QGIS:

1. Using the “Save As” Feature

  1. Load Your GeoJSON File: Start by adding your GeoJSON file to the QGIS map canvas. You can do this by dragging the file into QGIS or by using the “Layer” menu, then selecting “Add Layer” > “Add Vector Layer.”
  2. Export to CSV: Right-click on the layer in the “Layers Panel,” select “Export,” and then “Save Features As.” In the dialog box that appears, set the “Format” to “Comma Separated Value [CSV].”
  3. Configure Export Settings: Choose your desired file name and location. You can also select which fields (attributes) to include in the CSV, coordinate precision, and whether to export geometry (as WKT, Well-Known Text).
  4. Save: Click on “OK” to export the layer to a CSV file.

2. Using the “Processing Toolbox”

  1. Open the Processing Toolbox: You can find it under “Processing” in the menu bar, then select “Toolbox.” Alternatively, you can press Ctrl+Alt+T on your keyboard.
  2. Search for “Convert Format”: In the search bar of the Processing Toolbox, type “convert format” or “reproject layer” and look for a tool that mentions converting vector formats. The exact name might vary depending on your QGIS version.
  3. Configure the Tool: Select your input layer (the GeoJSON file), output format (CSV), and specify any additional options like field selection, geometry conversion (if necessary), and file name.
  4. Run the Tool: Click “Run” to execute the process. Once completed, you will have your CSV file.

Converting Geojson to CSV Using GDAL (Geospatial Data Abstraction Library)

Using GDAL (Geospatial Data Abstraction Library) to convert GeoJSON to CSV is an efficient method, especially for those comfortable with command-line interfaces. GDAL includes the ogr2ogr command-line utility, which is very powerful for converting between different spatial data formats. Here’s how to do it:

Prerequisites

Make sure GDAL is installed on your system. GDAL is available for various operating systems, including Windows, macOS, and Linux. If it’s not installed, you can download it from the official GDAL website or install it using a package manager like brew on macOS, apt on Ubuntu, or conda for Anaconda environments.

Basic Conversion (Attributes Only)

To convert a GeoJSON file to a CSV file containing only the attribute data (no geometry), you can use the following command in your terminal or command prompt:

ogr2ogr -f "CSV" output.csv input.geojson

Replace input.geojson with the path to your GeoJSON file and output.csv with the desired output CSV file path.

Including Geometry as WKT

If you want to include the geometry in the CSV file in Well-Known Text (WKT) format, you can use the -lco GEOMETRY=AS_WKT option. This tells GDAL to add a column in the CSV for the geometry expressed as WKT.

ogr2ogr -f "CSV" output.csv input.geojson -lco GEOMETRY=AS_WKT

Specifying Fields

To specify which fields to include in the CSV, you can use the -select option followed by a comma-separated list of field names. For example, if you only want to include fields named Field1 and Field2, you would use:

ogr2ogr -f "CSV" output.csv input.geojson -select Field1,Field2

Excluding Geometry

If your GeoJSON includes geometry but you only want the attribute data in your CSV, you simply don’t include the -lco GEOMETRY=AS_WKT option. GDAL will ignore the geometry and only include the attribute data in the CSV file.

Additional Options

GDAL’s ogr2ogr utility offers many other options for transforming and filtering data during the conversion process. For instance, you can:

  • Use the -where option to filter features based on attribute values.
  • Apply spatial queries with the -spat option.
  • Reproject the data to a different coordinate system with the -t_srs option.

For a full list of options, you can check the official ogr2ogr documentation or use the ogr2ogr --help command.

Example

ogr2ogr -f "CSV" output.csv input.geojson -lco GEOMETRY=AS_WKT

This command converts input.geojson to a CSV file named output.csv, including the geometry of each feature in WKT format in the CSV.

Note

Remember to adjust file paths and options according to your specific needs and the structure of your GeoJSON file.

Converting a GeoJSON file to a CSV file using Python

To convert a GeoJSON file to a CSV file using Python, you can use a combination of libraries such as geopandas for handling geospatial data and pandas for CSV output. GeoPandas leverages the capabilities of pandas, shapely, fiona, and pyproj to provide an intuitive interface for geospatial data operations, including reading and writing different formats.

Here’s a step-by-step guide on how to perform the conversion:

Step 1: Install GeoPandas and Pandas

If you haven’t already installed GeoPandas and Pandas, you can do so using pip. Open your terminal or command prompt and run:

pip install geopandas pandas

Step 2: Read GeoJSON and Convert to CSV

Here’s a simple Python script that reads a GeoJSON file using GeoPandas, and then exports the attribute data to a CSV file. If your GeoJSON file includes geometry information and you wish to preserve it, you’ll convert the geometry to a format such as Well-Known Text (WKT) before exporting.

import geopandas as gpd

# Path to your GeoJSON file
geojson_path = 'input.geojson'
# Desired output CSV file path
csv_path = 'output.csv'

# Read the GeoJSON file
gdf = gpd.read_file(geojson_path)

# Optionally, convert the geometry to a string if you want to include it in the CSV
# This will convert the geometry to Well-Known Text (WKT) format
gdf['geometry'] = gdf['geometry'].apply(lambda x: x.wkt)

# Save the DataFrame to CSV
gdf.to_csv(csv_path, index=False)

Including Geometry

In the script above, the line gdf['geometry'] = gdf['geometry'].apply(lambda x: x.wkt) is responsible for converting the geometry column to WKT format, making it a readable string in the CSV. If you do not wish to include geometry information in your CSV, you can skip this step and directly export the DataFrame to a CSV file.

Selective Fields

If you only want to include specific fields (attributes) in the CSV, you can select these columns before saving:

# Assuming you only want to include 'Field1' and 'Field2' in the CSV
gdf[['Field1', 'Field2', 'geometry']].to_csv(csv_path, index=False)

Replace 'Field1' and 'Field2' with the actual names of the fields you wish to include from your GeoJSON file.

Additional Considerations

  • GeoPandas and Pandas offer a wide range of functionalities for manipulating geospatial data, so you can perform complex filtering, transformations, and analyses before exporting to CSV.
  • Keep in mind that GeoJSON files can contain complex geometries (e.g., polygons, multipolygons) which, when converted to WKT format, may result in very long strings. Ensure that your CSV handling process can accommodate this if necessary.

This approach provides a flexible and powerful way to work with geospatial data in Python, making it easy to convert between GeoJSON and CSV formats while also offering the possibility for sophisticated data processing steps in between.

Frequently asked questions about converting GeoJson to CSV

What is GeoJSON, and how does it differ from CSV?

GeoJSON and CSV (Comma Separated Values) are both popular formats used for storing and exchanging data, but they serve different purposes, especially in how they handle geospatial information.

What is GeoJSON?

GeoJSON is a format for encoding a variety of geographic data structures. It is based on JavaScript Object Notation (JSON), making it easy to read and work with in web applications and programming languages that support JSON. GeoJSON can represent simple geographical features, along with their non-spatial attributes, but it’s especially suited for encoding complex geographic structures such as:

  • Points (e.g., locations)
  • LineStrings (e.g., streets, highways, borders)
  • Polygons (e.g., countries, districts, zones)
  • MultiPoints, MultiLineStrings, MultiPolygons, and GeometryCollections for more complex structures

A GeoJSON object might look something like this:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [-105.01621, 39.57422]
  },
  "properties": {
    "name": "Denver",
    "population": 682545
  }
}

What is CSV?

CSV is a simple file format used to store tabular data, such as spreadsheets or databases. Each line in a CSV file corresponds to a row in the table, and columns are separated by commas (or sometimes other delimiters like semicolons). CSV files are easy to generate and can be edited with simple text editors or spreadsheet software. A CSV file representing the same information as the GeoJSON example might look like this:

name,population,coordinates
Denver,682545,"-105.01621, 39.57422"

Key Differences

  • Geospatial Encoding: GeoJSON is specifically designed to encode geospatial data and supports various geometries (points, lines, polygons), making it more suited for mapping and geographic applications. CSV, while versatile, doesn’t inherently support spatial data structures; spatial information must be encoded as text (e.g., a pair of comma-separated coordinates).
  • Data Structure: GeoJSON is a structured format that can represent complex hierarchical data due to its JSON foundation. CSV is flat and tabular, with a straightforward row-column structure.
  • Human-Readability: Both formats are human-readable, but GeoJSON’s JSON-based structure can be easier to read and understand for those familiar with JSON. CSV’s simplicity makes it universally understandable, but it may require additional context or definitions for complex data.
  • Compatibility: GeoJSON’s rich geographical data representation makes it highly compatible with web mapping tools and GIS software. CSV is widely supported across various applications, from spreadsheet editors to database management systems, but may require additional processing to be used effectively for geospatial purposes.

Can I convert a GeoJSON file to CSV without losing spatial data?

Yes, you can convert a GeoJSON file to CSV without losing spatial data, but the method depends on how you want to represent spatial information in the CSV format. GeoJSON inherently supports spatial data structures such as points, lines, and polygons, which are not natively supported by the CSV format. However, you can include spatial information in a CSV file as text, typically using one of the following approaches:

1. Coordinates as Text Columns

For point data, you can convert the coordinates into two separate columns in the CSV (e.g., latitude and longitude). For lines and polygons, you could represent them as a list of coordinate pairs or as more complex strings.

2. Well-Known Text (WKT)

Another common approach is to convert the spatial data into Well-Known Text (WKT) format, which is a text markup language for representing vector geometry on a map. WKT can represent points, lines, polygons, and more complex shapes in a single column of your CSV. This method is widely used because it preserves the geometry in a format that many GIS and mapping tools can interpret.

How to Do It

  • Using QGIS: You can use the “Save As” feature in QGIS to export your GeoJSON file as a CSV and include the geometry as a WKT string. During the export process, you’ll have the option to select “GEOMETRY=AS_WKT” to include the spatial data in the CSV.
  • Using GDAL’s ogr2ogr: This command-line tool can convert GeoJSON to CSV and include the geometry as a WKT string using the -lco GEOMETRY=AS_WKT option. The command might look something like this:
  ogr2ogr -f "CSV" output.csv input.geojson -lco GEOMETRY=AS_WKT
  • Using Python with GeoPandas: GeoPandas makes it easy to read GeoJSON and export to CSV, including geometry. You can use the .to_csv() method on a GeoDataFrame and first convert the geometry column to WKT using the .apply(lambda x: x.to_wkt()) method.
  • In ArcGIS Pro: While ArcGIS Pro does not directly export GeoJSON to CSV with spatial data, you can first import the GeoJSON as a feature layer, then use the “Calculate Geometry Attributes” tool to create new fields for X and Y coordinates (for points) or to generate WKT for more complex geometries. Finally, export the attribute table to CSV.

What are the limitations of CSV files for storing geospatial data?

CSV (Comma Separated Values) files are widely used for storing and sharing tabular data due to their simplicity and compatibility with a broad range of applications. However, when it comes to storing geospatial data, CSV files have several limitations:

1. Lack of Native Geospatial Support

CSV files do not inherently support spatial data types like points, lines, polygons, or complex geometries. Spatial information must be encoded as text (e.g., WKT – Well-Known Text) or separated into latitude and longitude columns, which can oversimplify or inadequately represent more complex geometries.

2. No Direct Support for Spatial Relationships and Operations

Unlike specialized geospatial formats (e.g., GeoJSON, Shapefiles, or GML), CSV files cannot directly encode spatial relationships (e.g., adjacency, containment) or support spatial operations (e.g., intersect, buffer). Any spatial analysis requires additional processing, typically in a GIS software that can interpret the spatial data encoded in text.

3. Limited Metadata

CSV files lack a standard way to include metadata about the data they contain, such as the coordinate reference system (CRS), scale, accuracy, or the data collection method. This information is crucial for geospatial data analysis and must be managed separately when using CSV files.

4. Complexity in Handling Multi-part Geometries

Representing complex geometries (e.g., multi-part polygons, lines with multiple segments) in a CSV format can be cumbersome. While possible through encoding as WKT, this approach can make the data difficult to read and process without specialized tools.

5. Data Integrity and Formatting Issues

CSV files are susceptible to formatting issues that can affect data integrity, such as improper handling of commas, new lines within fields, or inconsistencies in data types across rows. These issues can complicate the parsing of spatial data encoded as text.

6. No Standardization for Encoding Spatial Data

There’s no universally accepted standard for encoding spatial data in CSV files. Different applications might use various methods for representing geometries (e.g., WKT, lat/long columns), leading to compatibility issues.

7. Efficiency and Scalability

Large geospatial datasets can become inefficient to process and analyze in CSV format due to its text-based nature and the lack of indexing or spatial querying capabilities. This can lead to slower performance compared to using formats specifically designed for geospatial data.

Mitigating the Limitations

While CSV files have limitations for geospatial data, they remain a popular choice for sharing data due to their accessibility and simplicity. When working with CSV files for geospatial purposes, it’s important to:

  • Clearly document any conventions used for encoding spatial data.
  • Use accompanying metadata files to describe the dataset fully.
  • Consider converting to or working in tandem with more suitable geospatial formats for analysis and visualization tasks.

For applications that primarily involve tabular data with simple spatial components (e.g., locations represented by latitude and longitude), CSV files can still be a practical choice, provided the limitations are understood and managed.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.