def create(dir_data, dir_temp, bounds=None): database = get_database(dir_data, bounds) file = __create_waypoint_file(database, dir_temp) list = FileList() list.add(file, True) return list
def __convert(dir_temp, input_file, rc): print('Converting terrain to JP2 format...') output_file = os.path.join(dir_temp, 'terrain.jp2') args = [__cmd_geojasper, '-f', input_file, '-F', output_file, '-T', 'jp2', '-O', 'rate=1.0', '-O', 'tilewidth=256', '-O', 'tileheight=256'] if not __use_world_file: args.extend(['-O', 'xcsoar=1', '-O', 'lonmin=' + str(rc.left), '-O', 'lonmax=' + str(rc.right), '-O', 'latmax=' + str(rc.top), '-O', 'latmin=' + str(rc.bottom)]) subprocess.check_call(args) output = FileList() output.add(output_file, False) world_file_tiff = os.path.join(dir_temp, "terrain.tfw") world_file = os.path.join(dir_temp, "terrain.j2w") if __use_world_file and os.path.exists(world_file_tiff): os.rename(world_file_tiff, world_file) output.add(world_file, True) return output
def create(dir_data, dir_temp, bounds = None): database = get_database(dir_data, bounds) file = __create_waypoint_file(database, dir_temp) list = FileList() list.add(file, True) return list
def create(bounds, downloader, dir_temp): topology = downloader.manifest()['topology'] layers = topology['layers'] datasets = topology['datasets'] files = FileList() index = [] for layer in layers: __create_layer(bounds, layer, datasets[layer['dataset']], downloader, dir_temp, files, index) files.add(__create_index_file(dir_temp, index), True) return files
def create(bounds, downloader, dir_temp, compressed=False, level_of_detail=3): topology = downloader.manifest()['topology'] layers = topology['layers'] datasets = topology['datasets'] files = FileList() index = [] for layer in layers: if layer['level_of_detail'] <= level_of_detail: __create_layer(bounds, layer, datasets[layer['dataset']], downloader, dir_temp, files, index, compressed) files.add(__create_index_file(dir_temp, index), True) return files
def __init__(self, dir_data, dir_temp): ''' Constructor of the MapGenerator class @param dir_data: Path of the data folder @param dir_temp: Path of the temporary folder ''' self.__downloader = Downloader(dir_data) self.__dir_temp = os.path.abspath(dir_temp) if not os.path.exists(self.__dir_temp): os.mkdir(self.__dir_temp) self.__bounds = None self.__files = FileList()
def create(bounds, arcseconds_per_pixel, downloader, dir_temp): # Make sure the tiles are available tiles = __retrieve_tiles(downloader, dir_temp, bounds) if len(tiles) < 1: return FileList() try: terrain_file = __create(dir_temp, tiles, arcseconds_per_pixel, bounds) return __convert(dir_temp, terrain_file, bounds) finally: __cleanup(dir_temp)
def __convert(dir_temp, input_file, rc): print('Converting terrain to JP2 format...') output_file = os.path.join(dir_temp, 'terrain.jp2') args = [ __cmd_geojasper, '-f', input_file, '-F', output_file, '-T', 'jp2', '-O', 'rate=1.0', '-O', 'tilewidth=256', '-O', 'tileheight=256' ] if not __use_world_file: args.extend([ '-O', 'xcsoar=1', '-O', 'lonmin=' + str(rc.left), '-O', 'lonmax=' + str(rc.right), '-O', 'latmax=' + str(rc.top), '-O', 'latmin=' + str(rc.bottom) ]) subprocess.check_call(args) output = FileList() output.add(output_file, False) world_file_tiff = os.path.join(dir_temp, "terrain.tfw") world_file = os.path.join(dir_temp, "terrain.j2w") if __use_world_file and os.path.exists(world_file_tiff): os.rename(world_file_tiff, world_file) output.add(world_file, True) return output
def __convert(dir_temp, input_file, rc): print('Converting terrain to JP2 format...') output_file = os.path.join(dir_temp, 'terrain.jp2') args = ['gdal_translate', '-of', 'JP2OpenJPEG', '-co', 'BLOCKXSIZE=256', '-co', 'BLOCKYSIZE=256', '-co', 'QUALITY=95', input_file, output_file] subprocess.check_call(args) output = FileList() output.add(output_file, False) world_file_tiff = os.path.join(dir_temp, "terrain.tfw") world_file = os.path.join(dir_temp, "terrain.j2w") if __use_world_file and os.path.exists(world_file_tiff): os.rename(world_file_tiff, world_file) output.add(world_file, True) return output
def create(bounds, arcseconds_per_pixel, downloader, dir_temp): # calculate height and width (in pixels) of map from geo coordinates px = round((bounds.right - bounds.left) * 3600 / arcseconds_per_pixel) py = round((bounds.top - bounds.bottom) * 3600 / arcseconds_per_pixel) # round up so only full jpeg2000 tiles (256x256) are used # works around a bug in openjpeg 2.0.0 library px = (int(px / 256) + 1) * 256 py = (int(py / 256) + 1) * 256 # and back to geo coordinates for size bounds.right = bounds.left + (px * arcseconds_per_pixel / 3600) bounds.bottom = bounds.top - (py * arcseconds_per_pixel / 3600) # Make sure the tiles are available tiles = __retrieve_tiles(downloader, dir_temp, bounds) if len(tiles) < 1: return FileList() try: terrain_file = __create(dir_temp, tiles, arcseconds_per_pixel, bounds) return __convert(dir_temp, terrain_file, bounds) finally: __cleanup(dir_temp)
def __convert(dir_temp, input_file, rc): print('Converting terrain to JP2 format...') output_file = os.path.join(dir_temp, 'terrain.jp2') args = [ 'gdal_translate', '-of', 'JP2OpenJPEG', '-co', 'BLOCKXSIZE=256', '-co', 'BLOCKYSIZE=256', '-co', 'QUALITY=95', input_file, output_file ] subprocess.check_call(args) output = FileList() output.add(output_file, False) world_file_tiff = os.path.join(dir_temp, "terrain.tfw") world_file = os.path.join(dir_temp, "terrain.j2w") if __use_world_file and os.path.exists(world_file_tiff): os.rename(world_file_tiff, world_file) output.add(world_file, True) return output
class Generator: def __init__(self, dir_data, dir_temp): ''' Constructor of the MapGenerator class @param dir_data: Path of the data folder @param dir_temp: Path of the temporary folder ''' self.__downloader = Downloader(dir_data) self.__dir_temp = os.path.abspath(dir_temp) if not os.path.exists(self.__dir_temp): os.mkdir(self.__dir_temp) self.__bounds = None self.__files = FileList() def add_information_file(self, name, author = 'unknown'): ''' Adds an information file to the map ''' if not self.__bounds: raise RuntimeError('Boundaries undefined.') dst = os.path.join(self.__dir_temp, 'info.txt') spew(dst, '''map name: {name} generator: XCSoar Map Generator creation time: {time:%d.%m.%Y %T} ({time:%s}) latitude range: {minlat} to {maxlat} longitude range: {minlon} to {maxlon} author: {author} '''.format(name=name, time=datetime.now(), minlat=self.__bounds.bottom, maxlat=self.__bounds.top, minlon=self.__bounds.left, maxlon=self.__bounds.right, author=author)) self.__files.add(dst, True) def add_waypoint_file(self, filename): ''' Adds a waypoint file to the map @param filename: The file that should be added ''' print('Adding waypoint file...') if not os.path.exists(filename): raise RuntimeError('Waypoint file {} does not exist.'.format(filename)) dst = os.path.join(self.__dir_temp, "waypoints.xcw") shutil.copy(filename, dst) if not os.path.exists(dst): raise RuntimeError('Copying {} to {} failed.'.format(os.path.basename(filename), dst)) self.__files.add(dst, True) def add_waypoint_details_file(self, filename): ''' Adds a waypoint details file to the map @param filename: The file that should be added ''' print('Adding waypoint details file...') if not os.path.exists(filename): raise RuntimeError('Waypoint details file {} does not exist.'.format(filename)) dst = os.path.join(self.__dir_temp, 'airfields.txt') shutil.copy(filename, dst) if not os.path.exists(dst): raise RuntimeError('Copying {} to {} failed.'.format(os.path.basename(filename), dst)) self.__files.add(dst, True) def add_airspace_file(self, filename): ''' Adds a airspace file to the map @param filename: The file that should be added ''' print('Adding airspace file...') if not os.path.exists(filename): raise RuntimeError('Airspace file {} does not exist.'.format(filename)) dst = os.path.join(self.__dir_temp, 'airspace.txt') shutil.copy(filename, dst) if not os.path.exists(dst): raise RuntimeError('Copying {} to {} failed.'.format(os.path.basename(filename), dst)) self.__files.add(dst, True) def add_topology(self, bounds = None, compressed = False): print('Adding topology...') if not bounds: if not self.__bounds: raise RuntimeError('Boundaries undefined.') bounds = self.__bounds self.__files.extend(shapefiles.create(bounds, self.__downloader, self.__dir_temp, compressed)) def add_terrain(self, arcseconds_per_pixel = 9.0, bounds = None): print('Adding terrain...') if not bounds: if not self.__bounds: raise RuntimeError('Boundaries undefined.') bounds = self.__bounds self.__files.extend(srtm.create(bounds, arcseconds_per_pixel, self.__downloader, self.__dir_temp)) def set_bounds(self, bounds): if not isinstance(bounds, GeoRect): raise RuntimeError('GeoRect expected.') print('Setting map boundaries: {}'.format(bounds)) self.__bounds = bounds def create(self, filename): ''' Creates the map at the given location @param filename: Location of the map file that should be created ''' print('Creating map file...') z = ZipFile(filename, 'w', ZIP_DEFLATED) try: for file in self.__files: if os.path.isfile(file[0]): # file[1] is the flag if we should compress the file z.write(file[0], os.path.basename(file[0]), ZIP_DEFLATED if file[1] else ZIP_STORED) finally: z.close() def cleanup(self): for file in self.__files: if os.path.exists(file[0]): os.unlink(file[0]) self.__files.clear()
class Generator: def __init__(self, dir_data, dir_temp): ''' Constructor of the MapGenerator class @param dir_data: Path of the data folder @param dir_temp: Path of the temporary folder ''' self.__downloader = Downloader(dir_data) self.__dir_data = os.path.abspath(dir_data) self.__dir_temp = os.path.abspath(dir_temp) if not os.path.exists(self.__dir_temp): os.mkdir(self.__dir_temp) self.__bounds = None self.__files = FileList() def add_information_file(self, name, author = 'unknown'): ''' Adds an information file to the map ''' if not self.__bounds: raise RuntimeError('Boundaries undefined.') dst = os.path.join(self.__dir_temp, 'info.txt') spew(dst, '''map name: {name} generator: XCSoar Map Generator creation time: {time:%d.%m.%Y %T} ({time:%s}) latitude range: {minlat} to {maxlat} longitude range: {minlon} to {maxlon} author: {author} '''.format(name=name, time=datetime.now(), minlat=self.__bounds.bottom, maxlat=self.__bounds.top, minlon=self.__bounds.left, maxlon=self.__bounds.right, author=author)) self.__files.add(dst, True) def add_waypoint_file(self, filename): ''' Adds a waypoint file to the map @param filename: The file that should be added ''' print('Adding waypoint file...') if not os.path.exists(filename): raise RuntimeError('Waypoint file {} does not exist.'.format(filename)) if filename.lower().endswith('.cup'): dst = os.path.join(self.__dir_temp, "waypoints.cup") else: dst = os.path.join(self.__dir_temp, "waypoints.xcw") shutil.copy(filename, dst) if not os.path.exists(dst): raise RuntimeError('Copying {} to {} failed.'.format(os.path.basename(filename), dst)) self.__files.add(dst, True) def add_waypoint_details_file(self, filename): ''' Adds a waypoint details file to the map @param filename: The file that should be added ''' print('Adding waypoint details file...') if not os.path.exists(filename): raise RuntimeError('Waypoint details file {} does not exist.'.format(filename)) dst = os.path.join(self.__dir_temp, 'airfields.txt') shutil.copy(filename, dst) if not os.path.exists(dst): raise RuntimeError('Copying {} to {} failed.'.format(os.path.basename(filename), dst)) self.__files.add(dst, True) def add_airspace_file(self, filename): ''' Adds a airspace file to the map @param filename: The file that should be added ''' print('Adding airspace file...') if not os.path.exists(filename): raise RuntimeError('Airspace file {} does not exist.'.format(filename)) dst = os.path.join(self.__dir_temp, 'airspace.txt') shutil.copy(filename, dst) if not os.path.exists(dst): raise RuntimeError('Copying {} to {} failed.'.format(os.path.basename(filename), dst)) self.__files.add(dst, True) def add_topology(self, bounds = None, compressed = False, level_of_detail = 3): print('Adding topology...') if not bounds: if not self.__bounds: raise RuntimeError('Boundaries undefined.') bounds = self.__bounds self.__files.extend(shapefiles.create(bounds, self.__downloader, self.__dir_temp, compressed, level_of_detail)) def add_terrain(self, arcseconds_per_pixel = 9.0, bounds = None): print('Adding terrain...') if not bounds: if not self.__bounds: raise RuntimeError('Boundaries undefined.') bounds = self.__bounds self.__files.extend(srtm.create(bounds, arcseconds_per_pixel, self.__downloader, self.__dir_temp)) def add_welt2000(self, bounds = None): print('Adding welt2000 cup waypoints...') if not bounds: if not self.__bounds: raise RuntimeError('Boundaries undefined.') bounds = self.__bounds self.__files.extend(welt2000cup.create(self.__dir_data, self.__dir_temp, bounds)) def set_bounds(self, bounds): if not isinstance(bounds, GeoRect): raise RuntimeError('GeoRect expected.') print('Setting map boundaries: {}'.format(bounds)) self.__bounds = bounds def create(self, filename): ''' Creates the map at the given location @param filename: Location of the map file that should be created ''' print('Creating map file...') z = ZipFile(filename, 'w', ZIP_DEFLATED) try: for file in self.__files: if os.path.isfile(file[0]): # file[1] is the flag if we should compress the file z.write(file[0], os.path.basename(file[0]), ZIP_DEFLATED if file[1] else ZIP_STORED) finally: z.close() def cleanup(self): for file in self.__files: if os.path.exists(file[0]): os.unlink(file[0]) self.__files.clear()