| Conditions | 3 |
| Total Lines | 59 |
| Code Lines | 37 |
| 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 | """The central module containing all code dealing with importing OSM data. |
||
| 45 | def to_postgres(num_processes=1, cache_size=4096): |
||
| 46 | """Import OSM data from a Geofabrik `.pbf` file into a PostgreSQL database. |
||
| 47 | |||
| 48 | Parameters |
||
| 49 | ---------- |
||
| 50 | num_processes : int, optional |
||
| 51 | Number of parallel processes used for processing during data import |
||
| 52 | cache_size: int, optional |
||
| 53 | Memory used during data import |
||
| 54 | |||
| 55 | """ |
||
| 56 | # Read database configuration from docker-compose.yml |
||
| 57 | docker_db_config = db.credentials() |
||
| 58 | |||
| 59 | # Get dataset config |
||
| 60 | data_config = egon.data.config.datasets() |
||
| 61 | osm_config = data_config["openstreetmap"]["original_data"] |
||
| 62 | |||
| 63 | if settings()["egon-data"]["--dataset-boundary"] == "Everything": |
||
| 64 | target_path = osm_config["target"]["path"] |
||
| 65 | else: |
||
| 66 | target_path = osm_config["target"]["path_testmode"] |
||
| 67 | |||
| 68 | input_file = Path(".") / "openstreetmap" / target_path |
||
| 69 | |||
| 70 | with resources.path( |
||
| 71 | "egon.data.importing.openstreetmap", osm_config["source"]["stylefile"] |
||
| 72 | ) as p: |
||
| 73 | stylefile = shutil.copy(p, Path(".") / "openstreetmap") |
||
| 74 | |||
| 75 | # Prepare osm2pgsql command |
||
| 76 | cmd = [ |
||
| 77 | "osm2pgsql", |
||
| 78 | "--create", |
||
| 79 | "--slim", |
||
| 80 | "--hstore-all", |
||
| 81 | "--number-processes", |
||
| 82 | f"{num_processes}", |
||
| 83 | "--cache", |
||
| 84 | f"{cache_size}", |
||
| 85 | "-H", |
||
| 86 | f"{docker_db_config['HOST']}", |
||
| 87 | "-P", |
||
| 88 | f"{docker_db_config['PORT']}", |
||
| 89 | "-d", |
||
| 90 | f"{docker_db_config['POSTGRES_DB']}", |
||
| 91 | "-U", |
||
| 92 | f"{docker_db_config['POSTGRES_USER']}", |
||
| 93 | "-p", |
||
| 94 | f"{osm_config['target']['table_prefix']}", |
||
| 95 | "-S", |
||
| 96 | stylefile, |
||
| 97 | f"{input_file.absolute()}", |
||
| 98 | ] |
||
| 99 | |||
| 100 | # Execute osm2pgsql for import OSM data |
||
| 101 | subprocess.run( |
||
| 102 | cmd, |
||
| 103 | env={"PGPASSWORD": docker_db_config["POSTGRES_PASSWORD"]}, |
||
| 104 | ) |
||
| 208 |