| Conditions | 4 |
| Total Lines | 59 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from urllib.request import urlretrieve |
||
| 64 | def post_import_modifications(): |
||
| 65 | """ |
||
| 66 | Adjust primary keys, indices and schema of OSM tables. |
||
| 67 | |||
| 68 | * Column gid is introduced as new primary key |
||
| 69 | * Indices (GIST, GIN) are reset |
||
| 70 | * Tables are move to schema 'openstreemap' |
||
| 71 | """ |
||
| 72 | # Replace indices and primary keys |
||
| 73 | for table in [ |
||
| 74 | "osm_" + suffix for suffix in ["line", "point", "polygon", "roads"] |
||
| 75 | ]: |
||
| 76 | |||
| 77 | # Drop indices |
||
| 78 | sql_statements = [f"DROP INDEX {table}_index;"] |
||
| 79 | |||
| 80 | # Drop primary keys |
||
| 81 | sql_statements.append(f"DROP INDEX {table}_pkey;") |
||
| 82 | |||
| 83 | # Add primary key on newly created column "gid" |
||
| 84 | sql_statements.append(f"ALTER TABLE public.{table} ADD gid SERIAL;") |
||
| 85 | sql_statements.append( |
||
| 86 | f"ALTER TABLE public.{table} ADD PRIMARY KEY (gid);" |
||
| 87 | ) |
||
| 88 | sql_statements.append( |
||
| 89 | f"ALTER TABLE public.{table} RENAME COLUMN way TO geom;" |
||
| 90 | ) |
||
| 91 | |||
| 92 | # Add indices (GIST and GIN) |
||
| 93 | sql_statements.append( |
||
| 94 | f"CREATE INDEX {table}_geom_idx ON public.{table} " |
||
| 95 | f"USING gist (geom);" |
||
| 96 | ) |
||
| 97 | sql_statements.append( |
||
| 98 | f"CREATE INDEX {table}_tags_idx ON public.{table} " |
||
| 99 | f"USING GIN (tags);" |
||
| 100 | ) |
||
| 101 | |||
| 102 | # Execute collected SQL statements |
||
| 103 | for statement in sql_statements: |
||
| 104 | db.execute_sql(statement) |
||
| 105 | |||
| 106 | # Get data set config |
||
| 107 | data_config = egon.data.config.datasets()["openstreetmap"][ |
||
| 108 | "original_data" |
||
| 109 | ]["osm"] |
||
| 110 | |||
| 111 | # Move table to schema "openstreetmap" |
||
| 112 | db.execute_sql( |
||
| 113 | f"CREATE SCHEMA IF NOT EXISTS {data_config['output_schema']};" |
||
| 114 | ) |
||
| 115 | |||
| 116 | for out_table in data_config["output_tables"]: |
||
| 117 | sql_statement = ( |
||
| 118 | f"ALTER TABLE public.{out_table} " |
||
| 119 | f"SET SCHEMA {data_config['output_schema']};" |
||
| 120 | ) |
||
| 121 | |||
| 122 | db.execute_sql(sql_statement) |
||
| 123 | |||
| 226 |