├── .gitignore ├── LICENSE ├── README.md ├── setup.py └── wms_downloader ├── __init__.py └── download.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *~ 3 | *.swp 4 | .DS_Store 5 | 6 | build 7 | dist 8 | *.egg-info 9 | 10 | env 11 | env3 12 | 13 | *.yml 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jochen Klar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | wms-downloader 2 | ============== 3 | 4 | Install 5 | ------- 6 | 7 | ```bash 8 | pip install wms-downloader 9 | ``` 10 | 11 | Usage 12 | ----- 13 | 14 | Create a `config.yml` specifying your setup like this: 15 | 16 | ```yml 17 | service: 18 | version: 1.1.1 19 | url: http://fbinter.stadt-berlin.de/fb/wms/senstadt/k_luftbild1953? 20 | srs: EPSG:25833 21 | format: jpeg 22 | transparent: false 23 | layer: 0 24 | bbox: 25 | west: 370000.0 26 | south: 5800000.0 27 | east: 415000.0 28 | north: 5837000.0 29 | 30 | size: 10000 31 | resolution: 600 32 | timeout: 300 33 | projection: EPSG:25833 34 | bandscount: 3 35 | directory: images 36 | vrtfile: tiles.vrt 37 | tmpfile: /tmp/wms.xml 38 | ``` 39 | 40 | where: 41 | 42 | * `service` describes the used WMS service, 43 | * `bbox` is the bounding box for the map you want to retrieve, 44 | * `size` is the size of an individual tile in projection units, 45 | * `resolution` is the pixel dimension of an individual tile, 46 | * `directory` is the directory where the downloaded images are stored, 47 | * `vrtfile` is the path to the created vrt file, and 48 | * `tmpfile` is the path to the (temporary) xml file used for the WMS requests. 49 | 50 | Then run the script with the `config.yml` as argument: 51 | 52 | ``` 53 | wms-downloader config.yml 54 | ``` 55 | 56 | Help 57 | ---- 58 | 59 | ``` 60 | $ wms-downloader --help 61 | usage: Downloads large geo TIFF files from a WMS service. 62 | 63 | positional arguments: 64 | config config file 65 | 66 | optional arguments: 67 | -h, --help show this help message and exit 68 | ``` 69 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | from wms_downloader import ( 4 | __title__ as title, 5 | __version__ as version, 6 | __author__ as author, 7 | __license__ as license 8 | ) 9 | 10 | description = 'Downloads large geo TIFF files from a WMS service.' 11 | email = 'jochenklar@gmail.com' 12 | url = 'https://github.com/codeforberlin/wms-downloader' 13 | 14 | requirements = [ 15 | 'PyYAML' 16 | ] 17 | 18 | console_scripts = [ 19 | 'wms-downloader=wms_downloader.download:main' 20 | ] 21 | 22 | setup( 23 | name=title, 24 | version=version, 25 | description=description, 26 | url=url, 27 | author=author, 28 | author_email=email, 29 | maintainer=author, 30 | maintainer_email=email, 31 | license=license, 32 | packages=find_packages(), 33 | install_requires=requirements, 34 | classifiers=[], 35 | entry_points={ 36 | 'console_scripts': console_scripts 37 | } 38 | ) 39 | -------------------------------------------------------------------------------- /wms_downloader/__init__.py: -------------------------------------------------------------------------------- 1 | __title__ = 'wms-downloader' 2 | __version__ = '1.0.3' 3 | __author__ = 'Jochen Klar' 4 | __license__ = 'MIT' 5 | __copyright__ = 'Copyright 2017 Jochen Klar' 6 | -------------------------------------------------------------------------------- /wms_downloader/download.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import argparse 4 | import glob 5 | import os 6 | import subprocess 7 | import yaml 8 | import logging 9 | 10 | logger = logging.getLogger(__name__) 11 | 12 | xml_template = ''' 13 | 14 | %(version)s 15 | %(url)s 16 | %(srs)s 17 | image/%(format)s 18 | %(layer)s 19 | %(transparent)s 20 | 21 | 22 | %(west)s 23 | %(north)s 24 | %(east)s 25 | %(south)s 26 | %(resolution)s 27 | %(resolution)s 28 | 29 | %(timeout)s 30 | %(projection)s 31 | %(bandscount)s 32 | ''' 33 | 34 | 35 | def main(): 36 | parser = argparse.ArgumentParser(usage='Downloads large geo TIFF files from a WMS service.') 37 | parser.add_argument('config', help='config file') 38 | parser.add_argument('--debug', action='store_true', help='debug output') 39 | args = parser.parse_args() 40 | 41 | handler = logging.StreamHandler() 42 | handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) 43 | logger.addHandler(handler) 44 | 45 | if args.debug: 46 | logger.setLevel(logging.DEBUG) 47 | 48 | with open(args.config) as f: 49 | config = yaml.load(f.read(), Loader=yaml.FullLoader) 50 | 51 | create_directory(config) 52 | download_images(config) 53 | create_vrt_file(config) 54 | 55 | 56 | def create_directory(config): 57 | try: 58 | os.makedirs(config['directory']) 59 | logger.debug('directory "%s" created', config['directory']) 60 | except OSError: 61 | logger.debug('directory "%s" exists', config['directory']) 62 | 63 | 64 | def download_images(config): 65 | west_range = list(arange(config['bbox']['west'], config['bbox']['east'], config['size'])) 66 | south_range = list(arange(config['bbox']['south'], config['bbox']['north'], config['size'])) 67 | 68 | for west in west_range: 69 | for south in south_range: 70 | filename = os.path.join(config['directory'], '%(west)s_%(south)s_%(resolution)s.gdal.%(format)s' % { 71 | 'west': west, 72 | 'south': south, 73 | 'resolution': config['resolution'], 74 | 'format': config['service']['format'] 75 | }) 76 | 77 | if not os.path.exists(filename + '.aux.xml'): 78 | print('fetching %s' % filename) 79 | xml_params = { 80 | 'west': west, 81 | 'south': south, 82 | 'east': west + config['size'], 83 | 'north': south + config['size'], 84 | 'resolution': config['resolution'], 85 | 'timeout': config['timeout'], 86 | 'projection': config['projection'], 87 | 'transparent': config['service']['transparent'], 88 | 'bandscount': config['bandscount'] 89 | } 90 | xml_params.update(config['service']) 91 | 92 | with open(config['tmpfile'], 'w') as f: 93 | f.write(xml_template % xml_params) 94 | 95 | logger.info('fetching "%s"', filename) 96 | args = ['gdal_translate', '-of', config['service']['format'], config['tmpfile'], filename] 97 | subprocess.check_call(args) 98 | 99 | 100 | def create_vrt_file(config): 101 | if not os.path.exists(config['vrtfile']): 102 | args = ['gdalbuildvrt', '-a_srs', config['projection'], config['vrtfile']] 103 | args += glob.glob('%s/*.gdal.%s' % (config['directory'],config['service']['format']) ) 104 | subprocess.check_call(args) 105 | 106 | 107 | def arange(start, stop, step): 108 | current = start 109 | while current < stop: 110 | yield current 111 | current += step 112 | 113 | if __name__ == "__main__": 114 | main() 115 | --------------------------------------------------------------------------------