diff options
| author | Hsieh Chin Fan <typebrook@gmail.com> | 2020-08-28 10:19:25 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <typebrook@gmail.com> | 2020-08-28 10:19:25 +0800 |
| commit | c2c497544e005239411896632b592901e4fc475a (patch) | |
| tree | bffe344ed519f09d6c095cf4b49d0d7a6c03fbe9 /tools/csv | |
| parent | 70abb62283f1a224f8c194323b8c2056561bf354 (diff) | |
update
Diffstat (limited to 'tools/csv')
| -rwxr-xr-x | tools/csv/csv.move_column | 18 | ||||
| -rwxr-xr-x | tools/csv/csv.reorder | 17 | ||||
| -rwxr-xr-x | tools/csv/csv2geojson | 46 |
3 files changed, 81 insertions, 0 deletions
diff --git a/tools/csv/csv.move_column b/tools/csv/csv.move_column new file mode 100755 index 0000000..a62701f --- /dev/null +++ b/tools/csv/csv.move_column | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | # show each field with index in csv | ||
| 4 | echo -------------- > /dev/tty | ||
| 5 | head -1 < $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty | ||
| 6 | echo -------------- > /dev/tty | ||
| 7 | echo > /dev/tty | ||
| 8 | |||
| 9 | # get index of lon/lat column | ||
| 10 | read -p "Move which column? " origin_col | ||
| 11 | read -p "To which index? " new_col | ||
| 12 | |||
| 13 | cat $1 | | ||
| 14 | # move lon and lat to the first and second column | ||
| 15 | awk -F',' -v OFS="," -v origin_th=$origin_col -v new_th=$new_col '\ | ||
| 16 | {for (i=1; i<= NF; i++) if (i == new_th) printf $origin_th OFS $i OFS; else if (i == origin_th); else printf $i OFS; print ""}\ | ||
| 17 | ' |\ | ||
| 18 | sed 's/,$//g' | ||
diff --git a/tools/csv/csv.reorder b/tools/csv/csv.reorder new file mode 100755 index 0000000..8a64239 --- /dev/null +++ b/tools/csv/csv.reorder | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | # show each field with index in csv | ||
| 4 | echo -------------- > /dev/tty | ||
| 5 | head -1 < $1 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty | ||
| 6 | echo -------------- > /dev/tty | ||
| 7 | echo > /dev/tty | ||
| 8 | |||
| 9 | read -p "type column numbers by new order, like 3 2 1: " order | ||
| 10 | |||
| 11 | arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',') | ||
| 12 | |||
| 13 | cat $1 |\ | ||
| 14 | awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" |\ | ||
| 15 | tee /tmp/csv | ||
| 16 | |||
| 17 | echo "Also copied to /tmp/csv" > /dev/tty | ||
diff --git a/tools/csv/csv2geojson b/tools/csv/csv2geojson new file mode 100755 index 0000000..e742be6 --- /dev/null +++ b/tools/csv/csv2geojson | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | # -s to skip specify columns of longitude and latitude | ||
| 4 | for i in "$@" | ||
| 5 | do | ||
| 6 | case $i in | ||
| 7 | -s) | ||
| 8 | lon_col=0; lat_col=1 | ||
| 9 | shift;; | ||
| 10 | |||
| 11 | *) | ||
| 12 | csv=$i | ||
| 13 | shift;; | ||
| 14 | esac | ||
| 15 | done | ||
| 16 | |||
| 17 | # if no -s option, just read from input | ||
| 18 | if [ "$lon_col" != "0" ]; then | ||
| 19 | # show each field with index in csv | ||
| 20 | echo -------------- > /dev/tty | ||
| 21 | head -1 < $csv | tr ',' '\n' | nl > /dev/tty | ||
| 22 | echo -------------- > /dev/tty | ||
| 23 | echo > /dev/tty | ||
| 24 | |||
| 25 | # get index of lon/lat column | ||
| 26 | read -p "Number of longitude column: " lon_col | ||
| 27 | read -p "Number of latitude column: " lat_col | ||
| 28 | fi | ||
| 29 | |||
| 30 | (which dos2unix &>/dev/null && dos2unix <$csv || cat $csv) |\ | ||
| 31 | # move lon and lat to the first and second column | ||
| 32 | awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ | ||
| 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 ""}\ | ||
| 35 | ' |\ | ||
| 36 | # change csv into array format, like [lon, lat, "field1", field2...] | ||
| 37 | sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ | ||
| 38 | # wrap other fields as a json object, like [lon, lat, {...}] | ||
| 39 | jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\ | ||
| 40 | # create array of geojson point features | ||
| 41 | jq '{"type": "Feature", "properties": .[2], "geometry":{ "type": "Point", "coordinates": [(.[0] | tonumber), (.[1] | tonumber)] } }' |\ | ||
| 42 | # wrap features as geojson format | ||
| 43 | jq -s '{"type": "FeatureCollection", "features": .}' |\ | ||
| 44 | tee /tmp/geojson | ||
| 45 | |||
| 46 | echo stored into /tmp/geojson > /dev/tty | ||