CLI GuideChannel: csvgeojson

CSV to GeoJSON Converter

Guidelines and commands for translating coordinate-rich CSV sheets to GeoJSON.

CSV files lack intrinsic spatial structures. To perform a robust translation, you must declare columns containing Latitude/Longitude or X/Y parameters. Direct conversions can fail without configuration, so using explicit parameters is recommended.

Indirect Mapping Execution Required

Because database connection attributes or local attributes parsing is required, direct web streams are disabled. Use the GDAL CLI script below for precise terminal conversions.

Specification Rules

  • Map business location spreadsheets onto custom web interfaces
  • Convert CSV address listings into interactive geo coordinates
  • Prepare spatial CSV exports for desktop mapping tools
  • Columns must be clearly defined (e.g., Latitude, Longitude)
  • Cannot generate polygon records from standard tabular files
  • Subject to string encoding mismatches on special characters

GDAL Direct Equivalent Terminal Command

sh — gdalready
$ogr2ogr -f GeoJSON output.geojson input.csv -oo X_POSSIBLE_NAMES=lon* -oo Y_POSSIBLE_NAMES=lat* -a_srs EPSG:4326

When to use this conversion

Convert when you have a spreadsheet or database export with latitude and longitude columns (or a WKT geometry column) and need to plot it on a web map. This is the most common entry point into GIS for non-specialists.

Technical Details

GDAL reads the CSV with the configured delimiter and encoding, identifies coordinate columns from -oo X_POSSIBLE_NAMES and -oo Y_POSSIBLE_NAMES (default: lon,longitude,x,easting and lat,latitude,y,northing), creates Point geometries from each row, and writes a FeatureCollection. CRS is assigned explicitly via -a_srs because CSVs carry no projection metadata. For non-point geometries, a WKT column can be designated with -oo GEOMETRY=AS_WKT.
Output file size

GeoJSON is typically 1.5–3× the size of the source CSV because each row gains JSON property keys, structural braces, and geometry markup. Files with many text attributes see smaller blow-up than coordinate-only files.

Common Errors & Fixes

  • Coordinate columns not detected: the column is named "Geo_Lng" instead of "lon" or "longitude". Fix: pass explicit names with -oo X_POSSIBLE_NAMES=geo_lng -oo Y_POSSIBLE_NAMES=geo_lat.
  • Encoding garbled: accented characters arrive as é or ?. Fix: pass -oo ENCODING=UTF-8 (or ISO-8859-1, depending on the source) when reading.
  • CRS missing: output coordinates have no projection metadata. Fix: always include -a_srs EPSG:4326 for lat/lon source data.
  • Decimal separator wrong: European Excel exports use commas as decimal separators, breaking coordinate parsing. Fix: change source delimiter to semicolon and ensure decimals use dots, or pre-process with sed.

Alternative Tools

  • QGIS: Layer → Add Layer → Add Delimited Text Layer, then export as GeoJSON
  • ogr2ogr CLI with the full set of -oo flags (see exampleCommand below)
  • Python: pandas.read_csv then geopandas.GeoDataFrame, save with .to_file()
  • kepler.gl or Mapbox Studio for interactive CSV upload and conversion

Frequently Asked Questions

How does GDAL detect spatial columns in a CSV?

GDAL references system column configurations using standard flags like 'X_POSSIBLE_NAMES' and 'Y_POSSIBLE_NAMES' to auto-locate latitude/longitude attributes.

Can I represent complex polygons inside a CSV?

Yes, but only if the spreadsheet contains geometry columns formatted in WKT (Well-Known Text). Otherwise, only point vector coordinates can be parsed.

What is the best coordinate system for CSV exports?

Standard WGS 84 (EPSG:4326) coordinate values are widely supported and prevent reprojection errors.

Related Transformations Map