-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathload-regions
executable file
·60 lines (48 loc) · 1.65 KB
/
load-regions
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
58
59
60
#!/bin/bash
set -euo pipefail
export PGPASSWORD=$POSTGRES_PASSWORD;
if [ $# -lt 2 ]; then
echo "Usage $0 <ISO> <Country Name>"
exit 1
fi
COUNTRY_CODE=$1
COUNTRY_NAME=$2
#LEVELS=${2:-2,4}
LEVELS=(0,1)
echo " -> Importing $COUNTRY_NAME at levels $LEVELS"
# check if country was already imported
REGIONS=$(psql -d $POSTGRES_DB -U $POSTGRES_USER -h $POSTGRES_HOST -p $POSTGRES_PORT -t -A -c "SELECT id, country FROM regions WHERE country = '$COUNTRY_NAME';")
REGIONS_LEN=$(echo $REGIONS | wc -w | tr -d '[[:space:]]')
if [ ${REGIONS_LEN} -gt 0 ]; then
echo " -> Regions for country $COUNTRY_NAME already imported."
exit 0
fi
DATA_PATH=${DATA_PATH:-/data}/geojson
mkdir -p ${DATA_PATH}
# if the folder (still) doesn't exist then exit with error
if [[ ! -e ${DATA_PATH}/$COUNTRY_CODE ]]; then
echo " -> GeoJSON folder not found for country $COUNTRY_NAME"
exit 1
fi
IFS=',';
for i in $LEVELS; do
FILE=${DATA_PATH}/${COUNTRY_CODE}/${COUNTRY_CODE}_adm${i}.geojson
echo -n " -> Processing $FILE ... "
psql -q -d $POSTGRES_DB -U $POSTGRES_USER -h $POSTGRES_HOST -p $POSTGRES_PORT << SQL_SCRIPT
WITH data AS (SELECT \$$`cat $FILE`\$$::json AS fc)
INSERT INTO "regions" (country, name, admin_level, the_geom)
SELECT
'${COUNTRY_NAME}',
feat#>>'{properties,name}' AS name,
${i},
ST_SetSRID(ST_CollectionExtract(ST_Multi(ST_GeomFromGeoJSON(feat->>'geometry')), 3), 4326) as the_geom
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM data
) AS f;
SQL_SCRIPT
# TODO: print errors
echo " done!"
done;
echo "Don't forget to update the region previews by running"
echo " update-region-previews gen"