Created
June 25, 2015 07:11
-
-
Save benbalter/9064be0fc9852f41742b to your computer and use it in GitHub Desktop.
Convert CSVs to GeoJSON, in bulk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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