Skip to content

Commit

Permalink
Merge pull request #417 from OpenHistoricalMap/imposm/tables
Browse files Browse the repository at this point in the history
Refactoring imposm3 config
  • Loading branch information
Rub21 authored Nov 29, 2024
2 parents fc9fbdd + f97ad2b commit c4fa022
Show file tree
Hide file tree
Showing 36 changed files with 1,958 additions and 1,385 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/chartpress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
- 'main'
- 'staging'
- 'development'
- 'tiler-cache'
- 'imposm/tables'
jobs:
build:
runs-on: ubuntu-20.04
Expand Down
73 changes: 73 additions & 0 deletions images/tiler-imposm/build_imposm3_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json
import os

def load_json(file_path):
"""Load a JSON file."""
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)

def merge_configs(template, configs):
"""Merge multiple JSON configs into the template."""
merged = template.copy()

for config in configs:
if 'generalized_tables' in config:
merged['generalized_tables'].update(config['generalized_tables'])

for config in configs:
if 'tables' in config:
merged['tables'].update(config['tables'])

return merged

def main(folder_path, template_path, output_path):
"""Main function to merge JSON configs."""
template = load_json(template_path)

import_layers = os.getenv("IMPOSM3_IMPORT_LAYERS", "all").strip()

configs = []
if "all" in import_layers:
print("Importing all layer files.")
# Import all JSON files in the folder
json_files = [f for f in os.listdir(folder_path) if f.endswith('.json')]
for json_file in json_files:
file_path = os.path.join(folder_path, json_file)
try:
print(f"Importing {file_path}")
configs.append(load_json(file_path))
except json.JSONDecodeError as e:
print(f"Error reading {file_path}: {e}")
else:
# Import only specified layers
layer_names = [layer.strip() for layer in import_layers.split(",") if layer.strip()]
if not layer_names:
print("No layers specified in IMPOSM3_IMPORT_LAYERS. Exiting.")
return

for layer_name in layer_names:
file_path = os.path.join(folder_path, f"{layer_name}.json")
if os.path.exists(file_path):
try:
print(f"Importing {file_path}")
configs.append(load_json(file_path))
except json.JSONDecodeError as e:
print(f"Error reading {file_path}: {e}")
else:
print(f"Layer config file {file_path} not found. Skipping.")

if not configs:
print("No valid layer configurations found. Exiting.")
return

merged_config = merge_configs(template, configs)
with open(output_path, 'w', encoding='utf-8') as output_file:
json.dump(merged_config, output_file, indent=2)

print(f"Merged configuration saved to {output_path}")

if __name__ == "__main__":
folder_path = "./config/layers"
template_path = "./config/imposm3.template.json"
output_path = "./config/imposm3.json"
main(folder_path, template_path, output_path)
Loading

0 comments on commit c4fa022

Please sign in to comment.