def main(): g.message("Pocitam NDVI...") # nastavit region g.region(rast=options['tm4']) # vypocitat NDVI r.mapcalc('ndvi = float({y} - {x}) / ({y} + {x})'.format(x=options['tm3'], y=options['tm4']), overwrite = True) # r.reclass podporuje pouze datovy typ CELL r.mapcalc('temp1 = 100 * ndvi', overwrite = True) g.message("Reklasifikuji data...") # reklasifikovat data reclass_rules = """-100 thru 5 = 1 bez vegetace, vodni plochy 5 thru 35 = 2 plochy s minimalni vegetaci 35 thru 100 = 3 plochy pokryte vegetaci""" r.reclass(overwrite = True, rules = '-', input = 'temp1', output = 'r_ndvi', stdin_ = reclass_rules) # nastavit tabulku barev color_rules = """1 red 2 yellow 3 0 136 26""" r.colors(quiet = True, map = 'r_ndvi', rules = '-', stdin_ = color_rules) # vytiskout zakladni charakteristiku dat r.report(map = 'r_ndvi', units = ['c', 'p', 'h'])
def main(): g.message("Pocitam NDVI...") # nastavit region g.region(rast=options['tm4']) # vypocitat NDVI r.mapcalc('ndvi = float({y} - {x}) / ({y} + {x})'.format(x=options['tm3'], y=options['tm4']), overwrite=True) # r.reclass podporuje pouze datovy typ CELL r.mapcalc('temp1 = 100 * ndvi', overwrite=True) g.message("Reklasifikuji data...") # reklasifikovat data reclass_rules = """-100 thru 5 = 1 bez vegetace, vodni plochy 5 thru 35 = 2 plochy s minimalni vegetaci 35 thru 100 = 3 plochy pokryte vegetaci""" r.reclass(overwrite=True, rules='-', input='temp1', output='r_ndvi', stdin_=reclass_rules) # nastavit tabulku barev color_rules = """1 red 2 yellow 3 0 136 26""" r.colors(quiet=True, map='r_ndvi', rules='-', stdin_=color_rules) # vytiskout zakladni charakteristiku dat r.report(map='r_ndvi', units=['c', 'p', 'h'])
def check_raster_or_landuse(opts, params): """Return a list of raster name, i f necessary the rasters are generated using the file with the rules to convert a landuse map to the raster that it is needed. """ msgr = get_msgr() rasters = [] for par in params: if opts[par]: rasters.append(opts[par]) elif opts['rules_%s' % par] and opts['landuse']: output = rname(par) msg = 'Creating: {out} using the rules: {rul}' msgr.verbose(msg.format(out=output, rul='rules_%s' % par)) r.reclass(input=opts['landuse'], output=output, rules=opts['rules_%s' % par]) rasters.append(output) else: msg = '{par} or rule_{par} are required' raise ParameterError(msg.format(par=par)) return rasters
def compute_supply( base, recreation_spectrum, highest_spectrum, base_reclassification_rules, reclassified_base, reclassified_base_title, flow, flow_map_name, aggregation, ns_resolution, ew_resolution, print_only=False, flow_column_name=None, vector=None, supply_filename=None, use_filename=None, ): """ Algorithmic description of the "Contribution of Ecosysten Types" # FIXME ''' 1 B ← {0, .., m-1} : Set of aggregational boundaries 2 T ← {0, .., n-1} : Set of land cover types 3 WE ← 0 : Set of weighted extents 4 R ← 0 : Set of fractions 5 F ← 0 6 MASK ← HQR : High Quality Recreation 7 foreach {b} ⊆ B do : for each aggregational boundary 'b' 8 RB ← 0 9 foreach {t} ⊆ T do : for each Land Type 10 WEt ← Et * Wt : Weighted Extent = Extent(t) * Weight(t) 11 WE ← WE⋃{WEt} : Add to set of Weighted Extents 12 S ← ∑t∈WEt 13 foreach t ← T do 14 Rt ← WEt / ∑WE 15 R ← R⋃{Rt} 16 RB ← RB⋃{R} ''' # FIXME Parameters ---------- recreation_spectrum: Map scoring access to and quality of recreation highest_spectrum : Expected is a map of areas with highest recreational value (category 9 as per the report ... ) base : Base land types map for final zonal statistics. Specifically to ESTIMAP's recrceation mapping algorithm base_reclassification_rules : Reclassification rules for the input base map reclassified_base : Name for the reclassified base cover map reclassified_base_title : Title for the reclassified base map ecosystem_types : flow : Map of visits, derived from the mobility function, depicting the number of people living inside zones 0, 1, 2, 3. Used as a cover map for zonal statistics. flow_map_name : A name for the 'flow' map. This is required when the 'flow' input option is not defined by the user, yet some of the requested outputs required first the production of the 'flow' map. An example is the request for a supply table without requesting the 'flow' map itself. aggregation : ns_resolution : ew_resolution : statistics_filename : supply_filename : Name for CSV output file of the supply table use_filename : Name for CSV output file of the use table flow_column_name : Name for column to populate with 'flow' values vector : If 'vector' is given, a vector map of the 'flow' along with appropriate attributes will be produced. ? : Land cover class percentages in ROS9 (this is: relative percentage) output : Supply table (distribution of flow for each land cover class) Returns ------- This function produces a map to base the production of a supply table in form of CSV. Examples -------- """ # Inputs flow_in_base = flow + "_" + base base_scores = base + ".scores" # Define lists and dictionaries to hold intermediate data statistics_dictionary = {} weighted_extents = {} flows = [] # MASK areas of high quality recreation r.mask(raster=highest_spectrum, overwrite=True, quiet=True) # Reclassify land cover map to MAES ecosystem types r.reclass( input=base, rules=base_reclassification_rules, output=reclassified_base, quiet=True, ) # add to "remove_at_exit" after the reclassified maps! # Discard areas out of MASK copy_equation = EQUATION.format(result=reclassified_base, expression=reclassified_base) r.mapcalc(copy_equation, overwrite=True) # Count flow within each land cover category r.stats_zonal( base=base, flags="r", cover=flow_map_name, method="sum", output=flow_in_base, overwrite=True, quiet=True, ) # Set colors for "flow" map r.colors(map=flow_in_base, color=MOBILITY_COLORS, quiet=True) # Parse aggregation raster categories and labels categories = grass.parse_command("r.category", map=aggregation, delimiter="\t") for category in categories: # Intermediate names cells = highest_spectrum + ".cells" + "." + category remove_map_at_exit(cells) extent = highest_spectrum + ".extent" + "." + category remove_map_at_exit(extent) weighted = highest_spectrum + ".weighted" + "." + category remove_map_at_exit(weighted) fractions = base + ".fractions" + "." + category remove_map_at_exit(fractions) flow_category = "_flow_" + category flow = base + flow_category remove_map_at_exit(flow) flow_in_reclassified_base = reclassified_base + "_flow" flow_in_category = reclassified_base + flow_category flows.append(flow_in_category) # add to list for patching remove_map_at_exit(flow_in_category) # Output names msg = "Processing aggregation raster category: {r}" msg = msg.format(r=category) grass.debug(_(msg)) # g.message(_(msg)) # First, set region to extent of the aggregation map # and resolution to the one of the population map # Note the `-a` flag to g.region: ? # To safely modify the region: grass.use_temp_region() # FIXME g.region( raster=aggregation, nsres=ns_resolution, ewres=ew_resolution, flags="a", quiet=True, ) msg = "|! Computational resolution matched to {raster}" msg = msg.format(raster=aggregation) grass.debug(_(msg)) # Build MASK for current category & high quality recreation areas msg = "Setting category '{c}' of '{a}' as a MASK" grass.verbose(_(msg.format(c=category, a=aggregation))) masking = "if( {spectrum} == {highest_quality_category} && " masking += "{aggregation} == {category}, " masking += "1, null() )" masking = masking.format( spectrum=recreation_spectrum, highest_quality_category=HIGHEST_RECREATION_CATEGORY, aggregation=aggregation, category=category, ) masking_equation = EQUATION.format(result="MASK", expression=masking) grass.mapcalc(masking_equation, overwrite=True) # zoom to MASK g.region(zoom="MASK", nsres=ns_resolution, ewres=ew_resolution, quiet=True) # Count number of cells within each land category r.stats_zonal( flags="r", base=base, cover=highest_spectrum, method="count", output=cells, overwrite=True, quiet=True, ) cells_categories = grass.parse_command("r.category", map=cells, delimiter="\t") grass.debug(_("Cells: {c}".format(c=cells_categories))) # Build cell category and label rules for `r.category` cells_rules = "\n".join([ "{0}:{1}".format(key, value) for key, value in cells_categories.items() ]) # Discard areas out of MASK copy_equation = EQUATION.format(result=cells, expression=cells) r.mapcalc(copy_equation, overwrite=True) # Reassign cell category labels r.category(map=cells, rules="-", stdin=cells_rules, separator=":") # Compute extent of each land category extent_expression = "@{cells} * area()" extent_expression = extent_expression.format(cells=cells) extent_equation = EQUATION.format(result=extent, expression=extent_expression) r.mapcalc(extent_equation, overwrite=True) # Write extent figures as labels r.stats_zonal( flags="r", base=base, cover=extent, method="average", output=extent, overwrite=True, verbose=False, quiet=True, ) # Write land suitability scores as an ASCII file temporary_reclassified_base_map = temporary_filename( filename=reclassified_base) suitability_scores_as_labels = string_to_file( SUITABILITY_SCORES_LABELS, filename=temporary_reclassified_base_map) remove_files_at_exit(suitability_scores_as_labels) # Write scores as raster category labels r.reclass( input=base, output=base_scores, rules=suitability_scores_as_labels, overwrite=True, quiet=True, verbose=False, ) remove_map_at_exit(base_scores) # Compute weighted extents weighted_expression = "@{extent} * float(@{scores})" weighted_expression = weighted_expression.format(extent=extent, scores=base_scores) weighted_equation = EQUATION.format(result=weighted, expression=weighted_expression) r.mapcalc(weighted_equation, overwrite=True) # Write weighted extent figures as labels r.stats_zonal( flags="r", base=base, cover=weighted, method="average", output=weighted, overwrite=True, verbose=False, quiet=True, ) # Get weighted extents in a dictionary weighted_extents = grass.parse_command("r.category", map=weighted, delimiter="\t") # Compute the sum of all weighted extents and add to dictionary category_sum = sum([ float(x) if not math.isnan(float(x)) else 0 for x in weighted_extents.values() ]) weighted_extents["sum"] = category_sum # Create a map to hold fractions of each weighted extent to the sum # See also: # https://grasswiki.osgeo.org/wiki/LANDSAT#Hint:_Minimal_disk_space_copies r.reclass( input=base, output=fractions, rules="-", stdin="*=*", verbose=False, quiet=True, ) # Compute weighted fractions of land types fraction_category_label = { key: float(value) / weighted_extents["sum"] for (key, value) in weighted_extents.iteritems() if key is not "sum" } # Build fraction category and label rules for `r.category` fraction_rules = "\n".join([ "{0}:{1}".format(key, value) for key, value in fraction_category_label.items() ]) # Set rules r.category(map=fractions, rules="-", stdin=fraction_rules, separator=":") # Assert that sum of fractions is ~1 fraction_categories = grass.parse_command("r.category", map=fractions, delimiter="\t") fractions_sum = sum([ float(x) if not math.isnan(float(x)) else 0 for x in fraction_categories.values() ]) msg = "Fractions: {f}".format(f=fraction_categories) grass.debug(_(msg)) # g.message(_("Sum: {:.17g}".format(fractions_sum))) assert abs(fractions_sum - 1) < 1.0e-6, "Sum of fractions is != 1" # Compute flow flow_expression = "@{fractions} * @{flow}" flow_expression = flow_expression.format(fractions=fractions, flow=flow_in_base) flow_equation = EQUATION.format(result=flow, expression=flow_expression) r.mapcalc(flow_equation, overwrite=True) # Write flow figures as raster category labels r.stats_zonal( base=reclassified_base, flags="r", cover=flow, method="sum", output=flow_in_category, overwrite=True, verbose=False, quiet=True, ) # Parse flow categories and labels flow_categories = grass.parse_command("r.category", map=flow_in_category, delimiter="\t") grass.debug(_("Flow: {c}".format(c=flow_categories))) # Build flow category and label rules for `r.category` flow_rules = "\n".join([ "{0}:{1}".format(key, value) for key, value in flow_categories.items() ]) # Discard areas out of MASK # Check here again! # Output patch of all flow maps? copy_equation = EQUATION.format(result=flow_in_category, expression=flow_in_category) r.mapcalc(copy_equation, overwrite=True) # Reassign cell category labels r.category(map=flow_in_category, rules="-", stdin=flow_rules, separator=":") # Update title reclassified_base_title += " " + category r.support(flow_in_category, title=reclassified_base_title) # debugging # r.report( # flags='hn', # map=(flow_in_category), # units=('k','c','p'), # ) if print_only: r.stats( input=(flow_in_category), output="-", flags="nacpl", separator=COMMA, quiet=True, ) if not print_only: if flow_column_name: flow_column_prefix = flow_column_name + category else: flow_column_name = "flow" flow_column_prefix = flow_column_name + category # Produce vector map(s) if vector: # The following is wrong # update_vector(vector=vector, # raster=flow_in_category, # methods=METHODS, # column_prefix=flow_column_prefix) # What can be done? # Maybe update columns of an existing map from the columns of # the following vectorised raster map(s) # ? raster_to_vector(raster=flow_in_category, vector=flow_in_category, type="area") # get statistics dictionary = get_raster_statistics( map_one=aggregation, # reclassified_base map_two=flow_in_category, separator="|", flags="nlcap", ) # merge 'dictionary' with global 'statistics_dictionary' statistics_dictionary = merge_two_dictionaries( statistics_dictionary, dictionary) # It is important to remove the MASK! r.mask(flags="r", quiet=True) # FIXME # Add "reclassified_base" map to "remove_at_exit" here, so as to be after # all reclassified maps that derive from it # remove the map 'reclassified_base' # g.remove(flags='f', type='raster', name=reclassified_base, quiet=True) # remove_map_at_exit(reclassified_base) if not print_only: r.patch(flags="", input=flows, output=flow_in_reclassified_base, quiet=True) if vector: # Patch all flow vector maps in one v.patch( flags="e", input=flows, output=flow_in_reclassified_base, overwrite=True, quiet=True, ) # export to csv if supply_filename: supply_filename += CSV_EXTENSION nested_dictionary_to_csv(supply_filename, statistics_dictionary) if use_filename: use_filename += CSV_EXTENSION uses = compile_use_table(statistics_dictionary) dictionary_to_csv(use_filename, uses) # Maybe return list of flow maps? Requires unique flow map names return flows
def main(): soilloss = options['soilloss'] soilloss9 = soilloss.split('@')[0] + '.9' soilloss3 = soilloss.split('@')[0] + '.3' colorschema = options['colorschema'] flag_u = flags['u'] flag_f = flags['f'] quiet = True if gscript.verbosity() > 2: quiet=False #color shemes - contents: classrules = { 'soillossbare9' : {}, 'soillossbare3' : {}, 'soillossgrow9' : {}, 'cfactor6' : {}, 'kfactor6' : {} } colorrules = { 'soillossbare9': {}, 'soillossbare3': {}, 'soillossbare' : {}, 'soillossgrow9' : {}, 'soillossgrow' : {}, 'cfactor6' : {}, 'cfactor' : {}, 'kfactor6' : {}, 'kfactor' : {} } # (c) Gisler 2010 classrules['soillossbare9'] = '\n '.join([ "0 thru 20 = 1 < 20", "20 thru 30 = 2 20 - 30", "30 thru 40 = 3 30 - 40", "40 thru 55 = 4 40 - 55", "55 thru 100 = 5 55 - 100", "100 thru 150 = 6 100 - 150", "150 thru 250 = 7 150 - 250", "250 thru 500 = 8 250 - 500", "500 thru 50000 = 9 > 500", ]) # (c) Gisler 2010 classrules['soillossbare3'] = '\n '.join([ "0 thru 30 = 1 keine Gefährdung", "30 thru 55 = 2 Gefährdung", "55 thru 50000 = 3 grosse Gefährdung", ]) # (c) BLW 2011 colorrules['soillossbare9'] = '\n '.join([ "1 0:102:0", "2 51:153:0", "3 204:255:0", "4 255:255:0", "5 255:102:0", "6 255:0:0", "7 204:0:0", "8 153:0:0", "9 102:0:0", ]) # (c) BLW 2011 colorrules['soillossbare3'] = '\n '.join([ "1 51:153:0", "2 255:255:0", "3 255:0:0" ]) # (c) BLW 2011 colorrules['soillossbare'] = '\n '.join([ "0 0:102:0", "20 51:153:0", "30 204:255:0", "40 255:255:0", "55 255:102:0", "100 255:0:0", "150 204:0:0", "250 153:0:0", "500 102:0:0", "5000 102:0:0" ]) # (c) Gisler 2010 colorrules['soillossgrow9'] = '\n '.join([ "1 69:117:183", "2 115:146:185", "3 163:179:189", "4 208:216:193", "5 255:255:190", "6 252:202:146", "7 245:153:106", "8 233:100:70", "9 213:47:39" ]) # (c) Gisler 2010 colorrules['soillossgrow9'] = '\n '.join([ "1 69:117:183", "2 115:146:185", "3 163:179:189", "4 208:216:193", "5 255:255:190", "6 252:202:146", "7 245:153:106", "8 233:100:70", "9 213:47:39" ]) # (c) Gisler 2010 colorrules['soillossgrow3'] = '\n '.join([ "1 69:117:183", "2 163:179:189", "3 208:216:193", "4 245:153:106" ]) # (c) Gisler 2010 colorrules['soillossgrow'] = '\n '.join([ "0 69:117:183", "1 115:146:185", "2 163:179:189", "4 208:216:193", "7.5 255:255:190", "10 252:202:146", "15 245:153:106", "20 233:100:70", "30 213:47:39", "300 213:47:39" ]) # (c) Gisler 2010 classrules['soillossgrow9'] = '\n '.join([ "0 thru 1 = 1 < 1 ", "1 thru 2 = 2 1 - 2 ", "2 thru 4 = 3 2 - 4 ", "4 thru 7.5 = 4 4 - 7.5", "7.5 thru 10 = 5 7.5 - 10", "10 thru 15 = 6 10 - 15", "15 thru 20 = 7 15 - 20", "20 thru 30 = 8 20 - 30", "30 thru 5000 = 9 > 30" ]) # (c) Gisler 2010 classrules['soillossgrow3'] = '\n '.join([ "0 thru 2 = 1 Toleranz mittelgründige Böden", "2 thru 4 = 2 Toleranz tiefgründige Böden", "4 thru 7.5 = 3 leichte Überschreitung", "7.5 thru 5000 = 4 starke Überschreitung" ]) # Gisler 2010 colorrules['cfactor6'] = '\n '.join([ "1 56:145:37", "2 128:190:91", "3 210:233:153", "4 250:203:147", "5 225:113:76", "6 186:20:20" ]) # Gisler 2010 colorrules['cfactor'] = '\n '.join([ "0.00 56:145:37", "0.01 128:190:91", "0.05 210:233:153", "0.1 250:203:147", "0.15 225:113:76", "0.2 186:20:20", "1 186:20:20", ]) # (c) Gisler 2010 classrules['kfactor5'] = '\n '.join([ "0 thru 0.20 = 1 < 0.20", "0.20 thru 0.25 = 2 0.20 - 0.25", "0.25 thru 0.30 = 3 0.25 - 0.3", "0.3 thru 0.35 = 4 0.3 - 0.35", "0.35 thru 1 = 5 > 0.30" ]) # (c) Gisler 2010 colorrules['kfactor6'] = '\n '.join([ "1 15:70:15", "2 98:131:52", "3 204:204:104", "4 151:101:50", "5 98:21:15" ]) # (c) Gisler 2010 colorrules['kfactor'] = '\n '.join([ "0.00 15:70:15", "0.20 98:131:52", "0.25 204:204:104", "0.30 151:101:50", "0.35 98:21:15" ]) # own definitions colorrules['cpmax'] = '\n '.join([ "0.01 102:0:0", "0.01 153:0:0", "0.02 204:0:0", "0.04 255:0:0", "0.06 255:102:0", "0.08 255:255:0", "0.10 204:255:0", "0.12 51:153:0", "0.15 0:102:0", "1000.00 0:102:0" ]) classrules9 = '\n '.join([ "0 thru 20 = 1 <20", "20 thru 30 = 2 20 - 30", "30 thru 40 = 3 30 - 40", "40 thru 55 = 4 40 - 55", "55 thru 100 = 5 55 - 100", "100 thru 150 = 6 100 - 150", "150 thru 250 = 7 150 - 250", "250 thru 500 = 8 250 - 500", "500 thru 50000 = 9 >500", ]) if colorschema == 'soillossbare': classrules9 = classrules['soillossbare9'] colorrules9 = colorrules['soillossbare9'] classrules3 = classrules['soillossbare3'] colorrules3 = colorrules['soillossbare3'] colorrules = colorrules['soillossbare'] if colorschema == 'soillossgrow': classrules9 = classrules['soillossgrow9'] colorrules9 = colorrules['soillossgrow9'] classrules3 = classrules['soillossgrow3'] colorrules3 = colorrules['soillossgrow3'] colorrules = colorrules['soillossgrow'] r.reclass(input=soilloss, rules='-', stdin = classrules9, output=soilloss9) r.colors(map = soilloss9, rules = '-', stdin = colorrules9, quiet = quiet) r.reclass(input=soilloss, rules='-', stdin = classrules3, output=soilloss3) r.colors(map = soilloss3, rules = '-', stdin = colorrules3, quiet = quiet) if flag_f: soilloss3f = soilloss3 + 'f' r.neighbors(method='mode', input=soilloss3, selection=soilloss3, output=soilloss3f, size=7) soilloss3 = soilloss3f if flag_u: r.colors(map = soilloss, rules = '-', stdin = colorrules, quiet = quiet)