-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathosm2pgsql_recommendation.py
94 lines (75 loc) · 3.17 KB
/
osm2pgsql_recommendation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Determine the appropriate osm2pgsql command to run from the osm2pgsql-tuner
project.
"""
import logging
import os
import osm2pgsql_tuner as tuner
import db, import_mode
LOGGER = logging.getLogger('pgosm-flex')
def osm2pgsql_recommendation(ram: float, pbf_filename: str, out_path: str,
import_mode: import_mode.ImportMode) -> str:
"""Returns recommended osm2pgsql command from the osm2pgsql-tuner
Python module: https://pypi.org/project/osm2pgsql-tuner/
Recommendation from Python project.
Public API available at https://osm2pgsql-tuner.com
Parameters
----------------------
ram : float
Total system RAM available in GB
pbf_filename : str
out_path : str
import_mode : import_mode.ImportMode
Returns
----------------------
osm2pgsql_cmd : str
"""
system_ram_gb = ram
if not os.path.isabs(pbf_filename):
pbf_file = os.path.join(out_path, pbf_filename)
else:
pbf_file = pbf_filename
osm_pbf_gb = os.path.getsize(pbf_file) / 1024 / 1024 / 1024
LOGGER.debug(f'PBF size (GB): {osm_pbf_gb}')
osm2pgsql_cmd = get_recommended_script(system_ram_gb,
osm_pbf_gb,
import_mode,
pbf_file,
out_path)
return osm2pgsql_cmd
def get_recommended_script(system_ram_gb: float,
osm_pbf_gb: float,
import_mode:import_mode.ImportMode,
pbf_filename: str,
output_path: str) -> str:
"""Generates recommended osm2pgsql command from osm2pgsql-tuner.
Parameters
-------------------------------
system_ram_gb : float
osm_pbf_gb : float
import_mode : import_mode.ImportMode
pbf_filename : str
Can be filename or absolute path.
output_path : str
Returns
-------------------------------
osm2pgsql_cmd : str
The osm2pgsql command to run, customized for this run of pgosm flex.
Warning: Do not print this string, it includes password in the
connection string. Might not matter too much with in-docker use and
throwaway containers/passwords, but intend to support external Postgres
connections.
"""
LOGGER.debug('Generating recommended osm2pgsql command')
rec = tuner.Recommendation(system_ram_gb=system_ram_gb,
osm_pbf_gb=osm_pbf_gb,
slim_no_drop=import_mode.slim_no_drop,
append_first_run=import_mode.append_first_run,
ssd=True)
osm2pgsql_cmd = rec.get_osm2pgsql_command(pbf_path=pbf_filename)
osm2pgsql_cmd = osm2pgsql_cmd.replace('~/pgosm-data', output_path)
LOGGER.debug(f'Generic command to run: {osm2pgsql_cmd}')
# Replace generic connection string with specific conn string
conn_string = db.connection_string()
osm2pgsql_cmd = osm2pgsql_cmd.replace('-d $PGOSM_CONN', f'-d {conn_string}')
# Warning: Do not print() this string any more! Includes password
return osm2pgsql_cmd