diff options
| author | Hsieh Chin Fan <typebrook@gmail.com> | 2020-07-21 17:22:27 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <typebrook@gmail.com> | 2020-07-21 17:22:27 +0800 |
| commit | 9b7d8677c4bcd7ce785eb3ed71efca4120c8a451 (patch) | |
| tree | 4716f56e2570652bd0ada250e70b1de4b54a9ad8 | |
| parent | e47e3067e7d08b6b6fa8925c74d316afa302a1fc (diff) | |
Refactor csv2geojson
| -rwxr-xr-x | tools/csv2geojson | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/tools/csv2geojson b/tools/csv2geojson index 028aed7..e742be6 100755 --- a/tools/csv2geojson +++ b/tools/csv2geojson | |||
| @@ -4,11 +4,11 @@ | |||
| 4 | for i in "$@" | 4 | for i in "$@" |
| 5 | do | 5 | do |
| 6 | case $i in | 6 | case $i in |
| 7 | -s) | 7 | -s) |
| 8 | lon_col=0; lat_col=1 | 8 | lon_col=0; lat_col=1 |
| 9 | shift;; | 9 | shift;; |
| 10 | 10 | ||
| 11 | *) | 11 | *) |
| 12 | csv=$i | 12 | csv=$i |
| 13 | shift;; | 13 | shift;; |
| 14 | esac | 14 | esac |
| @@ -16,23 +16,23 @@ done | |||
| 16 | 16 | ||
| 17 | # if no -s option, just read from input | 17 | # if no -s option, just read from input |
| 18 | if [ "$lon_col" != "0" ]; then | 18 | if [ "$lon_col" != "0" ]; then |
| 19 | # show each field with index in csv | 19 | # show each field with index in csv |
| 20 | echo -------------- > /dev/tty | 20 | echo -------------- > /dev/tty |
| 21 | head -1 < $csv | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty | 21 | head -1 < $csv | tr ',' '\n' | nl > /dev/tty |
| 22 | echo -------------- > /dev/tty | 22 | echo -------------- > /dev/tty |
| 23 | echo > /dev/tty | 23 | echo > /dev/tty |
| 24 | 24 | ||
| 25 | # get index of lon/lat column | 25 | # get index of lon/lat column |
| 26 | read -p "Number of latitude column: " lat_col | 26 | read -p "Number of longitude column: " lon_col |
| 27 | read -p "Number of longitude column: " lon_col | 27 | read -p "Number of latitude column: " lat_col |
| 28 | fi | 28 | fi |
| 29 | 29 | ||
| 30 | cat $csv | | 30 | (which dos2unix &>/dev/null && dos2unix <$csv || cat $csv) |\ |
| 31 | # move lon and lat to the first and second column | 31 | # move lon and lat to the first and second column |
| 32 | awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ | 32 | awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ |
| 33 | BEGIN{OFS=","}\ | 33 | BEGIN{OFS=","}\ |
| 34 | {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ | 34 | {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ |
| 35 | ' |\ | 35 | ' |\ |
| 36 | # change csv into array format, like [lon, lat, "field1", field2...] | 36 | # change csv into array format, like [lon, lat, "field1", field2...] |
| 37 | sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ | 37 | sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ |
| 38 | # wrap other fields as a json object, like [lon, lat, {...}] | 38 | # wrap other fields as a json object, like [lon, lat, {...}] |