From 6fae25b305d714b3ab7608fa003f1af9bf024545 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 14 Feb 2023 13:33:23 +0800 Subject: Rename tools into bin --- bin/csv/csv.2geojson | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 bin/csv/csv.2geojson (limited to 'bin/csv/csv.2geojson') diff --git a/bin/csv/csv.2geojson b/bin/csv/csv.2geojson new file mode 100755 index 0000000..e742be6 --- /dev/null +++ b/bin/csv/csv.2geojson @@ -0,0 +1,46 @@ +#! /bin/bash + +# -s to skip specify columns of longitude and latitude +for i in "$@" +do +case $i in + -s) + lon_col=0; lat_col=1 + shift;; + + *) + csv=$i + shift;; +esac +done + +# if no -s option, just read from input +if [ "$lon_col" != "0" ]; then + # show each field with index in csv + echo -------------- > /dev/tty + head -1 < $csv | tr ',' '\n' | nl > /dev/tty + echo -------------- > /dev/tty + echo > /dev/tty + + # get index of lon/lat column + read -p "Number of longitude column: " lon_col + read -p "Number of latitude column: " lat_col +fi + +(which dos2unix &>/dev/null && dos2unix <$csv || cat $csv) |\ +# move lon and lat to the first and second column +awk -F',' -v lon_th=$lon_col -v lat_th=$lat_col '\ + BEGIN{OFS=","}\ + {printf $lon_th "," $lat_th; for (i=1; i<= NF; i++) if (i != lat_th && i != lon_th) printf "," $i; print ""}\ + ' |\ +# change csv into array format, like [lon, lat, "field1", field2...] +sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\ +# wrap other fields as a json object, like [lon, lat, {...}] +jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\ +# create array of geojson point features +jq '{"type": "Feature", "properties": .[2], "geometry":{ "type": "Point", "coordinates": [(.[0] | tonumber), (.[1] | tonumber)] } }' |\ +# wrap features as geojson format +jq -s '{"type": "FeatureCollection", "features": .}' |\ +tee /tmp/geojson + +echo stored into /tmp/geojson > /dev/tty -- cgit v1.2.3-70-g09d2