Skip to content

Instantly share code, notes, and snippets.

@benbalter
Created June 25, 2015 07:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benbalter/9064be0fc9852f41742b to your computer and use it in GitHub Desktop.
Save benbalter/9064be0fc9852f41742b to your computer and use it in GitHub Desktop.
Convert CSVs to GeoJSON, in bulk
#!/bin/bash
# Convert CSVs to GeoJSON, in bulk
# Usage: ./csv-to-geojson.sh [URL to list of CSV files]
# Outputs geoJSON files to the `output` folder
# Install csv2geojson if it's not installed
type csv2geojson || npm install -g csv2geojson
# Cleanup
rm -Rf ./tmp
rm -Rf ./output
mkdir tmp
mkdir output
# Grab all CSVs listed on the request URL
csvs=$(wget --quiet -O - $1 | LC_ALL=c sed 's/a href/\'$'\n/g' | grep -o 'http://[^"]*\.csv' | sort | uniq)
# Loop and convert
for i in $csvs; do
filename=$(echo $i | sed 's/.*\///')
outfile=$(echo $filename | sed 's/\.csv$/\.geojson/')
wget --quiet -O - $i | LC_ALL=c sed 's/緯度/latitude/' | LC_ALL=c sed 's/経度/longitude/' > ./tmp/$filename
csv2geojson "./tmp/$filename" > "./output/$outfile"
# Cleanup unconvertable files
if [ "$(cat ./output/$outfile)" == "undefined" ]; then
rm -f ./output/$outfile
fi
done
# Cleanup temp folder
rm -Rf ./tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment