def make_contours(o):
    # Zjistim, zda mam hotove vrstevnice
    try:
        if not os.path.isfile('./pbf/' + o.state.data_id + '-SRTM.osm.pbf'):
            say('Generate contour line', o)

            # --no-zero-contour
            os.system('phyghtmap \
				--polygon=./poly/' + o.state.data_id + '.poly \
				-o ./pbf/' + o.state.data_id + '-SRTM \
				--pbf \
				-j 2 \
				-s 10 \
				-c 200,100 \
				--source=view3 \
				--start-node-id=20000000000 \
				--start-way-id=10000000000 \
				--write-timestamp \
				--max-nodes-per-tile=0 \
			')
            os.rename(
                glob.glob('./pbf/' + o.state.data_id + '-SRTM*.osm.pbf')[0],
                './pbf/' + o.state.data_id + '-SRTM.osm.pbf')
        else:
            say('Use previously generated contour lines', o)
    except:
        error("Cann't generate contour lines!", o)
def get_area(o):
    while True:
        if o.state in STATES:
            # doplnim id
            STATES[o.state].id = o.state

            # popr. i data_id
            if STATES[o.state].data_id is None:
                STATES[o.state].data_id = o.state

            # Vratim hodnotu
            o.state = STATES[o.state]

            say('Area id: ' + o.state.id, o)
            say('Data id: ' + o.state.data_id, o)

            return

        else:
            print('Zadejte zkratku statu, pro ktery chcete vytvorit mapu.')
            for o.state in STATES:
                print('  * [' + o.state + '] - ' + STATES[o.state].name)

            o.state = input('Vybrana mapa: ')
def download_map_data(o):
    # Zjistim, zda mam stahovat data
    if not o.state.data_url:
        o.download_map = False

    # Data j*z byla stazena
    if os.path.isfile('./pbf/' + o.state.data_id + '.osm.pbf'):
        say('File ' + o.state.data_id + '.osm.pbf found', o)

        # Uzivatel nespecifikoval, co se ma stat
        if o.download_map is None and o.state.data_url != False:
            o.download_map = question(
                'Map data has already been download, do you want to use it?')

    else:
        if not o.state.data_url:
            error('Map file does NOT exist!', o)

        o.download_map = True

    # Stahnu data
    if o.download_map:
        try:
            say('Downloading map data', o)
            download(o.state.data_url, './pbf/' + o.state.data_id + '.osm.pbf')
        except:
            error("Cann't download map data!", o)

    # Stahnu polygon
    try:
        if not os.path.isfile('./poly/' + o.state.data_id + '.poly'):
            if o.state.poly_url == False:
                error("Polygon '" + o.state.data_id + "' does NOT exist!", o)

            say('Downloading polygon', o)
            download(o.state.poly_url, './poly/' + o.state.data_id + '.poly')
    except:
        error("Cann't download polygon!", o)
def make_garmin(o):
    say('Making map for garmin...', o)
    state = o.state

    # Rozdelim soubory
    input_file = './pbf/' + state.data_id + '.osm.pbf'
    input_srtm_file = './pbf/' + state.data_id + '-SRTM.osm.pbf'

    if o.split:
        if not os.path.exists('./pbf/' + state.data_id +
                              '-SPLITTED') or o.download_map:
            for file in glob.glob('./pbf/' + state.data_id + '-SPLITTED/*'):
                os.remove(file)

            # max-areas = 512
            # max-nodes = 1600000
            os.system('java ' + o.JAVAMEM + ' -jar ./splitter/splitter.jar \
				' + input_file + ' \
				--max-areas=4096 \
				--max-nodes=1600000 \
				--output-dir=./pbf/' + state.data_id + '-SPLITTED \
			')

        input_file = ''
        for file in glob.glob('./pbf/' + state.data_id +
                              '-SPLITTED/*.osm.pbf'):
            input_file += file + ' '

        if not os.path.isdir('./pbf/' + state.data_id + '-SPLITTED-SRTM/'):
            os.system('java ' + o.JAVAMEM + ' -jar ./splitter/splitter.jar \
				' + input_srtm_file + ' \
				--max-areas=4096 \
				--max-nodes=1600000 \
				--output-dir=./pbf/' + state.data_id + '-SPLITTED-SRTM \
			')

        input_srtm_file = ''
        for file in glob.glob('./pbf/' + state.data_id +
                              '-SPLITTED-SRTM/*.osm.pbf'):
            input_srtm_file += file + ' '

    pois_files = ''
    if state.pois is not None:
        for x in state.pois:
            pois_files += ' ./pois/' + x + '.osm.xml'

    # Vytvorim licencni soubor
    license = open('./template/license.txt', 'r')
    content = license.read()
    license.close()

    license = open('license.txt', 'w')
    license.write(content + "\n" + str(datetime.now()))
    license.close()

    # Spustim generator
    # inputs = input_file + ' ' + input_srtm_file + ' ' + pois_files
    err = os.system('java ' + o.JAVAMEM + ' -jar ./mkgmap/mkgmap.jar \
		-c mkgmap-settings.conf \
		--check-roundabouts \
		--max-jobs=' + str(o.MAX_JOBS) + ' \
		--mapname="' + str(state.number) + '0001\" \
		--overview-mapnumber="' + str(state.number) + '0000\" \
		--family-id="' + str(state.number) + '" \
		--description="' + state.name + '_VasaM" \
		--family-name="' + state.name + '_VasaM" \
		--series-name="' + state.name + '_VasaM" \
		--area-name="' + state.name + '_VasaM" \
		--country-name="' + state.name + '_VasaM" \
		--country-abbr="' + state.id + '" \
		--region-name="' + state.name + '_VasaM" \
		--region-abbr="' + state.id + '" \
		--product-version=' + str(o.VERSION) + ' \
		--output-dir=./img/' + state.id + '_VasaM \
		--dem-poly=./poly/' + state.data_id + '.poly \
		--license-file=license.txt \
		' + state.lang + ' \
		' + state.code + ' \
		' + input_file + ' \
		' + input_srtm_file + ' \
		' + pois_files + ' \
		./garmin-style/style.txt \
	')

    if err != 0:
        sys.stderr.write('mkgmap error')
        sys.exit()

    os.remove('license.txt')

    # Prevedu ID do hexa tvaru
    state.number_hex = format(state.number, 'x')
    state.number_hex = state.number_hex[2:4] + state.number_hex[0:2]

    # Vytvorim instalacni bat soubor
    install = open('./template/install.bat', 'r')
    content = install.read()
    install.close()

    content = content.replace('%NAME%', state.name)
    content = content.replace('%ID%', str(state.number))
    content = content.replace('%ID_HEX%', state.number_hex)

    install = open('./img/' + state.id + '_VasaM/install.bat', 'w')
    install.write(content)
    install.close()

    # Vytvorim odinstalacni bat soubor
    uninstall = open('./template/uninstall.bat', 'r')
    content = uninstall.read()
    uninstall.close()

    content = content.replace('%NAME%', state.name)
    content = content.replace('%ID%', str(state.number))

    uninstall = open('./img/' + state.id + '_VasaM/uninstall.bat', 'w')
    uninstall.write(content)
    uninstall.close()

    # Prejmenuji vystupni soubor
    if os.path.isfile('./img/' + state.id + '_VasaM.img'):
        os.remove('./img/' + state.id + '_VasaM.img')

    os.rename('./img/' + state.id + '_VasaM/gmapsupp.img',
              './img/' + state.id + '_VasaM.img')

    # Vytvorim archiv
    os.chdir('./img/')
    zip = zipfile.ZipFile('./' + state.id + '_VasaM.zip', 'w')
    for dirname, subdirs, files in os.walk('./' + state.id + '_VasaM/'):
        zip.write(dirname)
        for filename in files:
            zip.write(os.path.join(dirname, filename))
    zip.close()
    os.chdir('..')

    # Vytvorim info soubor
    info = open('./template/info.info', 'r')
    content = info.read()
    info.close()

    # Spocitam hashe
    def sha1(filename):
        hash_func = hashlib.sha1()

        with open(filename, 'rb') as f:
            while True:
                data = f.read(67108864)  # read 64Mb of file
                if not data:
                    break
                hash_func.update(data)

        return hash_func.hexdigest()

    hash_zip = sha1('./img/' + state.id + '_VasaM.zip')
    hash_img = sha1('./img/' + state.id + '_VasaM.img')
    map_timestamp = round(o.time_start.timestamp())

    content = content.replace('%VERSION%', str(o.VERSION))
    content = content.replace('%TIMESTAMP%', str(map_timestamp))
    content = content.replace('%HASH_IMG%', hash_img)
    content = content.replace('%HASH_ZIP%', hash_zip)

    info = open('./img/' + state.id + '_VasaM.info', 'w')
    info.write(content)
    info.close()
def main():

	try:
		# Objekt pro ulozeni globalnich promennych a nastaveni
		o = Options()


		# Zaznamenam cas spusteni
		o.time_start = datetime.now()


		# Nactu arumenty
		parse_args(o)


		# Vytvorim logovaci soubor
		make_log_file(o)


		say('Start at ' + str(o.time_start), o)


		# Ziskam stat, nebyl-li zadan
		get_area(o)

		# Stahnu mapova data a polygon
		download_map_data(o)


		# Vytvorim vrstevnice
		make_contours(o)


		# Vytvorim cilovou podslozku
		if not os.path.exists('./img/' + o.state.id + '_VasaM'):
			os.makedirs('./img/' + o.state.id + '_VasaM')


		# parse_poly(o)

		# Generuji Mapsforge
		o.mapsforge = False
		if o.mapsforge:
			make_mapsforge(o)
		# Generuji Garmin
		else:
			make_garmin(o)


		# Ukoncim generovani
		end(o)



	except Exception as e:
		error('Some error...')
		print(e)

		exit(1)


	finally:
		if hasattr(o, 'log_file'):
			if o.log_file and o.log_file.close:
				o.log_file.close()
def end(o):
    time_end = datetime.now()
    runtime = time_end - o.time_start
    say('Konec v ' + str(time_end) + ', beh ' + str(runtime), o)
    print('\007')