1
|
|
|
from egon.data import db |
2
|
|
|
import egon.data.config |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
def modify_tables(): |
6
|
|
|
"""Adjust primary keys, indices and schema of OSM tables. |
7
|
|
|
|
8
|
|
|
* The Column "gid" is added and used as the new primary key. |
9
|
|
|
* Indices (GIST, GIN) are reset |
10
|
|
|
* The tables are moved to the schema configured as the "output_schema". |
11
|
|
|
""" |
12
|
|
|
# Replace indices and primary keys |
13
|
|
|
for table in [ |
14
|
|
|
"osm_" + suffix for suffix in ["line", "point", "polygon", "roads"] |
15
|
|
|
]: |
16
|
|
|
|
17
|
|
|
# Drop indices |
18
|
|
|
sql_statements = [f"DROP INDEX {table}_index;"] |
19
|
|
|
|
20
|
|
|
# Drop primary keys |
21
|
|
|
sql_statements.append(f"DROP INDEX {table}_pkey;") |
22
|
|
|
|
23
|
|
|
# Add primary key on newly created column "gid" |
24
|
|
|
sql_statements.append(f"ALTER TABLE public.{table} ADD gid SERIAL;") |
25
|
|
|
sql_statements.append( |
26
|
|
|
f"ALTER TABLE public.{table} ADD PRIMARY KEY (gid);" |
27
|
|
|
) |
28
|
|
|
sql_statements.append( |
29
|
|
|
f"ALTER TABLE public.{table} RENAME COLUMN way TO geom;" |
30
|
|
|
) |
31
|
|
|
|
32
|
|
|
# Add indices (GIST and GIN) |
33
|
|
|
sql_statements.append( |
34
|
|
|
f"CREATE INDEX {table}_geom_idx ON public.{table} " |
35
|
|
|
f"USING gist (geom);" |
36
|
|
|
) |
37
|
|
|
sql_statements.append( |
38
|
|
|
f"CREATE INDEX {table}_tags_idx ON public.{table} " |
39
|
|
|
f"USING GIN (tags);" |
40
|
|
|
) |
41
|
|
|
|
42
|
|
|
# Execute collected SQL statements |
43
|
|
|
for statement in sql_statements: |
44
|
|
|
db.execute_sql(statement) |
45
|
|
|
|
46
|
|
|
# Get dataset config |
47
|
|
|
data_config = egon.data.config.datasets()["openstreetmap"][ |
48
|
|
|
"original_data" |
49
|
|
|
]["osm"] |
50
|
|
|
|
51
|
|
|
# Move table to schema "openstreetmap" |
52
|
|
|
db.execute_sql( |
53
|
|
|
f"CREATE SCHEMA IF NOT EXISTS {data_config['output_schema']};" |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
for out_table in data_config["output_tables"]: |
57
|
|
|
sql_statement = ( |
58
|
|
|
f"ALTER TABLE public.{out_table} " |
59
|
|
|
f"SET SCHEMA {data_config['output_schema']};" |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
db.execute_sql(sql_statement) |
63
|
|
|
|