forked from giggls/mapnik-german-l10n
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_osml10n_extension.sh
executable file
·84 lines (73 loc) · 3.68 KB
/
gen_osml10n_extension.sh
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
# generate psql extension "osml10n"
# from plpgsql scripts
# version must be given as parameter
if [ $# -ne 2 ]; then
echo "usage: genextension.sh <data_target_dir> <version>" >&2
exit 1
fi
# check if commands we need are available
for cmd in curl sed basename; do
if ! command -v $cmd >/dev/null; then
echo "ERROR: command >>$cmd<< not found, please install!" >&2
exit 1
fi
done
# download country_osm_grid.sql from nominatim if not available
if ! [ -f "country_osm_grid.sql" ]; then
rm -f country_osm_grid.sql
echo -n "Trying to download country_grid.sql.gz from nominatim.org... "
curl -sL https://www.nominatim.org/data/country_grid.sql.gz |gzip -d >country_osm_grid.sql
if ! [ -s country_osm_grid.sql ]; then
rm -f country_osm_grid.sql
echo "failed."
exit 1
else
echo "done."
fi
fi
SCRIPTS="geo_transliterate.sql get_country_name.sql get_country.sql charset_helpers.sql street_abbrv.sql get_localized_name_from_tags.sql"
(
echo "-- complain if script is sourced in psql, rather than via CREATE EXTENSION"
echo '\echo Use "CREATE EXTENSION osml10n" to load this file. \quit'
echo
echo "-- enable ICU any-latin transliteration function -----------------------------------------------------------------"
echo
echo "CREATE OR REPLACE FUNCTION osml10n_kanji_transcript(text)RETURNS text AS"
echo "'\$libdir/osml10n_kanjitranscript', 'osml10n_kanji_transcript'"
echo "LANGUAGE C STRICT;"
echo
echo "-- enable libkakasi based kanji transcription function -----------------------------------------------------------------"
echo
echo "CREATE OR REPLACE FUNCTION osml10n_translit(text)RETURNS text AS"
echo "'\$libdir/osml10n_translit', 'osml10n_translit'"
echo "LANGUAGE C STRICT;" ) >>osml10n--$2.sql
echo "-- country_osm_grid.sql -----------------------------------------------------------------" >>osml10n--$2.sql
sed -e '/^COPY.*$/,/^\\\.$/d;//d' -e 's/CREATE TABLE country_osm_grid/CREATE TABLE IF NOT EXISTS country_osm_grid/g' country_osm_grid.sql |grep -v -e '^--' |grep -v 'CREATE INDEX' | cat -s >>osml10n--$2.sql
echo "DELETE from country_osm_grid;" >>osml10n--$2.sql
echo -e "COPY country_osm_grid (country_code, area, geometry) FROM '$1/osml10n_country_osm_grid.data';\n" >>osml10n--$2.sql
echo -e "DROP INDEX IF EXISTS idx_country_osm_grid_geometry;" >>osml10n--$2.sql
grep 'CREATE INDEX' country_osm_grid.sql >>osml10n--$2.sql
echo "GRANT SELECT on country_osm_grid to public;" >>osml10n--$2.sql
echo -e "\n-- country_languages table from http://wiki.openstreetmap.org/wiki/Nominatim/Country_Codes -----------------------------" >>osml10n--$2.sql
echo "CREATE TABLE IF NOT EXISTS country_languages(iso text, langs text[]);" >>osml10n--$2.sql
echo "DELETE from country_languages;" >>osml10n--$2.sql
echo "COPY country_languages (iso, langs) FROM '$1/country_languages.data';" >>osml10n--$2.sql
# for now we need to force srid here because boundaries/hkmo2psql.py does not include srid in geometry output
echo -e "UPDATE country_osm_grid SET geometry=ST_SetSRID(geometry,4326);\n" >>osml10n--$2.sql
echo -e "GRANT SELECT on country_languages to public;\n" >>osml10n--$2.sql
for f in $SCRIPTS; do
echo "" >>osml10n--$2.sql
echo "-- pl/pgSQL code from file $f -----------------------------------------------------------------" >>osml10n--$2.sql
cat plpgsql/$f >>osml10n--$2.sql
done
echo "
-- function osml10n_version -----------------------------------------------------------------
CREATE or REPLACE FUNCTION osml10n_version() RETURNS TEXT AS \$\$
BEGIN
RETURN '$2';
END;
\$\$ LANGUAGE 'plpgsql' IMMUTABLE;
" >>osml10n--$2.sql
sed '/^COPY.*$/,/^\\\.$/!d;//d' country_osm_grid.sql >osml10n_country_osm_grid.data
cat boundaries/boundaries.data >>osml10n_country_osm_grid.data