-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch
More file actions
executable file
·51 lines (47 loc) · 1.89 KB
/
Copy pathfetch
File metadata and controls
executable file
·51 lines (47 loc) · 1.89 KB
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
#!/usr/bin/env bash
# Fetch administrative boundary shapefiles from 政府資料開放平臺 (data.gov.tw).
#
# The actual files live on tgos.tw and their names carry a release date that
# changes on every update ( e.g. 鄉(鎮、市、區)界線1140318.zip ), so we resolve the
# current download url from the dataset api instead of hardcoding it:
#
# county - https://data.gov.tw/dataset/7442 直轄市、縣市界線(TWD97經緯度)
# town - https://data.gov.tw/dataset/7441 鄉鎮市區界線(TWD97經緯度)
# village - https://data.gov.tw/dataset/7438 村里界圖(TWD97經緯度)
set -e
api="https://data.gov.tw/api/v2/rest/dataset"
# resolve the SHP distribution url of a dataset. prints url, or nothing if not found.
resolve() {
curl -sfL --max-time 60 "$api/$1" | python3 -c '
import sys, json, urllib.parse
r = json.load(sys.stdin).get("result") or {}
for d in r.get("distribution", []):
if d.get("resourceFormat", "").upper() == "SHP":
u = d["resourceDownloadUrl"]
# the path may contain chinese characters / parentheses. percent-encode it.
p = urllib.parse.urlsplit(u)
print(urllib.parse.urlunsplit(p._replace(path=urllib.parse.quote(p.path))))
break
'
}
get() {
local lv=$1 dataset=$2 url
echo "resolving $lv (dataset $dataset) ..."
url=$(resolve "$dataset")
if [ -z "$url" ]; then
echo " !! cannot resolve SHP url for dataset $dataset. see https://data.gov.tw/dataset/$dataset" >&2
return 1
fi
echo " url: $url"
rm -rf "download/$lv"
mkdir -p "download/$lv"
echo "$url" > "download/$lv/source.txt"
curl -fL --max-time 900 -o "download/$lv/shp.zip" "$url"
# extract map files only: the zips also carry an xlsx whose filename is not valid
# utf-8, which makes unzip fail on macos.
( cd "download/$lv" && unzip -o -q -j shp.zip '*.shp' '*.dbf' '*.prj' '*.shx' )
}
get county 7442
get town 7441
get village 7438
echo "done."