Skip to content

Commit

Permalink
Fix land polygons download/extraction handling.
Browse files Browse the repository at this point in the history
Downloaded to common_resources but the constant which checks file existance
already points to common_download.
  • Loading branch information
mweirauch committed Oct 11, 2021
1 parent 4f57dec commit 05a2b3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- have more different tag-wahoo-xml files and move them to folders. Modify tag-wahoo.xml to differently display some "place"-tags [PR34](https://github.com/treee111/wahooMapsCreator/issues/34)
### Changed
- move "central" download functions from downloader to file_directory_functions
- move files from common_resources into two new folders: common_download & common_python and into tooling [PR33](https://github.com/treee111/wahooMapsCreator/issues/33)
- move files from common_resources into two new folders: common_download & common_python and into tooling [PR33](https://github.com/treee111/wahooMapsCreator/pull/33)
- fix bug in download handling of land polygons file which was introduced with [PR33](https://github.com/treee111/wahooMapsCreator/pull/33) [PR35](https://github.com/treee111/wahooMapsCreator/pull/35)

## [0.8.1] - 2021-09-10
### Fixed
Expand Down
47 changes: 24 additions & 23 deletions common_python/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def __init__(self, max_days_old, force_download, tiles, border_countries):
self.tiles_from_json = tiles
self.border_countries = border_countries


def check_and_download_files_if_needed(self):
"""
check land_poligons and OSM map files if not existing or are not up-to-date
Expand All @@ -46,10 +45,9 @@ def check_and_download_files_if_needed(self):
self.download_land_poligons_file()
force_processing = True

# logging
# logging
print('# check land_polygons.shp file: OK')


if self.check_osm_pbf_file() is True or self.force_download is True:
self.download_osm_pbf_file()
force_processing = True
Expand All @@ -62,7 +60,6 @@ def check_and_download_files_if_needed(self):

return force_processing


def check_poligons_file(self):
"""
check if land_poligons file is up-to-date
Expand All @@ -75,23 +72,22 @@ def check_poligons_file(self):
try:
chg_time = os.path.getctime(fd_fct.LAND_POLYGONS_PATH)
if older_than_x_days(chg_time, self.max_days_old):
print ('# Deleting old land polygons file')
print('# Deleting old land polygons file')
os.remove(fd_fct.LAND_POLYGONS_PATH)
need_to_download = True

except:
need_to_download = True

# if land poligons file does not exists --> download
if not os.path.exists(fd_fct.LAND_POLYGONS_PATH) or \
not os.path.isfile(fd_fct.LAND_POLYGONS_PATH):
if not os.path.exists(fd_fct.LAND_POLYGONS_PATH) or \
not os.path.isfile(fd_fct.LAND_POLYGONS_PATH):
need_to_download = True
# logging
print('# land_polygons.shp file needs to be downloaded')

return need_to_download


def download_land_poligons_file(self):
"""
download land_poligons file
Expand All @@ -102,15 +98,15 @@ def download_land_poligons_file(self):
url = 'https://osmdata.openstreetmap.de/download/land-polygons-split-4326.zip'

# download URL to file
land_poligons_file_path = os.path.join (fd_fct.COMMON_DIR, 'land-polygons-split-4326.zip')
fd_fct.download_url_to_file(url, land_poligons_file_path)
land_poligons_download_path = os.path.join(
fd_fct.COMMON_DL_DIR, 'land-polygons-split-4326.zip')
fd_fct.download_url_to_file(url, land_poligons_download_path)

# unpack it - should work on macOS and Windows
fd_fct.unzip(land_poligons_file_path, fd_fct.COMMON_DIR)
fd_fct.unzip(land_poligons_download_path, fd_fct.COMMON_DL_DIR)

# delete .zip file
os.remove(os.path.join (fd_fct.COMMON_DIR,
'land-polygons-split-4326.zip'))
os.remove(land_poligons_download_path)

# Check if land polygons file exists
if not os.path.isfile(fd_fct.LAND_POLYGONS_PATH):
Expand All @@ -119,7 +115,6 @@ def download_land_poligons_file(self):
else:
print(f'+ Downloaded: {fd_fct.LAND_POLYGONS_PATH}')


def check_osm_pbf_file(self):
"""
check if the relevant countries' OSM files are up-to-date
Expand All @@ -139,18 +134,22 @@ def check_osm_pbf_file(self):
# check for already existing .osm.pbf file
map_file_path = glob.glob(f'{fd_fct.MAPS_DIR}/{transl_c}*.osm.pbf')
if len(map_file_path) != 1:
map_file_path = glob.glob(f'{fd_fct.MAPS_DIR}/**/{transl_c}*.osm.pbf')
map_file_path = glob.glob(
f'{fd_fct.MAPS_DIR}/**/{transl_c}*.osm.pbf')

# delete .osm.pbf file if out of date
if len(map_file_path) == 1 and os.path.isfile(map_file_path[0]):
chg_time = os.path.getctime(map_file_path[0])
if older_than_x_days(chg_time, self.max_days_old) or self.force_download is True:
print(f'+ mapfile for {transl_c}: deleted. Input: {country}.')
print(
f'+ mapfile for {transl_c}: deleted. Input: {country}.')
os.remove(map_file_path[0])
need_to_download = True
else:
self.border_countries[country] = {'map_file':map_file_path[0]}
print(f'+ mapfile for {transl_c}: up-to-date. Input: {country}.')
self.border_countries[country] = {
'map_file': map_file_path[0]}
print(
f'+ mapfile for {transl_c}: up-to-date. Input: {country}.')

# mark country .osm.pbf file for download if there exists no file or it is no file
map_file_path = self.border_countries[country].get('map_file')
Expand All @@ -162,7 +161,6 @@ def check_osm_pbf_file(self):

return need_to_download


def download_osm_pbf_file(self):
"""
trigger download of relevant countries' OSM files
Expand All @@ -172,7 +170,8 @@ def download_osm_pbf_file(self):
try:
if self.border_countries[country]['download'] is True:
map_file_path = self.download_map(country)
self.border_countries[country] = {'map_file':map_file_path}
self.border_countries[country] = {
'map_file': map_file_path}
except KeyError:
pass

Expand All @@ -189,16 +188,18 @@ def download_map(self, country):

if region != 'no':
url = 'https://download.geofabrik.de/' + region + \
'/' + transl_c + '-latest.osm.pbf'
'/' + 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')
map_file_path = os.path.join(
fd_fct.MAPS_DIR, f'{transl_c}' + '-latest.osm.pbf')
fd_fct.download_url_to_file(url, map_file_path)

if not os.path.isfile(map_file_path):
print(f'! failed to find or download country: {transl_c}. Input: {country}.')
print(
f'! failed to find or download country: {transl_c}. Input: {country}.')
sys.exit()
else:
print(f'+ Map of {transl_c} downloaded. Input: {country}.')
Expand Down

0 comments on commit 05a2b3c

Please sign in to comment.