aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/csv
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-02-14 13:33:23 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-02-14 13:33:23 +0800
commit6fae25b305d714b3ab7608fa003f1af9bf024545 (patch)
tree05507b2c0505659d2fd847ecce988dacab63a236 /bin/csv
parent41ad31a2dee9ff912f222652f022b4c55cddcbf7 (diff)
Rename tools into bin
Diffstat (limited to 'bin/csv')
-rwxr-xr-xbin/csv/csv.2geojson46
-rwxr-xr-xbin/csv/csv.move_column18
-rwxr-xr-xbin/csv/csv.reorder17
3 files changed, 81 insertions, 0 deletions
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 @@
1#! /bin/bash
2
3# -s to skip specify columns of longitude and latitude
4for i in "$@"
5do
6case $i in
7 -s)
8 lon_col=0; lat_col=1
9 shift;;
10
11 *)
12 csv=$i
13 shift;;
14esac
15done
16
17# if no -s option, just read from input
18if [ "$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
28fi
29
30(which dos2unix &>/dev/null && dos2unix <$csv || cat $csv) |\
31# move lon and lat to the first and second column
32awk -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...]
37sed 's/[^,]*/"\0"/g; s/.*/[\0]/g' |\
38# wrap other fields as a json object, like [lon, lat, {...}]
39jq -s '.[0][2:] as $fields | .[1:][] | [.[0], .[1], ([$fields, .[2:]] | transpose | map({(.[0]): .[1]}) | add)]' |\
40# create array of geojson point features
41jq '{"type": "Feature", "properties": .[2], "geometry":{ "type": "Point", "coordinates": [(.[0] | tonumber), (.[1] | tonumber)] } }' |\
42# wrap features as geojson format
43jq -s '{"type": "FeatureCollection", "features": .}' |\
44tee /tmp/geojson
45
46echo stored into /tmp/geojson > /dev/tty
diff --git a/bin/csv/csv.move_column b/bin/csv/csv.move_column
new file mode 100755
index 0000000..a62701f
--- /dev/null
+++ b/bin/csv/csv.move_column
@@ -0,0 +1,18 @@
1#! /bin/bash
2
3# show each field with index in csv
4echo -------------- > /dev/tty
5head -1 < $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty
6echo -------------- > /dev/tty
7echo > /dev/tty
8
9# get index of lon/lat column
10read -p "Move which column? " origin_col
11read -p "To which index? " new_col
12
13cat $1 |
14# move lon and lat to the first and second column
15awk -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 ' |\
18sed 's/,$//g'
diff --git a/bin/csv/csv.reorder b/bin/csv/csv.reorder
new file mode 100755
index 0000000..8a64239
--- /dev/null
+++ b/bin/csv/csv.reorder
@@ -0,0 +1,17 @@
1#! /bin/bash
2
3# show each field with index in csv
4echo -------------- > /dev/tty
5head -1 < $1 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty
6echo -------------- > /dev/tty
7echo > /dev/tty
8
9read -p "type column numbers by new order, like 3 2 1: " order
10
11arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',')
12
13cat $1 |\
14awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" |\
15tee /tmp/csv
16
17echo "Also copied to /tmp/csv" > /dev/tty