From ddafd9c1f61bb2a359a254e99d637cd6dba4b6ce Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 17 Nov 2019 11:11:13 +0800 Subject: update --- scripts/csv.move_column | 18 +++++++++++++++ scripts/csv.reorder | 19 +++++++++++++++ scripts/csv/csv.move_column | 18 --------------- scripts/csv/csv.reorder | 19 --------------- scripts/csv/csv2geojson | 43 ---------------------------------- scripts/csv2geojson | 43 ++++++++++++++++++++++++++++++++++ scripts/osm/osm.pbf.update | 56 +++++++++++++++++++++++---------------------- 7 files changed, 109 insertions(+), 107 deletions(-) create mode 100755 scripts/csv.move_column create mode 100755 scripts/csv.reorder delete mode 100755 scripts/csv/csv.move_column delete mode 100755 scripts/csv/csv.reorder delete mode 100755 scripts/csv/csv2geojson create mode 100755 scripts/csv2geojson (limited to 'scripts') diff --git a/scripts/csv.move_column b/scripts/csv.move_column new file mode 100755 index 0000000..a62701f --- /dev/null +++ b/scripts/csv.move_column @@ -0,0 +1,18 @@ +#! /bin/bash + +# show each field with index in csv +echo -------------- > /dev/tty +head -1 < $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty +echo -------------- > /dev/tty +echo > /dev/tty + +# get index of lon/lat column +read -p "Move which column? " origin_col +read -p "To which index? " new_col + +cat $1 | +# move lon and lat to the first and second column +awk -F',' -v OFS="," -v origin_th=$origin_col -v new_th=$new_col '\ + {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 ""}\ + ' |\ +sed 's/,$//g' diff --git a/scripts/csv.reorder b/scripts/csv.reorder new file mode 100755 index 0000000..df928f1 --- /dev/null +++ b/scripts/csv.reorder @@ -0,0 +1,19 @@ +#! /bin/bash + +# show each field with index in csv +echo -------------- > /dev/tty +head -1 < $1 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty +echo -------------- > /dev/tty +echo > /dev/tty + +# get index of lon/lat column +read -p "type column numbers by new order, like 3 2 1: " order + +arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',') + +cat $1 |\ +awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" |\ +tee /tmp/csv |\ +echo "Also copied to /tmp/csv" > /dev/tty + + diff --git a/scripts/csv/csv.move_column b/scripts/csv/csv.move_column deleted file mode 100755 index a62701f..0000000 --- a/scripts/csv/csv.move_column +++ /dev/null @@ -1,18 +0,0 @@ -#! /bin/bash - -# show each field with index in csv -echo -------------- > /dev/tty -head -1 < $1 | sed 's/,/ /g' | awk '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty -echo -------------- > /dev/tty -echo > /dev/tty - -# get index of lon/lat column -read -p "Move which column? " origin_col -read -p "To which index? " new_col - -cat $1 | -# move lon and lat to the first and second column -awk -F',' -v OFS="," -v origin_th=$origin_col -v new_th=$new_col '\ - {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 ""}\ - ' |\ -sed 's/,$//g' diff --git a/scripts/csv/csv.reorder b/scripts/csv/csv.reorder deleted file mode 100755 index df928f1..0000000 --- a/scripts/csv/csv.reorder +++ /dev/null @@ -1,19 +0,0 @@ -#! /bin/bash - -# show each field with index in csv -echo -------------- > /dev/tty -head -1 < $1 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty -echo -------------- > /dev/tty -echo > /dev/tty - -# get index of lon/lat column -read -p "type column numbers by new order, like 3 2 1: " order - -arrange=$(echo $order | sed -r 's/([^ ]+)/$\1/g' | tr ' ' ',') - -cat $1 |\ -awk -F',' "BEGIN{OFS=\",\"}{print $arrange}" |\ -tee /tmp/csv |\ -echo "Also copied to /tmp/csv" > /dev/tty - - diff --git a/scripts/csv/csv2geojson b/scripts/csv/csv2geojson deleted file mode 100755 index 085e12e..0000000 --- a/scripts/csv/csv2geojson +++ /dev/null @@ -1,43 +0,0 @@ -#! /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 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty - echo -------------- > /dev/tty - echo > /dev/tty - - # get index of lon/lat column - read -p "Number of latitude column: " lat_col - read -p "Number of longitude column: " lon_col -fi - -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": .}' diff --git a/scripts/csv2geojson b/scripts/csv2geojson new file mode 100755 index 0000000..085e12e --- /dev/null +++ b/scripts/csv2geojson @@ -0,0 +1,43 @@ +#! /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 | awk -F',' '{for (i=1; i<=NF; i++) printf $i "_" i " "; print ""}' > /dev/tty + echo -------------- > /dev/tty + echo > /dev/tty + + # get index of lon/lat column + read -p "Number of latitude column: " lat_col + read -p "Number of longitude column: " lon_col +fi + +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": .}' diff --git a/scripts/osm/osm.pbf.update b/scripts/osm/osm.pbf.update index 694ae4d..599076f 100755 --- a/scripts/osm/osm.pbf.update +++ b/scripts/osm/osm.pbf.update @@ -1,30 +1,32 @@ - PBF_FILE=$1 - GEOFABRICK_SERVER=http://download.geofabrik.de/asia/taiwan-updates +#!/bin/sh - # get next sequence number and store it into NEW_SEQ - osmium fileinfo $PBF_FILE | \ - grep osmosis_replication_sequence_number | \ - cut -d'=' -f2 | \ - sed 's/$/+1/' | bc | \ - read NEW_SEQ +PBF_FILE=$1 +GEOFABRICK_SERVER=http://download.geofabrik.de/asia/taiwan-updates - # while server has osc file with given sequence number, - # get it and do file update - while - SEQ_PATH=$(echo $NEW_SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/') - STATE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.state.txt - echo $STATE_URL - [ $(curl.code $STATE_URL) != "404" ] - do - mkdir -p changes - CHANGE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.osc.gz - echo $CHANGE_URL - curl -o changes/$NEW_SEQ.osc.gz $CHANGE_URL && \ - osmium apply-changes $PBF_FILE changes/$NEW_SEQ.osc.gz \ - --output-header=osmosis_replication_sequence_number=$NEW_SEQ \ - --overwrite \ - --output $NEW_SEQ.osm.pbf +# get next sequence number and store it into NEW_SEQ +osmium fileinfo $PBF_FILE | \ +grep osmosis_replication_sequence_number | \ +cut -d'=' -f2 | \ +sed 's/$/+1/' | bc | \ +read NEW_SEQ - PBF_FILE=$NEW_SEQ.osm.pbf - ((NEW_SEQ++)) - done +# while server has osc file with given sequence number, +# get it and do file update +while + SEQ_PATH=$(echo $NEW_SEQ | sed -r 's/(.{1})(.{3})/00\1\/\2/') + STATE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.state.txt + echo $STATE_URL + [ $(curl.code $STATE_URL) != "404" ] +do + mkdir -p changes + CHANGE_URL=$GEOFABRICK_SERVER/000/$SEQ_PATH.osc.gz + echo $CHANGE_URL + curl -o changes/$NEW_SEQ.osc.gz $CHANGE_URL && \ + osmium apply-changes $PBF_FILE changes/$NEW_SEQ.osc.gz \ + --output-header=osmosis_replication_sequence_number=$NEW_SEQ \ + --overwrite \ + --output $NEW_SEQ.osm.pbf + + PBF_FILE=$NEW_SEQ.osm.pbf + ((NEW_SEQ++)) +done -- cgit v1.2.3-70-g09d2