Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert CSVs to GeoJSON, in bulk
# Convert CSVs to GeoJSON, in bulk
# Usage: ./ [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
# Cleanup temp folder
rm -Rf ./tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment