def _initializeParameters(self, options, flags): ''' Initialize all given and needed parameters. Get region information and calculate boundingbox according to it ''' self._debug("_initializeParameters", "started") self._env = os.environ.copy() self._env['GRASS_MESSAGE_FORMAT'] = 'gui' g.gisenv(set="GRASS_MESSAGE_FORMAT=gui") for key in ['url', 'coverage','output','location']: self.params[key] = options[key].strip() if not self.params['output']: self.params['output'] = self.params['coverage'] if not gscript.overwrite(): result = gscript.find_file(name = self.params['output'], element = 'cell') if result['file']: gscript.fatal("Raster map <%s> does already exist. Choose other output name or toggle flag --o." % self.params['output']) for key in ['password', 'username', 'version','region']: self.params[key] = options[key] # check if authentication information is complete if (self.params['password'] and self.params['username'] == '') or \ (self.params['password'] == '' and self.params['username']): gscript.fatal(_("Please insert both %s and %s parameters or none of them." % ('password', 'username'))) # configure region extent (specified name or current region) self.params['region'] = self._getRegionParams(options['region']) self.params['boundingbox'] = self._computeBbox(self.params['region']) self._debug("_initializeParameters", "finished")
def view(rasters=None, vectors=None, pngfile=None, width=640, height=480, transparent=True, read_file=True, truecolor=True, engine='cairo', compression=9, rkw=None, vkw=None, corner=False, meta=False): """Return an IPython image object rendered by GRASS GIS. Parameters ----------- rasters: list List with the raster map name to be rendered vectors: list List with the vector map name to be rendered pngfile: path Path to the PNG file, default creates a temporary file width: int Width size of the image height: int Height size of the image """ def gdisplay(): for raster in rasters if rasters else []: d.rast(map=raster, **(rkw if rkw else {})) for vector in vectors if vectors else []: d.vect(map=vector, **(vkw if vkw else {})) unique_filename = uuid.uuid4() pngfile = str(unique_filename.int)+'.png' bounds = llcorner().corners if corner: dict2html(bounds) # set the enviornmental variables os.environ['GRASS_RENDER_IMMEDIATE'] = engine os.environ['GRASS_RENDER_FILE'] = pngfile os.environ['GRASS_RENDER_FILE_COMPRESSION'] = str(compression) os.environ['GRASS_RENDER_WIDTH'] = str(width) os.environ['GRASS_RENDER_HEIGHT'] = str(height) os.environ['GRASS_RENDER_TRANSPARENT'] = 'TRUE' if transparent else None os.environ['GRASS_RENDER_READ_FILE'] = 'TRUE' if read_file else None os.environ['GRASS_RENDER_TRUECOLOR'] = 'TRUE' if truecolor else None os.environ['GRASS_RENDER_PNG_AUTO_WRITE'] = 'TRUE' monitor = gcore.gisenv().get('MONITOR', None) if monitor: g.gisenv(unset='MONITOR') gdisplay() g.gisenv(set='MONITOR=%s' % monitor) else: gdisplay() if meta: metadata={'corners': bounds, 'filename': pngfile, 'raster':rasters, 'vector':vectors} if not meta: metadata=None return display(Image(pngfile),metadata=metadata)
def view(rasters=None, vectors=None, pngfile=None, width=640, height=480, transparent=True, read_file=True, truecolor=True, engine='cairo', compression=9, rkw=None, vkw=None): """Return an IPython image object rendered by GRASS GIS. Parameters ----------- rasters: list List with the raster map name to be rendered vectors: list List with the vector map name to be rendered pngfile: path Path to the PNG file, default creates a temporary file width: int Width size of the image height: int Height size of the image """ def display(): for raster in rasters if rasters else []: d.rast(map=raster, **(rkw if rkw else {})) for vector in vectors if vectors else []: d.vect(map=vector, **(vkw if vkw else {})) pngfile = (tempfile.mkstemp(suffix='.png')[1] if pngfile is None else pngfile) # set the enviornmental variables os.environ['GRASS_RENDER_IMMEDIATE'] = engine os.environ['GRASS_RENDER_FILE'] = pngfile os.environ['GRASS_RENDER_FILE_COMPRESSION'] = str(compression) os.environ['GRASS_RENDER_WIDTH'] = str(width) os.environ['GRASS_RENDER_HEIGHT'] = str(height) os.environ['GRASS_RENDER_TRANSPARENT'] = 'TRUE' if transparent else None os.environ['GRASS_RENDER_READ_FILE'] = 'TRUE' if read_file else None os.environ['GRASS_RENDER_TRUECOLOR'] = 'TRUE' if truecolor else None os.environ['GRASS_RENDER_PNG_AUTO_WRITE'] = 'TRUE' monitor = gcore.gisenv().get('MONITOR', None) if monitor: g.gisenv(unset='MONITOR') display() g.gisenv(set='MONITOR=%s' % monitor) else: display() return Image(pngfile)
def main(): os.environ["GRASS_RENDER_IMMEDIATE"] = "png" os.environ["GRASS_RENDER_FILE"] = options["output"] os.environ["GRASS_RENDER_FILE_COMPRESSION"] = options["compression"] os.environ["GRASS_RENDER_WIDTH"] = options["width"] os.environ["GRASS_RENDER_HEIGHT"] = options["height"] if flags["w"]: # get display region info s = grass.read_command("d.info", flags="g") win = grassutils.parse_key_val(s, val_type=float) monitor_old = None genv = gisenv() if "MONITOR" in genv: monitor_old = genv["MONITOR"] g.gisenv(unset="MONITOR") if options["rgb_column"]: d.vect( map=options["input"], rgb_column=options["rgb_column"], flags="a", quiet=True, ) else: d.vect(map=options["input"]) if monitor_old: g.gisenv(set="MONITOR=%s" % monitor_old) if flags["w"]: wldfile = options["output"].split(".")[0] + ".wld" file_ = open(wldfile, "w") file_.write("%36.15f \n" % win["ewres"]) file_.write("%36.15f \n" % 0.0) file_.write("%36.15f \n" % 0.0) file_.write("%36.15f \n" % (-1 * win["nsres"])) file_.write("%36.15f \n" % (win["w"] + win["ewres"] / 2.0)) file_.write("%36.15f \n" % (win["n"] - win["nsres"] / 2.0)) file_.close()
def main(): os.environ['GRASS_RENDER_IMMEDIATE'] = 'png' os.environ['GRASS_RENDER_FILE'] = options['output'] os.environ['GRASS_RENDER_FILE_COMPRESSION'] = options['compression'] os.environ['GRASS_RENDER_WIDTH'] = options['width'] os.environ['GRASS_RENDER_HEIGHT'] = options['height'] if flags['w']: # get display region info s = grass.read_command('d.info', flags='g') win = grassutils.parse_key_val(s, val_type=float) monitor_old = None genv = gisenv() if 'MONITOR' in genv: monitor_old = genv['MONITOR'] g.gisenv(unset='MONITOR') if options['rgb_column']: d.vect(map=options['input'], rgb_column=options['rgb_column'], flags='a', quiet=True) else: d.vect(map=options['input']) if monitor_old: g.gisenv(set='MONITOR=%s' % monitor_old) if flags['w']: wldfile = options['output'].split('.')[0] + '.wld' file_ = open(wldfile, "w") file_.write("%36.15f \n" % win['ewres']) file_.write("%36.15f \n" % 0.0) file_.write("%36.15f \n" % 0.0) file_.write("%36.15f \n" % (-1 * win['nsres'])) file_.write("%36.15f \n" % (win['w'] + win['ewres'] / 2.0)) file_.write("%36.15f \n" % (win['n'] - win['nsres'] / 2.0)) file_.close()
def view( rasters=None, vectors=None, pngfile=None, width=640, height=480, transparent=True, read_file=True, truecolor=True, engine="cairo", compression=9, rkw=None, vkw=None, corner=False, meta=False, ): """Return an IPython image object rendered by GRASS GIS. Parameters ----------- rasters: list List with the raster map name to be rendered vectors: list List with the vector map name to be rendered pngfile: path Path to the PNG file, default creates a temporary file width: int Width size of the image height: int Height size of the image """ def gdisplay(): for raster in rasters if rasters else []: d.rast(map=raster, **(rkw if rkw else {})) for vector in vectors if vectors else []: d.vect(map=vector, **(vkw if vkw else {})) unique_filename = uuid.uuid4() pngfile = str(unique_filename.int) + ".png" bounds = llcorner().corners if corner: dict2html(bounds) # set the enviornmental variables os.environ["GRASS_RENDER_IMMEDIATE"] = engine os.environ["GRASS_RENDER_FILE"] = pngfile os.environ["GRASS_RENDER_FILE_COMPRESSION"] = str(compression) os.environ["GRASS_RENDER_WIDTH"] = str(width) os.environ["GRASS_RENDER_HEIGHT"] = str(height) os.environ["GRASS_RENDER_TRANSPARENT"] = "TRUE" if transparent else None os.environ["GRASS_RENDER_READ_FILE"] = "TRUE" if read_file else None os.environ["GRASS_RENDER_TRUECOLOR"] = "TRUE" if truecolor else None os.environ["GRASS_RENDER_PNG_AUTO_WRITE"] = "TRUE" monitor = gcore.gisenv().get("MONITOR", None) if monitor: g.gisenv(unset="MONITOR") gdisplay() g.gisenv(set="MONITOR=%s" % monitor) else: gdisplay() if meta: metadata = {"corners": bounds, "filename": pngfile, "raster": rasters, "vector": vectors} if not meta: metadata = None return display(Image(pngfile), metadata=metadata)
width=width, height=height, overlap=20, processes=cpus, input=grassfile, output=grasscontoursfile, minlevel=200, maxlevel=800, step=10, overwrite=True) grd.run() #Write output to file v.out_ogr(input=grasscontoursfile, output=contoursfile, overwrite=True) # These can be left out, just debug info print("\n\n ***DEBUG INFO***") print("GRASS version") print(g.version()) print("GRASS env settings: gisdatabase, location, mapset") g.gisenv() print("Available datasets:") g.list(type="all", flags='m') print("Input file info") r.info(map=grassfile, verbose=True) print("Output info") v.info(map=grasscontoursfile, verbose=True)
# Set GRASS region g.region(raster=grassfile) # Perform GRASS analysis, here calculate contours from DEM r.contour(input=grassfile, output=grasscontoursfile, minlevel=200, maxlevel=800, step=10, overwrite=True) #Write output to file v.out_ogr(input=grasscontoursfile, output=contoursfile, overwrite=True) # These can be left out, just debug info # TODO: not working properly! print("\n\n ***DEBUG INFO***") print("GRASS version") print(g.version()) print("GRASS env settings: gisdatabase, location, mapset") print(g.gisenv()) print("Available datasets:") print(g.list(type="all", flags='m')) print("Input file info") print(r.info(map=grassfile, verbose=True)) print("Output info") print(v.info(map=grasscontoursfile, verbose=True))