diff options
author | typebrook <typebrook@gmail.com> | 2019-10-25 01:16:33 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-10-25 01:16:33 +0800 |
commit | 7e6f0cd00e2a604916552a7a3f48ce37bcae95c2 (patch) | |
tree | 138861bf0afe583e7e5409946ff9e0a31b57bad3 | |
parent | ac7fcfaf9abf12fa284652779c1e8ed922c3c7d9 (diff) |
update
-rwxr-xr-x | scripts/csv2geojson | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/scripts/csv2geojson b/scripts/csv2geojson index 47b481c..988ead0 100755 --- a/scripts/csv2geojson +++ b/scripts/csv2geojson | |||
@@ -1,23 +1,41 @@ | |||
1 | #! /bin/bash | 1 | #! /bin/bash |
2 | 2 | ||
3 | # show each field with index in csv | 3 | # -s to skip specify columns of longitude and latitude |
4 | echo -------------- > /dev/tty | 4 | for i in "$@" |
5 | head -1 $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty | 5 | do |
6 | case $i in | ||
7 | -s) | ||
8 | lon_col=0 | ||
9 | lat_col=1 | ||
10 | shift | ||
11 | ;; | ||
12 | *) | ||
13 | csv=$i | ||
14 | shift | ||
15 | ;; | ||
16 | esac | ||
17 | done | ||
6 | 18 | ||
7 | echo -------------- > /dev/tty | 19 | # if no -s option, just read from input |
8 | echo > /dev/tty | 20 | if [ "$lon_col" != "0" ]; then |
21 | # show each field with index in csv | ||
22 | echo -------------- > /dev/tty | ||
23 | head -1 < $csv | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty | ||
24 | echo -------------- > /dev/tty | ||
25 | echo > /dev/tty | ||
9 | 26 | ||
10 | # get index of lon/lat column | 27 | # get index of lon/lat column |
11 | read -p "Number of latitude column: " lat_col | 28 | read -p "Number of latitude column: " lat_col |
12 | read -p "Number of longitude column: " lon_col | 29 | read -p "Number of longitude column: " lon_col |
30 | fi | ||
13 | 31 | ||
14 | cat $1 | | 32 | cat $csv | |
15 | # move lon/lat column to the start | 33 | # move lon and lat to the first and second column |
16 | awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ | 34 | awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ |
17 | BEGIN{OFS=","}\ | 35 | BEGIN{OFS=","}\ |
18 | {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ | 36 | {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ |
19 | ' |\ | 37 | ' |\ |
20 | # change csv into array format | 38 | # change csv into array format, like [lon, lat, "field1", field2...] |
21 | sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ | 39 | sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ |
22 | # wrap other fields as a json object, like [lon, lat, {...}] | 40 | # wrap other fields as a json object, like [lon, lat, {...}] |
23 | jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\ | 41 | jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\ |