def main(): options, flags = gs.parser() # main options map_name = options['input'] output_file = options['output'] # TODO: other options of g.proj are not supported epsg_code = int(options['epsg']) # r.out.png options compression = int(options['compression']) # both flags (tw) passed to r.out.png routpng_flags = '' if flags['t']: routpng_flags += 't' if flags['w']: routpng_flags += 'w' if flags['l']: wgs84_file = output_file + '.wgs84' else: wgs84_file = None if flags['m']: use_region = False else: use_region = True # TODO: mixing current and map's mapset at this point # or perhaps not an issue if parser adds mapset automatically (?) if '@' in map_name: map_name, src_mapset_name = map_name.split('@') else: src_mapset_name = gs.gisenv()['MAPSET'] export_png_in_projection(map_name=map_name, src_mapset_name=src_mapset_name, output_file=output_file, epsg_code=epsg_code, compression=compression, routpng_flags=routpng_flags, wgs84_file=wgs84_file, use_region=use_region)
def main(): options, flags = gcore.parser() # main options map_name = options['input'] output_file = options['output'] # TODO: other options of g.proj are not supported epsg_code = int(options['epsg']) # r.out.png options compression = int(options['compression']) # both flags (tw) passed to r.out.png routpng_flags = '' if flags['t']: routpng_flags += 't' if flags['w']: routpng_flags += 'w' if flags['l']: wgs84_file = output_file + '.wgs84' else: wgs84_file = None if flags['m']: use_region = False else: use_region = True if '@' in map_name: map_name, src_mapset_name = map_name.split('@') else: # TODO: maybe mapset is mandatory for those out of current mapset? src_mapset_name = '' export_png_in_projection(map_name=map_name, src_mapset_name=src_mapset_name, output_file=output_file, epsg_code=epsg_code, compression=compression, routpng_flags=routpng_flags, wgs84_file=wgs84_file, use_region=use_region)
def main(): options, flags = gcore.parser() # it does not check if pngs and other files exists, # maybe it could check the any/all file(s) dir if options['raster'] and options['strds']: gcore.fatal(_("Options raster and strds cannot be specified together." " Please decide for one of them.")) if options['raster'] and options['where']: gcore.fatal(_("Option where cannot be combined with the option raster." " Please don't set where option or use strds option" " instead of raster option.")) if options['raster']: if ',' in options['raster']: maps = options['raster'].split(',') # TODO: skip empty parts else: maps = [options['raster']] elif options['strds']: # import and init only when needed # init is called anyway when the generated form is used import grass.temporal as tgis strds = options['strds'] where = options['where'] # make sure the temporal database exists tgis.init() # create the space time raster object ds = tgis.open_old_space_time_dataset(strds, 'strds') # check if the dataset is in the temporal database if not ds.is_in_db(): gcore.fatal(_("Space time dataset <%s> not found") % strds) # we need a database interface dbiface = tgis.SQLDatabaseInterfaceConnection() dbiface.connect() # the query rows = ds.get_registered_maps(columns='id', where=where, order='start_time') if not rows: gcore.fatal(_("Cannot get any maps for spatio-temporal raster" " dataset <%s>." " Dataset is empty or you temporal WHERE" " condition filtered all maps out." " Please, specify another dataset," " put maps into this dataset" " or correct your WHERE condition.") % strds) maps = [row['id'] for row in rows] else: gcore.fatal(_("Either raster or strds option must be specified." " Please specify one of them.")) # get the number of maps for later use num_maps = len(maps) out_dir = options['output'] if not os.path.exists(out_dir): # TODO: maybe we could create the last dir on specified path? gcore.fatal(_("Output path <%s> does not exists." " You need to create the (empty) output directory" " yourself before running this module.") % out_dir) epsg = int(options['epsg']) if ',' in options['opacity']: opacities = [float(opacity) for opacity in options['opacity'].split(',')] if len(opacities) != num_maps: gcore.fatal(_("Number of opacities <{no}> does not match number" " of maps <{nm}>.").format(no=len(opacities), nm=num_maps)) else: opacities = [float(options['opacity'])] * num_maps if ',' in options['info']: infos = options['info'].split(',') else: infos = [options['info']] # r.out.png options compression = int(options['compression']) # flag w is passed to r.out.png.proj # our flag n is inversion of r.out.png.proj's t flag # (transparent NULLs are better for overlay) # we always need the l flag (ll .wgs84 file) routpng_flags = '' if not flags['n']: routpng_flags += 't' if flags['w']: routpng_flags += 'w' # r.out.png.proj l flag for LL .wgs84 file is now function parameter # and is specified bellow if flags['m']: use_region = False # we will use map extent gcore.use_temp_region() else: use_region = True # hard coded file names data_file_name = 'data_file.csv' js_data_file_name = 'data_file.js' data_file = open(os.path.join(out_dir, data_file_name), 'w') js_data_file = open(os.path.join(out_dir, js_data_file_name), 'w') js_data_file.write('/* This file was generated by r.out.leaflet GRASS GIS' ' module. */\n\n') js_data_file.write('var layerInfos = [\n') for i, map_name in enumerate(maps): if not use_region: if gcore.run_command('g.region', rast=map_name): raise RuntimeError("Cannot set region from map <%s>." % map_name) if '@' in map_name: pure_map_name = map_name.split('@')[0] else: pure_map_name = map_name # TODO: mixing current and map's mapset at this point if '@' in map_name: map_name, src_mapset_name = map_name.split('@') else: # TODO: maybe mapset is mandatory for those out of current mapset? src_mapset_name = gcore.gisenv()['MAPSET'] image_file_name = pure_map_name + '.png' image_file_path = os.path.join(out_dir, image_file_name) # TODO: skip writing to file and extract the information from # function, or use object if function is so large wgs84_file = image_file_path + '.wgs84' export_png_in_projection(map_name=map_name, src_mapset_name=src_mapset_name, output_file=image_file_path, epsg_code=epsg, compression=compression, routpng_flags=routpng_flags, wgs84_file=wgs84_file, use_region=True) data_file.write(pure_map_name + ',' + image_file_name + '\n') # it doesn't matter in which location we are, it just uses the current # location, not tested for LL loc, assuming that to be nop. map_extent = get_map_extent_for_file(wgs84_file) bounds = map_extent_to_js_leaflet_list(map_extent) extra_attributes = [] generate_infos(map_name=map_name, projected_png_file=image_file_path, required_infos=infos, output_directory=out_dir, attributes=extra_attributes) # http://www.w3schools.com/js/js_objects.asp js_data_file.write(""" {{title: "{title}", file: "{file_}",""" """ bounds: {bounds}, opacity: {opacity}""" .format(title=pure_map_name, file_=image_file_name, bounds=bounds, opacity=opacities[i])) if extra_attributes: extra_js_attributes = [pair[0] + ': "' + escape_quotes( escape_endlines( escape_backslashes( pair[1] ))) + '"' for pair in extra_attributes] js_data_file.write(', ' + ', '.join(extra_js_attributes)) js_data_file.write("""}\n""") # do not write after the last item if i < num_maps - 1: js_data_file.write(',') js_data_file.write('];\n') data_file.close()
def main(): options, flags = gs.parser() # it does not check if pngs and other files exists, # maybe it could check the any/all file(s) dir if options['raster'] and options['strds']: gs.fatal( _("Options raster and strds cannot be specified together." " Please decide for one of them.")) if options['raster'] and options['where']: gs.fatal( _("Option where cannot be combined with the option raster." " Please don't set where option or use strds option" " instead of raster option.")) if options['raster']: if ',' in options['raster']: maps = options['raster'].split(',') # TODO: skip empty parts else: maps = [options['raster']] elif options['strds']: # import and init only when needed # init is called anyway when the generated form is used import grass.temporal as tgis strds = options['strds'] where = options['where'] # make sure the temporal database exists tgis.init() # create the space time raster object ds = tgis.open_old_space_time_dataset(strds, 'strds') # check if the dataset is in the temporal database if not ds.is_in_db(): gs.fatal(_("Space time dataset <%s> not found") % strds) # we need a database interface dbiface = tgis.SQLDatabaseInterfaceConnection() dbiface.connect() # the query rows = ds.get_registered_maps(columns='id', where=where, order='start_time') if not rows: gs.fatal( _("Cannot get any maps for spatio-temporal raster" " dataset <%s>." " Dataset is empty or you temporal WHERE" " condition filtered all maps out." " Please, specify another dataset," " put maps into this dataset" " or correct your WHERE condition.") % strds) maps = [row['id'] for row in rows] else: gs.fatal( _("Either raster or strds option must be specified." " Please specify one of them.")) # get the number of maps for later use num_maps = len(maps) out_dir = options['output'] if not os.path.exists(out_dir): # TODO: maybe we could create the last dir on specified path? gs.fatal( _("Output path <%s> does not exists." " You need to create the (empty) output directory" " yourself before running this module.") % out_dir) epsg = int(options['epsg']) if ',' in options['opacity']: opacities = [ float(opacity) for opacity in options['opacity'].split(',') ] if len(opacities) != num_maps: gs.fatal( _("Number of opacities <{no}> does not match number" " of maps <{nm}>.").format(no=len(opacities), nm=num_maps)) else: opacities = [float(options['opacity'])] * num_maps if ',' in options['info']: infos = options['info'].split(',') else: infos = [options['info']] if 'geotiff' in infos and not gs.find_program('r.out.tiff', '--help'): gs.fatal(_("Install r.out.tiff add-on module to export GeoTIFF")) # r.out.png options compression = int(options['compression']) # flag w is passed to r.out.png.proj # our flag n is inversion of r.out.png.proj's t flag # (transparent NULLs are better for overlay) # we always need the l flag (ll .wgs84 file) routpng_flags = '' if not flags['n']: routpng_flags += 't' if flags['w']: routpng_flags += 'w' # r.out.png.proj l flag for LL .wgs84 file is now function parameter # and is specified bellow if flags['m']: use_region = False # we will use map extent gs.use_temp_region() else: use_region = True # hard coded file names data_file_name = 'data_file.csv' js_data_file_name = 'data_file.js' data_file = open(os.path.join(out_dir, data_file_name), 'w') js_data_file = open(os.path.join(out_dir, js_data_file_name), 'w') js_data_file.write('/* This file was generated by r.out.leaflet GRASS GIS' ' module. */\n\n') js_data_file.write('var layerInfos = [\n') for i, map_name in enumerate(maps): if not use_region: gs.run_command('g.region', rast=map_name) if '@' in map_name: pure_map_name = map_name.split('@')[0] else: pure_map_name = map_name # TODO: mixing current and map's mapset at this point if '@' in map_name: map_name, src_mapset_name = map_name.split('@') else: # TODO: maybe mapset is mandatory for those out of current mapset? src_mapset_name = gs.gisenv()['MAPSET'] image_file_name = pure_map_name + '.png' image_file_path = os.path.join(out_dir, image_file_name) # TODO: skip writing to file and extract the information from # function, or use object if function is so large wgs84_file = image_file_path + '.wgs84' export_png_in_projection(map_name=map_name, src_mapset_name=src_mapset_name, output_file=image_file_path, epsg_code=epsg, compression=compression, routpng_flags=routpng_flags, wgs84_file=wgs84_file, use_region=True) data_file.write(pure_map_name + ',' + image_file_name + '\n') # it doesn't matter in which location we are, it just uses the current # location, not tested for LL loc, assuming that to be nop. map_extent = get_map_extent_for_file(wgs84_file) bounds = map_extent_to_js_leaflet_list(map_extent) extra_attributes = [] generate_infos(map_name=map_name, projected_png_file=image_file_path, required_infos=infos, output_directory=out_dir, attributes=extra_attributes) # http://www.w3schools.com/js/js_objects.asp js_data_file.write(""" {{title: "{title}", file: "{file_}",""" """ bounds: {bounds}, opacity: {opacity}""".format( title=pure_map_name, file_=image_file_name, bounds=bounds, opacity=opacities[i])) if extra_attributes: extra_js_attributes = [ pair[0] + ': "' + escape_quotes(escape_endlines(escape_backslashes(pair[1]))) + '"' for pair in extra_attributes ] js_data_file.write(', ' + ', '.join(extra_js_attributes)) js_data_file.write("""}\n""") # do not write after the last item if i < num_maps - 1: js_data_file.write(',') js_data_file.write('];\n') data_file.close()