summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xscripts/csv2geojson29
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
3echo -------------- 3# show each field with index in csv
4head -1 $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' 4echo -------------- > /dev/tty
5echo -------------- 5head -1 $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty
6echo
7 6
7echo -------------- > /dev/tty
8echo > /dev/tty
9
10# get index of lon/lat column
8read -p "Number of latitude column: " lat_col 11read -p "Number of latitude column: " lat_col
9read -p "Number of longitude column: " lon_col 12read -p "Number of longitude column: " lon_col
10 13
11sed 's/[^,]*/"\0"/g' $1 |\ 14cat $1 |
12sed 's/.*/[\0]/g' |\ 15# move lon/lat column to the start
13sed '1d' |\ 16awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\
14jq --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
21sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\
22# wrap other fields as a json object, like [lon, lat, {...}]
23jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\
24# create array of geojson point features
25jq '{"type": "Feature", "properties": .[2], "geometry":{ "type": "Point", "coordinates": [(.[0] | tonumber), (.[1] | tonumber)] } }' |\
26# wrap features as geojson format
15jq -s '{"type": "FeatureCollection", "features": .}' 27jq -s '{"type": "FeatureCollection", "features": .}'
16#awk '{for (i=1; i<= NF; i++) if (i != 2 && i != 3) printf $i " "; printf [ " " {; print ""}' test' ]}'