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
|
|
|
# Get dataset config |
13
|
|
|
data_config = egon.data.config.datasets()["openstreetmap"] |
14
|
|
|
|
15
|
|
|
# Replace indices and primary keys |
16
|
|
|
for table in [ |
17
|
|
|
f"{data_config['original_data']['target']['table_prefix']}_" + suffix |
18
|
|
|
for suffix in ["line", "point", "polygon", "roads"] |
19
|
|
|
]: |
20
|
|
|
|
21
|
|
|
# Drop indices |
22
|
|
|
sql_statements = [f"DROP INDEX {table}_index;"] |
23
|
|
|
|
24
|
|
|
# Drop primary keys |
25
|
|
|
sql_statements.append(f"DROP INDEX {table}_pkey;") |
26
|
|
|
|
27
|
|
|
# Add primary key on newly created column "gid" |
28
|
|
|
sql_statements.append(f"ALTER TABLE public.{table} ADD gid SERIAL;") |
29
|
|
|
sql_statements.append( |
30
|
|
|
f"ALTER TABLE public.{table} ADD PRIMARY KEY (gid);" |
31
|
|
|
) |
32
|
|
|
sql_statements.append( |
33
|
|
|
f"ALTER TABLE public.{table} RENAME COLUMN way TO geom;" |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
# Add indices (GIST and GIN) |
37
|
|
|
sql_statements.append( |
38
|
|
|
f"CREATE INDEX {table}_geom_idx ON public.{table} " |
39
|
|
|
f"USING gist (geom);" |
40
|
|
|
) |
41
|
|
|
sql_statements.append( |
42
|
|
|
f"CREATE INDEX {table}_tags_idx ON public.{table} " |
43
|
|
|
f"USING GIN (tags);" |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
# Execute collected SQL statements |
47
|
|
|
for statement in sql_statements: |
48
|
|
|
db.execute_sql(statement) |
49
|
|
|
|
50
|
|
|
# Move table to schema "openstreetmap" |
51
|
|
|
db.execute_sql( |
52
|
|
|
f"CREATE SCHEMA IF NOT EXISTS {data_config['processed']['schema']};" |
53
|
|
|
) |
54
|
|
|
|
55
|
|
|
for out_table in data_config["processed"]["tables"]: |
56
|
|
|
sql_statement = ( |
57
|
|
|
f"ALTER TABLE public.{out_table} " |
58
|
|
|
f"SET SCHEMA {data_config['processed']['schema']};" |
59
|
|
|
) |
60
|
|
|
|
61
|
|
|
db.execute_sql(sql_statement) |
62
|
|
|
|