def contours(o): say(_('Generuji vrstevnice'), o) # Zjistim, zda mam hotove vrstevnice if not os.path.isfile(o.pbf + o.area.id + '-SRTM.osm.pbf'): run( 'phyghtmap \ --polygon=' + o.temp + 'polygon.poly \ -o ' + o.pbf + o.area.id + '-SRTM \ --pbf \ -j 2 \ -s 10 \ -c 200,100 \ --hgtdir=' + o.hgt + ' \ --source=view3 \ --start-node-id=20000000000 \ --start-way-id=10000000000 \ --write-timestamp \ --max-nodes-per-tile=0', o) os.rename( glob.glob(o.pbf + o.area.id + '-SRTM*.osm.pbf')[0], o.pbf + o.area.id + '-SRTM.osm.pbf') else: say(_('Pouzivam drive vytvorene vrstevcnice'), o)
def _makeAreaObject(id, obj, options, continent=None): # Doplnim id a dataId obj.id = id if hasattr(obj, 'parent') and obj.parent is not None: say(_('Oblast je zavisla na datech oblasti ') + obj.parent, options) state = _findState(obj.parent) if state is not None: obj.url = "http://download.geofabrik.de/%s/%s-latest.osm.pbf" % ( state[1], state[0].url) elif obj.parent in USER_AREAS: obj.url = USER_AREAS[obj.parent].url else: raise ValueError( _('Neplatne ID rodice') + ' \'' + obj.parent + '\' ' + _('v') + ' \'' + id + '\'') obj.mapDataName = options.pbf + obj.parent + '.osm.pbf' else: obj.url = "http://download.geofabrik.de/%s/%s-latest.osm.pbf" % ( continent, obj.url) obj.mapDataName = options.pbf + id + '.osm.pbf' if continent is not None: obj.continent = continent return obj
def crop(o): if o.crop or o.area.crop: if platform.system() == 'Windows' and platform.architecture( )[0] == '32bit' and os.path.getsize(o.area.mapDataName) > 2000000000: raise ValueError( _('Soubor pro orez je prilis velky') + ' (' + "{:.2f}".format( os.path.getsize(o.area.mapDataName) / 1000000000) + ' GB), ' + _('maximum jsou 2 GB. Detaily viz GitHub.')) say(_('Vytvarim vyrez oblasti'), o) osmconvert = ('' if platform.system() == 'Windows' else './' ) + 'osmconvert' + platform.architecture()[0][0:2] + ( '.exe' if platform.system() == 'Windows' else '') os.chdir('osmconvert') run( osmconvert + ' \ ../' + o.area.mapDataName + ' -B=../' + o.temp + 'polygon.poly \ --complete-ways --complete-multipolygons --complete-boundaries \ --out-pbf \ -o=../' + o.temp + o.area.id + '.osm.pbf', o) os.chdir('..') o.area.mapDataName = o.temp + o.area.id + '.osm.pbf'
def garmin(o): say(_('Vytvarim mapu pro Garmin...'), o) # Vytvorim cilovou podslozku if not os.path.exists(o.img + o.area.id + o.sufix): os.makedirs(o.img + o.area.id + o.sufix) input_file, input_srtm_file = _splitFiles(o) _prepareLicence(o) say(_('Generuji mapu'), o) run( 'java ' + o.JAVAMEM + ' -jar ./mkgmap-r' + str(o.mkgmap) + '/mkgmap.jar \ -c ./garmin-style/mkgmap-settings.conf \ --bounds=' + o.bounds + ' \ --precomp-sea=' + o.sea + 'sea/ \ --dem=' + o.hgt + 'VIEW3/ \ --max-jobs=' + str(o.MAX_JOBS) + ' \ --mapname="' + str(o.area.number).zfill(4) + '0001\" \ --overview-mapnumber="' + str(o.area.number).zfill(4) + '0000\" \ --family-id="' + str(o.area.number).zfill(4) + '" \ --description="' + o.area.nameCs + o.sufix + '" \ --family-name="' + o.area.nameCs + o.sufix + '" \ --series-name="' + o.area.nameCs + o.sufix + '" \ --area-name="' + o.area.nameCs + o.sufix + '" \ --country-name="' + o.area.nameCs + o.sufix + '" \ --country-abbr="' + o.area.id + '" \ --region-name="' + o.area.nameCs + o.sufix + '" \ --region-abbr="' + o.area.id + '" \ --product-version=' + str(o.VERSION) + ' \ --output-dir=' + o.img + o.area.id + o.sufix + ' \ --dem-poly=' + o.polygons + o.area.id + '.poly \ --license-file=' + o.temp + 'license.txt \ --code-page=' + o.code + ' \ ' + input_file + ' \ ' + input_srtm_file + ' \ ' + ' '.join(o.area.pois) + ' \ ./garmin-style/style.txt', o) _makeBat('install', o) _makeBat('uninstall', o) # Prejmenuji vystupni soubor say(_('Prejmenuji soubory'), o) if os.path.isfile(o.img + o.area.id + o.sufix + '.img'): os.remove(o.img + o.area.id + o.sufix + '.img') os.rename(o.img + o.area.id + o.sufix + '/gmapsupp.img', o.img + o.area.id + o.sufix + '.img') _makeZip(o) _makeInfo(o)
def _prepareLicence(o): # Vytvorim licencni soubor say(_('Pripravuji licencni soubor'), o) with open('./template/license.txt', 'r') as license: content = license.read() with open(o.temp + 'license.txt', 'w') as license: license.write(content + "\n" + str(o.area.timestamp))
def _splitFiles(o): input_file = o.area.mapDataName input_srtm_file = o.pbf + o.area.id + '-SRTM.osm.pbf' if o.split: say(_('Spoustim rozdeleni souboru'), o) # Data neexistuji nebo jsem stahl nova if not os.path.exists(o.pbf + o.area.id + '-SPLITTED') or o.downloaded: # Smazu puvodni soubory for file in glob.glob(o.pbf + o.area.id + '-SPLITTED/*'): os.remove(file) # Spustim splitter run( 'java ' + o.JAVAMEM + ' -jar \ ./splitter-r' + str(o.splitter) + '/splitter.jar ' + input_file + ' --max-areas=4096 \ --max-nodes=1600000 \ --output-dir=' + o.pbf + o.area.id + '-SPLITTED', o) # Aktualizuji seznam vstupnich souboru input_file = o.pbf + o.area.id + '-SPLITTED/*.osm.pbf' # input_file = [] # for file in glob.glob( o.pbf + o.area.id + '-SPLITTED/*.osm.pbf' ): # input_file.append(file) # Rozdelim soubor s vrstevnicemi if not os.path.isdir(o.pbf + o.area.id + '-SPLITTED-SRTM'): run( 'java ' + o.JAVAMEM + ' -jar \ ./splitter-r' + str(o.splitter) + '/splitter.jar ' + input_srtm_file + ' --max-areas=4096 \ --max-nodes=1600000 \ --output-dir=' + o.pbf + o.area.id + '-SPLITTED-SRTM', o) say(_('Rozdeleni souboru - HOTOVO'), o) # Aktualizuji seznam vstupnich souboru input_srtm_file = o.pbf + o.area.id + '-SPLITTED-SRTM/*.osm.pbf' # input_srtm_file = [] # for file in glob.glob( o.pbf + o.area.id + '-SPLITTED-SRTM/*.osm.pbf' ): # input_srtm_file.append(file) return input_file, input_srtm_file
def _makeZip(o): say(_('Vytvarim zip soubor'), o) os.chdir(o.img) zip = zipfile.ZipFile('./' + o.area.id + o.sufix + '.zip', 'w') for dirname, subdirs, files in os.walk('./' + o.area.id + o.sufix): zip.write(dirname) for filename in files: zip.write(os.path.join(dirname, filename)) zip.close() os.chdir('..')
def fileHeader(o): say(_('Ctu hlavicku souboru'), o) o.area.timestamp = None o.area.fileHeader = None if os.path.isfile(o.area.mapDataName): o.area.fileHeader = osmium.io.Reader( o.area.mapDataName, osmium.osm.osm_entity_bits.NOTHING).header() o.area.timestamp = o.area.fileHeader.get( "osmosis_replication_timestamp") try: o.area.timestamp = datetime.strptime(o.area.timestamp, "%Y-%m-%dT%H:%M:%SZ") o.area.timestamp = o.area.timestamp.replace(tzinfo=timezone.utc) except ValueError: error( _('Datum v hlavicce OSM souboru neni ve formatu ISO8601 (napr. 2015-12-24T08:08Z). Ignoruji' ), o) o.area.timestamp = None say(_("Soubor z ") + str(o.area.timestamp), o)
def _makeInfo(o): say(_('Vytvarim info soubor'), o) infoData = { 'ID': o.area.id, 'version': str(o.VERSION), 'datetime': str(o.area.timestamp), 'timestamp': str(o.area.timestamp.timestamp()), 'hashImg': _sha1(o.img + o.area.id + o.sufix + '.img'), 'hashZip': _sha1(o.img + o.area.id + o.sufix + '.zip'), 'codePage': o.code } with open(o.img + o.area.id + o.sufix + '.info', 'w') as info: info.write(json.dumps(infoData))
def run(program, o): program = ' '.join(program.split()) say(program, o, '[RUN] ') process = subprocess.Popen(program, universal_newlines=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: output = process.stdout.readline() if output == '' and process.poll() is not None: break if output: say(output, o, '', '') log(output, o) if process.poll() != 0: error('stderr: ' + process.stderr.read(), o) raise ValueError(program + ' ' + _('vratil') + ' ' + str(process.poll()) + ' (ocekavana 0)')
def mapData(o): say(_('Spoustim stahovani mapovych dat'), o) o.downloaded = False # Zjistim, zda mam stahovat data if o.area.url is None: say(_('Neznam URL adresu - preskakuji'), o) if o.area.fileHeader is None: raise ValueError(_('Mapový soubor NEEXISTUJE!')) return if o.downloadMap == 'skip': say(_('Uzivatel nastavil "--download skip" - nestahuji'), o) return if o.downloadMap == 'auto': if o.area.timestamp is None: o.downloaded = True else: diff = datetime.now(timezone.utc) - o.area.timestamp if diff.total_seconds() > o.maximumDataAge: o.downloaded = True else: say(_('Mapova data jsou prilis mlada - nestahuji'), o) if o.downloadMap == 'force' or o.downloaded is True: try: say(_('Stahuji mapova data'), o) download(o.area.url, o.area.mapDataName) parser.fileHeader(o) except: raise ValueError(_('Nelze stahnout mapova data!'))
def main(): try: # TODO vytvorit TMP slozku # Objekt pro ulozeni globalnich promennych a nastaveni o = Options() # Nactu konfiguracni soubor config.load(o) # Nactu a zpracuji arumenty args.parse(o) # Nastavim jazyk if o.en: Lang.bindLanguage('en') # Zaznamenam cas spusteni o.timeStart = datetime.now() say(_('Spusteno v ') + str(o.timeStart), o) # Ziskam informace o statu parser.area(o) # Nactu informace z hlavicky parser.fileHeader(o) # Stahnu mapova data download.mapData(o) # Stahnu polygon download.polygon(o) # Zpracuji polygon polygon.load(o) # Vytvorim vrstevnice generator.contours(o) # Oriznu mapovy soubor generator.crop(o) # Generuji Garmin mapu generator.garmin(o) # TODO remove temp except KeyboardInterrupt: error("\n" + _('Ukonceno uzivatelem')) except Exception as e: error(str(e)) exit(1) finally: # Ukoncim generovani end(o)
def area(o): if o.area is None: while True: print('\n' + _('Vyberte svetadil')) for continent in STATES: print(continent) continent = input(_('Vybrano: ')) if continent not in STATES: continue else: break while True: print('\n' + _('Vyberte stat')) for state in STATES[continent]: if Lang.getLanguage() == 'cs': print(state, ' (', STATES[continent][state].nameCs, ')', sep='') else: print(state, ' (', STATES[continent][state].nameEn, ')', sep='') state = input(_('Vybrano: ')) if state not in STATES[continent]: continue else: o.area = state break say(_('Dekoduji oblast ') + o.area, o) if o.area in USER_AREAS: say(_('Oblast nalezena v uzivatelskych oblastech'), o) o.area = _makeAreaObject(id=o.area, obj=USER_AREAS[o.area], options=o) else: state = _findState(o.area) if state is not None: o.area = _makeAreaObject(id=o.area, obj=state[0], options=o, continent=state[1]) else: raise ValueError(_('Neplatna oblast ') + o.area) if o.mapNumber is not None: o.area.number = o.mapNumber if o.variant is not None: o.area.number += int(o.variant) say(str(o.area), o) say(_('ID oblasti: ') + o.area.id, o)
def download(url, output, quiet = False): # url = 'https://speed.hetzner.de/100MB.bin' # url = 'https://speed.hetzner.de/1GB.bin' if not os.path.exists(os.path.dirname(output)): try: os.makedirs(os.path.dirname(output)) except OSError as exc: if exc.errno != errno.EEXIST: raise output = open(output, 'wb') try: response = urllib.request.urlopen(url) length = response.getheader('content-length') unit = 'MB' unitSize = 1048576 if length: length = int(length) blocksize = max(4096, length // 1000) # blocksize = 4096 # just made something up # FIXME if length < unitSize: unit = 'kB' unitSize = 1024 else: length = 0 blocksize = 1000000 # just made something up if not quiet: print(_("Stahuji") + " '" + url + "' ", length // unitSize, unit) # Prevedu na megabyte _printProgres(0, 0, length, 0, 0, unit, unitSize) speedHistory = [0] * 10 speedHistoryPointer = 0 size = 0 while True: tmp_time = datetime.now() data = response.read(blocksize) time_diff = (datetime.now() - tmp_time).total_seconds() if not data: _printProgres(100, length, length, 0, 0, unit, unitSize) break output.write(data) if length: size += len(data) percent = round(size / length * 100) speed = 0 if time_diff != 0: # speed = round((blocksize / unitSize) / time_diff, 2) speedHistory[speedHistoryPointer] = (blocksize / unitSize) / time_diff speedHistoryPointer = (speedHistoryPointer + 1) % len(speedHistory) speed = round(sum(speedHistory) / len(speedHistory), 2) eta = 0 if speed != 0: eta = round(((length - size) // unitSize) / speed) # v sekundach if eta > 99: eta = str(floor(eta / 60)) + ' min ' + str(eta % 60) + ' s' else: eta = str(eta) + ' s' if not quiet: _printProgres(percent, size, length, speed, eta, unit, unitSize) if not quiet: print() except urllib.error.HTTPError as e: print('Error code: ', e.code) raise except urllib.error.URLError as e: print('Reason: ', e.reason) raise
def polygon(o): if hasattr(o.area, 'continent') and not os.path.isfile(o.polygons + o.area.id + '.poly'): say(_('Stahuji polygon'), o) download(o.area.url[0:-15] + '.poly', o.polygons + o.area.id + '.poly')