def difference(real_elev, scanned_elev, new, zexag=1, env=None): """Compute difference and set color table using standard deviations""" tmp = 'tmp_resampled' gcore.run_command('r.resamp.interp', input=real_elev, output=tmp, method='bilinear', env=env) grast.mapcalc(f"{new} = {tmp} - {scanned_elev}", env=env) univar = gcore.parse_command("r.univar", flags="g", map=real_elev, env=env) std1 = zexag * float(univar["stddev"]) std2 = zexag * 2 * std1 std3 = zexag * 3 * std1 rules = [ f"-1000000 black", f"-{std3} black", f"-{std2} 202:000:032", f"-{std1} 244:165:130", "0 247:247:247", f"{std1} 146:197:222", f"{std2} 5:113:176", f"{std3} black", f"1000000 black", ] gcore.write_command("r.colors", map=new, rules="-", stdin="\n".join(rules), env=env)
def depression(scanned_elev, new, env, filter_depth=0, repeat=2): """Run r.fill.dir to compute depressions""" suffix = str(uuid.uuid4()).replace('-', '')[:5] input_dem = scanned_elev output = "tmp_filldir" + suffix tmp_dir = "tmp_dir" + suffix for i in range(repeat): gcore.run_command('r.fill.dir', input=input_dem, output=output, direction=tmp_dir, env=env) input_dem = output grast.mapcalc( '{new} = if({out} - {scan} > {depth}, {out} - {scan}, null())'.format( new=new, out=output, scan=scanned_elev, depth=filter_depth), env=env) gcore.write_command('r.colors', map=new, rules='-', stdin='0% aqua\n100% blue', env=env) gcore.run_command('g.remove', flags='f', type='raster', name=[output, tmp_dir], env=env)
def polylines(points_map, output, env): """Cluster points and connect points by line in each cluster""" tmp_cluster = 'tmp_cluster' gcore.run_command('v.cluster', flags='t', input=points_map, min=3, layer='3', output=tmp_cluster, method='optics', env=env) cats = gcore.read_command('v.category', input=tmp_cluster, layer=3, option='print', env=env).strip() cats = list(set(cats.split())) line = '' for cat in cats: point_list = [] distances = {} points = gcore.read_command('v.out.ascii', input=tmp_cluster, layer=3, type='point', cats=cat, format='point', env=env).strip().split() for point in points: point = point.split('|')[:2] point_list.append((float(point[0]), float(point[1]))) for i, point1 in enumerate(point_list[:-1]): for point2 in point_list[i + 1:]: distances[(point1, point2)] = sqrt((point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1])) ordered = sorted(distances.items(), key=lambda x: x[1])[:len(points) - 1] for key, value in ordered: line += 'L 2 1\n' line += '{x} {y}\n'.format(x=key[0][0], y=key[0][1]) line += '{x} {y}\n'.format(x=key[1][0], y=key[1][1]) line += '1 {cat}\n\n'.format(cat=cat) gcore.write_command('v.in.ascii', input='-', stdin=line, output=output, format='standard', flags='n', env=env) gcore.run_command('v.to.rast', input=output, output=output, type='line', use='cat', env=env)
def importGeom(vname, typ, c, owrite, z, cat=None): psel = "SELECT lon, lat" if z == 'z': psel += ", altim" zcol = 3 else: zcol = 0 psel += " from %s" % typ if cat: psel += " where cat = '%s' order by _id" % cat points = returnAll(c, psel) wpoi = '\n'.join(['|'.join([str(col) for col in row]) for row in points]) # import points using v.in.ascii try: grass.write_command('v.in.ascii', flags='t%s' % z, input='-', z=zcol, output=vname, stdin=wpoi, overwrite=owrite, quiet=True) except CalledModuleError: grass.fatal(_("Error importing %s" % vname)) return points
def difference(real_elev, scanned_elev, new, color_coeff=1, env=None): """!Computes difference of original and scanned (scan - orig). color_coeff modifies the intensity of the color, values > 1 mean the difference shows only bigger changes, values < 1 highlight smaller changes (and noise)""" tmp = 'tmp_resampled' gcore.run_command('r.resamp.interp', input=real_elev, output=tmp, method='bilinear', env=env) gcore.run_command('r.mapcalc', expression='{diff} = {scan} - {real}'.format( diff=new, real=tmp, scan=scanned_elev), env=env) info = grast.raster_info(real_elev) range = info['max'] - info['min'] percentages = [-1000, -100, -50, -5, 5, 50, 100, 1000] colors = [ 'black', 'black', 'blue', 'white', 'white', 'red', 'black', 'black' ] rules = [] for p, c in zip(percentages, colors): p = range / 100. * p * color_coeff rules.append('{p} {c}'.format(p=p, c=c)) gcore.write_command('r.colors', map=new, rules='-', stdin='\n'.join(rules), env=env)
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 viewshed(scanned_elev, output, vector, visible_color, invisible_color, obs_elev=1.7, env=None): coordinates = gcore.read_command('v.out.ascii', input=vector, separator=',', env=env).strip() coordinate = None for line in coordinates.split(os.linesep): try: coordinate = [float(c) for c in line.split(',')[0:2]] except ValueError: # no points in map pass break if coordinate: gcore.run_command('r.viewshed', flags='b', input=scanned_elev, output=output, coordinates=coordinate, observer_elevation=obs_elev, env=env) gcore.run_command('r.null', map=output, null=0, env=env) gcore.write_command('r.colors', map=output, rules='-', stdin='0 {invis}\n1 {vis}'.format( vis=visible_color, invis=invisible_color), env=env)
def polygons(points_map, output, env): """Clusters markers together and creates polygons. Requires GRASS 7.1.""" tmp_cluster = 'tmp_cluster' tmp_hull = 'tmp_hull' gcore.run_command('v.cluster', flags='t', input=points_map, min=3, layer='3', output=tmp_cluster, method='optics', env=env) cats = gcore.read_command('v.category', input=tmp_cluster, layer='3', option='print', env=env).strip().split() cats_list = list(set(cats)) cats_dict = dict([(x, cats.count(x)) for x in cats_list]) for cat in cats_list: if cats_dict[cat] > 2: gcore.run_command('v.hull', input=tmp_cluster, output=tmp_hull + "_%s" % cat, cats=cat, layer='3', env=env) elif cats_dict[cat] == 2: points = gcore.read_command('v.out.ascii', input=tmp_cluster, format='point', separator='space', layer='3', cats=cat, env=env).strip().splitlines() ascii = 'L 2 1\n' + points[0] + '\n' + points[1] + '\n' + '1 1' gcore.write_command('v.in.ascii', format='standard', input='-', flags='n', output=tmp_hull + '_%s' % cat, stdin=ascii, env=env) gcore.run_command('v.patch', input=[tmp_hull + '_%s' % cat for cat in cats_list], output=output, env=env) gcore.run_command('v.to.rast', input=output, output=output, type='area,line', use='val', value=1, env=env)
def depression(scanned_elev, new, env, filter_depth=0, repeat=2): """Run r.fill.dir to compute depressions""" suffix = str(uuid.uuid4()).replace('-', '')[:5] input_dem = scanned_elev output = "tmp_filldir" + suffix tmp_dir = "tmp_dir" + suffix for i in range(repeat): gcore.run_command('r.fill.dir', input=input_dem, output=output, direction=tmp_dir, env=env) input_dem = output grast.mapcalc('{new} = if({out} - {scan} > {depth}, {out} - {scan}, null())'.format(new=new, out=output, scan=scanned_elev, depth=filter_depth), env=env) gcore.write_command('r.colors', map=new, rules='-', stdin='0% aqua\n100% blue', env=env) gcore.run_command('g.remove', flags='f', type='raster', name=[output, tmp_dir], env=env)
def main(): test_file = options['test'] expected = grass.tempfile() result = grass.tempfile() dbconn = grassdb.db_connection() grass.message(_("Using DB driver: %s") % dbconn['driver']) infile = os.path.join(os.environ['GISBASE'], 'etc', 'db.test', test_file) inf = file(infile) while True: type = inf.readline() if not type: break type = type.rstrip('\r\n') sql = inf.readline().rstrip('\r\n') sys.stdout.write(sql + '\n') # Copy expected result to temp file try: if type == 'X': grass.write_command('db.execute', input='-', stdin=sql + '\n') else: resf = file(result, 'w') grass.write_command('db.select', input='-', flags='c', stdin=sql + '\n', stdout=resf) resf.close() except CalledModuleError: grass.error("EXECUTE: ******** ERROR ********") else: grass.message(_("EXECUTE: OK")) expf = file(expected, 'w') while True: res = inf.readline().rstrip('\r\n') if not res: break expf.write(res + '\n') expf.close() if type == 'S': if grass.call(['diff', result, expected]) != 0: grass.error("RESULT: ******** ERROR ********") else: grass.message(_("RESULT: OK"))
def main(): test_file = options["test"] expected = gcore.tempfile() result = gcore.tempfile() dbconn = grassdb.db_connection() gcore.message(_("Using DB driver: %s") % dbconn["driver"]) infile = os.path.join(os.environ["GISBASE"], "etc", "db.test", test_file) inf = open(infile) while True: type = inf.readline() if not type: break type = type.rstrip("\r\n") sql = inf.readline().rstrip("\r\n") sys.stdout.write(sql + "\n") # Copy expected result to temp file try: if type == "X": gcore.write_command("db.execute", input="-", stdin=sql + "\n") else: resf = open(result, "w") gcore.write_command("db.select", input="-", flags="c", stdin=sql + "\n", stdout=resf) resf.close() except CalledModuleError: gcore.error("EXECUTE: ******** ERROR ********") else: gcore.message(_("EXECUTE: OK")) expf = open(expected, "w") while True: res = inf.readline().rstrip("\r\n") if not res: break expf.write(res + "\n") expf.close() if type == "S": if gcore.call(["diff", result, expected]) != 0: gcore.error("RESULT: ******** ERROR ********") else: gcore.message(_("RESULT: OK"))
def test_write_labels_unicode(self): """This tests if Python module works""" find_program("ls", "--version") write_command( "r.category", map=self.raster, rules="-", stdin="1:kůň\n2:kráva\n3:ovečka\n4:býk", separator=":", ) res = read_command("r.category", map=self.raster, separator=":").strip() self.assertEquals(res, "1:kůň\n2:kráva\n3:ovečka\n4:býk") self.assertIsInstance(res, str)
def viewshed(scanned_elev, output, vector, visible_color, invisible_color, obs_elev=1.7, env=None): coordinates = gcore.read_command('v.out.ascii', input=vector, separator=',', env=env).strip() coordinate = None for line in coordinates.split(os.linesep): try: coordinate = [float(c) for c in line.split(',')[0:2]] except ValueError: # no points in map pass break if coordinate: gcore.run_command('r.viewshed', flags='b', input=scanned_elev, output=output, coordinates=coordinate, observer_elevation=obs_elev, env=env) gcore.run_command('r.null', map=output, null=0, env=env) gcore.write_command('r.colors', map=output, rules='-', stdin='0 {invis}\n1 {vis}'.format(vis=visible_color, invis=invisible_color), env=env)
def main(): test_file = options['test'] expected = grass.tempfile() result = grass.tempfile() dbconn = grassdb.db_connection() grass.message(_("Using DB driver: %s") % dbconn['driver']) infile = os.path.join(os.environ['GISBASE'], 'etc', 'db.test', test_file) inf = file(infile) while True: type = inf.readline() if not type: break type = type.rstrip('\r\n') sql = inf.readline().rstrip('\r\n') sys.stdout.write(sql + '\n') # Copy expected result to temp file try: if type == 'X': grass.write_command('db.execute', input = '-', stdin = sql + '\n') else: resf = file(result, 'w') grass.write_command('db.select', input = '-', flags = 'c', stdin = sql + '\n', stdout = resf) resf.close() except CalledModuleError: grass.error("EXECUTE: ******** ERROR ********") else: grass.message(_("EXECUTE: OK")) expf = file(expected, 'w') while True: res = inf.readline().rstrip('\r\n') if not res: break expf.write(res + '\n') expf.close() if type == 'S': if grass.call(['diff', result, expected]) != 0: grass.error("RESULT: ******** ERROR ********") else: grass.message(_("RESULT: OK"))
def test_write_labels_bytes(self): """This tests if Python module works""" write_command( "r.category", map=self.raster, rules="-", stdin="1:kůň\n2:kráva\n3:ovečka\n4:býk", separator=":", encoding=None, ) res = read_command( "r.category", map=self.raster, separator=":", encoding=None ).strip() self.assertEquals(res, encode("1:kůň\n2:kráva\n3:ovečka\n4:býk")) self.assertIsInstance(res, bytes)
def change_detection(before, after, change, height_threshold, cells_threshold, add, max_detected, debug, env): diff_thr = 'diff_thr_' + str(uuid.uuid4()).replace('-', '') diff_thr_clump = 'diff_thr_clump_' + str(uuid.uuid4()).replace('-', '') coeff = gcore.parse_command('r.regression.line', mapx=after, mapy=before, flags='g', env=env) grast.mapcalc('diff = {a} + {b} * {after} - {before}'.format(a=coeff['a'], b=coeff['b'],before=before,after=after), env=env) try: if add: grast.mapcalc("{diff_thr} = if(({a} + {b} * {after} - {before}) > {thr1} &&" " ({a} + {b} * {after} - {before}) < {thr2}, 1, null())".format(a=coeff['a'], b=coeff['b'], diff_thr=diff_thr, after=after, before=before, thr1=height_threshold[0], thr2=height_threshold[1]), env=env) else: grast.mapcalc("{diff_thr} = if(({before} - {a} + {b} * {after}) > {thr}, 1, null())".format(diff_thr=diff_thr, a=coeff['a'], b=coeff['b'], after=after, before=before, thr=height_threshold), env=env) gcore.run_command('r.clump', input=diff_thr, output=diff_thr_clump, env=env) stats = gcore.read_command('r.stats', flags='cn', input=diff_thr_clump, sort='desc', env=env).strip().splitlines() if debug: print 'DEBUG: {}'.format(stats) if len(stats) > 0 and stats[0]: cats = [] found = 0 for stat in stats: if found >= max_detected: break if float(stat.split()[1]) < cells_threshold[1] and float(stat.split()[1]) > cells_threshold[0]: # larger than specified number of cells found += 1 cat, value = stat.split() cats.append(cat) if cats: rules = ['{c}:{c}:1'.format(c=c) for c in cats] gcore.write_command('r.recode', input=diff_thr_clump, output=change, rules='-', stdin='\n'.join(rules), env=env) gcore.run_command('r.volume', flags='f', input=change, clump=diff_thr_clump, centroids=change, env=env) else: gcore.warning("No change found!") gcore.run_command('v.edit', map=change, tool='create', env=env) else: gcore.warning("No change found!") gcore.run_command('v.edit', map=change, tool='create', env=env) gcore.run_command('g.remove', flags='f', type=['raster'], name=[diff_thr, diff_thr_clump], env=env) except: gcore.run_command('g.remove', flags='f', type=['raster'], name=[diff_thr, diff_thr_clump], env=env)
def usped(scanned_elev, k_factor, c_factor, flowacc, slope, aspect, new, env): """!Computes net erosion and deposition (USPED model)""" sedflow = 'sedflow_' + str(os.getpid()) qsx = 'qsx_' + str(os.getpid()) qsxdx = 'qsxdx_' + str(os.getpid()) qsy = 'qsy_' + str(os.getpid()) qsydy = 'qsydy_' + str(os.getpid()) slope_sm = 'slope_sm' + str(os.getpid()) gcore.run_command('r.neighbors', overwrite=True, input=slope, output=slope_sm, size=5, env=env) gcore.run_command('r.mapcalc', expression="{sedflow} = 270. * {k_factor} * {c_factor} * {flowacc} * sin({slope})".format(c_factor=c_factor, k_factor=k_factor, slope=slope_sm, flowacc=flowacc, sedflow=sedflow), overwrite=True, env=env) gcore.run_command('r.mapcalc', expression="{qsx} = {sedflow} * cos({aspect})".format(sedflow=sedflow, aspect=aspect, qsx=qsx), overwrite=True, env=env) gcore.run_command('r.mapcalc', expression="{qsy} = {sedflow} * sin({aspect})".format(sedflow=sedflow, aspect=aspect, qsy=qsy), overwrite=True, env=env) gcore.run_command('r.slope.aspect', elevation=qsx, dx=qsxdx, overwrite=True, env=env) gcore.run_command('r.slope.aspect', elevation=qsy, dy=qsydy, overwrite=True, env=env) gcore.run_command('r.mapcalc', expression="{erdep} = {qsxdx} + {qsydy}".format(erdep=new, qsxdx=qsxdx, qsydy=qsydy), overwrite=True, env=env) gcore.write_command('r.colors', map=new, rules='-', stdin='-15000 100 0 100\n-100 magenta\n-10 red\n-1 orange\n-0.1 yellow\n0 200 255 200\n0.1 cyan\n1 aqua\n10 blue\n100 0 0 100\n18000 black', env=env) gcore.run_command('g.remove', rast=[sedflow, qsx, qsxdx, qsy, qsydy, slope_sm])
def difference(real_elev, scanned_elev, new, color_coeff=1, env=None): """!Computes difference of original and scanned (scan - orig). color_coeff modifies the intensity of the color, values > 1 mean the difference shows only bigger changes, values < 1 highlight smaller changes (and noise)""" tmp = 'tmp_resampled' gcore.run_command('r.resamp.interp', input=real_elev, output=tmp, method='bilinear', env=env) gcore.run_command('r.mapcalc', expression='{diff} = {scan} - {real}'.format(diff=new, real=tmp, scan=scanned_elev), env=env) info = grast.raster_info(real_elev) range = info['max'] - info['min'] percentages = [-1000, -100, -50, -5, 5, 50, 100, 1000] colors = ['black', 'black', 'blue', 'white', 'white', 'red', 'black', 'black'] rules = [] for p, c in zip(percentages, colors): p = range / 100. * p * color_coeff rules.append('{p} {c}'.format(p=p, c=c)) gcore.write_command('r.colors', map=new, rules='-', stdin='\n'.join(rules), env=env)
def usped(scanned_elev, k_factor, c_factor, flowacc, slope, aspect, new, env): """!Computes net erosion and deposition (USPED model)""" suffix = str(uuid.uuid4()).replace('-', '')[:5] sedflow = 'sedflow_' + suffix qsx = 'qsx_' + suffix qsxdx = 'qsxdx_' + suffix qsy = 'qsy_' + suffix qsydy = 'qsydy_' + suffix slope_sm = 'slope_sm' + suffix gcore.run_command('r.neighbors', overwrite=True, input=slope, output=slope_sm, size=5, env=env) gcore.run_command('r.mapcalc', expression="{sedflow} = 270. * {k_factor} * {c_factor} * {flowacc} * sin({slope})".format(c_factor=c_factor, k_factor=k_factor, slope=slope_sm, flowacc=flowacc, sedflow=sedflow), overwrite=True, env=env) gcore.run_command('r.mapcalc', expression="{qsx} = {sedflow} * cos({aspect})".format(sedflow=sedflow, aspect=aspect, qsx=qsx), overwrite=True, env=env) gcore.run_command('r.mapcalc', expression="{qsy} = {sedflow} * sin({aspect})".format(sedflow=sedflow, aspect=aspect, qsy=qsy), overwrite=True, env=env) gcore.run_command('r.slope.aspect', elevation=qsx, dx=qsxdx, overwrite=True, env=env) gcore.run_command('r.slope.aspect', elevation=qsy, dy=qsydy, overwrite=True, env=env) gcore.run_command('r.mapcalc', expression="{erdep} = {qsxdx} + {qsydy}".format(erdep=new, qsxdx=qsxdx, qsydy=qsydy), overwrite=True, env=env) gcore.write_command('r.colors', map=new, rules='-', stdin='-15000 100 0 100\n-100 magenta\n-10 red\n-1 orange\n-0.1 yellow\n0 200 255 200\n0.1 cyan\n1 aqua\n10 blue\n100 0 0 100\n18000 black', env=env) gcore.run_command('g.remove', flags='f', type='raster', name=[sedflow, qsx, qsxdx, qsy, qsydy, slope_sm])
def main(): indb = options['database'] prefix = options['basename'] env = grass.gisenv() #fix sqlite3 db field string multibyte character problem sys.setdefaultencoding('utf-8') # check if 3d or not if flags['z']: d3 = 'z' else: d3 = '' owrite = grass.overwrite() # check if location it is latlong if grass.locn_is_latlong(): locn = True else: locn = False # connection to sqlite geopaparazzi database import sqlite3 conn = sqlite3.connect(indb) curs = conn.cursor() # if it is not a latlong location create a latlong location on the fly if not locn: # create new location and move to it creating new gisrc file new_loc = basename(grass.tempfile(create=False)) new_loc_name = 'geopaparazzi_%s' % new_loc grass.create_location(dbase=env['GISDBASE'], epsg='4326', location=new_loc_name, desc='Temporary location for v.in.geopaparazzi') grc = os.getenv('GISRC') shutil.copyfile(grc, grc + '.old') newrc = open(grc, 'w') newrc.write('GISDBASE: %s\n' % env['GISDBASE']) newrc.write('LOCATION_NAME: %s\n' % new_loc_name) newrc.write('MAPSET: PERMANENT\n') newrc.write('GRASS_GUI: text\n') newrc.close() grass.run_command('db.connect', flags="d", quiet=True) # load bookmarks if flags['b']: # check if elements in bookmarks table are more the 0 if checkEle(curs, 'bookmarks') != 0: bookname = prefix + '_book' pois = importGeom(bookname, 'bookmarks', curs, owrite, '') sql = 'CREATE TABLE %s (cat int, text text)' % bookname grass.write_command('db.execute', input='-', stdin=sql) # select attributes sql = "select text from bookmarks order by _id" allattri = returnClear(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s'" % (idcat, str(row)) sql = "insert into %s values(%s)" % (bookname, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command('v.db.connect', map=bookname, table=bookname, quiet=True) else: grass.warning(_("No bookmarks found, escape them")) # load images if flags['i']: # check if elements in images table are more the 0 if checkEle(curs, 'images') != 0: imagename = prefix + '_image' pois = importGeom(imagename, 'images', curs, owrite, d3) sql = 'CREATE TABLE %s (cat int, azim int, ' % imagename sql += 'path text, ts text, text text)' grass.write_command('db.execute', input='-', stdin=sql) # select attributes sql = "select azim, path, ts, text from images order by _id" allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%d','%s','%s','%s'" % (idcat, row[0], str(row[1]), str(row[2]), str(row[3])) sql = "insert into %s values(%s)" % (imagename, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command('v.db.connect', map=imagename, table=imagename, quiet=True) else: grass.warning(_("No images found, escape them")) # if tracks or nodes should be imported create a connection with sqlite3 # load notes if flags['n']: # check if elements in notes table are more the 0 if checkEle(curs, 'notes') != 0: # select each categories categories = returnClear(curs, "select cat from notes group by cat") # for each category for cat in categories: # select lat, lon for create point layer catname = prefix + '_notes_' + cat pois = importGeom(catname, 'notes', curs, owrite, d3, cat) # select form to understand the number forms = returnClear(curs, "select _id from notes where cat = '%s' " "and form is not null order by _id" % cat) # if number of form is different from 0 and number of point # remove the vector because some form it is different if len(forms) != 0 and len(forms) != len(pois): grass.run_command('g.remove', flags='f', type='vector', name=catname, quiet=True) grass.warning(_("Vector %s not imported because number" " of points and form is different")) # if form it's 0 there is no form elif len(forms) == 0: # create table without form sql = 'CREATE TABLE %s (cat int, ts text, ' % catname sql += 'text text, geopap_cat text)' grass.write_command('db.execute', input='-', stdin=sql) # select attributes sql = "select ts, text, cat from notes where "\ "cat='%s' order by _id" % cat allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s','%s','%s'" % (idcat, str(row[0]), str(row[1]), str(row[2])) sql = "insert into %s values(%s)" % (catname, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command('v.db.connect', map=catname, table=catname, quiet=True) # create table with form else: # select all the attribute sql = "select ts, text, cat, form from notes where "\ "cat='%s' order by _id" % cat allattri = returnAll(curs, sql) # return string of form's categories too create table keys = returnFormKeys(allattri) sql = 'CREATE TABLE %s (cat int, ts text, ' % catname sql += 'text text, geopap_cat text %s)' % keys grass.write_command('db.execute', input='-', stdin=sql) # it's for the number of categories idcat = 1 # for each feature insert value for row in allattri: values = "%d,'%s','%s','%s'," % (idcat, str(row[0]), str(row[1]), str(row[2])) values += returnFormValues(row[3]) sql = "insert into %s values(%s)" % (catname, values) grass.write_command('db.execute', input='-', stdin=sql) idcat += 1 # at the end connect table with vector grass.run_command('v.db.connect', map=catname, table=catname, quiet=True) else: grass.warning(_("No notes found, escape them")) # load tracks if flags['t']: # check if elements in bookmarks table are more the 0 if checkEle(curs, 'gpslogs') != 0: tracksname = prefix + '_tracks' # define string for insert data at the end tracks = '' # return ids of tracks ids = returnClear(curs, "select _id from gpslogs") # for each track for i in ids: # select all the points coordinates tsel = "select lon, lat" if flags['z']: tsel += ", altim" tsel += " from gpslog_data where logid=%s order by _id" % i trackpoints = returnAll(curs, tsel) wpoi = '\n'.join(['|'.join([str(col) for col in row]) for row in trackpoints]) tracks += "%s\n" % wpoi if flags['z']: tracks += 'NaN|NaN|Nan\n' else: tracks += 'NaN|Nan\n' # import lines try: grass.write_command('v.in.lines', flags=d3, input='-', out=tracksname, stdin=tracks, overwrite=owrite, quiet=True) except CalledModuleError: grass.fatal(_("Error importing %s" % tracksname)) # create table for line sql = 'CREATE TABLE %s (cat int, startts text, ' % tracksname sql += 'endts text, text text, color text, width int)' grass.write_command('db.execute', input='-', stdin=sql) sql = "select logid, startts, endts, text, color, width from" \ " gpslogs, gpslogsproperties where gpslogs._id=" \ "gpslogsproperties.logid" # return attributes allattri = returnAll(curs, sql) # for each line insert attribute for row in allattri: values = "%d,'%s','%s','%s','%s',%d" % (row[0], str(row[1]), str(row[2]), str(row[3]), str(row[4]), row[5]) sql = "insert into %s values(%s)" % (tracksname, values) grass.write_command('db.execute', input='-', stdin=sql) # at the end connect map with table grass.run_command('v.db.connect', map=tracksname, table=tracksname, quiet=True) else: grass.warning(_("No tracks found, escape them")) # if location it's not latlong reproject it if not locn: # copy restore the original location shutil.copyfile(grc + '.old', grc) # reproject bookmarks if flags['b'] and checkEle(curs, 'bookmarks') != 0: grass.run_command('v.proj', quiet=True, input=bookname, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT') # reproject images if flags['i'] and checkEle(curs, 'images') != 0: grass.run_command('v.proj', quiet=True, input=imagename, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT') # reproject notes if flags['n'] and checkEle(curs, 'notes') != 0: for cat in categories: catname = prefix + '_node_' + cat grass.run_command('v.proj', quiet=True, input=catname, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT') # reproject track if flags['t'] and checkEle(curs, 'gpslogs') != 0: grass.run_command('v.proj', quiet=True, input=tracksname, location='geopaparazzi_%s' % new_loc, mapset='PERMANENT')
def main(): layers = options["map"].split(",") if len(layers) < 2: gcore.error(_("At least 2 maps are required")) tmpfile = gcore.tempfile() for map in layers: if not gcore.find_file(map, element="cell")["file"]: gcore.fatal(_("Raster map <%s> not found") % map) gcore.write_command("d.text", color="black", size=4, line=1, stdin="CORRELATION") os.environ["GRASS_RENDER_FILE_READ"] = "TRUE" colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) gcore.write_command("d.text", color=color, size=4, line=line, stdin="%s %s" % (i, j)) line += 1 ofile = file(tmpfile, "w") gcore.run_command("r.stats", flags="cnA", input=(i, j), stdout=ofile) ofile.close() ifile = file(tmpfile, "r") first = True for l in ifile: f = l.rstrip("\r\n").split(" ") x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0 / (maxx - minx + 1) ky = 100.0 / (maxy - miny + 1) p = gcore.feed_command("d.graph", color=color) ofile = p.stdin ifile = file(tmpfile, "r") for l in ifile: f = l.rstrip("\r\n").split(" ") x = float(f[0]) y = float(f[1]) ofile.write("icon + 0.1 %f %f\n" % ((x - minx + 1) * kx, (y - miny + 1) * ky)) ifile.close() ofile.close() p.wait() try_remove(tmpfile)
def nanrunoff(conf,inputs,outputs): # Set path result rain idw method path = '/var/www/html/dl/data/nanrainfall/' #path = '/usr/local/lib/geoserver-2.5/data_dir/coverages/result_data/' #path = '/var/lib/tomcat6/webapps/geoserver/data/coverages/result_data/' #path2 = '/home/user/data/chai/rasters/' t = time.strftime("%Y%m%d:%H%M%S") print t+"=> Hello times it work[200]!" connection = psycopg2.connect(dbname='nanrunf', host='localhost', port='5432', user='******', password='******') # Import point rainfall data from PostgreSQL/PostGIS mg.run_command('v.in.ogr', dsn='PG:dbname=nanrunf', layer='nan_raingrass', output='nan_raingrass', overwrite= True) # Set Extent and Interpolation IDW Method #mg.run_command('g.region', rast='rainthai_idw@PERMANENT',res='1000', quiet= True) mg.run_command('g.region', n='2172120.86468637', e='748094.237955', s='1735854.76591878', w='558440.48801759', res='40') mg.run_command('v.surf.idw', input='nan_raingrass@PERMANENT', output='nan_idw', power='2', column='rain',flags='n', overwrite= True) #mg.run_command('v.surf.idw', input=inputs["idw_vect"] ["value"], output=inputs["idw_rast"] ["value"], power='2', column='rain',flags='n', overwrite= True) #mg.run_command('v.surf.idw', input=inputs["idw_vect"] ["value"], output=inputs["idw_rast"] ["value"], power=inputs["idw_power"] ["value"], column=inputs["idw_column"] ["value"],flags='n', overwrite= True) # Genarate Contour line (isolines) #mg.run_command('r.contour', input='rainthai_idw@PERMANENT', output='rain_isoline', step='5', overwrite= True) # Export rain_idw gdal to GeoTiff file and timestamp #mg.run_command('r.out.gdal', input='nan_idw@PERMANENT', type='Float64', output= path+'nanrainf_'+t+'.tif') #, overwrite= True #mg.run_command('r.out.gdal', input=inputs["idw_rast"] ["value"], type='Float64', output= inputs["dsn_export"] ["value"]+'_nanrain_'+t+'.tif') #, overwrite= True #mg.run_command('r.out.gdal', input='nan_idw@PERMANENT', type='Float64', output= path+'nanrainf.tif', overwrite= True) #mg.run_command('r.out.gdal', input='nan_idw@PERMANENT', type='Float64', output= path+'nanrainf_'+t+'.tif') #, overwrite= True # test mapcal runoff nanbasin # r.mapcalc nrunoff = ( pow( ( nan_idw@PERMANENT - ( 0.2 * grid_s@PERMANENT ) ) ,2 ) ) / ( nan_idw@PERMANENT + ( 0.8 * grid_s@PERMANENT ) ) mg.write_command('r.mapcalc', stdin= 'nrunoff = ( pow( ( nan_idw@PERMANENT - ( 0.2 * grid_s@PERMANENT ) ) ,2 ) ) / ( nan_idw@PERMANENT + ( 0.8 * grid_s@PERMANENT ) )') mg.write_command('r.mapcalc', stdin= 'nrunoff_sqm3rai = (nrunoff@PERMANENT/1000)*1600') # to raster runoff output #mg.run_command('r.out.gdal', input=inputs["nrunoff"] ["value"], type='Float64', output= inputs["dsn_export"] ["value"]+'_runoff_'+t+'.tif') #, overwrite= True mg.run_command('r.out.gdal', input='nrunoff_sqm3rai@PERMANENT', type='Float64', output= path+'nanrunoff.tif', overwrite= True) # to vector #mg.run_command('r.to.vect', input='raincal@PERMANENT', output='warnings', feature='area', overwrite= True) # remove rast #mg.run_command('g.remove', rast='rainthai_idw@PERMANENT',quiet= True) #connection = psycopg2.connect(dbname='nanrunf', host='localhost', port='5432', user='******', password='******') #res = connection.cursor() #res.execute("DROP VIEW IF EXISTS _villrisk ;") #res.execute("DROP VIEW IF EXISTS vill_warn ;") #res.execute("DROP TABLE IF EXISTS warnings ;") #connection.commit() # Export warnning areas into PostgreSQL/PostGIS #v.out.ogr input=rain_isoline@PERMANENT type=line dsn=PG:host=localhost dbname=rain user=user olayer=rain_isoline format=PostgreSQL #mg.run_command('v.out.ogr', input='warnings@PERMANENT', type='area', dsn='PG:dbname=nanrunf', olayer='warnings', format='PostgreSQL',overwrite= True) #,flags='u',lco="OVERWRITE=YES" #mg.run_command('v.out.ogr', input='warnings@PERMANENT', type='area', flags='c', dsn='PG:host=localhost dbname=nanrunf user=user password=user', olayer='warnings', format='PostgreSQL') #mg.run_command('v.delaunay', input='archsites',output='test_delaunay') # Create Voronoi from Rain Station #v.voronoi input=rainthai_f@PERMANENT output=rain_voronoi --overwrite #mg.run_command('v.voronoi', input='rainthai_f@PERMANENT',output='rain_voronoi', overwrite= True) #mg.run_command('v.out.ogr', input='rain_voronoi@PERMANENT', type='area', dsn='PG:dbname=rain', olayer='rain_voronoi', format='PostgreSQL',overwrite= True) #,flags='u' # connection = psycopg2.connect(dbname='nanrunf', host='localhost', port='5432', user='******', password='******') # res = connection.cursor() # res.execute("DROP VIEW IF EXISTS _villrisk ;") # res.execute("DROP VIEW IF EXISTS vill_warn ;") # res.execute("DROP TABLE IF EXISTS warnings ;") # res.execute("CREATE VIEW vill_warn AS SELECT v.gid, v.vill_code, v.vill_nam_t, v.geom FROM village_nbasin v, warnings w WHERE ST_Within (v.geom,w.wkb_geometry);") # res.execute("CREATE VIEW _villrisk AS SELECT vw.gid, vw.vill_code, vw.vill_nam_t, vw.geom FROM vill_warn vw, landslide ls WHERE ST_Within (vw.geom,ls.geom);") # connection.commit() outputs["Result"]["value"]=\ "--> start process by: "+inputs["name"] ["value"]+ " --> Nan runoff processing successfully" return 3
def main(): options, flags = gcore.parser() aspect = options['aspect'] speed = options['speed'] probability = options['probability'] if options['particle_base']: particle_base = options['particle_base'] + '_' else: particle_base = None if options['particles']: particles = options['particles'] min_size = float(options['min_size']) max_size = float(options['max_size']) comet_length = int(options['comet_length']) else: particles = min_size = max_size = comet_length = None try: total_time = int(options['total_time']) step = int(options['step']) age = int(options['age']) count = int(options['count']) except ValueError: gcore.fatal(_("Parameter should be integer")) gcore.use_temp_region() # create aspect in x and y direction aspect_x = 'aspect_x_' + str(os.getpid()) aspect_y = 'aspect_y_' + str(os.getpid()) xshift_tmp = 'xshift_tmp_' + str(os.getpid()) yshift_tmp = 'yshift_tmp_' + str(os.getpid()) TMP_RAST.append(aspect_x) TMP_RAST.append(aspect_y) grast.mapcalc(exp="{aspect_x} = cos({aspect})".format(aspect_x=aspect_x, aspect=aspect)) grast.mapcalc(exp="{aspect_y} = sin({aspect})".format(aspect_y=aspect_y, aspect=aspect)) grast.mapcalc(exp="{xshift} = {aspect_x}*{speed}*{t}".format(xshift=xshift_tmp, t=step, speed=speed, aspect_x=aspect_x), overwrite=True) grast.mapcalc(exp="{yshift} = {aspect_y}*{speed}*{t}".format(yshift=yshift_tmp, t=step, speed=speed, aspect_y=aspect_y), overwrite=True) # initialize vector_tmp1 = 'vector_tmp1_' + str(os.getpid()) vector_tmp2 = 'vector_tmp2_' + str(os.getpid()) vector_tmp3 = 'vector_tmp3_' + str(os.getpid()) vector_region = 'vector_region_' + str(os.getpid()) TMP_VECT.extend([vector_tmp1, vector_tmp2, vector_tmp3, vector_region]) random_tmp = 'random_tmp_' + str(os.getpid()) TMP_RAST.extend([xshift_tmp, yshift_tmp, random_tmp]) gcore.run_command('v.in.region', output=vector_region, type='area') loop = 0 vector_1 = particle_base + "{0:03d}".format(loop) generate_points(name=vector_1, probability_map=probability, count=count) grast.mapcalc(exp="{random} = int(rand(1, {maxt}))".format(random=random_tmp, maxt=age + 1)) gcore.run_command('v.what.rast', map=vector_1, raster=random_tmp, column='t') write_vect_history('v.particles', options, flags, vector_1) vector_names = [vector_1, ] for time in range(0, total_time + step, step): vector_1 = particle_base + "{0:03d}".format(loop) vector_2 = particle_base + "{0:03d}".format(loop + 1) vector_names.append(vector_2) gcore.run_command('v.what.rast', map=vector_1, raster=xshift_tmp, column='xshift') gcore.run_command('v.what.rast', map=vector_1, raster=yshift_tmp, column='yshift') gcore.run_command('v.transform', layer=1, input=vector_1, output=vector_2, columns='xshift:xshift,yshift:yshift', quiet=True) # increase age gcore.info("Increasing age...") sql = 'UPDATE {table} SET t=t+1;'.format(table=vector_2) gcore.run_command('db.execute', sql=sql) # remove old points gcore.info("Removing old points...") gcore.run_command('v.select', overwrite=True, ainput=vector_2, atype='point', binput=vector_region, btype='area', operator='within', output=vector_tmp1) gcore.run_command('v.extract', input=vector_tmp1, layer=1, type='point', where="t <= " + str(age) + " AND xshift IS NOT NULL", output=vector_tmp2, overwrite=True) # generate new points gcore.info("Generating new points...") count_to_generate = count - gvect.vector_info(vector_tmp2)['points'] if count_to_generate > 0: generate_points(name=vector_tmp3, probability_map=probability, count=count_to_generate, overwrite=True) gcore.info("Patchig new and old points...") gcore.run_command('v.patch', flags='e', input=[vector_tmp2, vector_tmp3], output=vector_2, overwrite=True) sql = 'UPDATE {table} SET t={t} WHERE t IS NULL;'.format(table=vector_2, t=0) gcore.run_command('db.execute', sql=sql) write_vect_history('v.particles', options, flags, vector_2) loop += 1 # Make sure the temporal database exists tgis.init() tgis.open_new_space_time_dataset(particle_base[:-1], type='stvds', temporaltype='relative', title="title", descr='desc', semantic='mean', dbif=None, overwrite=gcore.overwrite()) # TODO: we must start from 1 because there is a bug in register_maps_in_space_time_dataset tgis.register_maps_in_space_time_dataset( type='vect', name=particle_base[:-1], maps=','.join(vector_names), start=str(1), end=None, unit='seconds', increment=step, interval=False, dbif=None) # create one vector map with multiple layers fd, path = tempfile.mkstemp(text=True) tmpfile = open(path, 'w') k = 0 for vector in vector_names: k += 1 layers = [x for x in range(k - comet_length + 1, k + 1) if x > 0] categories = list(range(len(layers), 0, -1)) text = '' for layer, cat in zip(layers, categories): text += '{l} {c}\n'.format(l=layer, c=cat) coords = gcore.read_command('v.to.db', flags='p', quiet=True, map=vector, type='point', option='coor', separator=" ").strip() for coord in coords.split('\n'): coord = coord.split() tmpfile.write('P 1 {n_cat}\n{x} {y}\n'.format(n_cat=len(categories), x=coord[1], y=coord[2])) tmpfile.write(text) tmpfile.close() gcore.run_command('v.in.ascii', flags='n', overwrite=True, input=path, output=particles, format='standard', separator=" ") os.close(fd) os.remove(path) k = 0 sql = [] sizes = get_sizes(max_size, min_size, comet_length) temporal_maps = [] for vector in vector_names: k += 1 table = 't' + str(k) gcore.run_command('v.db.addtable', map=particles, table=table, layer=k, column="width double precision") temporal_maps.append(particles + ':' + str(k)) for i in range(comet_length): sql.append("UPDATE {table} SET width={w:.1f} WHERE cat={c}".format(table=table, w=sizes[i][1], c=sizes[i][0])) gcore.write_command('db.execute', input='-', stdin=';\n'.join(sql)) tgis.open_new_space_time_dataset(particles, type='stvds', temporaltype='relative', title="title", descr='desc', semantic='mean', dbif=None, overwrite=True) # TODO: we must start from 1 because there is a bug in register_maps_in_space_time_dataset tgis.register_maps_in_space_time_dataset( type='vect', name=particles, maps=','.join(temporal_maps), start=str(1), end=None, unit='seconds', increment=step, interval=False, dbif=None) write_vect_history('v.particles', options, flags, particles)
def main(): layers = options['map'].split(',') if len(layers) < 2: grass.error(_("At least 2 maps are required")) tmpfile = grass.tempfile() for map in layers: if not grass.find_file(map, element='cell')['file']: grass.fatal(_("Raster map <%s> not found") % map) grass.write_command('d.text', color='black', size=4, line=1, stdin="CORRELATION") os.environ['GRASS_PNG_READ'] = 'TRUE' colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) grass.write_command('d.text', color=color, size=4, line=line, stdin="%s %s" % (i, j)) line += 1 ofile = file(tmpfile, 'w') grass.run_command('r.stats', flags='cnA', input=(i, j), stdout=ofile) ofile.close() ifile = file(tmpfile, 'r') first = True for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0 / (maxx - minx + 1) ky = 100.0 / (maxy - miny + 1) p = grass.feed_command('d.graph', color=color) ofile = p.stdin ifile = file(tmpfile, 'r') for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) ofile.write("icon + 0.1 %f %f\n" % ((x - minx + 1) * kx, (y - miny + 1) * ky)) ifile.close() ofile.close() p.wait() try_remove(tmpfile)
def nanlandslide(conf, inputs, outputs): # Set path result rain idw method path = '/var/www/html/dl/data/nanrainfall/' #path = '/usr/local/lib/geoserver-2.5/data_dir/coverages/result_data/' #path = '/var/lib/tomcat6/webapps/geoserver/data/coverages/result_data/' #path2 = '/home/user/data/chai/rasters/' t = time.strftime("%Y%m%d:%H%M%S") print t + "=> Hello times it work[200]!" connection = psycopg2.connect(dbname='nanrunf', host='localhost', port='5432', user='******', password='******') # Import point rainfall data from PostgreSQL/PostGIS mg.run_command('v.in.ogr', dsn='PG:dbname=nanrunf', layer='nan_raingrass', output='nan_raingrass', overwrite=True) # Set Extent and Interpolation IDW Method #mg.run_command('g.region', rast='rainthai_idw@PERMANENT',res='1000', quiet= True) mg.run_command('g.region', n='2172120.86468637', e='748094.237955', s='1735854.76591878', w='558440.48801759', res='40') #mg.run_command('v.surf.idw', input='nan_raingrass@PERMANENT', output='nan_idw', power='2', column='rain',flags='n', overwrite= True) #mg.run_command('v.surf.idw', input=inputs["idw_vect"] ["value"], output=inputs["idw_rast"] ["value"], power='2', column='rain',flags='n', overwrite= True) mg.run_command('v.surf.idw', input=inputs["idw_vect"]["value"], output=inputs["idw_rast"]["value"], power=inputs["idw_power"]["value"], column=inputs["idw_column"]["value"], flags='n', overwrite=True) # Genarate Contour line (isolines) #mg.run_command('r.contour', input='rainthai_idw@PERMANENT', output='rain_isoline', step='5', overwrite= True) # Export rain_idw gdal to GeoTiff file and timestamp #mg.run_command('r.out.gdal', input='nan_idw@PERMANENT', type='Float64', output= path+'nanrainf_'+t+'.tif') #, overwrite= True mg.run_command('r.out.gdal', input=inputs["idw_rast"]["value"], type='Float64', output=inputs["dsn_export"]["value"] + '_nanrain_' + t + '.tif') #, overwrite= True mg.run_command('r.out.gdal', input='nan_idw@PERMANENT', type='Float64', output=path + 'nanrainf.tif', overwrite=True) #mg.run_command('r.out.gdal', input='nan_idw@PERMANENT', type='Float64', output= path+'nanrainf_'+t+'.tif') #, overwrite= True # test mapcal mg.write_command('r.mapcalc', stdin='raincal = if(nan_idw@PERMANENT > 25,1 ,null())') # to vector mg.run_command('r.to.vect', input='raincal@PERMANENT', output='warnings', feature='area', overwrite=True) # remove rast #mg.run_command('g.remove', rast='rainthai_idw@PERMANENT',quiet= True) connection = psycopg2.connect(dbname='nanrunf', host='localhost', port='5432', user='******', password='******') res = connection.cursor() res.execute("DROP VIEW IF EXISTS _villrisk ;") res.execute("DROP VIEW IF EXISTS vill_warn ;") res.execute("DROP TABLE IF EXISTS warnings ;") connection.commit() # Export warnning areas into PostgreSQL/PostGIS #v.out.ogr input=rain_isoline@PERMANENT type=line dsn=PG:host=localhost dbname=rain user=user olayer=rain_isoline format=PostgreSQL #mg.run_command('v.out.ogr', input='warnings@PERMANENT', type='area', dsn='PG:dbname=nanrunf', olayer='warnings', format='PostgreSQL',overwrite= True) #,flags='u',lco="OVERWRITE=YES" mg.run_command( 'v.out.ogr', input='warnings@PERMANENT', type='area', flags='c', dsn='PG:host=localhost dbname=nanrunf user=user password=user', olayer='warnings', format='PostgreSQL') #mg.run_command('v.delaunay', input='archsites',output='test_delaunay') # Create Voronoi from Rain Station #v.voronoi input=rainthai_f@PERMANENT output=rain_voronoi --overwrite #mg.run_command('v.voronoi', input='rainthai_f@PERMANENT',output='rain_voronoi', overwrite= True) #mg.run_command('v.out.ogr', input='rain_voronoi@PERMANENT', type='area', dsn='PG:dbname=rain', olayer='rain_voronoi', format='PostgreSQL',overwrite= True) #,flags='u' connection = psycopg2.connect(dbname='nanrunf', host='localhost', port='5432', user='******', password='******') res = connection.cursor() # res.execute("DROP VIEW IF EXISTS _villrisk ;") # res.execute("DROP VIEW IF EXISTS vill_warn ;") # res.execute("DROP TABLE IF EXISTS warnings ;") res.execute( "CREATE VIEW vill_warn AS SELECT v.gid, v.vill_code, v.vill_nam_t, v.geom FROM village_nbasin v, warnings w WHERE ST_Within (v.geom,w.wkb_geometry);" ) res.execute( "CREATE VIEW _villrisk AS SELECT vw.gid, vw.vill_code, vw.vill_nam_t, vw.geom FROM vill_warn vw, landslide ls WHERE ST_Within (vw.geom,ls.geom);" ) connection.commit() outputs["Result"]["value"]=\ "=> start process by: "+inputs["name"] ["value"]+ " | idw_inputvect: "+inputs["idw_vect"] ["value"]+ " | idw_outputrast: "+inputs["idw_rast"] ["value"]+ " | idw_power: "+inputs["idw_power"] ["value"]+ " | idw_column: "+inputs["idw_column"] ["value"]+ " | export_rast: "+inputs["dsn_export"] ["value"] return 3
def main(): layers = options['map'].split(',') if len(layers) < 2: grass.error(_("At least 2 maps are required")) tmpfile = grass.tempfile() for map in layers: if not grass.find_file(map, element = 'cell')['file']: grass.fatal(_("Raster map <%s> not found") % map) grass.write_command('d.text', color = 'black', size = 4, line = 1, stdin = "CORRELATION") os.environ['GRASS_PNG_READ'] = 'TRUE' colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) grass.write_command('d.text', color = color, size = 4, line = line, stdin = "%s %s" % (i, j)) line += 1 ofile = file(tmpfile, 'w') grass.run_command('r.stats', flags = 'cnA', input = (i, j), stdout = ofile) ofile.close() ifile = file(tmpfile, 'r') first = True for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0/(maxx-minx+1) ky = 100.0/(maxy-miny+1) p = grass.feed_command('d.graph', color = color) ofile = p.stdin ifile = file(tmpfile, 'r') for l in ifile: f = l.rstrip('\r\n').split(' ') x = float(f[0]) y = float(f[1]) ofile.write("icon + 0.1 %f %f\n" % ((x-minx+1) * kx, (y-miny+1) * ky)) ifile.close() ofile.close() p.wait() grass.try_remove(tmpfile)
grass.run_command('v.db.addtable', map=pplocs[i], col="cat int, Accumulation double precision, Discharge double precision") for i in range(len(ages)-1): grass.run_command('g.copy', vect=pplocs[i] + ',tmp', overwrite=True) grass.run_command('v.category', input='tmp', output=pplocs[i], overwrite=True) for i in range(len(ages)-1): grass.run_command('v.to.db', map=pplocs[i], columns="cat", option="cat", type='point') for i in range(len(ages)-1): print ages[i] grass.run_command('v.what.rast', vect=pplocs[i], rast=accumulation[i], col='Accumulation', layer=1) for i in range(len(ages)-1): print ages[i] grass.write_command('db.execute', stdin = "UPDATE " + pplocs[i] + " SET Discharge=Accumulation/1000" ) # As well as discharges at confluences for i in range(len(ages)-1): print ages[i] grass.run_command('v.db.addcol', map=tributary_junctions[i], col="Discharge double precision") for i in range(len(ages)-1): print ages[i] grass.write_command('db.execute', stdin = "UPDATE " + tributary_junctions[i] + " SET Discharge=Accumulation/1000" ) # After this, need to create a set of points on all of the channel segments. # These points will contain flow direction and discharge, allowing me to map
def main(): indb = options["database"] prefix = options["basename"] env = grass.gisenv() # fix sqlite3 db field string multibyte character problem sys.setdefaultencoding("utf-8") # check if 3d or not if flags["z"]: d3 = "z" else: d3 = "" owrite = grass.overwrite() # check if location it is latlong if grass.locn_is_latlong(): locn = True else: locn = False # connection to sqlite geopaparazzi database import sqlite3 conn = sqlite3.connect(indb) curs = conn.cursor() # if it is not a latlong location create a latlong location on the fly if not locn: # create new location and move to it creating new gisrc file new_loc = basename(grass.tempfile(create=False)) new_loc_name = "geopaparazzi_%s" % new_loc grass.create_location( dbase=env["GISDBASE"], epsg="4326", location=new_loc_name, desc="Temporary location for v.in.geopaparazzi", ) grc = os.getenv("GISRC") shutil.copyfile(grc, grc + ".old") newrc = open(grc, "w") newrc.write("GISDBASE: %s\n" % env["GISDBASE"]) newrc.write("LOCATION_NAME: %s\n" % new_loc_name) newrc.write("MAPSET: PERMANENT\n") newrc.write("GRASS_GUI: text\n") newrc.close() grass.run_command("db.connect", flags="d", quiet=True) # load bookmarks if flags["b"]: # check if elements in bookmarks table are more the 0 if checkEle(curs, "bookmarks") != 0: bookname = prefix + "_book" pois = importGeom(bookname, "bookmarks", curs, owrite, "") sql = "CREATE TABLE %s (cat int, text text)" % bookname grass.write_command("db.execute", input="-", stdin=sql) # select attributes sql = "select text from bookmarks order by _id" allattri = returnClear(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s'" % (idcat, str(row)) sql = "insert into %s values(%s)" % (bookname, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command("v.db.connect", map=bookname, table=bookname, quiet=True) else: grass.warning(_("No bookmarks found, escape them")) # load images if flags["i"]: # check if elements in images table are more the 0 if checkEle(curs, "images") != 0: imagename = prefix + "_image" pois = importGeom(imagename, "images", curs, owrite, d3) sql = "CREATE TABLE %s (cat int, azim int, " % imagename sql += "path text, ts text, text text)" grass.write_command("db.execute", input="-", stdin=sql) # select attributes sql = "select azim, path, ts, text from images order by _id" allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%d','%s','%s','%s'" % ( idcat, row[0], str(row[1]), str(row[2]), str(row[3]), ) sql = "insert into %s values(%s)" % (imagename, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command( "v.db.connect", map=imagename, table=imagename, quiet=True ) else: grass.warning(_("No images found, escape them")) # if tracks or nodes should be imported create a connection with sqlite3 # load notes if flags["n"]: # check if elements in notes table are more the 0 if checkEle(curs, "notes") != 0: # select each categories categories = returnClear(curs, "select cat from notes group by cat") # for each category for cat in categories: # select lat, lon for create point layer catname = prefix + "_notes_" + cat pois = importGeom(catname, "notes", curs, owrite, d3, cat) # select form to understand the number forms = returnClear( curs, "select _id from notes where cat = '%s' " "and form is not null order by _id" % cat, ) # if number of form is different from 0 and number of point # remove the vector because some form it is different if len(forms) != 0 and len(forms) != len(pois): grass.run_command( "g.remove", flags="f", type="vector", name=catname, quiet=True ) grass.warning( _( "Vector %s not imported because number" " of points and form is different" ) ) # if form it's 0 there is no form elif len(forms) == 0: # create table without form sql = "CREATE TABLE %s (cat int, ts text, " % catname sql += "text text, geopap_cat text)" grass.write_command("db.execute", input="-", stdin=sql) # select attributes sql = ( "select ts, text, cat from notes where " "cat='%s' order by _id" % cat ) allattri = returnAll(curs, sql) # add values using insert statement idcat = 1 for row in allattri: values = "%d,'%s','%s','%s'" % ( idcat, str(row[0]), str(row[1]), str(row[2]), ) sql = "insert into %s values(%s)" % (catname, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table to vector grass.run_command( "v.db.connect", map=catname, table=catname, quiet=True ) # create table with form else: # select all the attribute sql = ( "select ts, text, cat, form from notes where " "cat='%s' order by _id" % cat ) allattri = returnAll(curs, sql) # return string of form's categories too create table keys = returnFormKeys(allattri) sql = "CREATE TABLE %s (cat int, ts text, " % catname sql += "text text, geopap_cat text %s)" % keys grass.write_command("db.execute", input="-", stdin=sql) # it's for the number of categories idcat = 1 # for each feature insert value for row in allattri: values = "%d,'%s','%s','%s'," % ( idcat, str(row[0]), str(row[1]), str(row[2]), ) values += returnFormValues(row[3]) sql = "insert into %s values(%s)" % (catname, values) grass.write_command("db.execute", input="-", stdin=sql) idcat += 1 # at the end connect table with vector grass.run_command( "v.db.connect", map=catname, table=catname, quiet=True ) else: grass.warning(_("No notes found, escape them")) # load tracks if flags["t"]: # check if elements in bookmarks table are more the 0 if checkEle(curs, "gpslogs") != 0: tracksname = prefix + "_tracks" # define string for insert data at the end tracks = "" # return ids of tracks ids = returnClear(curs, "select _id from gpslogs") # for each track for i in ids: # select all the points coordinates tsel = "select lon, lat" if flags["z"]: tsel += ", altim" tsel += " from gpslog_data where logid=%s order by _id" % i trackpoints = returnAll(curs, tsel) wpoi = "\n".join( ["|".join([str(col) for col in row]) for row in trackpoints] ) tracks += "%s\n" % wpoi if flags["z"]: tracks += "NaN|NaN|Nan\n" else: tracks += "NaN|Nan\n" # import lines try: grass.write_command( "v.in.lines", flags=d3, input="-", out=tracksname, stdin=tracks, overwrite=owrite, quiet=True, ) except CalledModuleError: grass.fatal(_("Error importing %s" % tracksname)) # create table for line sql = "CREATE TABLE %s (cat int, startts text, " % tracksname sql += "endts text, text text, color text, width int)" grass.write_command("db.execute", input="-", stdin=sql) sql = ( "select logid, startts, endts, text, color, width from" " gpslogs, gpslogsproperties where gpslogs._id=" "gpslogsproperties.logid" ) # return attributes allattri = returnAll(curs, sql) # for each line insert attribute for row in allattri: values = "%d,'%s','%s','%s','%s',%d" % ( row[0], str(row[1]), str(row[2]), str(row[3]), str(row[4]), row[5], ) sql = "insert into %s values(%s)" % (tracksname, values) grass.write_command("db.execute", input="-", stdin=sql) # at the end connect map with table grass.run_command( "v.db.connect", map=tracksname, table=tracksname, quiet=True ) else: grass.warning(_("No tracks found, escape them")) # if location it's not latlong reproject it if not locn: # copy restore the original location shutil.copyfile(grc + ".old", grc) # reproject bookmarks if flags["b"] and checkEle(curs, "bookmarks") != 0: grass.run_command( "v.proj", quiet=True, input=bookname, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", ) # reproject images if flags["i"] and checkEle(curs, "images") != 0: grass.run_command( "v.proj", quiet=True, input=imagename, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", ) # reproject notes if flags["n"] and checkEle(curs, "notes") != 0: for cat in categories: catname = prefix + "_node_" + cat grass.run_command( "v.proj", quiet=True, input=catname, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", ) # reproject track if flags["t"] and checkEle(curs, "gpslogs") != 0: grass.run_command( "v.proj", quiet=True, input=tracksname, location="geopaparazzi_%s" % new_loc, mapset="PERMANENT", )
def simulate_fire(params, simulation_intervals, data_indexes, outputs): """ n = ['a' , 'b', 'c'] o = ['i' , 'j', 'k', 'l'] params = FireSimulationParams(model='m', moistures_live=n, moistures_1h=n, moistures_10h=n, moistures_100h=n, wind_directions=n, wind_velocities=n, start_raster='x', slope='o', aspect='p', elevation='q') simulate_fire(params, [(0, 1), (1, 3), (3, 8), (8, 9)], [0, 1, 1, 2], o) """ start_raster = params.start_raster # TODO: clean the tmp maps: g.mremove rast="rfirespread_rros_out*" -f # r.ros output raster maps (.base, .max, .maxdir, .spotdist) ros_basename = 'rfirespread_rros_out' ros_base = ros_basename + '.base' ros_max = ros_basename + '.max' ros_maxdir = ros_basename + '.maxdir' ros_spotdist = ros_basename + '.spotdist' if params.spotting else None rros_params = dict(model=params.model, slope=params.slope, aspect=params.aspect, elevation=params.elevation, base_ros=ros_base, max_ros=ros_max, direction_ros=ros_maxdir) if ros_spotdist: rros_params['spotting_distance'] = ros_spotdist rspread_params = dict(max_ros=ros_max, direction_ros=ros_maxdir, base_ros=ros_base) first_run = True for index, interval in enumerate(simulation_intervals): # print ">>>>>>>>>", index, interval, params.model, params.moistures_100h[data_indexes[index]], params.wind_directions[data_indexes[index]], params.wind_velocities[data_indexes[index]] # TODO: change the print to message rros_params['moisture_live'] = params.moistures_live[ data_indexes[index]] if params.moistures_1h: rros_params['moisture_1h'] = params.moistures_1h[ data_indexes[index]] if params.moistures_10h: rros_params['moisture_10h'] = params.moistures_10h[ data_indexes[index]] if params.moistures_100h: rros_params['moisture_100h'] = params.moistures_100h[ data_indexes[index]] if params.wind_directions: rros_params['direction'] = params.wind_directions[ data_indexes[index]] if params.wind_velocities: rros_params['velocity'] = params.wind_velocities[ data_indexes[index]] ret = run_command('r.ros', **rros_params) if ret != 0: gcore.fatal(_("r.ros failed. Please check above error messages.")) if first_run: rspread_flags = '' first_run = False else: rspread_flags = 'i' if params.spotting: rspread_flags += 's' rspread_params['spotting_distance'] = ros_spotdist rspread_params['wind_speed'] = params.wind_velocities[ data_indexes[index]] rspread_params['fuel_moisture'] = params.moistures_1h[ data_indexes[index]] rspread_params.update( dict(start=start_raster, output=outputs[index], init_time=interval[0], lag=interval[1] - interval[0])) ret = run_command('r.spread', flags=rspread_flags, **rspread_params) if ret != 0: gcore.fatal( _("r.spread failed. Please check above error messages.")) rast_to_remove = [ros_base, ros_max, ros_maxdir] if params.spotting: rast_to_remove.append(ros_spotdist) ret = run_command('g.remove', type='raster', name=rast_to_remove, flags='f') if ret != 0: gcore.fatal( _("g.remove failed when cleaning after r.ros and r.spread." " This might mean the error of programmer or unexpected behavior of one of the modules." " Please check above error messages.")) ret = run_command('r.null', map=outputs[index], setnull=0) if ret != 0: gcore.fatal(_("r.null failed. Please check above error messages.")) ret = write_command('r.colors', map=outputs[index], rules='-', stdin=""" 0% 50:50:50 60% yellow 100% red """) if ret != 0: gcore.fatal( _("r.colors failed. Please check above error messages.")) start_raster = outputs[index]
def main(): layers = options["map"].split(",") if len(layers) < 2: gcore.error(_("At least 2 maps are required")) tmpfile = gcore.tempfile() for map in layers: if not gcore.find_file(map, element="cell")["file"]: gcore.fatal(_("Raster map <%s> not found") % map) try: gcore.write_command( "d.text", color="black", size=4, line=1, stdin="CORRELATION" ) except CalledModuleError: return 1 os.environ["GRASS_RENDER_FILE_READ"] = "TRUE" colors = "red black blue green gray violet".split() line = 2 iloop = 0 jloop = 0 for iloop, i in enumerate(layers): for jloop, j in enumerate(layers): if i != j and iloop <= jloop: color = colors[0] colors = colors[1:] colors.append(color) gcore.write_command( "d.text", color=color, size=4, line=line, stdin="%s %s" % (i, j) ) line += 1 ofile = open(tmpfile, "w") gcore.run_command("r.stats", flags="cnA", input=(i, j), stdout=ofile) ofile.close() ifile = open(tmpfile, "r") first = True for line in ifile: f = line.rstrip("\r\n").split(" ") x = float(f[0]) y = float(f[1]) if first: minx = maxx = x miny = maxy = y first = False if minx > x: minx = x if maxx < x: maxx = x if miny > y: miny = y if maxy < y: maxy = y ifile.close() kx = 100.0 / (maxx - minx + 1) ky = 100.0 / (maxy - miny + 1) p = gcore.feed_command("d.graph", color=color) ofile = p.stdin ifile = open(tmpfile, "r") for line in ifile: f = line.rstrip("\r\n").split(" ") x = float(f[0]) y = float(f[1]) ofile.write( b"icon + 0.1 %f %f\n" % ((x - minx + 1) * kx, (y - miny + 1) * ky) ) ifile.close() ofile.close() p.wait() try_remove(tmpfile) return 0
def main(input_, coordinates, output, axes, slice_line, units, offset): prefix = 'r3_to_rast_tmp_' + str(os.getpid()) gcore.run_command('r3.to.rast', input=input_, output=prefix) maps = gcore.read_command('g.list', type='raster', pattern=PREFIX + '*').strip().split(os.linesep) region = gcore.region(region3d=True) res = (region['ewres'] + region['nsres']) / 2. if coordinates[0][0] > coordinates[1][0]: coordinates = (coordinates[1], coordinates[0]) profile = gcore.read_command('r.profile', coordinates=coordinates, input=maps[0], output='-').strip().split(os.linesep) cols = len(profile) rows = len(maps) s = w = 0 e = cols * res n = rows * region['tbres'] if offset: offset[0] = w + (e - w) * float(offset[0]) offset[1] = (n - s) * float(offset[1]) e += offset[0] w += offset[0] n += offset[1] s += offset[1] ascii_input = [("north: {n}\nsouth: {s}\neast: {e}\nwest: {w}\n" "rows: {r}\ncols: {c}\n".format(n=n, s=s, e=e, w=w, r=rows, c=cols))] for map_ in reversed(maps): profile = gcore.read_command('r.profile', coordinates=coordinates, input=map_, output='-', quiet=True).strip().split(os.linesep) ascii_input.append(' '.join([line.split()[1] for line in profile])) gcore.write_command('r.in.ascii', input='-', stdin='\n'.join(ascii_input), output=output, type='FCELL') gcore.run_command('r.colors', map=output, raster_3d=input_) if slice_line: vector_ascii = [] vector_ascii.append('L 2 1') vector_ascii.append('{x} {y}'.format(x=coordinates[0][0], y=coordinates[0][1])) vector_ascii.append('{x} {y}'.format(x=coordinates[1][0], y=coordinates[1][1])) vector_ascii.append('1 1') gcore.write_command('v.in.ascii', format='standard', input='-', stdin='\n'.join(vector_ascii), flags='n', output=slice_line) if axes: vector_ascii = [] vector_ascii.append('L 2 1') vector_ascii.append('{x} {y}'.format(x=w, y=n + 0.1 * (n - s))) vector_ascii.append('{x} {y}'.format(x=e, y=n + 0.1 * (n - s))) vector_ascii.append('1 1') vector_ascii.append('P 1 1') vector_ascii.append('{x} {y}'.format(x=w, y=n + 0.1 * (n - s))) vector_ascii.append('2 1') vector_ascii.append('P 1 1') vector_ascii.append('{x} {y}'.format(x=e, y=n + 0.1 * (n - s))) vector_ascii.append('2 2') vector_ascii.append('L 2 1') vector_ascii.append('{x} {y}'.format(x=e + 0.05 * (e - w), y=n)) vector_ascii.append('{x} {y}'.format(x=e + 0.05 * (e - w), y=s)) vector_ascii.append('2 3') vector_ascii.append('P 1 1') vector_ascii.append('{x} {y}'.format(x=e + 0.05 * (e - w), y=n)) vector_ascii.append('1 2') vector_ascii.append('P 1 1') vector_ascii.append('{x} {y}'.format(x=e + 0.05 * (e - w), y=s)) vector_ascii.append('1 3') gcore.write_command('v.in.ascii', format='standard', input='-', stdin='\n'.join(vector_ascii), flags='n', output=axes) if units: units = units.split(',') else: units = ['', ''] gcore.run_command('v.db.addtable', map=axes, layer=1, columns="label varchar(50)") sql = ('UPDATE {axes} SET label = "{length} {u1}" WHERE cat = 1;\n' 'UPDATE {axes} SET label = "{top} {u2}" WHERE cat = 2;\n' 'UPDATE {axes} SET label = "{bottom} {u2}" WHERE cat = 3;\n'.format(axes=axes, length=int(e - w), top=region['t'], bottom=region['b'], u1=units[0], u2=units[1])) gcore.write_command('db.execute', input='-', stdin=sql)
return array = trim_edges_nsew(array, trim_nsew) gcore.run_command('g.region', rast=real_elev) array = scale_subsurface_flat(real_elev, array, zexag, base=table_height, height_mm=37, info_text=info_text) # save resulting array np.savetxt(temp_path, array, delimiter=" ") # import tmp_regions = [] env = get_environment(tmp_regions, n=np.max(array[:, 1]), s=np.min(array[:, 1]), e=np.max(array[:, 0]), w=np.min(array[:, 0]), res=mm_resolution) bin_surface(input_file=temp_path, output_raster=output_elev, temporary_raster=output_tmp1, env=env) adjust_boundaries(real_elev=real_elev, scanned_elev=output_elev, env=env) env = get_environment(tmp_regions, rast=output_elev) gcore.write_command('r.colors', map=output_elev, rules='-', stdin="0% 0:66:0\n100% 197:255:138") # gcore.run_command('r.colors', map=output_elev, color='elevation') difference(real_elev=real_elev, scanned_elev=output_elev, new='diff', env=env) env = get_environment(tmp_regions, rast3d=voxel, nsres=3, ewres=3) # voxels = gcore.read_command('g.mlist', quiet=True, type='rast3d', pattern='interp_2003*', separator=',').strip() # for voxel in voxels.split(','): # output_cross_ = output_cross + '2' + voxel cross_section(output_elev, voxel, output_cross, env=env) contours(scanned_elev=output_elev, new='scanned_contours', step=5., env=env) # cross_section_fill(output_elev, voxel, output_cross) # gcore.run_command('r3.cross.rast', input=voxel, elevation=output_elev, output=output_cross, overwrite=True)
def change_detection(before, after, change, height_threshold, cells_threshold, add, max_detected, debug, env): diff_thr = 'diff_thr_' + str(uuid.uuid4()).replace('-', '') diff_thr_clump = 'diff_thr_clump_' + str(uuid.uuid4()).replace('-', '') coeff = gcore.parse_command('r.regression.line', mapx=after, mapy=before, flags='g', env=env) grast.mapcalc('diff = {a} + {b} * {after} - {before}'.format(a=coeff['a'], b=coeff['b'], before=before, after=after), env=env) try: if add: grast.mapcalc( "{diff_thr} = if(({a} + {b} * {after} - {before}) > {thr1} &&" " ({a} + {b} * {after} - {before}) < {thr2}, 1, null())". format(a=coeff['a'], b=coeff['b'], diff_thr=diff_thr, after=after, before=before, thr1=height_threshold[0], thr2=height_threshold[1]), env=env) else: grast.mapcalc( "{diff_thr} = if(({before} - {a} + {b} * {after}) > {thr}, 1, null())" .format(diff_thr=diff_thr, a=coeff['a'], b=coeff['b'], after=after, before=before, thr=height_threshold), env=env) gcore.run_command('r.clump', input=diff_thr, output=diff_thr_clump, env=env) stats = gcore.read_command('r.stats', flags='cn', input=diff_thr_clump, sort='desc', env=env).strip().splitlines() if debug: print('DEBUG: {}'.format(stats)) if len(stats) > 0 and stats[0]: cats = [] found = 0 for stat in stats: if found >= max_detected: break if float(stat.split()[1]) < cells_threshold[1] and float( stat.split()[1]) > cells_threshold[ 0]: # larger than specified number of cells found += 1 cat, value = stat.split() cats.append(cat) if cats: rules = ['{c}:{c}:1'.format(c=c) for c in cats] gcore.write_command('r.recode', input=diff_thr_clump, output=change, rules='-', stdin='\n'.join(rules), env=env) gcore.run_command('r.volume', flags='f', input=change, clump=diff_thr_clump, centroids=change, env=env) else: gcore.warning("No change found!") gcore.run_command('v.edit', map=change, tool='create', env=env) else: gcore.warning("No change found!") gcore.run_command('v.edit', map=change, tool='create', env=env) gcore.run_command('g.remove', flags='f', type=['raster'], name=[diff_thr, diff_thr_clump], env=env) except: gcore.run_command('g.remove', flags='f', type=['raster'], name=[diff_thr, diff_thr_clump], env=env)