-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-table-from-geojson
executable file
·59 lines (46 loc) · 1.54 KB
/
extract-table-from-geojson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
## Extract tabular data from a simple geojson:
##
## bash $ bash ./extract-table-from-geojson tst.json
## bash $ bash ./extract-table-from-geojson tst.json > tst.tsv
##
## Dt: 2022.06.27
##
## NOTE: This script cannot deal with arrays/objects in the GeoJSON!
## Most probably will need Python/R/JavaScript to extract more complex json objects from GeoJSON.
##
## GeoJSON Feature properties reference: https://datatracker.ietf.org/doc/html/rfc7946#section-3.2
##
set -euo pipefail
traditionalIFS="${IFS}"
IFS=$'\n\t'
tmpdir=$(mktemp -d /tmp/tmp.VL.$(basename $0).XXXXXXXXXX)
cleanup() {
rm -rf "${tmpdir}"
}
trap cleanup EXIT QUIT INT
## pushd ${tmpdir}
usage() {
echo "Usage: ${0} <geojson-file-name>"
exit 159
}
if [[ $# -ne 1 ]]; then
usage
fi
inputjson="$1"
## 1. first extract all the keys that are in the geojson
## 2. Copy the list printed from the previous command into the following command
## 3. It's better to output the resulting tsv to a file to be analyzed by R or pandas.
## The below script does all the three steps listed above.
cols=$( jq '[.features[].properties|keys]|unique[0][]' "${inputjson}" )
cols=${cols//$'"'/}
## cols=( ${cols//$'"'/} )
## declare -p cols
header="${cols//$'\n'/$'\t'}"
cols="[ .${cols//$'\n'/, .} ]"
cols=".features[].properties|${cols} | @tsv"
## declare -p cols
## outputname="${1%%.json}-$(date +%Y%m%d).tsv" ## Make sure extension matches what we are outputting...
## jq --raw-output "${cols}" "${inputjson}" >"${outputname}"
echo "${header}"
jq --raw-output "${cols}" "${inputjson}"