| Conditions | 4 |
| Total Lines | 58 |
| 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 |
||
| 63 | def post_import_modifications(): |
||
| 64 | """Adjust primary keys, indices and schema of OSM tables. |
||
| 65 | |||
| 66 | * The Column "gid" is added and used as the new primary key. |
||
| 67 | * Indices (GIST, GIN) are reset |
||
| 68 | * The tables are moved to the schema configured as the "output_schema". |
||
| 69 | """ |
||
| 70 | # Replace indices and primary keys |
||
| 71 | for table in [ |
||
| 72 | "osm_" + suffix for suffix in ["line", "point", "polygon", "roads"] |
||
| 73 | ]: |
||
| 74 | |||
| 75 | # Drop indices |
||
| 76 | sql_statements = [f"DROP INDEX {table}_index;"] |
||
| 77 | |||
| 78 | # Drop primary keys |
||
| 79 | sql_statements.append(f"DROP INDEX {table}_pkey;") |
||
| 80 | |||
| 81 | # Add primary key on newly created column "gid" |
||
| 82 | sql_statements.append(f"ALTER TABLE public.{table} ADD gid SERIAL;") |
||
| 83 | sql_statements.append( |
||
| 84 | f"ALTER TABLE public.{table} ADD PRIMARY KEY (gid);" |
||
| 85 | ) |
||
| 86 | sql_statements.append( |
||
| 87 | f"ALTER TABLE public.{table} RENAME COLUMN way TO geom;" |
||
| 88 | ) |
||
| 89 | |||
| 90 | # Add indices (GIST and GIN) |
||
| 91 | sql_statements.append( |
||
| 92 | f"CREATE INDEX {table}_geom_idx ON public.{table} " |
||
| 93 | f"USING gist (geom);" |
||
| 94 | ) |
||
| 95 | sql_statements.append( |
||
| 96 | f"CREATE INDEX {table}_tags_idx ON public.{table} " |
||
| 97 | f"USING GIN (tags);" |
||
| 98 | ) |
||
| 99 | |||
| 100 | # Execute collected SQL statements |
||
| 101 | for statement in sql_statements: |
||
| 102 | db.execute_sql(statement) |
||
| 103 | |||
| 104 | # Get dataset config |
||
| 105 | data_config = egon.data.config.datasets()["openstreetmap"][ |
||
| 106 | "original_data" |
||
| 107 | ]["osm"] |
||
| 108 | |||
| 109 | # Move table to schema "openstreetmap" |
||
| 110 | db.execute_sql( |
||
| 111 | f"CREATE SCHEMA IF NOT EXISTS {data_config['output_schema']};" |
||
| 112 | ) |
||
| 113 | |||
| 114 | for out_table in data_config["output_tables"]: |
||
| 115 | sql_statement = ( |
||
| 116 | f"ALTER TABLE public.{out_table} " |
||
| 117 | f"SET SCHEMA {data_config['output_schema']};" |
||
| 118 | ) |
||
| 119 | |||
| 120 | db.execute_sql(sql_statement) |
||
| 121 | |||
| 220 |