Skip to content

Commit

Permalink
[FIX] country & regions constants and mapping (#113)
Browse files Browse the repository at this point in the history
* correct contries/region constants and mapping

- correct countries per region in constants.py and constants_functions.py
- introduce new function to get path of static tile .json file
- use new function

* unittests for country/region constants and mapping

* cherry pick folder functions

- from dabe117

* launch config for mexico

* introducing northamerica_us geofabrik

- for processing "mexico -bc"
- california etc. have not been able to download

* mapping for new_mexico and function to build url (unittesting!)
  • Loading branch information
treee111 authored May 2, 2022
1 parent 528bf44 commit e45d89c
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 59 deletions.
15 changes: 14 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@
"C:\\VSCode\\python\\wahooMapsCreator\\output\\138\\100\\land.shp",
"C:\\VSCode\\python\\wahooMapsCreator\\output\\138\\100\\land1.osm"
]
}
},
{
"name": "mexico with bc",
"type": "python",
"request": "launch",
"program": "${workspaceRoot}/wahoo_map_creator.py",
"console": "integratedTerminal",
"args": [
"mexico",
"-md",
"100",
"-bc"
]
},
]
}
27 changes: 20 additions & 7 deletions common_python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
'virgin_islands_u.s.': 'central-america',
'western_sahara': 'morocco',
'qatar': 'gcc-states',
'åland': 'finland'
'åland': 'finland',
'new_mexico': 'new-mexico'
}

continents = ['europe', 'unitedstates', 'north-america', 'south-america', 'asia', 'oceania',
Expand Down Expand Up @@ -100,7 +101,7 @@
'bermuda', 'british_virgin_islands', 'canada', 'cayman_islands', 'costa_rica',
'cuba', 'dominica', 'dominican_republic', 'el_salvador', 'greenland', 'grenada',
'guadeloupe', 'guatemala', 'haiti', 'honduras', 'jamaica', 'martinique',
'mexico', 'montserrat', 'nicaragua', 'panama', 'saint-barth‚lemy', 'saint-martin',
'mexico', 'montserrat', 'nicaragua', 'panama', 'saint-barthélemy', 'saint-martin',
'saint_kitts_and_nevis', 'saint_lucia', 'saint_vincent_and_the_grenadines',
'sint_maarten', 'turks_and_caicos_islands', 'virgin_islands_u.s.']

Expand All @@ -110,11 +111,11 @@
'pitcairn_islands', 'samoa', 'solomon_islands', 'tokelau', 'tonga', 'tuvalu',
'united_states_minor_outlying_islands', 'vanuatu', 'wallis_and_futuna']

southamerica = ['australia', 'cook_islands', 'fiji', 'french_polynesia', 'kiribati',
'marshall_islands', 'micronesia', 'nauru', 'new_caledonia', 'new_zealand', 'niue',
'norfolk_island', 'northern_mariana_islands', 'palau', 'papua_new_guinea',
'pitcairn_islands', 'samoa', 'solomon_islands', 'tokelau', 'tonga', 'tuvalu',
'united_states_minor_outlying_islands', 'vanuatu', 'wallis_and_futuna']
southamerica = ['argentina', 'aruba', 'bolivia', 'bonaire_saint_eustatius_and_saba', 'brazil',
'chile', 'clipperton_island', 'colombia', 'curacao', 'ecuador',
'falkland_islands', 'french_guiana', 'guyana', 'paraguay', 'peru',
'saint_pierre_and_miquelon', 'suriname', 'trinidad_and_tobago',
'uruguay', 'venezuela']

unitedstates = ['alabama', 'alaska', 'american_samoa', 'arizona', 'arkansas',
'california', 'colorado', 'commonwealth_of_the_northern_mariana_islands', 'connecticut',
Expand Down Expand Up @@ -168,6 +169,18 @@
northamerica_geofabrik = ['canada', 'greenland', 'mexico', 'us midwest', 'us northeast',
'us pacific', 'us south', 'us west']

northamerica_us_geofabrik = ['alabama', 'alaska', 'arizona', 'arkansas', 'california', 'colorado',
'connecticut', 'delaware', 'district-of-columbia', 'florida',
'georgia', 'hawaii', 'idaho', 'illinois', 'indiana', 'iowa', 'kansas',
'kentucky', 'louisiana', 'maine', 'maryland', 'massachusetts',
'michigan', 'minnesota', 'mississippi', 'missouri', 'montana',
'nebraska', 'nevada', 'new-hampshire', 'new-jersey', 'new-mexico',
'new-york', 'north-carolina', 'north-dakota', 'ohio', 'oklahoma'
'oregon', 'pennsylvania', 'puerto-rico', 'rhode-island',
'south-carolina', 'south-dakota', 'tennessee', 'texas',
'us-virgin-islands', 'utah', 'vermont', 'virginia', 'washington',
'west-virginia', 'wisconsin', 'wyoming']

southamerica_geofabrik = ['argentina', 'bolivia', 'brazil', 'chile', 'colombia', 'ecuador',
'paraguay', 'peru', 'suriname', 'uruguay', 'venezuela']

Expand Down
20 changes: 16 additions & 4 deletions common_python/constants_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

# import official python packages
import sys
import os

# import custom python packages
from common_python import constants
from common_python import file_directory_functions as fd_fct


def get_region_of_country(county):
Expand All @@ -24,13 +26,13 @@ def get_region_of_country(county):
if county in constants.europe:
region = 'europe'
if county in constants.northamerica:
region = 'north-america'
region = 'north_america'
if county in constants.oceania:
region = 'oceania'
if county in constants.southamerica:
region = 'south-america'
region = 'south_america'
if county in constants.unitedstates:
region = 'united-states'
region = 'united_states'

return region

Expand Down Expand Up @@ -58,6 +60,8 @@ def get_geofabrik_region_of_country(input_county):
region = 'europe'
if c_translated in constants.northamerica_geofabrik:
region = 'north-america'
if c_translated in constants.northamerica_us_geofabrik:
region = 'north-america/us'
if c_translated in constants.southamerica_geofabrik:
region = 'south-america'
if c_translated in constants.germany_subregions_geofabrik:
Expand All @@ -73,7 +77,7 @@ def get_geofabrik_region_of_country(input_county):

def translate_country_input_to_geofabrik(county):
"""
translates the given country to the geofabrik country
translates the given country to the geofabrik country
the geofabrik country is sometimes written different
"""
# search for user entered country name in translated (to geofabrik). if match continue with matched else continue with user entered country
Expand All @@ -84,3 +88,11 @@ def translate_country_input_to_geofabrik(county):
c_translated = county

return c_translated


def get_path_to_static_tile_json(country):
"""
return the path to the static .json file with the files for the given country
"""
return os.path.join(fd_fct.COMMON_DIR, 'json',
get_region_of_country(country), country + '.json')
18 changes: 13 additions & 5 deletions common_python/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,26 @@ def download_osm_pbf_file(country):
"""
print(f'+ Trying to download missing map of {country}.')
# get .osm.pbf region of country
url = build_url_for_country_osm_pbf_download(country)
# download URL to file
map_file_path = os.path.join(
fd_fct.MAPS_DIR, f'{country}' + '-latest.osm.pbf')
download_file(map_file_path, url, False)
return map_file_path


def build_url_for_country_osm_pbf_download(country):
"""
build the geofabrik download url to a countries' OSM file
"""
transl_c = const_fct.translate_country_input_to_geofabrik(country)
region = const_fct.get_geofabrik_region_of_country(country)
if region != 'no':
url = 'https://download.geofabrik.de/' + region + \
'/' + transl_c + '-latest.osm.pbf'
else:
url = 'https://download.geofabrik.de/' + transl_c + '-latest.osm.pbf'
# download URL to file
map_file_path = os.path.join(
fd_fct.MAPS_DIR, f'{transl_c}' + '-latest.osm.pbf')
download_file(map_file_path, url, False)
return map_file_path
return url


class Downloader:
Expand Down
20 changes: 20 additions & 0 deletions common_python/file_directory_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# import official python packages
import json
import os
from os.path import isfile, join
import subprocess
import sys
import zipfile
Expand Down Expand Up @@ -124,3 +125,22 @@ def write_to_file(file_path, request):
with open(file_path, 'wb') as file_handle:
for chunk in request.iter_content(chunk_size=1024*100):
file_handle.write(chunk)


def get_folders_in_folder(folder):
"""
return foldernames of given folder without path as list
"""
onlyfolders = [f for f in os.listdir(
folder) if not isfile(join(folder, f))]

return onlyfolders


def get_files_in_folder(folder):
"""
return filenames of given folder without path as list
"""
onlyfiles = [f for f in os.listdir(folder) if isfile(join(folder, f))]

return onlyfiles
4 changes: 2 additions & 2 deletions common_python/osm_maps_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def process_input(self, input_argument, calc_border_countries):

# option 2b: use static json files in the repo to calculate relevant tiles
else:
json_file_path = os.path.join(fd_fct.COMMON_DIR, 'json',
const_fct.get_region_of_country(input_argument), input_argument + '.json')
json_file_path = const_fct.get_path_to_static_tile_json(
input_argument)
self.tiles = fd_fct.read_json_file(json_file_path)

# country name is the input argument
Expand Down
30 changes: 0 additions & 30 deletions common_resources/json/south_america/south-america

This file was deleted.

Loading

0 comments on commit e45d89c

Please sign in to comment.