diff options
author | typebrook <typebrook@gmail.com> | 2019-10-25 00:09:50 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-10-25 00:09:50 +0800 |
commit | ac7fcfaf9abf12fa284652779c1e8ed922c3c7d9 (patch) | |
tree | ab6a164e384dd0c89e4f6ddeee3feaf7df23072e /scripts | |
parent | 687521c6a1d401ba03abdf24b0de0ab3032aa2ec (diff) |
update
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/csv2geojson | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/scripts/csv2geojson b/scripts/csv2geojson index 90e6141..47b481c 100755 --- a/scripts/csv2geojson +++ b/scripts/csv2geojson | |||
@@ -1,16 +1,27 @@ | |||
1 | #! /bin/bash | 1 | #! /bin/bash |
2 | 2 | ||
3 | echo -------------- | 3 | # show each field with index in csv |
4 | head -1 $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' | 4 | echo -------------- > /dev/tty |
5 | echo -------------- | 5 | head -1 $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty |
6 | echo | ||
7 | 6 | ||
7 | echo -------------- > /dev/tty | ||
8 | echo > /dev/tty | ||
9 | |||
10 | # get index of lon/lat column | ||
8 | read -p "Number of latitude column: " lat_col | 11 | read -p "Number of latitude column: " lat_col |
9 | read -p "Number of longitude column: " lon_col | 12 | read -p "Number of longitude column: " lon_col |
10 | 13 | ||
11 | sed 's/[^,]*/"\0"/g' $1 |\ | 14 | cat $1 | |
12 | sed 's/.*/[\0]/g' |\ | 15 | # move lon/lat column to the start |
13 | sed '1d' |\ | 16 | awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ |
14 | jq --arg LAT_INDEX $lat_col LON_INDEX $lon_col '. | {"type": "Feature", "properties": {}, "geometry":{ "type": "Point", "coordinates": [(.[LAT_INDEX] | tonumber), (.[LON_INDEX] | tonumber)] } }'|\ | 17 | BEGIN{OFS=","}\ |
18 | {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ | ||
19 | ' |\ | ||
20 | # change csv into array format | ||
21 | sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ | ||
22 | # 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)]' |\ | ||
24 | # create array of geojson point features | ||
25 | jq '{"type": "Feature", "properties": .[2], "geometry":{ "type": "Point", "coordinates": [(.[0] | tonumber), (.[1] | tonumber)] } }' |\ | ||
26 | # wrap features as geojson format | ||
15 | jq -s '{"type": "FeatureCollection", "features": .}' | 27 | jq -s '{"type": "FeatureCollection", "features": .}' |
16 | #awk '{for (i=1; i<= NF; i++) if (i != 2 && i != 3) printf $i " "; printf [ " " {; print ""}' test' ]}' | ||