def main(): options, flags = gcore.parser() probability = options['probability'] output = options['output'] count = int(options['count']) gcore.use_temp_region() # probability map probab_01 = 'probability_01_' + str(os.getpid()) TMP_RAST.append(probab_01) info = grast.raster_info(probability) gcore.write_command('r.recode', flags='d', input=probability, output=probab_01, title="Recoded probability map to 0 to 1", rules='-', stdin='{minim}:{maxim}:0:1'.format(minim=info['min'], maxim=info['max'])) mean = gcore.parse_key_val(gcore.read_command('r.univar', map=probab_01, flags='g'), val_type=float)['mean'] resolution = count / (mean * (info['north'] - info['south'] + info['east'] - info['west'])) resolution = sqrt((mean * (info['north'] - info['south']) * (info['east'] - info['west'])) / count) gcore.run_command('g.region', res=resolution) random_name = 'random_' + str(os.getpid()) point_map = 'points_' + str(os.getpid()) point_grid = 'points_' + str(os.getpid()) TMP_RAST.append(random_name) TMP_RAST.append(point_map) TMP_VECT.append(point_grid) gcore.run_command('r.surf.random', output=random_name, min=0, max=1) grast.mapcalc(exp='{point_map} = if({rand} <= {prob}, 1, null())'.format(rand=random_name, prob=probab_01, point_map=point_map)) gcore.run_command('r.to.vect', flags='t', input=point_map, output=point_grid, type='point') gcore.run_command('v.perturb', input=point_grid, output=output, parameter=resolution / 2., seed=os.getpid())
def GetRequests(self): ret = [] rf = open(self.request_file) try: for line in rf.readlines(): ret.append(grass.parse_key_val(line, vsep = ';')) finally: rf.close() return ret
def get_region(): """Returns current computational region as dictionary. Uses standardized key names. Outputs only 2D region values which are usable for conversion to another location. """ gregion_out = gcore.read_command('g.region', flags='pg') region = gcore.parse_key_val(gregion_out, sep='=') return {'east': region['e'], 'north': region['n'], 'west': region['w'], 'south': region['s'], 'rows': region['rows'], 'cols': region['cols']}
def ComputeStatitistics(self): """!Computes statistics for raster map using 'r.univar' module. @return statistic in form of dictionary """ # RunCommand enables to run GRASS module res = RunCommand('r.univar', # module name flags='g', # command flags map=self.currentRaster, # module parameters read=True) # get command output return gcore.parse_key_val(res, val_type=float)
def main(): shell = flags['g'] image = {} for band in bands: image[band] = options['image%d' % band] # calculate the Stddev for TM bands grass.message(_("Calculating Standard deviations for all bands...")) stddev = {} for band in bands: grass.verbose("band %d" % band) s = grass.read_command('r.univar', flags = 'g', map = image[band]) kv = grass.parse_key_val(s) stddev[band] = float(kv['stddev']) grass.message(_("Calculating Correlation Matrix...")) correlation = {} s = grass.read_command('r.covar', flags = 'r', map = [image[band] for band in bands]) for i, row in zip(bands, s.splitlines()): for j, cell in zip(bands, row.split(' ')): correlation[i,j] = float(cell) # Calculate all combinations grass.message(_("Calculating OIF for the 20 band combinations...")) oif = [] for p in perms(): oif.append((oifcalc(stddev, correlation, *p), p)) oif.sort(reverse = True) grass.verbose(_("The Optimum Index Factor analysis result " "(Best combination comes first):")) if shell: fmt = "%d%d%d:%f\n" else: fmt = "%d%d%d: %f\n" outf = file('i.oif.result', 'w') for v, p in oif: sys.stdout.write(fmt % (p + (v,))) outf.write(fmt % (p + (v,))) outf.close()
def __set_options(self): # if the user asserts that this projection is the same as the # source use this projection as the source to get a trivial # tiling from r.tileset if self.flags['p']: self.proj_srs = grass.read_command('g.proj', flags='j') if not self.proj_srs: grass.fatal(_('g.proj failed')) self.srs_scale = int(grass.parse_key_val(proj_srs['+to_meter'])) else: self.proj_srs = '+init=%s' % self.options['srs'].lower() self.srs_scale = 1 # options for r.tileset self.tileset_options = grass.parse_key_val(self.options['tileoptions']) if self.options['region']: self.tileset_options['region'] = self.options['region'] # set transparency if self.flags['o']: self.transparency = "transparent=FALSE" else: self.transparency = "transparent=FALSE" # image format format_opt = self.options['format'] if format_opt == "geotiff": self.format = "image/geotiff" self.worldfile = ".tfw" self.file_extent = ".geotiff" elif format_opt == "tiff": self.format = "image/tiff" self.worldfile = ".tfw" self.file_extent = ".tiff" elif format_opt == "png": self.format = "image/png" self.worldfile = ".pgw" self.file_extent = ".png" elif format_opt == "jpeg": self.format = "image/jpeg" self.worldfile = ".jgw" self.file_extent = ".jpeg" elif format_opt == "gif": self.format = "image/gif" self.worldfile = ".gfw" self.file_extent = ".gif" else: grass.fatal(_("Uknown image format '%s'") % format_opt) # create download directory if not os.path.exists(self.options['folder']): os.mkdir(self.options['folder']) odir = os.path.join(self.options['folder'], self.options['output']) if not os.path.exists(odir): os.mkdir(odir) self._tdir = os.path.join(self.options['folder'], self.options['output'], self.options['region']) if not os.path.exists(self._tdir): os.mkdir(self._tdir) self.request_file = os.path.join(self._tdir, 'request')
def GetTiles(self): grass.message(_("Calculating tiles...")) tiles = grass.read_command('r.tileset', quiet = True, flags = 'g', sourceproj = self.proj_srs, sourcescale = self.srs_scale, overlap = 2, maxcols = int(self.options['maxcols']), maxrows = int(self.options['maxrows']), **self.tileset_options) if not tiles: grass.fatal(_("r.tileset failed")) tiles = tiles.splitlines() if self.flags['c']: rmfiles = os.path.join(self._tdir, '*') grass.verbose("Removing files '%s'" % rmfiles) for file in glob.glob(rmfiles): if os.path.isdir(file): os.rmdir(file) else: os.remove(file) rf = open(self.request_file, 'w') i = 0 for tile in tiles: outputfile = os.path.join(self._tdir, str(i) + self.file_extent) worldfile = os.path.join(self._tdir, str(i) + self.worldfile) dtile = grass.parse_key_val(tile, vsep=';') n = float(dtile['n']) self.data['n'] = n s = float(dtile['s']) self.data['s'] = s e = float(dtile['e']) self.data['e'] = e w = float(dtile['w']) self.data['w'] = w nr = int(dtile['rows']) nc = int(dtile['cols']) self.data['width'] = nc self.data['height'] = nr size = "bbox=%f,%f,%f,%f&width=%d&height=%d" % \ (w, s, e, n, nc, nr) xres = (e - w) / nc yres = (s - n) / nr # center of top left cell top_left_cell_center_x = w + xres / 2 top_left_cell_center_y = n + yres / 2 # write the world file wf = open(worldfile, 'w') try: wf.write("%f\n0.0\n0.0\n%f\n%f\n%f\n" % \ (xres, yres, top_left_cell_center_x, top_left_cell_center_y)) finally: wf.close() # request for data string = "service=WMS&request=GetMap&layers=%s&styles=%s&srs=%s&%s&format=%s&%s&%s" % \ (self.options['layers'], self.options['styles'], self.options['srs'], size, self.format, self.transparency, self.options['wmsquery']) rf.write('output=%s;server=%s;string=%s\n' % \ (outputfile, self.options['mapserver'], string)) i += 1 rf.close() grass.message(_("Done: requesting %d tiles") % len(tiles)) if len(tiles) > 200: grass.warning("Proceed with care. This number of tiles may \ exceed the maximum command line arguments available from \ the operating system later on in the r.in.gdalwarp step. \ In addition it may be considered abusive behaivor by the \ server providers - check their terms of use.")
def export_png_in_projection(src_mapset_name, map_name, output_file, epsg_code, routpng_flags, compression, wgs84_file, use_region=True): """ :param use_region: use computation region and not map extent """ if use_region: src_region = get_region() src_proj_string = get_location_proj_string() # TODO: change only location and not gisdbase? # we rely on the tmp dir having enough space for our map tgt_gisdbase = tempfile.mkdtemp() # this is not needed if we use mkdtemp but why not tgt_location = 'r.out.png.proj_location_%s' % epsg_code # because we are using PERMANENT we don't have to create mapset explicitly tgt_mapset_name = 'PERMANENT' src_mapset = Mapset(src_mapset_name) # get source (old) and set target (new) GISRC enviromental variable # TODO: set environ only for child processes could be enough and it would # enable (?) parallel runs src_gisrc = os.environ['GISRC'] tgt_gisrc = gsetup.write_gisrc(tgt_gisdbase, tgt_location, tgt_mapset_name) os.environ['GISRC'] = tgt_gisrc if os.environ.get('WIND_OVERRIDE'): old_temp_region = os.environ['WIND_OVERRIDE'] del os.environ['WIND_OVERRIDE'] else: old_temp_region = None # these lines looks good but anyway when developing the module # switching location seemed fragile and on some errors (while running # unfinished module) location was switched in the command line try: # the function itself is not safe for other (backgroud) processes # (e.g. GUI), however we already switched GISRC for us # and child processes, so we don't influece others gcore.create_location(dbase=tgt_gisdbase, location=tgt_location, epsg=epsg_code, datum=None, datum_trans=None) # Mapset object cannot be created if the real mapset does not exists tgt_mapset = Mapset(gisdbase=tgt_gisdbase, location=tgt_location, mapset=tgt_mapset_name) # set the current mapset in the library # we actually don't need to switch when only calling modules # (right GISRC is enough for them) tgt_mapset.current() # setting region if use_region: # respecting computation region of the src location # by previous use g.region in src location # and m.proj and g.region now # respecting MASK of the src location would be hard # null values in map are usually enough tgt_proj_string = get_location_proj_string() tgt_region = reproject_region(src_region, from_proj=src_proj_string, to_proj=tgt_proj_string) # uses g.region thus and sets region only for child processes # which is enough now set_region(tgt_region) else: # find out map extent to import everything # using only classic API because of some problems with pygrass # on ms windows rproj_out = gcore.read_command('r.proj', input=map_name, dbase=src_mapset.gisdbase, location=src_mapset.location, mapset=src_mapset.name, output=map_name, flags='g') a = gcore.parse_key_val(rproj_out, sep='=', vsep=' ') gcore.run_command('g.region', **a) # map import gcore.run_command('r.proj', input=map_name, dbase=src_mapset.gisdbase, location=src_mapset.location, mapset=src_mapset.name, output=map_name) # actual export gcore.run_command('r.out.png', input=map_name, output=output_file, compression=compression, flags=routpng_flags) # outputting file with WGS84 coordinates if wgs84_file: gcore.message("Projecting coordinates to LL WGS 84...") with open(wgs84_file, 'w') as data_file: if use_region: # map which is smaller than region is imported in its own # small extent, but we export image in region, so we need # bounds to be for region, not map # hopefully this is consistent with r.out.png behavior data_file.write( map_extent_to_file_content( proj_to_wgs84(get_region())) + '\n') else: # use map to get extent # the result is actually the same as using map # if region is the same as map (use_region == False) data_file.write( map_extent_to_file_content( get_map_extent_for_location(map_name)) + '\n') finally: # juts in case we need to do something in the old location # our callers probably do os.environ['GISRC'] = src_gisrc if old_temp_region: os.environ['WIND_OVERRIDE'] = old_temp_region # set current in library src_mapset.current() # delete the whole gisdbase # delete file by file to ensure that we are deleting only our things # exception will be raised when removing non-empty directory tgt_location_path = Location(gisdbase=tgt_gisdbase, location=tgt_location).path() tgt_mapset.delete() os.rmdir(tgt_location_path) # dir created by tempfile.mkdtemp() needs to be romved manually os.rmdir(tgt_gisdbase) # we have to remove file created by tempfile.mkstemp function # in write_gisrc function os.remove(tgt_gisrc)
def get_map_extent_for_location(map_name): info_out = gcore.read_command('r.info', map=map_name, flags='g') info = gcore.parse_key_val(info_out, sep='=') return proj_to_wgs84(info)
def main(): input = options["input"] output = options["output"] fs = options["fs"] proj_in = options["proj_input"] proj_out = options["proj_output"] ll_in = flags["i"] ll_out = flags["o"] decimal = flags["d"] copy_input = flags["e"] include_header = flags["c"] #### check for cs2cs if not grass.find_program("cs2cs"): grass.fatal(_("cs2cs program not found, install PROJ.4 first: http://proj.maptools.org")) #### check for overenthusiasm if proj_in and ll_in: grass.fatal(_("Choose only one input parameter method")) if proj_out and ll_out: grass.fatal(_("Choose only one output parameter method")) if ll_in and ll_out: grass.fatal(_("Choise only one auto-projection parameter method")) if output and not grass.overwrite() and os.path.exists(output): grass.fatal(_("Output file already exists")) #### parse field separator # FIXME: input_x,y needs to split on multiple whitespace between them if fs == ",": ifs = ofs = "," else: try: ifs, ofs = fs.split(",") except ValueError: ifs = ofs = fs ifs = ifs.lower() ofs = ofs.lower() if ifs in ("space", "tab"): ifs = " " elif ifs == "comma": ifs = "," else: if len(ifs) > 1: grass.warning(_("Invalid field separator, using '%s'") % ifs[0]) try: ifs = ifs[0] except IndexError: grass.fatal(_("Invalid field separator '%s'") % ifs) if ofs.lower() == "space": ofs = " " elif ofs.lower() == "tab": ofs = "\t" elif ofs.lower() == "comma": ofs = "," else: if len(ofs) > 1: grass.warning(_("Invalid field separator, using '%s'") % ofs[0]) try: ofs = ofs[0] except IndexError: grass.fatal(_("Invalid field separator '%s'") % ifs) #### set up projection params s = grass.read_command("g.proj", flags="j") kv = grass.parse_key_val(s) if "XY location" in kv["+proj"] and (ll_in or ll_out): grass.fatal(_("Unable to project to or from a XY location")) in_proj = None if ll_in: in_proj = "+proj=longlat +datum=WGS84" grass.verbose("Assuming LL WGS84 as input, current projection as output ") if ll_out: in_proj = grass.read_command("g.proj", flags="jf") if proj_in: in_proj = proj_in if not in_proj: grass.verbose("Assuming current location as input") in_proj = grass.read_command("g.proj", flags="jf") in_proj = in_proj.strip() grass.verbose("Input parameters: '%s'" % in_proj) out_proj = None if ll_out: out_proj = "+proj=longlat +datum=WGS84" grass.verbose("Assuming current projection as input, LL WGS84 as output ") if ll_in: out_proj = grass.read_command("g.proj", flags="jf") if proj_out: out_proj = proj_out if not out_proj: grass.fatal(_("Missing output projection parameters ")) out_proj = out_proj.strip() grass.verbose("Output parameters: '%s'" % out_proj) #### set up input file if input == "-": infile = None inf = sys.stdin else: infile = input if not os.path.exists(infile): grass.fatal(_("Unable to read input data")) inf = file(infile) grass.debug("input file=[%s]" % infile) #### set up output file if not output: outfile = None outf = sys.stdout else: outfile = output outf = open(outfile, "w") grass.debug("output file=[%s]" % outfile) #### set up output style if not decimal: outfmt = ["-w5"] else: outfmt = ["-f", "%.8f"] if not copy_input: copyinp = [] else: copyinp = ["-E"] #### do the conversion # Convert cs2cs DMS format to GRASS DMS format: # cs2cs | sed -e 's/d/:/g' -e "s/'/:/g" -e 's/"//g' cmd = ["cs2cs"] + copyinp + outfmt + in_proj.split() + ["+to"] + out_proj.split() p = grass.Popen(cmd, stdin=grass.PIPE, stdout=grass.PIPE) tr = TrThread(ifs, inf, p.stdin) tr.start() if not copy_input: if include_header: outf.write("x%sy%sz\n" % (ofs, ofs)) for line in p.stdout: xy, z = line.split(" ", 1) x, y = xy.split("\t") outf.write("%s%s%s%s%s\n" % (x.strip(), ofs, y.strip(), ofs, z.strip())) else: if include_header: outf.write("input_x%sinput_y%sx%sy%sz\n" % (ofs, ofs, ofs, ofs)) for line in p.stdout: inXYZ, x, rest = line.split("\t") inX, inY = inXYZ.split(" ")[:2] y, z = rest.split(" ", 1) outf.write( "%s%s%s%s%s%s%s%s%s\n" % (inX.strip(), ofs, inY.strip(), ofs, x.strip(), ofs, y.strip(), ofs, z.strip()) ) p.wait() if p.returncode != 0: grass.warning(_("Projection transform probably failed, please investigate"))
def main(): fileorig = options['input'] filevect = options['output'] if not filevect: filevect = grass.basename(fileorig, 'txt') #are we in LatLong location? s = grass.read_command("g.proj", flags='j') kv = grass.parse_key_val(s) if kv['+proj'] != 'longlat': grass.fatal(_("This module only operates in LatLong/WGS84 locations")) #### setup temporary file tmpfile = grass.tempfile() coldescs = [("RC", "rc integer"), ("UFI", "uf1 integer"), ("UNI", "uni integer"), ("LAT", "lat double precision"), ("LONG", "lon double precision"), ("DMS_LAT", "dms_lat varchar(6)"), ("DMS_LONG", "dms_long varchar(7)"), ("UTM", "utm varchar(4)"), ("JOG", "jog varchar(7)"), ("FC", "fc varchar(1)"), ("DSG", "dsg varchar(5)"), ("PC", "pc integer"), ("CC1", "cci varchar(2)"), ("ADM1", "adm1 varchar(2)"), ("ADM2", "adm2 varchar(200)"), ("DIM", "dim integer"), ("CC2", "cc2 varchar(2)"), ("NT", "nt varchar(1)"), ("LC", "lc varchar(3)"), ("SHORT_FORM", "shortform varchar(128)"), ("GENERIC", "generic varchar(128)"), ("SORT_NAME", "sortname varchar(200)"), ("FULL_NAME", "fullname varchar(200)"), ("FULL_NAME_ND","funamesd varchar(200)"), ("MODIFY_DATE", "mod_date date")] colnames = [desc[0] for desc in coldescs] coltypes = dict([(desc[0], 'integer' in desc[1]) for desc in coldescs]) header = None num_places = 0 inf = file(fileorig) outf = file(tmpfile, 'wb') for line in inf: fields = line.rstrip('\r\n').split('\t') if not header: header = fields continue vars = dict(zip(header, fields)) fields2 = [] for col in colnames: if col in vars: if coltypes[col] and vars[col] == '': fields2.append('0') else: fields2.append(vars[col]) else: if coltypes[col]: fields2.append('0') else: fields2.append('') line2 = ';'.join(fields2) + '\n' outf.write(line2) num_places += 1 outf.close() inf.close() grass.message(_("Converted %d place names.") % num_places) #TODO: fix dms_lat,dms_long DDMMSS -> DD:MM:SS # Solution: # IN=DDMMSS # DEG=`echo $IN | cut -b1,2` # MIN=`echo $IN | cut -b3,4` # SEC=`echo $IN | cut -b5,6` # DEG_STR="$DEG:$MIN:$SEC" #modifications (to match DBF 10 char column name limit): # short_form -> shortform # sort_name -> sortname # full_name -> fullname # full_name_sd -> funamesd # pump data into GRASS: columns = [desc[1] for desc in coldescs] grass.run_command('v.in.ascii', cat = 0, x = 5, y = 4, fs = ';', input = tmpfile, output = filevect, columns = columns) grass.try_remove(tmpfile) # write cmd history: vgrass.vector_history(filevect)