Skip to content

Instantly share code, notes, and snippets.

@simonw
Created March 21, 2024 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/0343cdd3568bbe28cad15d1097b1b1c7 to your computer and use it in GitHub Desktop.
Save simonw/0343cdd3568bbe28cad15d1097b1b1c7 to your computer and use it in GitHub Desktop.

2024-03-21T20:13:25 conversation: 01hshac0dmbmaancjdt82grawj

Model: claude-3-opus-20240229

Prompt:

give me options on macOS for CLI tools to turn a shapefile into GeoJSON

Response:

Certainly! Here are a couple of options for CLI tools on macOS that you can use to convert a shapefile into GeoJSON:

  1. ogr2ogr (GDAL):

    • GDAL (Geospatial Data Abstraction Library) is a popular open-source library for handling geospatial data.
    • It includes the ogr2ogr command-line tool, which can convert between various geospatial data formats, including shapefiles and GeoJSON.
    • To install GDAL on macOS, you can use Homebrew by running: brew install gdal.
    • Once installed, you can use the following command to convert a shapefile to GeoJSON:
      ogr2ogr -f GeoJSON output.geojson input.shp
      
    • Replace output.geojson with the desired output filename and input.shp with the path to your input shapefile.
  2. mapshaper:

    • mapshaper is a powerful tool for editing and converting geospatial data, including shapefiles and GeoJSON.
    • It provides a command-line interface as well as a web-based interface.
    • To install mapshaper on macOS, you can use npm (Node Package Manager) by running: npm install -g mapshaper.
    • Once installed, you can use the following command to convert a shapefile to GeoJSON:
      mapshaper input.shp -o output.geojson
      
    • Replace input.shp with the path to your input shapefile and output.geojson with the desired output filename.

Both ogr2ogr and mapshaper are powerful tools that offer additional options and functionality beyond simple format conversion. They allow you to perform various operations on geospatial data, such as filtering, simplifying, and reprojecting.

Make sure you have the necessary dependencies installed (GDAL or Node.js) before running the respective commands.

I recommend referring to the official documentation of each tool for more detailed information on their usage and available options:

2024-03-21T20:15:56

Prompt:

i tried using ogr2ogr but it gave me back GeoJSON with a weird coordinate system that was not lat/lon that i am used to

Response:

Apologies for the confusion. By default, ogr2ogr preserves the coordinate system of the input shapefile. If your shapefile uses a coordinate system other than WGS84 (EPSG:4326), which is the commonly used lat/lon coordinate system, the resulting GeoJSON will have coordinates in that same coordinate system.

To convert the shapefile to GeoJSON with WGS84 (lat/lon) coordinates, you can use the -t_srs option in ogr2ogr to specify the target coordinate system. Here's the updated command:

ogr2ogr -f GeoJSON -t_srs EPSG:4326 output.geojson input.shp

Let's break down the command:

  • -f GeoJSON: Specifies the output format as GeoJSON.
  • -t_srs EPSG:4326: Sets the target coordinate system to WGS84 (EPSG:4326), which uses lat/lon coordinates.
  • output.geojson: Specifies the desired output filename for the GeoJSON file.
  • input.shp: Specifies the path to your input shapefile.

By including the -t_srs EPSG:4326 option, ogr2ogr will reproject the coordinates of the input shapefile to WGS84 (lat/lon) before writing the GeoJSON output.

Make sure to replace output.geojson with the desired output filename and input.shp with the path to your input shapefile.

If you encounter any further issues or need additional assistance, please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment