Esempio n. 1
0
def main():
    soilloss = options['soilloss']
    soilloss3 = soilloss # + '.3'
    map = options['map']
    parcelnumcol = options['parcelnumcol']
    flag_l = flags['l']
    flag_h = flags['h']
  
    quiet = True
    if gscript.verbosity() > 2:
        quiet=False

    zones = map.split('@')[0] + '.zones'
    v.to_rast( input=map, use='attr', attrcolumn=parcelnumcol,
              output=zones, quiet=quiet)
    
    
    def printStats(table, tablefmt='simple'):
        try:
            from tabulate import tabulate
        except:
            gscript.warning('Install tabulate for pretty printing tabular output ($ pip install tabulate). Using pprint instead...')
            from pprint import pprint
            pprint(statout)
        print tabulate(table,headers='firstrow',tablefmt=tablefmt)
         
    if not flag_h:
        runivar = r.univar(map=soilloss, zones=zones, flags='t', stdout=gscript.PIPE)
        rstats = runivar.outputs.stdout.strip().split('\n')
        #show all available values columns
        #rstats[0].split('|')
        rstatout = []
        for line in range(len(rstats)):
            lineout = []
            for i in (0,7,9,4,5): 
                lineout += (rstats[line].split('|')[i],)
            rstatout.append(lineout)
            
        if flag_l: printStats(rstatout, tablefmt='latex')
        else: printStats(rstatout)
        
    if flag_h:
            
        r.report(map=(zones,soilloss3),units='p',flags='')
Esempio n. 2
0
def main():
    soillossbare = options['soillossbare']
    soillossgrow = options['soillossgrow']
    cfactor = options['cfactor']
    pfactor = options['pfactor']    
    map = options['map']
    factorcols = options['factorcols'].split(',')
    
    quiet = True
    if gscript.verbosity() > 2:
        quiet=False

    if not (cfactor or pfactor):
        if not map:
            gscript.fatal('Please give either factor raster map(s) or vector map with factor(s)')
        elif not factorcols:
            gscript.fatal("Please give 'factorcols' (attribute columns with factor(s))  for <%s>" %map)
        
        factors = ()
        for factorcol in factorcols:
            output = map.split('@')[0] + '.' + factorcol
            gscript.message('Rasterize <%s> with attribute <%s>' %(map, factorcol) 
                + '\n to raster map <%s> ...' %(output) )
            v.to_rast(input=map, use='attr', attrcolumn=factorcol, 
                      output=output, quiet=quiet)
            factors += (output,)
    
    else: factors = (cfactor, pfactor)
    
    gscript.message('Multiply factors <%s> with <%s> ...' %(factors, soillossbare) )
    formula = soillossgrow + '=' + soillossbare 
    for factor in factors:
        formula += '*' + factor
    r.mapcalc(formula)
    
    ## apply color rules
    r.colors(map = soillossgrow,
                    rules = '-', stdin = colorrules['soillossgrow'],
                    quiet = quiet)
Esempio n. 3
0
def main():
    raster = options['raster']
    maskcats = options['maskcats']
    vector = options['vector']
    layer = options['layer']
    cats = options['cats']
    where = options['where']
    remove = flags['r']
    invert = flags['i']

    if not remove and not raster and not vector:
        grass.fatal(_("Either parameter <raster> ot parameter <vector> is required"))

    mapset = grass.gisenv()['MAPSET']
    exists = bool(grass.find_file('MASK', element='cell', mapset=mapset)['file'])

    if remove:
        # -> remove
        if exists:
            if sys.platform == 'win32':
                grass.run_command('g.remove', flags='if', quiet=True,
                                  type='raster', name='MASK')
            else:
                grass.run_command('g.remove', flags='f', quiet=True,
                                  type='raster', name='MASK')
            grass.message(_("Raster MASK removed"))
        else:
            grass.fatal(_("No existing MASK to remove"))
    else:
        # -> create
        if exists:
            if not grass.overwrite():
                grass.fatal(_("MASK already found in current mapset. Delete first or overwrite."))
            else:
                grass.warning(_("MASK already exists and will be overwritten"))
                grass.run_command('g.remove', flags='f', quiet=True,
                                  type='raster', name='MASK')

        if raster:
            # check if input raster exists
            if not grass.find_file(raster)['file']:
                grass.fatal(_("Raster map <%s> not found") % raster)

            if maskcats != '*' and not remove:
                if grass.raster_info(raster)['datatype'] != "CELL":
                    grass.fatal(_("The raster map <%s> must be integer (CELL type) "
                                  " in order to use the 'maskcats' parameter") % raster)

            p = grass.feed_command(
                'r.reclass',
                input=raster,
                output='MASK',
                overwrite=True,
                rules='-')
            p.stdin.write("%s = 1" % maskcats)
            p.stdin.close()
            p.wait()
        elif vector:
            vector_name = grass.find_file(vector, 'vector')['fullname']
            if not vector_name:
                grass.fatal(_("Vector map <%s> not found") % vector)

            # parser bug?
            if len(cats) == 0:
                cats = None
            if len(where) == 0:
                where = None

            if grass.vector_info_topo(vector_name)['areas'] < 1:
                grass.warning(_("No area found in vector map <%s>. "
                                "Creating a convex hull for MASK.") % vector_name)
                global tmp_hull
                tmp_hull = "tmp_hull_%d" % os.getpid()
                to_rast_input = tmp_hull
                # force 'flat' convex hull for 3D vector maps
                try:
                    grass.run_command('v.hull', flags='f', quiet=True,
                                      input=vector_name, output=tmp_hull,
                                      layer=layer, cats=cats, where=where)
                except CalledModuleError:
                    grass.fatal(
                        _("Unable to create a convex hull for vector map <%s>") %
                        vector_name)
            else:
                to_rast_input = vector_name

            env = os.environ.copy()
            if grass.verbosity() > 1:
                env['GRASS_VERBOSE'] = '1'
            grass.run_command('v.to.rast', input=to_rast_input, layer=layer,
                              output='MASK', use='val', val='1',
                              type='area', cats=cats, where=where, env=env)

        if invert:
            global tmp
            tmp = "r_mask_%d" % os.getpid()
            grass.run_command('g.rename', raster=('MASK', tmp), quiet=True)
            grass.message(_("Creating inverted raster MASK..."))
            grass.mapcalc("MASK = if(isnull($tmp), 1, null())", tmp=tmp)
            grass.verbose(_("Inverted raster MASK created"))
        else:
            grass.verbose(_("Raster MASK created"))

        grass.message(_("All subsequent raster operations will be limited to "
                        "the MASK area. Removing or renaming raster map named "
                        "'MASK' will restore raster operations to normal."))
Esempio n. 4
0
def main():
    # Get the options
    input = options["input"]
    output = options["output"]
    method = options["method"]
    min_cat = None
    max_cat = None
    point = None
    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False

    in_info = grass.vector_info(input)
    # check for wild mixture of vector types
    if in_info['points'] > 0 and in_info['boundaries'] > 0:
        grass.fatal(_("The input vector map contains both polygons and points,"
                      " cannot handle mixed types"))

    pid = os.getpid()
    # process points via triangulation, then exit
    if in_info['points'] > 0:
        point = True
        layer = 1  # hardcoded for now
        out_temp = '{inp}_point_tmp_{pid}'.format(inp=input, pid=pid)
        if method == 'delaunay':
            grass.message(_("Processing point data (%d points found)...") % in_info['points'])
            grass.run_command('v.delaunay', input=input, layer=layer,
                              output=out_temp, quiet=quiet)

        grass.run_command('v.db.addtable', map=out_temp, quiet=True)
        input = out_temp
        in_info = grass.vector_info(input)

    # process areas
    if in_info['areas'] == 0 and in_info['boundaries'] == 0:
        grass.fatal(_("The input vector map does not contain polygons"))

    out_type = '{inp}_type_{pid}'.format(inp=input, pid=pid)
    input_tmp = '{inp}_tmp_{pid}'.format(inp=input, pid=pid)
    remove_names = "%s,%s" % (out_type, input_tmp)
    grass.message(_("Processing area data (%d areas found)...") % in_info['areas'])
    if 0 != grass.run_command('v.category', layer="2", type='boundary',
                              option='add', input=input, out=input_tmp,
                              quiet=quiet):
        grass.run_command('g.remove', flags='f', type='vect',
                          pattern=input_tmp, quiet=quiet)
        grass.fatal(_("Error creating layer 2"))
    if 0 != grass.run_command('v.db.addtable', map=input_tmp, layer="2",
                              columns="left integer,right integer",
                              quiet=quiet):
        grass.run_command('g.remove', flags='f', type='vect',
                          pattern=input_tmp, quiet=quiet)
        grass.fatal(_("Error creating new table for layer 2"))
    if 0 != grass.run_command('v.to.db', map=input_tmp, option="sides",
                              columns="left,right", layer="2", quiet=quiet):
        grass.run_command('g.remove', flags='f', type='vect',
                          pattern=input_tmp, quiet=quiet)
        grass.fatal(_("Error populating new table for layer 2"))

    if 0 != grass.run_command('v.type', input=input_tmp, output=out_type,
                              from_type='boundary', to_type='line',
                              quiet=quiet, layer="2"):
        grass.run_command('g.remove', flags='f', type='vect',
                          pattern=remove_names, quiet=quiet)
        grass.fatal(_("Error converting polygon to line"))
    report = grass.read_command('v.category', flags='g', input=out_type,
                                option='report', quiet=quiet).split('\n')
    for r in report:
        if r.find('centroid') != -1:
            min_cat = report[0].split()[-2]
            max_cat = report[0].split()[-1]
            break
    if min_cat and max_cat:
        if 0 != grass.run_command('v.edit', map=out_type, tool='delete',
                                  type='centroid', layer=2, quiet=quiet,
                                  cats='{mi}-{ma}'.format(mi=min_cat, ma=max_cat)):
            grass.run_command('g.remove', flags='f', type='vect',
                              pattern=remove_names, quiet=quiet)
            grass.fatal(_("Error removing centroids"))

    try:
        if 0 != grass.run_command('v.db.droptable', map=out_type, layer=1,
                                  flags='f', quiet=True):
            grass.run_command('g.remove', flags='f', type='vect',
                              pattern=remove_names, quiet=quiet)
            grass.fatal(_("Error removing table from layer 1"))
    except:
        grass.warning(_("No table for layer %d" % 1))
    if 0 != grass.run_command('v.category', input=out_type, option='transfer',
                              output=output, layer="2,1", quiet=quiet,
                              overwrite=overwrite):
        grass.run_command('g.remove', flags='f', type='vect',
                          pattern=remove_names, quiet=quiet)
        grass.fatal(_("Error adding categories"))
    grass.run_command('g.remove', flags='f', type='vect',
                      pattern=remove_names, quiet=quiet)
    if point:
        grass.run_command('g.remove', flags='f', type='vect',
                          pattern=out_temp, quiet=quiet)
Esempio n. 5
0
def main():
    # Get the options
    input = options["input"]
    output = options["output"]
    strds = options["strds"]
    where = options["where"]
    tempwhere = options["t_where"]

    if output and flags['u']:
        grass.fatal(_("Cannot combine 'output' option and 'u' flag"))
    elif not output and not flags['u']:
        grass.fatal(_("'output' option or 'u' flag must be given"))
    elif not output and flags['u']:
        grass.warning(_("Attribute table of vector {name} will be updated...").format(name=input))

    if where == "" or where == " " or where == "\n":
        where = None

    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False

    # Check the number of sample strds and the number of columns
    strds_names = strds.split(",")

    # Make sure the temporal database exists
    tgis.init()
    # We need a database interface
    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()

    samples = []

    first_strds = tgis.open_old_stds(strds_names[0], "strds", dbif)
    # Single space time raster dataset
    if len(strds_names) == 1:
        rows = first_strds.get_registered_maps("name,mapset,start_time,end_time",
                                               tempwhere, "start_time",
                                               dbif)

        if not rows:
            dbif.close()
            grass.fatal(_("Space time raster dataset <%s> is empty") %
                        first_strds.get_id())

        for row in rows:
            start = row["start_time"]
            end = row["end_time"]
            raster_maps = [row["name"] + "@" + row["mapset"], ]

            s = Sample(start, end, raster_maps, first_strds.get_name())
            samples.append(s)
    else:
        # Multiple space time raster datasets
        for name in strds_names[1:]:
            dataset = tgis.open_old_stds(name, "strds", dbif)
            if dataset.get_temporal_type() != first_strds.get_temporal_type():
                grass.fatal(_("Temporal type of space time raster "
                              "datasets must be equal\n<%(a)s> of type "
                              "%(type_a)s do not match <%(b)s> of type "
                              "%(type_b)s" % {"a": first_strds.get_id(),
                               "type_a": first_strds.get_temporal_type(),
                               "b": dataset.get_id(),
                               "type_b": dataset.get_temporal_type()}))

        mapmatrizes = tgis.sample_stds_by_stds_topology("strds", "strds",
                                                        strds_names,
                                                        strds_names[0],
                                                        False, None,
                                                        "equal", False,
                                                        False)

        for i in xrange(len(mapmatrizes[0])):
            isvalid = True
            mapname_list = []
            for mapmatrix in mapmatrizes:

                entry = mapmatrix[i]

                if entry["samples"]:
                    sample = entry["samples"][0]
                    name = sample.get_id()
                    if name is None:
                        isvalid = False
                        break
                    else:
                        mapname_list.append(name)

            if isvalid:
                entry = mapmatrizes[0][i]
                map = entry["granule"]

                start, end = map.get_temporal_extent_as_tuple()
                s = Sample(start, end, mapname_list, name)
                samples.append(s)

    # Get the layer and database connections of the input vector
    if output:
        gcopy(input, output, 'vector')
    else:
        output = input

    msgr = Messenger()
    perc_curr = 0
    perc_tot = len(samples)
    pymap = Vector(output)
    try:
        pymap.open('r')
    except:
        dbif.close()
        grass.fatal(_("It is not possible to open the new map %s" % output))

    if len(pymap.dblinks) == 0:
        try:
            grass.run_command("v.db.addtable", map=output)
        except CalledModuleError:
            dbif.close()
            grass.fatal(_("Impossible add table to vector %s" % output))
    pymap.close()
    for sample in samples:
        raster_names = sample.raster_names
        # Call v.what.rast for each raster map

        for name in raster_names:
            coltype = "DOUBLE PRECISION"
            # Get raster map type
            raster_map = tgis.RasterDataset(name)
            raster_map.load()
            if raster_map.metadata.get_datatype() == "CELL":
                coltype = "INT"
            day = sample.printDay()
            column_name = "%s_%s" % (sample.strds_name, day)
            column_string = "%s %s" % (column_name, coltype)
            column_string.replace('.', '_')
            try:
                grass.run_command("v.db.addcolumn", map=output,
                                  column=column_string,
                                  overwrite=overwrite)
            except CalledModuleError:
                dbif.close()
                grass.fatal(_("Unable to add column %s to vector map "
                              "<%s> ") % (column_string, output))
            try:
                grass.run_command("v.what.rast", map=output, raster=name,
                                  column=column_name, where=where,
                                  quiet=quiet)
            except CalledModuleError:
                dbif.close()
                grass.fatal(_("Unable to run v.what.rast for vector map"
                              " <%s> and raster map <%s>") %
                            (output, str(raster_names)))

        msgr.percent(perc_curr, perc_tot, 1)
        perc_curr += 1

    dbif.close()
Esempio n. 6
0
def main():
    raster = options['raster']
    maskcats = options['maskcats']
    vector = options['vector']
    layer = options['layer']
    cats = options['cats']
    where = options['where']
    remove = flags['r']
    invert = flags['i']

    if not remove and not raster and not vector:
        grass.fatal(
            _("Either parameter <raster> ot parameter <vector> is required"))

    mapset = grass.gisenv()['MAPSET']
    exists = bool(
        grass.find_file('MASK', element='cell', mapset=mapset)['file'])

    if remove:
        # -> remove
        if exists:
            grass.run_command('g.remove',
                              flags='f',
                              quiet=True,
                              type='raster',
                              name='MASK')
            grass.message(_("Raster MASK removed"))
        else:
            grass.fatal(_("No existing MASK to remove"))
    else:
        # -> create
        if exists:
            if not grass.overwrite():
                grass.fatal(
                    _("MASK already found in current mapset. Delete first or overwrite."
                      ))
            else:
                grass.warning(_("MASK already exists and will be overwritten"))
                grass.run_command('g.remove',
                                  flags='f',
                                  quiet=True,
                                  type='raster',
                                  name='MASK')

        if raster:
            # check if input raster exists
            if not grass.find_file(raster)['file']:
                grass.fatal(_("Raster map <%s> not found") % raster)

            if maskcats != '*' and not remove:
                if grass.raster_info(raster)['datatype'] != "CELL":
                    grass.fatal(
                        _("The raster map <%s> must be integer (CELL type) "
                          " in order to use the 'maskcats' parameter") %
                        raster)

            p = grass.feed_command('r.reclass',
                                   input=raster,
                                   output='MASK',
                                   overwrite=True,
                                   rules='-')
            p.stdin.write("%s = 1" % maskcats)
            p.stdin.close()
            p.wait()
        elif vector:
            vector_name = grass.find_file(vector, 'vector')['fullname']
            if not vector_name:
                grass.fatal(_("Vector map <%s> not found") % vector)

            # parser bug?
            if len(cats) == 0:
                cats = None
            if len(where) == 0:
                where = None

            if grass.vector_info_topo(vector_name)['areas'] < 1:
                grass.warning(
                    _("No area found in vector map <%s>. "
                      "Creating a convex hull for MASK.") % vector_name)
                global tmp_hull
                tmp_hull = "tmp_hull_%d" % os.getpid()
                to_rast_input = tmp_hull
                # force 'flat' convex hull for 3D vector maps
                try:
                    grass.run_command('v.hull',
                                      flags='f',
                                      quiet=True,
                                      input=vector_name,
                                      output=tmp_hull,
                                      layer=layer,
                                      cats=cats,
                                      where=where)
                except CalledModuleError:
                    grass.fatal(
                        _("Unable to create a convex hull for vector map <%s>")
                        % vector_name)
            else:
                to_rast_input = vector_name

            env = os.environ.copy()
            if grass.verbosity() > 1:
                env['GRASS_VERBOSE'] = '1'
            grass.run_command('v.to.rast',
                              input=to_rast_input,
                              layer=layer,
                              output='MASK',
                              use='val',
                              val='1',
                              type='area',
                              cats=cats,
                              where=where,
                              env=env)

        if invert:
            global tmp
            tmp = "r_mask_%d" % os.getpid()
            grass.run_command('g.rename', raster=('MASK', tmp), quiet=True)
            grass.message(_("Creating inverted raster MASK..."))
            grass.mapcalc("MASK = if(isnull($tmp), 1, null())", tmp=tmp)
            grass.verbose(_("Inverted raster MASK created"))
        else:
            grass.verbose(_("Raster MASK created"))

        grass.message(
            _("All subsequent raster operations will be limited to "
              "the MASK area. Removing or renaming raster map named "
              "'MASK' will restore raster operations to normal."))
Esempio n. 7
0
def main():
    raster = options["raster"]
    maskcats = options["maskcats"]
    vector = options["vector"]
    layer = options["layer"]
    cats = options["cats"]
    where = options["where"]
    remove = flags["r"]
    invert = flags["i"]

    if not remove and not raster and not vector:
        grass.fatal(_("Either parameter <raster> ot parameter <vector> is required"))

    mapset = grass.gisenv()["MAPSET"]
    exists = bool(grass.find_file("MASK", element="cell", mapset=mapset)["file"])

    if remove:
        # -> remove
        if exists:
            grass.run_command("g.remove", flags="f", quiet=True, type="rast", pattern="MASK")
            grass.message(_("Raster MASK removed"))
        else:
            grass.fatal(_("No existing MASK to remove"))
    else:
        # -> create
        if exists:
            if not grass.overwrite():
                grass.fatal(_("MASK already found in current mapset. Delete first or overwrite."))
            else:
                grass.warning(_("MASK already exists and will be overwritten"))
                grass.run_command("g.remove", flags="f", quiet=True, type="rast", pattern="MASK")

        if raster:
            # check if input raster exists
            if not grass.find_file(raster)["file"]:
                grass.fatal(_("Raster map <%s> not found") % raster)

            if maskcats != "*" and not remove:
                if grass.raster_info(raster)["datatype"] != "CELL":
                    grass.fatal(
                        _(
                            "The raster map <%s> must be integer (CELL type) "
                            " in order to use the 'maskcats' parameter"
                        )
                        % raster
                    )

            p = grass.feed_command("r.reclass", input=raster, output="MASK", overwrite=True, rules="-")
            p.stdin.write("%s = 1" % maskcats)
            p.stdin.close()
            p.wait()
        elif vector:
            vector_name = grass.find_file(vector, "vector")["fullname"]
            if not vector_name:
                grass.fatal(_("Vector map <%s> not found") % vector)

            # parser bug?
            if len(cats) == 0:
                cats = None
            if len(where) == 0:
                where = None

            if grass.vector_info_topo(vector_name)["areas"] < 1:
                grass.warning(_("No area found in vector map <%s>. " "Creating a convex hull for MASK.") % vector_name)
                global tmp_hull
                tmp_hull = "tmp_hull_%d" % os.getpid()
                to_rast_input = tmp_hull
                # force 'flat' convex hull for 3D vector maps
                if 0 != grass.run_command(
                    "v.hull",
                    flags="f",
                    quiet=True,
                    input=vector_name,
                    output=tmp_hull,
                    layer=layer,
                    cats=cats,
                    where=where,
                ):
                    grass.fatal(_("Unable to create a convex hull for vector map <%s>") % vector_name)
            else:
                to_rast_input = vector_name

            env = os.environ.copy()
            if grass.verbosity() > 1:
                env["GRASS_VERBOSE"] = "1"
            grass.run_command(
                "v.to.rast",
                input=to_rast_input,
                layer=layer,
                output="MASK",
                use="val",
                val="1",
                type="area",
                cats=cats,
                where=where,
                env=env,
            )

        if invert:
            global tmp
            tmp = "r_mask_%d" % os.getpid()
            grass.run_command("g.rename", rast=("MASK", tmp), quiet=True)
            grass.message(_("Creating inverted raster MASK..."))
            grass.mapcalc("MASK = if(isnull($tmp), 1, null())", tmp=tmp)
            grass.verbose(_("Inverted raster MASK created"))
        else:
            grass.verbose(_("Raster MASK created"))

        grass.message(
            _(
                "All subsequent raster operations will be limited to "
                "the MASK area. Removing or renaming raster map named "
                "'MASK' will restore raster operations to normal."
            )
        )
Esempio n. 8
0
def main():
    # lazy imports
    import grass.temporal as tgis

    # Get the options
    type = options["type"]
    temporal_type = options["temporaltype"]
    columns = options["columns"]
    order = options["order"]
    where = options["where"]
    separator = gscript.separator(options["separator"])
    outpath = options["output"]
    colhead = flags['c']

    # Make sure the temporal database exists
    tgis.init()

    sp = tgis.dataset_factory(type, None)
    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()
    first = True

    if  gscript.verbosity() > 0 and not outpath:
        sys.stderr.write("----------------------------------------------\n")

    if outpath:
        outfile = open(outpath, 'w')

    for ttype in temporal_type.split(","):
        if ttype == "absolute":
            time = "absolute time"
        else:
            time = "relative time"

        stds_list = tgis.get_dataset_list(type, ttype, columns, where, order, dbif=dbif)

        # Use the correct order of the mapsets, hence first the current mapset, then
        # alphabetic ordering
        mapsets = tgis.get_tgis_c_library_interface().available_mapsets()

        # Print for each mapset separately
        for key in mapsets:
            if key in stds_list.keys():
                rows = stds_list[key]

                if rows:
                    if  gscript.verbosity() > 0 and not outpath:
                        if issubclass(sp.__class__, tgis.AbstractMapDataset):
                            sys.stderr.write(_("Time stamped %s maps with %s available in mapset <%s>:\n")%
                                                     (sp.get_type(), time, key))
                        else:
                            sys.stderr.write(_("Space time %s datasets with %s available in mapset <%s>:\n")%
                                                     (sp.get_new_map_instance(None).get_type(), time, key))

                    # Print the column names if requested
                    if colhead and first:
                        output = ""
                        count = 0
                        for key in rows[0].keys():
                            if count > 0:
                                output += separator + str(key)
                            else:
                                output += str(key)
                            count += 1
                        if outpath:
                            outfile.write("{st}\n".format(st=output))
                        else:
                            print(output)
                        first = False

                    for row in rows:
                        output = ""
                        count = 0
                        for col in row:
                            if count > 0:
                                output += separator + str(col)
                            else:
                                output += str(col)
                            count += 1
                        if outpath:
                            outfile.write("{st}\n".format(st=output))
                        else:
                            print(output)
    if outpath:
        outfile.close()
    dbif.close()
Esempio n. 9
0
def main():
    """
    Gridded flexural isostatic solutions
    """

    options, flags = grass.parser()
    # if just interface description is requested, it will not get to this point
    # so gflex will not be needed

    # GFLEX
    # try to import gflex only after we know that
    # we will actually do the computation
    try:
        import gflex
    except:
        print("")
        print("MODULE IMPORT ERROR.")
        print("In order to run r.flexure or g.flexure, you must download and install")
        print("gFlex. The most recent development version is available from")
        print("https://github.com/awickert/gFlex.")
        print("Installation instructions are available on the page.")
        grass.fatal("Software dependency must be installed.")

    # This code is for 2D flexural isostasy
    flex = gflex.F2D()
    # And show that it is coming from GRASS GIS
    flex.grass = True

    # Flags
    latlon_override = flags["l"]

    # Inputs
    # Solution selection
    flex.Method = options["method"]
    if flex.Method == "FD":
        flex.Solver = options["solver"]
        if flex.Solver:
            flex.ConvergenceTolerance = options["tolerance"]
        # Always use the van Wees and Cloetingh (1994) solution type.
        # It is the best.
        flex.PlateSolutionType = "vWC1994"
    # Parameters that are often changed for the solution
    qs = options["input"]
    flex.qs = garray.array()
    flex.qs.read(qs)
    # Elastic thickness
    try:
        flex.Te = float(options["te"])
    except:
        flex.Te = garray.array()  # FlexureTe is the one that is used by Flexure
        flex.Te.read(options["te"])
        flex.Te = np.array(flex.Te)
    if options["te_units"] == "km":
        flex.Te *= 1000
    elif options["te_units"] == "m":
        pass
    # No "else"; shouldn't happen
    flex.rho_fill = float(options["rho_fill"])
    # Parameters that often stay at their default values
    flex.g = float(options["g"])
    flex.E = float(
        options["ym"]
    )  # Can't just use "E" because reserved for "east", I think
    flex.nu = float(options["nu"])
    flex.rho_m = float(options["rho_m"])
    # Solver type and iteration tolerance
    flex.Solver = options["solver"]
    flex.ConvergenceTolerance = float(options["tolerance"])
    # Boundary conditions
    flex.BC_N = options["northbc"]
    flex.BC_S = options["southbc"]
    flex.BC_W = options["westbc"]
    flex.BC_E = options["eastbc"]

    # Set verbosity
    if grass.verbosity() >= 2:
        flex.Verbose = True
    if grass.verbosity() >= 3:
        flex.Debug = True
    elif grass.verbosity() == 0:
        flex.Quiet = True

    # First check if output exists
    if len(grass.parse_command("g.list", type="rast", pattern=options["output"])):
        if not grass.overwrite():
            grass.fatal(
                "Raster map '"
                + options["output"]
                + "' already exists. Use '--o' to overwrite."
            )

    # Get grid spacing from GRASS
    # Check if lat/lon and proceed as directed
    if grass.region_env()[6] == "3":
        if latlon_override:
            if flex.Verbose:
                print("Latitude/longitude grid.")
                print("Based on r_Earth = 6371 km")
                print("Setting y-resolution [m] to 111,195 * [degrees]")
            flex.dy = grass.region()["nsres"] * 111195.0
            NSmid = (grass.region()["n"] + grass.region()["s"]) / 2.0
            dx_at_mid_latitude = (
                (3.14159 / 180.0) * 6371000.0 * np.cos(np.deg2rad(NSmid))
            )
            if flex.Verbose:
                print(
                    "Setting x-resolution [m] to "
                    + "%.2f" % dx_at_mid_latitude
                    + " * [degrees]"
                )
            flex.dx = grass.region()["ewres"] * dx_at_mid_latitude
        else:
            grass.fatal("Need the '-l' flag to enable lat/lon solution approximation.")
    # Otherwise straightforward
    else:
        flex.dx = grass.region()["ewres"]
        flex.dy = grass.region()["nsres"]

    # CALCULATE!
    flex.initialize()
    flex.run()
    flex.finalize()

    # Write to GRASS
    # Create a new garray buffer and write to it
    outbuffer = garray.array()  # Instantiate output buffer
    outbuffer[...] = flex.w
    outbuffer.write(
        options["output"], overwrite=grass.overwrite()
    )  # Write it with the desired name
    # And create a nice colormap!
    grass.run_command(
        "r.colors", map=options["output"], color="differences", quiet=True
    )
Esempio n. 10
0
def main():
    # parameters
    infile = options["input"]
    raster_reference = options["raster_reference"]
    raster_file = options["raster_file"]
    outfiles = options["output"]
    resolution = options["resolution"]
    methods = options["method"]
    zrange = options["zrange"]
    zscale = options["zscale"]
    output_type = options["type"]
    percent = options["percent"]
    pth = options["pth"]
    trim = options["trim"]
    footprint = options["footprint"]
    # flags
    scan = flags["s"]
    shell_script_style = flags["g"]

    if len(outfiles.split(",")) != len(methods.split(",")):
        grass.fatal(
            _("Number of outputs <%s> does not match number of methods <%s>" %
              (outfiles, methods)))

    # overwrite auf true setzen
    os.environ["GRASS_OVERWRITE"] = "1"

    # to hide non-error messages from subprocesses
    if grass.verbosity() <= 2:
        outdev = open(os.devnull, "w")
    else:
        outdev = sys.stdout

    # use temporary region
    grass.use_temp_region()

    n, s, w, e, t, b = scan_extent(infile)

    # scan -s or shell_script_style -g:
    if scan:
        if not shell_script_style:
            grass.message(
                _("north: %s\nsouth: %s\nwest: %s\neast: %s\ntop: %s\nbottom: %s"
                  ) % (n, s, w, e, t, b))
        else:
            # shell script style: print to stdout
            print("n=%s s=%s w=%s e=%s t=%s b=%s" % (n, s, w, e, t, b))
    elif footprint:
        footprint_to_vectormap(infile, footprint)
    else:
        # get region with pdal
        # footprint_to_vectormap(infile, 'tiles')

        if raster_file:
            raster_reference = "img" + str(os.getpid())
            grass.run_command("r.external",
                              input=raster_file,
                              flags="o",
                              output=raster_reference)
            result = grass.find_file(name=raster_reference, element="raster")
            if result[u"fullname"] == u"":
                raster_reference = raster_reference + ".1"
        # option 1: set region to extent of tiles while precisely aligning pixel
        # geometry to raster_reference (including both raster_reference and raster_file)
        if raster_reference:
            grass.run_command("g.region",
                              n=n,
                              s=s,
                              e=e,
                              w=w,
                              flags="g",
                              align=raster_reference)
        else:
            # option 2: change raster resolution to final resolution while best
            # effort aligning to pixel geometry
            grass.run_command("g.region",
                              n=n,
                              s=s,
                              e=e,
                              w=w,
                              flags="ap",
                              res=resolution)
        # generate PDAL pipeline
        # . pdal pipline laz2json (STDOUT) | r.in.xyz
        bn = os.path.basename(infile)
        infile_format = bn.split(".")[-1]
        # format_reader from https://pdal.io/stages/readers.html
        format_reader = ""
        if infile_format.lower() == "laz" or infile_format.lower() == "las":
            format_reader = "readers.las"
        # pts: not tested
        elif infile_format.lower() == "pts":
            format_reader = "readers.pts"
        else:
            grass.run_command("g.remove",
                              flags="f",
                              type="vector",
                              name="tiles",
                              quiet=True)
            grass.fatal(_("Format .%s is not supported.." % infile_format))

        grass.message(_("Converting input file <%s>...") % infile)
        tmp_file_json = "tmp_file_json_" + str(os.getpid())

        data = {}
        data["pipeline"] = []
        data["pipeline"].append({"type": format_reader, "filename": infile})
        data["pipeline"].append({
            "type": "writers.text",
            "format": "csv",
            "order": "X,Y,Z",
            "keep_unspecified": "false",
            "filename": "STDOUT",
            "quote_header": "false",
        })
        with open(tmp_file_json, "w") as f:
            json.dump(data, f)

        tmp_xyz = grass.tempfile()
        if tmp_xyz is None:
            grass.fatal("Unable to create temporary files")
        command_pdal1 = options["pdal_cmd"].split(" ")
        if options["pdal_cmd"] != "pdal":
            v_index = None
            cmd_entries = options["pdal_cmd"].split(" ")
            for cmd_entry, num in zip(cmd_entries, range(len(cmd_entries))):
                if cmd_entry == "-v":
                    v_index = num
                    break
            mnt_vol = cmd_entries[v_index + 1].split(":")[1]
            tmp_file_json2 = os.path.join(mnt_vol, tmp_file_json)
        else:
            tmp_file_json2 = tmp_file_json
        command_pdal1.extend(["pipeline", "--input", tmp_file_json2])

        fh = open(tmp_xyz, "wb")
        if grass.call(command_pdal1, stdout=fh) != 0:
            fh.close()
            # check to see if pdal pipeline executed properly
            print(command_pdal1)
            grass.fatal(_("pdal pipeline is broken..."))
        else:
            fh.close()

        for i in range(len(methods.split(","))):
            method = methods.split(",")[i]
            outfile = outfiles.split(",")[i]

            grass.message(_("Generating output raster map <%s>...") % outfile)

            command_pdal2 = [
                "r.in.xyz",
                "input=" + tmp_xyz,
                "output=" + outfile,
                "skip=1",
                "separator=comma",
                "method=" + method,
            ]

            if zrange:
                command_pdal2.append("zrange=" + zrange)
            if zscale:
                command_pdal2.append("zscale=" + zscale)
            if output_type:
                command_pdal2.append("type=" + output_type)
            if percent:
                command_pdal2.append("percent=" + percent)
            if pth:
                command_pdal2.append("pth=" + pth)
            if trim:
                command_pdal2.append("trim=" + trim)

            if grass.call(command_pdal2, stdout=outdev) != 0:
                # check to see if r.in.xyz executed properly
                os.remove(tmp_xyz)
                grass.fatal(_("r.in.xyz is broken..."))

            # metadata
            empty_history = grass.tempfile()
            if empty_history is None:
                grass.fatal("Unable to create temporary files")
            f = open(empty_history, "w")
            f.close()
            grass.run_command(
                "r.support",
                map=outfile,
                source1=infile,
                description="generated by r.in.pdal",
                loadhistory=empty_history,
            )
            grass.run_command("r.support",
                              map=outfile,
                              history=os.environ["CMDLINE"])
            os.remove(empty_history)

        # Cleanup
        grass.message(_("Cleaning up..."))
        grass.run_command("g.remove",
                          flags="f",
                          type="vector",
                          name="tiles",
                          quiet=True)
        if raster_file:
            grass.run_command(
                "g.remove",
                flags="f",
                type="raster",
                pattern=raster_reference[:-1] + "*",
                quiet=True,
            )
        os.remove(tmp_file_json)
        os.remove(tmp_xyz)
        grass.del_temp_region()
Esempio n. 11
0
def main():
    # Get the options
    input = options["input"]
    input_name = input.split("@")[0]
    output = options["output"]
    method = options["method"]
    min_cat = None
    max_cat = None
    point = None
    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False

    in_info = grass.vector_info(input)
    # check for wild mixture of vector types
    if in_info["points"] > 0 and in_info["boundaries"] > 0:
        grass.fatal(_("The input vector map contains both polygons and points," " cannot handle mixed types"))

    pid = os.getpid()
    # process points via triangulation, then exit
    if in_info["points"] > 0:
        point = True
        layer = 1  # hardcoded for now
        out_temp = "{inp}_point_tmp_{pid}".format(inp=input_name, pid=pid)
        if method == "delaunay":
            grass.message(_("Processing point data (%d points found)...") % in_info["points"])
            grass.run_command("v.delaunay", input=input, layer=layer, output=out_temp, quiet=quiet)

        grass.run_command("v.db.addtable", map=out_temp, quiet=True)
        input = out_temp
        in_info = grass.vector_info(input)

    # process areas
    if in_info["areas"] == 0 and in_info["boundaries"] == 0:
        grass.fatal(_("The input vector map does not contain polygons"))

    out_type = "{inp}_type_{pid}".format(inp=input_name, pid=pid)
    input_tmp = "{inp}_tmp_{pid}".format(inp=input_name, pid=pid)
    remove_names = "%s,%s" % (out_type, input_tmp)
    grass.message(_("Processing area data (%d areas found)...") % in_info["areas"])

    try:
        grass.run_command(
            "v.category", layer="2", type="boundary", option="add", input=input, out=input_tmp, quiet=quiet
        )
    except CalledModuleError:
        grass.run_command("g.remove", flags="f", type="vector", name=input_tmp, quiet=quiet)
        grass.fatal(_("Error creating layer 2"))
    try:
        grass.run_command("v.db.addtable", map=input_tmp, layer="2", columns="left integer,right integer", quiet=quiet)
    except CalledModuleError:
        grass.run_command("g.remove", flags="f", type="vector", name=input_tmp, quiet=quiet)
        grass.fatal(_("Error creating new table for layer 2"))
    try:
        grass.run_command("v.to.db", map=input_tmp, option="sides", columns="left,right", layer="2", quiet=quiet)
    except CalledModuleError:
        grass.run_command("g.remove", flags="f", type="vector", name=input_tmp, quiet=quiet)
        grass.fatal(_("Error populating new table for layer 2"))
    try:
        grass.run_command(
            "v.type", input=input_tmp, output=out_type, from_type="boundary", to_type="line", quiet=quiet, layer="2"
        )
    except CalledModuleError:
        grass.run_command("g.remove", flags="f", type="vector", name=remove_names, quiet=quiet)
        grass.fatal(_("Error converting polygon to line"))
    report = grass.read_command("v.category", flags="g", input=out_type, option="report", quiet=quiet).split("\n")
    for r in report:
        if r.find("centroid") != -1:
            min_cat = report[0].split()[-2]
            max_cat = report[0].split()[-1]
            break
    if min_cat and max_cat:
        try:
            grass.run_command(
                "v.edit",
                map=out_type,
                tool="delete",
                type="centroid",
                layer=2,
                quiet=quiet,
                cats="{mi}-{ma}".format(mi=min_cat, ma=max_cat),
            )
        except CalledModuleError:
            grass.run_command("g.remove", flags="f", type="vector", name=remove_names, quiet=quiet)
            grass.fatal(_("Error removing centroids"))

    try:
        try:
            # TODO: fix magic numbers for layer here and there
            grass.run_command("v.db.droptable", map=out_type, layer=1, flags="f", quiet=True)
        except CalledModuleError:
            grass.run_command("g.remove", flags="f", type="vector", name=remove_names, quiet=quiet)
            grass.fatal(_("Error removing table from layer 1"))
    # TODO: when this except is happaning, it seems that never, so it seems wrong
    except:
        grass.warning(_("No table for layer %d" % 1))
    try:
        grass.run_command(
            "v.category",
            input=out_type,
            option="transfer",
            output=output,
            layer="2,1",
            quiet=quiet,
            overwrite=overwrite,
        )
    except CalledModuleError:
        grass.run_command("g.remove", flags="f", type="vector", name=remove_names, quiet=quiet)
        grass.fatal(_("Error adding categories"))
    grass.run_command("g.remove", flags="f", type="vector", name=remove_names, quiet=quiet)
    if point:
        grass.run_command("g.remove", flags="f", type="vector", name=out_temp, quiet=quiet)
Esempio n. 12
0
def main():
    """Do the main work"""

    # set numpy printing options
    np.set_printoptions(formatter={"float": lambda x: "{0:0.2f}".format(x)})

    # ==========================================================================
    # Input data
    # ==========================================================================
    # Required
    r_output = options["output"]
    r_dsm = options["input"]
    dsm_type = grass.parse_command("r.info", map=r_dsm, flags="g")["datatype"]

    # Test if DSM exist
    gfile_dsm = grass.find_file(name=r_dsm, element="cell")
    if not gfile_dsm["file"]:
        grass.fatal("Raster map <{}> not found".format(r_dsm))

    # Exposure settings
    v_source = options["sampling_points"]
    r_source = options["source"]
    source_cat = options["sourcecat"]
    r_weights = options["weights"]

    # test if source vector map exist and contains points
    if v_source:
        gfile_vsource = grass.find_file(name=v_source, element="vector")
        if not gfile_vsource["file"]:
            grass.fatal("Vector map <{}> not found".format(v_source))
        if not grass.vector.vector_info_topo(v_source, layer=1)["points"] > 0:
            grass.fatal("Vector map <{}> does not contain any points.".format(
                v_source))

    if r_source:
        gfile_rsource = grass.find_file(name=r_source, element="cell")
        if not gfile_rsource["file"]:
            grass.fatal("Raster map <{}> not found".format(r_source))

        # if source_cat is set, check that r_source is CELL
        source_datatype = grass.parse_command("r.info",
                                              map=r_source,
                                              flags="g")["datatype"]

        if source_cat != "*" and source_datatype != "CELL":
            grass.fatal(
                "The raster map <%s> must be integer (CELL type) in order to \
                use the 'sourcecat' parameter" % r_source)

    if r_weights:
        gfile_weights = grass.find_file(name=r_weights, element="cell")
        if not gfile_weights["file"]:
            grass.fatal("Raster map <{}> not found".format(r_weights))

    # Viewshed settings
    range_inp = float(options["range"])
    v_elevation = float(options["observer_elevation"])
    b_1 = float(options["b1_distance"])
    pfunction = options["function"]
    refr_coeff = float(options["refraction_coeff"])
    flagstring = ""
    if flags["r"]:
        flagstring += "r"
    if flags["c"]:
        flagstring += "c"

    # test values
    if v_elevation < 0.0:
        grass.fatal("Observer elevation must be larger than or equal to 0.0.")

    if range_inp <= 0.0 and range_inp != -1:
        grass.fatal("Exposure range must be larger than 0.0.")

    if pfunction == "Fuzzy_viewshed" and range_inp == -1:
        grass.fatal("Exposure range cannot be \
            infinity for fuzzy viewshed approch.")

    if pfunction == "Fuzzy_viewshed" and b_1 > range_inp:
        grass.fatal("Exposure range must be larger than radius around \
            the viewpoint where clarity is perfect.")

    # Sampling settings
    source_sample_density = float(options["sample_density"])
    seed = options["seed"]

    if not seed:  # if seed is not set, set it to process number
        seed = os.getpid()

    # Optional
    cores = int(options["nprocs"])
    memory = int(options["memory"])

    # ==========================================================================
    # Region settings
    # ==========================================================================
    # check that location is not in lat/long
    if grass.locn_is_latlong():
        grass.fatal("The analysis is not available for lat/long coordinates.")

    # get comp. region parameters
    reg = Region()

    # check that NSRES equals EWRES
    if abs(reg.ewres - reg.nsres) > 1e-6:
        grass.fatal("Variable north-south and east-west 2D grid resolution \
            is not supported")

    # adjust exposure range as a multiplicate of region resolution
    # if infinite, set exposure range to the max of region size
    if range_inp != -1:
        multiplicate = math.floor(range_inp / reg.nsres)
        exp_range = multiplicate * reg.nsres
    else:
        range_inf = max(reg.north - reg.south, reg.east - reg.west)
        multiplicate = math.floor(range_inf / reg.nsres)
        exp_range = multiplicate * reg.nsres

    if RasterRow("MASK", Mapset().name).exist():
        grass.warning("Current MASK is temporarily renamed.")
        unset_mask()

    # ==========================================================================
    # Random sample exposure source with target points T
    # ==========================================================================
    if v_source:
        # go for using input vector map as sampling points
        v_source_sample = v_source
        grass.verbose("Using sampling points from input vector map")

    else:
        # go for sampling

        # min. distance between samples set to half of region resolution
        # (issue in r.random.cells)
        sample_distance = reg.nsres / 2
        v_source_sample = sample_raster_with_points(
            r_source,
            source_cat,
            source_sample_density,
            sample_distance,
            "{}_rand_pts_vect".format(TEMPNAME),
            seed,
        )

    # ==========================================================================
    # Get coordinates and attributes of target points T
    # ==========================================================================
    # Prepare a list of maps to extract attributes from
    # DSM values
    attr_map_list = [r_dsm]

    if pfunction in ["Solid_angle", "Visual_magnitude"]:
        grass.verbose("Precomputing parameter maps...")

    # Precompute values A, B, C, D for solid angle function
    # using moving window [row, col]
    if pfunction == "Solid_angle":
        r_a_z = "{}_A_z".format(TEMPNAME)
        r_b_z = "{}_B_z".format(TEMPNAME)
        r_c_z = "{}_C_z".format(TEMPNAME)
        r_d_z = "{}_D_z".format(TEMPNAME)

        expr = ";".join([
            "$outmap_A = ($inmap[0, 0] + \
                          $inmap[0, -1] + \
                          $inmap[1, -1] + \
                          $inmap[1, 0]) / 4",
            "$outmap_B = ($inmap[-1, 0] + \
                          $inmap[-1, -1] + \
                          $inmap[0, -1] + \
                          $inmap[0, 0]) / 4",
            "$outmap_C = ($inmap[-1, 1] + \
                          $inmap[-1, 0] + \
                          $inmap[0, 0] + \
                          $inmap[0, 1]) / 4",
            "$outmap_D = ($inmap[0, 1] + \
                          $inmap[0, 0] + \
                          $inmap[1, 0] + \
                          $inmap[1, 1]) / 4",
        ])
        grass.mapcalc(
            expr,
            inmap=r_dsm,
            outmap_A=r_a_z,
            outmap_B=r_b_z,
            outmap_C=r_c_z,
            outmap_D=r_d_z,
            overwrite=True,
            quiet=grass.verbosity() <= 1,
        )

        attr_map_list.extend([r_a_z, r_b_z, r_c_z, r_d_z])

    # Precompute values slopes in e-w direction, n-s direction
    # as atan(dz/dx) (e-w direction), atan(dz/dy) (n-s direction)
    # using moving window [row, col]
    elif pfunction == "Visual_magnitude":

        r_slope_ew = "{}_slope_ew".format(TEMPNAME)
        r_slope_ns = "{}_slope_ns".format(TEMPNAME)

        expr = ";".join([
            "$outmap_ew = atan((sqrt(2) * $inmap[-1, 1] + \
                          2 * $inmap[0, 1] + \
                          sqrt(2) * $inmap[1, 1] - \
                          sqrt(2) * $inmap[-1, -1] - \
                          2 * $inmap[0, -1] - \
                          sqrt(2) * $inmap[1, -1]) / \
                          (8 * $w_ew))",
            "$outmap_ns = atan((sqrt(2) * $inmap[-1, -1] + \
                          2 * $inmap[-1, 0] + \
                          sqrt(2) * $inmap[-1, 1] - \
                          sqrt(2) * $inmap[1, -1] - \
                          2 * $inmap[1, 0] - \
                          sqrt(2) * $inmap[1, 1]) / \
                          (8 * $w_ns))",
        ])

        grass.mapcalc(
            expr,
            inmap=r_dsm,
            outmap_ew=r_slope_ew,
            outmap_ns=r_slope_ns,
            w_ew=reg.ewres,
            w_ns=reg.nsres,
            overwrite=True,
            quiet=grass.verbosity() <= 1,
        )

        attr_map_list.extend([r_slope_ew, r_slope_ns])

    # Use viewshed weights if provided
    if r_weights:
        attr_map_list.append(r_weights)

    # Extract attribute values
    target_pts_grass = grass.read_command(
        "r.what",
        flags="v",
        map=attr_map_list,
        points=v_source_sample,
        separator="|",
        null_value="*",
        quiet=True,
    )

    # columns to use depending on parametrization function
    usecols = list(range(0, 4 + len(attr_map_list)))
    usecols.remove(3)  # skip 3rd column - site_name

    # convert coordinates and attributes of target points T to numpy array
    target_pts_np = txt2numpy(
        target_pts_grass,
        sep="|",
        names=None,
        null_value="*",
        usecols=usecols,
        structured=False,
    )

    # if one point only - 0D array which cannot be used in iteration
    if target_pts_np.ndim == 1:
        target_pts_np = target_pts_np.reshape(1, -1)

    target_pts_np = target_pts_np[~np.isnan(target_pts_np).any(axis=1)]

    no_points = target_pts_np.shape[0]

    # if viewshed weights not set by flag - set weight to 1 for all pts
    if not r_weights:
        weights_np = np.ones((no_points, 1))
        target_pts_np = np.hstack((target_pts_np, weights_np))

    grass.debug("target_pts_np: {}".format(target_pts_np))

    # ==========================================================================
    # Calculate weighted parametrised cummulative viewshed
    # by iterating over target points T
    # ==========================================================================
    grass.verbose("Calculating partial viewsheds...")

    # Parametrisation function
    if pfunction == "Solid_angle":
        parametrise_viewshed = solid_angle_reverse

    elif pfunction == "Distance_decay":
        parametrise_viewshed = distance_decay_reverse

    elif pfunction == "Fuzzy_viewshed":
        parametrise_viewshed = fuzzy_viewshed_reverse

    elif pfunction == "Visual_magnitude":
        parametrise_viewshed = visual_magnitude_reverse

    else:
        parametrise_viewshed = binary

    # Collect variables that will be used in do_it_all() into a dictionary
    global_vars = {
        "region": reg,
        "range": exp_range,
        "param_viewshed": parametrise_viewshed,
        "observer_elevation": v_elevation,
        "b_1": b_1,
        "memory": memory,
        "refr_coeff": refr_coeff,
        "flagstring": flagstring,
        "r_dsm": r_dsm,
        "dsm_type": dsm_type,
        "cores": cores,
        "tempname": TEMPNAME,
    }

    # Split target points to chunks for each core
    target_pnts = np.array_split(target_pts_np, cores)

    # Combine each chunk with dictionary
    combo = list(zip(itertools.repeat(global_vars), target_pnts))

    # Calculate partial cummulative viewshed
    with Pool(cores) as pool:
        np_sum = pool.starmap(do_it_all, combo)
        pool.close()
        pool.join()

    # We should probably use nansum here?
    all_nan = np.all(np.isnan(np_sum), axis=0)
    np_sum = np.nansum(np_sum, axis=0, dtype=np.single)
    np_sum[all_nan] = np.nan

    grass.verbose("Writing final result and cleaning up...")

    # Restore original computational region
    reg.read()
    reg.set_current()
    reg.set_raster_region()

    # Convert numpy array of cummulative viewshed to raster
    numpy2raster(np_sum, mtype="FCELL", rastname=r_output, overwrite=True)

    # Remove temporary files and reset mask if needed
    cleanup()

    # Set raster history to output raster
    grass.raster_history(r_output, overwrite=True)
    grass.run_command(
        "r.support",
        overwrite=True,
        map=r_output,
        title="Visual exposure index as {}".format(pfunction.replace("_",
                                                                     " ")),
        description="generated by r.viewshed.exposure",
        units="Index value",
        quiet=True,
    )
Esempio n. 13
0
def main():
    raster = options["raster"]
    maskcats = options["maskcats"]
    vector = options["vector"]
    layer = options["layer"]
    cats = options["cats"]
    where = options["where"]
    remove = flags["r"]
    invert = flags["i"]

    if not remove and not raster and not vector:
        grass.fatal(
            _("Either parameter <raster> or parameter <vector> is required"))

    mapset = grass.gisenv()["MAPSET"]
    exists = bool(
        grass.find_file("MASK", element="cell", mapset=mapset)["file"])

    if remove:
        # -> remove
        if exists:
            if sys.platform == "win32":
                grass.run_command("g.remove",
                                  flags="if",
                                  quiet=True,
                                  type="raster",
                                  name="MASK")
            else:
                grass.run_command("g.remove",
                                  flags="f",
                                  quiet=True,
                                  type="raster",
                                  name="MASK")
            grass.message(_("Raster MASK removed"))
        else:
            grass.fatal(_("No existing MASK to remove"))
    else:
        # -> create
        if exists:
            if not grass.overwrite():
                grass.fatal(
                    _("MASK already found in current mapset. Delete first or overwrite."
                      ))
            else:
                grass.warning(_("MASK already exists and will be overwritten"))
                grass.run_command("g.remove",
                                  flags="f",
                                  quiet=True,
                                  type="raster",
                                  name="MASK")

        if raster:
            # check if input raster exists
            if not grass.find_file(raster)["file"]:
                grass.fatal(_("Raster map <%s> not found") % raster)

            if maskcats != "*" and not remove:
                if grass.raster_info(raster)["datatype"] != "CELL":
                    grass.fatal(
                        _("The raster map <%s> must be integer (CELL type) "
                          " in order to use the 'maskcats' parameter") %
                        raster)

            p = grass.feed_command("r.reclass",
                                   input=raster,
                                   output="MASK",
                                   overwrite=True,
                                   rules="-")
            res = "%s = 1" % maskcats
            p.stdin.write(encode(res))
            p.stdin.close()
            p.wait()
        elif vector:
            vector_name = grass.find_file(vector, "vector")["fullname"]
            if not vector_name:
                grass.fatal(_("Vector map <%s> not found") % vector)

            # parser bug?
            if len(cats) == 0:
                cats = None
            if len(where) == 0:
                where = None

            if grass.vector_info_topo(vector_name)["areas"] < 1:
                grass.warning(
                    _("No area found in vector map <%s>. "
                      "Creating a convex hull for MASK.") % vector_name)
                global tmp_hull
                tmp_hull = "tmp_hull_%d" % os.getpid()
                to_rast_input = tmp_hull
                # force 'flat' convex hull for 3D vector maps
                try:
                    grass.run_command(
                        "v.hull",
                        flags="f",
                        quiet=True,
                        input=vector_name,
                        output=tmp_hull,
                        layer=layer,
                        cats=cats,
                        where=where,
                    )
                except CalledModuleError:
                    grass.fatal(
                        _("Unable to create a convex hull for vector map <%s>")
                        % vector_name)
            else:
                to_rast_input = vector_name

            env = os.environ.copy()
            if grass.verbosity() > 1:
                env["GRASS_VERBOSE"] = "1"
            grass.run_command(
                "v.to.rast",
                input=to_rast_input,
                layer=layer,
                output="MASK",
                use="val",
                val="1",
                type="area",
                cats=cats,
                where=where,
                env=env,
            )

        if invert:
            global tmp
            tmp = "r_mask_%d" % os.getpid()
            grass.run_command("g.rename", raster=("MASK", tmp), quiet=True)
            grass.message(_("Creating inverted raster MASK..."))
            grass.mapcalc("MASK = if(isnull($tmp), 1, null())", tmp=tmp)
            grass.verbose(_("Inverted raster MASK created"))
        else:
            grass.verbose(_("Raster MASK created"))

        grass.message(
            _("All subsequent raster operations will be limited to "
              "the MASK area. Removing or renaming raster map named "
              "'MASK' will restore raster operations to normal."))
Esempio n. 14
0
def main():
    vector = options["map"]
    table = options["table"]
    layer = options["layer"]
    columns = options["columns"]
    key = options["key"]

    # does map exist in CURRENT mapset?
    mapset = grass.gisenv()["MAPSET"]
    if not grass.find_file(vector, element="vector", mapset=mapset)["file"]:
        grass.fatal(_("Vector map <%s> not found in current mapset") % vector)

    map_name = vector.split("@")[0]

    if not table:
        if layer == "1":
            grass.verbose(
                _("Using vector map name as table name: <%s>") % map_name)
            table = map_name
        else:
            # to avoid tables with identical names on higher layers
            table = "%s_%s" % (map_name, layer)
            grass.verbose(
                _("Using vector map name extended by layer number as table name: <%s>"
                  ) % table)
    else:
        grass.verbose(_("Using user specified table name: %s") % table)

    # check if DB parameters are set, and if not set them.
    grass.run_command("db.connect", flags="c", quiet=True)
    grass.verbose(
        _("Creating new DB connection based on default mapset settings..."))
    kv = grass.db_connection()
    database = kv["database"]
    driver = kv["driver"]
    schema = kv["schema"]

    database2 = database.replace("$MAP/", map_name + "/")

    # maybe there is already a table linked to the selected layer?
    nuldev = open(os.devnull, "w")
    try:
        grass.vector_db(map_name, stderr=nuldev)[int(layer)]
        grass.fatal(_("There is already a table linked to layer <%s>") % layer)
    except KeyError:
        pass

    # maybe there is already a table with that name?
    tables = grass.read_command("db.tables",
                                flags="p",
                                database=database2,
                                driver=driver,
                                stderr=nuldev)
    tables = decode(tables)

    if table not in tables.splitlines():
        colnames = []
        column_def = []
        if columns:
            column_def = []
            for x in " ".join(columns.split()).split(","):
                colname = x.lower().split()[0]
                if colname in colnames:
                    grass.fatal(
                        _("Duplicate column name '%s' not allowed") % colname)
                colnames.append(colname)
                column_def.append(x)

        # if not existing, create it:
        if key not in colnames:
            column_def.insert(0, "%s integer" % key)
        column_def = ",".join(column_def)

        grass.verbose(_("Creating table with columns (%s)...") % column_def)

        sql = "CREATE TABLE %s (%s)" % (table, column_def)
        try:
            grass.run_command("db.execute",
                              database=database2,
                              driver=driver,
                              sql=sql)
        except CalledModuleError:
            grass.fatal(_("Unable to create table <%s>") % table)

    # connect the map to the DB:
    if schema:
        table = "{schema}.{table}".format(schema=schema, table=table)
    grass.verbose(_("Connecting new table to vector map <%s>...") % map_name)
    grass.run_command(
        "v.db.connect",
        quiet=True,
        map=map_name,
        database=database,
        driver=driver,
        layer=layer,
        table=table,
        key=key,
    )

    # finally we have to add cats into the attribute DB to make
    # modules such as v.what.rast happy: (creates new row for each
    # vector line):
    try:
        grass.run_command(
            "v.to.db",
            overwrite=True,
            map=map_name,
            layer=layer,
            option="cat",
            column=key,
            qlayer=layer,
        )
    except CalledModuleError:
        # remove link
        grass.run_command("v.db.connect",
                          quiet=True,
                          flags="d",
                          map=map_name,
                          layer=layer)
        return 1

    grass.verbose(_("Current attribute table links:"))
    if grass.verbosity() > 2:
        grass.run_command("v.db.connect", flags="p", map=map_name)

    # write cmd history:
    grass.vector_history(map_name)

    return 0
Esempio n. 15
0
def main():
    '''
    begin main

    '''

    global soillossbare
    global outprefix
    global flowaccmethod
    global elevation
    global kfactor
    global rfactor
    global resolution

    global flowacc
    global fieldblock
    global fieldblockvect

    global flag_r

    global quiet
    global options
    global flags


    soillossbare = options['soillossbare']

    outprefix = soillossbare + '_'
    flowaccmethod = options['flowaccmethod']
    flowacc = options['flowacc']
    elevation = options['elevation']
    kfactor = options['kfactor']
    rfactor = options['rfactor']
    resolution = options['resolution']
    #fieldblock = None
    fieldblock = options['fieldblock']
    fieldblockvect = options['map']

    flag_r = flags['r']     # remove temp maps

    quiet = True
    if gscript.verbosity() > 2:
        quiet=False

    gscript.run_command("g.region", flags="a", res=resolution)

    if flowacc:
        gscript.info("Using flowaccumulation from input raster map %s ..." %flowacc)
        ruslealg = rusle_base()

    elif flowaccmethod == "r.terraflow":
        gscript.info("Using flowaccumulation from module r.terraflow (MFD) ...")
        ruslealg = rusle_terraflow()

    elif flowaccmethod == "r.flow":
        gscript.info("Using flowaccumulation from module r.flow (SFD) ...")
        ruslealg = rusle_flow()

    elif flowaccmethod == "r.watershed":
        gscript.info("Using flowaccumulation from module r.watershed (MFD) ...")
        ruslealg = rusle_watershed()

    p = ruslealg.rusle()

    if flag_r:
        #remove = ruslealg.removeTempRasters() # already in destructor
        pass

    return
Esempio n. 16
0
def main():
    """
    Superposition of analytical solutions in gFlex for flexural isostasy in
    GRASS GIS
    """

    options, flags = grass.parser()
    # if just interface description is requested, it will not get to this point
    # so gflex will not be needed

    # GFLEX
    # try to import gflex only after we know that
    # we will actually do the computation
    try:
        import gflex
    except:
        print("")
        print("MODULE IMPORT ERROR.")
        print(
            "In order to run r.flexure or g.flexure, you must download and install"
        )
        print("gFlex. The most recent development version is available from")
        print("https://github.com/awickert/gFlex")
        print("Installation instructions are available on the page.")
        grass.fatal("Software dependency must be installed.")

    ##########
    # SET-UP #
    ##########

    # This code is for 2D flexural isostasy
    flex = gflex.F2D()
    # And show that it is coming from GRASS GIS
    flex.grass = True

    # Method
    flex.Method = 'SAS_NG'

    # Parameters that are often changed for the solution
    ######################################################

    # x, y, q
    flex.x, flex.y = get_points_xy(options['input'])
    # xw, yw: gridded output
    if len(
            grass.parse_command('g.list',
                                type='vect',
                                pattern=options['output'])):
        if not grass.overwrite():
            grass.fatal("Vector map '" + options['output'] +
                        "' already exists. Use '--o' to overwrite.")
    # Just check raster at the same time if it exists
    if len(
            grass.parse_command('g.list',
                                type='rast',
                                pattern=options['raster_output'])):
        if not grass.overwrite():
            grass.fatal("Raster map '" + options['raster_output'] +
                        "' already exists. Use '--o' to overwrite.")
    grass.run_command('v.mkgrid',
                      map=options['output'],
                      type='point',
                      overwrite=grass.overwrite(),
                      quiet=True)
    grass.run_command('v.db.addcolumn',
                      map=options['output'],
                      columns='w double precision',
                      quiet=True)
    flex.xw, flex.yw = get_points_xy(
        options['output'])  # gridded output coordinates
    vect_db = grass.vector_db_select(options['input'])
    col_names = np.array(vect_db['columns'])
    q_col = (col_names == options['column'])
    if np.sum(q_col):
        col_values = np.array(list(vect_db['values'].values())).astype(float)
        flex.q = col_values[:, q_col].squeeze(
        )  # Make it 1D for consistency w/ x, y
    else:
        grass.fatal("provided column name, " + options['column'] +
                    " does not match\nany column in " + options['q0'] + ".")
    # Elastic thickness
    flex.Te = float(options['te'])
    if options['te_units'] == 'km':
        flex.Te *= 1000
    elif options['te_units'] == 'm':
        pass
    else:
        grass.fatal(
            "Inappropriate te_units. How? Options should be limited by GRASS.")
    flex.rho_fill = float(options['rho_fill'])

    # Parameters that often stay at their default values
    ######################################################
    flex.g = float(options['g'])
    flex.E = float(options['ym']
                   )  # Can't just use "E" because reserved for "east", I think
    flex.nu = float(options['nu'])
    flex.rho_m = float(options['rho_m'])

    # Set verbosity
    if grass.verbosity() >= 2:
        flex.Verbose = True
    if grass.verbosity() >= 3:
        flex.Debug = True
    elif grass.verbosity() == 0:
        flex.Quiet = True

    # Check if lat/lon and let user know if verbosity is True
    if grass.region_env()[6] == '3':
        flex.latlon = True
        flex.PlanetaryRadius = float(
            grass.parse_command('g.proj', flags='j')['+a'])
        if flex.Verbose:
            print("Latitude/longitude grid.")
            print("Based on r_Earth = 6371 km")
            print(
                "Computing distances between load points using great circle paths"
            )

    ##########
    # SOLVE! #
    ##########

    flex.initialize()
    flex.run()
    flex.finalize()

    # Now to use lower-level GRASS vector commands to work with the database
    # table and update its entries
    # See for help:
    # http://nbviewer.ipython.org/github/zarch/workshop-pygrass/blob/master/02_Vector.ipynb
    w = vector.VectorTopo(options['output'])
    w.open('rw')  # Get ready to read and write
    wdb = w.dblinks[0]
    wtable = wdb.table()
    col = int((np.array(
        wtable.columns.names()) == 'w').nonzero()[0])  # update this column
    for i in range(1, len(w) + 1):
        # ignoring 1st column: assuming it will be category (always true here)
        wnewvalues = list(w[i].attrs.values())[1:col] + tuple(
            [flex.w[i - 1]]) + list(w[i].attrs.values())[col + 1:]
        wtable.update(key=i, values=wnewvalues)
    wtable.conn.commit()  # Save this
    w.close(build=False)  # don't build here b/c it is always verbose
    grass.run_command('v.build', map=options['output'], quiet=True)

    # And raster export
    # "w" vector defined by raster resolution, so can do direct v.to.rast
    # though if this option isn't selected, the user can do a finer-grained
    # interpolation, which shouldn't introduce much error so long as these
    # outputs are spaced at << 1 flexural wavelength.
    if options['raster_output']:
        grass.run_command('v.to.rast',
                          input=options['output'],
                          output=options['raster_output'],
                          use='attr',
                          attribute_column='w',
                          type='point',
                          overwrite=grass.overwrite(),
                          quiet=True)
        # And create a nice colormap!
        grass.run_command('r.colors',
                          map=options['raster_output'],
                          color='differences',
                          quiet=True)
Esempio n. 17
0
def main():
    # Get the options
    input = options["input"]
    input_name = input.split('@')[0]
    output = options["output"]
    method = options["method"]
    min_cat = None
    max_cat = None
    point = None
    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False

    in_info = grass.vector_info(input)
    # check for wild mixture of vector types
    if in_info['points'] > 0 and in_info['boundaries'] > 0:
        grass.fatal(_("The input vector map contains both polygons and points,"
                      " cannot handle mixed types"))

    pid = os.getpid()
    # process points via triangulation, then exit
    if in_info['points'] > 0:
        point = True
        layer = 1  # hardcoded for now
        out_temp = '{inp}_point_tmp_{pid}'.format(inp=input_name, pid=pid)
        if method == 'delaunay':
            grass.message(_("Processing point data (%d points found)...") % in_info['points'])
            grass.run_command('v.delaunay', input=input, layer=layer,
                              output=out_temp, quiet=quiet)

        grass.run_command('v.db.addtable', map=out_temp, quiet=True)
        input = out_temp
        in_info = grass.vector_info(input)

    # process areas
    if in_info['areas'] == 0 and in_info['boundaries'] == 0:
        grass.fatal(_("The input vector map does not contain polygons"))

    out_type = '{inp}_type_{pid}'.format(inp=input_name, pid=pid)
    input_tmp = '{inp}_tmp_{pid}'.format(inp=input_name, pid=pid)
    remove_names = "%s,%s" % (out_type, input_tmp)
    grass.message(_("Processing area data (%d areas found)...") % in_info['areas'])

    try:
        grass.run_command('v.category', layer="2", type='boundary',
                          option='add', input=input, out=input_tmp,
                          quiet=quiet)
    except CalledModuleError:
        grass.run_command('g.remove', flags='f', type='vector',
                          name=input_tmp, quiet=quiet)
        grass.fatal(_("Error creating layer 2"))
    try:
        grass.run_command('v.db.addtable', map=input_tmp, layer="2",
                          quiet=quiet)
    except CalledModuleError:
        grass.run_command('g.remove', flags='f', type='vector',
                          name=input_tmp, quiet=quiet)
        grass.fatal(_("Error creating new table for layer 2"))
    try:
        grass.run_command('v.to.db', map=input_tmp, option="sides",
                          columns="left,right", layer="2", quiet=quiet)
    except CalledModuleError:
        grass.run_command('g.remove', flags='f', type='vector',
                          name=input_tmp, quiet=quiet)
        grass.fatal(_("Error populating new table for layer 2"))
    try:
        grass.run_command('v.type', input=input_tmp, output=out_type,
                          from_type='boundary', to_type='line',
                          quiet=quiet, layer="2")
    except CalledModuleError:
        grass.run_command('g.remove', flags='f', type='vector',
                          name=remove_names, quiet=quiet)
        grass.fatal(_("Error converting polygon to line"))
    report = grass.read_command('v.category', flags='g', input=out_type,
                                option='report', quiet=quiet)
    report = decode(report).split('\n')
    for r in report:
        if r.find('centroid') != -1:
            min_cat = report[0].split()[-2]
            max_cat = report[0].split()[-1]
            break
    if min_cat and max_cat:
        try:
            grass.run_command('v.edit', map=out_type, tool='delete',
                              type='centroid', layer=2, quiet=quiet,
                              cats='{mi}-{ma}'.format(mi=min_cat, ma=max_cat))
        except CalledModuleError:
            grass.run_command('g.remove', flags='f', type='vector',
                              name=remove_names, quiet=quiet)
            grass.fatal(_("Error removing centroids"))

    try:
        try:
            # TODO: fix magic numbers for layer here and there
            grass.run_command('v.db.droptable', map=out_type, layer=1,
                              flags='f', quiet=True)
        except CalledModuleError:
            grass.run_command('g.remove', flags='f', type='vector',
                              name=remove_names, quiet=quiet)
            grass.fatal(_("Error removing table from layer 1"))
    # TODO: when this except is happaning, it seems that never, so it seems wrong
    except:
        grass.warning(_("No table for layer %d" % 1))
    try:
        grass.run_command('v.category', input=out_type, option='transfer',
                          output=output, layer="2,1", quiet=quiet,
                          overwrite=overwrite)
    except CalledModuleError:
        grass.run_command('g.remove', flags='f', type='vector',
                          name=remove_names, quiet=quiet)
        grass.fatal(_("Error adding categories"))
    grass.run_command('g.remove', flags='f', type='vector',
                      name=remove_names, quiet=quiet)
    if point:
        grass.run_command('g.remove', flags='f', type='vector',
                          name=out_temp, quiet=quiet)
Esempio n. 18
0
def main():
    # Get the options
    input = options["input"]
    output = options["output"]
    strds = options["strds"]
    tempwhere = options["t_where"]
    where = options["where"]
    methods = options["method"]
    percentile = options["percentile"]

    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False

    if where == "" or where == " " or where == "\n":
        where = None

    # Check the number of sample strds and the number of columns
    strds_names = strds.split(",")

    # Make sure the temporal database exists
    tgis.init()
    # We need a database interface
    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()

    samples = []

    first_strds = tgis.open_old_stds(strds_names[0], "strds", dbif)
    # Single space time raster dataset
    if len(strds_names) == 1:
        granu = first_strds.get_granularity()
        rows = first_strds.get_registered_maps(
            "name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
        if not rows:
            dbif.close()
            grass.fatal(
                _("Space time raster dataset <%s> is empty") %
                first_strds.get_id())

        for row in rows:
            start = row["start_time"]
            end = row["end_time"]
            raster_maps = [
                row["name"] + "@" + row["mapset"],
            ]

            s = Sample(start, end, raster_maps, first_strds.get_name(), granu)
            samples.append(s)
    else:
        # Multiple space time raster datasets
        for name in strds_names[1:]:
            dataset = tgis.open_old_stds(name, "strds", dbif)
            if dataset.get_temporal_type() != first_strds.get_temporal_type():
                grass.fatal(
                    _(
                        "Temporal type of space time raster "
                        "datasets must be equal\n<%(a)s> of type "
                        "%(type_a)s do not match <%(b)s> of type "
                        "%(type_b)s" % {
                            "a": first_strds.get_id(),
                            "type_a": first_strds.get_temporal_type(),
                            "b": dataset.get_id(),
                            "type_b": dataset.get_temporal_type()
                        }))

        mapmatrizes = tgis.sample_stds_by_stds_topology(
            "strds", "strds", strds_names, strds_names[0], False, None,
            "equal", False, False)
        #TODO check granularity for multiple STRDS
        for i in range(len(mapmatrizes[0])):
            isvalid = True
            mapname_list = []
            for mapmatrix in mapmatrizes:

                entry = mapmatrix[i]

                if entry["samples"]:
                    sample = entry["samples"][0]
                    name = sample.get_id()
                    if name is None:
                        isvalid = False
                        break
                    else:
                        mapname_list.append(name)

            if isvalid:
                entry = mapmatrizes[0][i]
                map = entry["granule"]

                start, end = map.get_temporal_extent_as_tuple()
                s = Sample(start, end, mapname_list, name)
                samples.append(s)
    # Get the layer and database connections of the input vector
    if where:
        try:
            grass.run_command("v.extract",
                              input=input,
                              where=where,
                              output=output)
        except CalledModuleError:
            dbif.close()
            grass.fatal(
                _("Unable to run v.extract for vector map"
                  " <%s> and where <%s>") % (input, where))
    else:
        gcopy(input, output, 'vector')

    msgr = Messenger()
    perc_curr = 0
    perc_tot = len(samples)
    pymap = Vector(output)
    try:
        pymap.open('r')
    except:
        dbif.close()
        grass.fatal(_("Unable to create vector map <%s>" % output))
    pymap.close()

    for sample in samples:
        raster_names = sample.raster_names
        # Call v.what.rast for each raster map
        for name in raster_names:
            day = sample.printDay()
            column_name = "%s_%s" % (sample.strds_name, day)
            try:
                grass.run_command("v.rast.stats",
                                  map=output,
                                  raster=name,
                                  column=column_name,
                                  method=methods,
                                  percentile=percentile,
                                  quiet=quiet,
                                  overwrite=overwrite)
            except CalledModuleError:
                dbif.close()
                grass.fatal(
                    _("Unable to run v.what.rast for vector map"
                      " <%s> and raster map <%s>") % (output, name))

        msgr.percent(perc_curr, perc_tot, 1)
        perc_curr += 1

    dbif.close()
Esempio n. 19
0
def main():
    vector = options['map']
    table = options['table']
    layer = options['layer']
    columns = options['columns']
    key = options['key']

    # does map exist in CURRENT mapset?
    mapset = grass.gisenv()['MAPSET']
    if not grass.find_file(vector, element='vector', mapset=mapset)['file']:
        grass.fatal(_("Vector map <%s> not found in current mapset") % vector)

    map_name = vector.split('@')[0]

    if not table:
        if layer == '1':
            grass.verbose(_("Using vector map name as table name: <%s>") % map_name)
            table = map_name
        else:
            # to avoid tables with identical names on higher layers
            table = "%s_%s" % (map_name, layer)
            grass.verbose(
                _("Using vector map name extended by layer number as table name: <%s>") %
                table)
    else:
        grass.verbose(_("Using user specified table name: %s") % table)

    # check if DB parameters are set, and if not set them.
    grass.run_command('db.connect', flags='c')
    grass.verbose(_("Creating new DB connection based on default mapset settings..."))
    kv = grass.db_connection()
    database = kv['database']
    driver = kv['driver']
    schema = kv['schema']

    # maybe there is already a table linked to the selected layer?
    nuldev = file(os.devnull, 'w')
    try:
        grass.vector_db(map_name, stderr=nuldev)[int(layer)]
        grass.fatal(_("There is already a table linked to layer <%s>") % layer)
    except KeyError:
        pass

    # maybe there is already a table with that name?
    tables = grass.read_command('db.tables', flags='p', database=database, driver=driver,
                                stderr=nuldev)

    if not table in tables.splitlines():
        if columns:
            column_def = [x.strip().lower() for x in columns.strip().split(',')]
        else:
            column_def = []

        # if not existing, create it:
        column_def_key = "%s integer" % key
        if column_def_key not in column_def:
            column_def.insert(0, column_def_key)
        column_def = ','.join(column_def)

        grass.verbose(_("Creating table with columns (%s)...") % column_def)

        sql = "CREATE TABLE %s (%s)" % (table, column_def)
        try:
            grass.run_command('db.execute',
                              database=database, driver=driver, sql=sql)
        except CalledModuleError:
            grass.fatal(_("Unable to create table <%s>") % table)

    # connect the map to the DB:
    if schema:
        table = '{schema}.{table}'.format(schema=schema, table=table)
    grass.run_command('v.db.connect', quiet=True,
                      map=map_name, database=database, driver=driver,
                      layer=layer, table=table, key=key)

    # finally we have to add cats into the attribute DB to make modules such as v.what.rast happy:
    # (creates new row for each vector line):
    grass.run_command('v.to.db', map=map_name, layer=layer,
                      option='cat', column=key, qlayer=layer)

    grass.verbose(_("Current attribute table links:"))
    if grass.verbosity() > 2:
        grass.run_command('v.db.connect', flags='p', map=map_name)

    # write cmd history:
    grass.vector_history(map_name)

    return 0
Esempio n. 20
0
def main():
    '''
    begin main

    '''


    global soillossbare
    global outprefix
    global flowaccmethod
    global elevation
    global kfactor
    global rfactor
    global resolution

    global flowacc
    global fieldblock
    global fieldblockvect

    global flag_r
    global quiet
    global options
    global flags


    soillossbare = options['soillossbare']
    outprefix = soillossbare + '_'
    flowaccmethod = options['flowaccmethod']
    flowacc = options['flowacc']
    elevation = options['elevation']
    kfactor = options['kfactor']
    rfactor = options['rfactor']
    resolution = options['resolution']
    #fieldblock = None
    fieldblock = options['fieldblock']
    fieldblockvect = options['map']

    flag_r = flags['r']     # remove temp maps



    quiet = True
    if g.verbosity() > 2:
        quiet = False

    g.run_command("g.region", flags="a", res=resolution)

    if flowacc:
        g.info("Using flowaccumulation from input raster map %s ..." % flowacc)
        ruslealg = rusle_base()

    elif flowaccmethod == "r.terraflow":
        g.info("Using flowaccumulation from module r.terraflow (MFD) ...")
        ruslealg = rusle_terraflow()

    elif flowaccmethod == "r.flow":
        g.info("Using flowaccumulation from module r.flow (SFD) ...")
        ruslealg = rusle_flow()

    elif flowaccmethod == "r.watershed":
        g.info("Using flowaccumulation from module r.watershed (MFD) ...")
        ruslealg = rusle_watershed()

    p = ruslealg.rusle()

    if flag_r:
        remove = ruslealg.removeTempRasters()

    return
Esempio n. 21
0
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)
Esempio n. 22
0
def main():
    # parameters
    infile = options['input']
    raster_reference = options['raster_reference']
    raster_file = options['raster_file']
    outfiles = options['output']
    resolution = options['resolution']
    methods = options['method']
    zrange = options['zrange']
    zscale = options['zscale']
    output_type = options['type']
    percent = options['percent']
    pth = options['pth']
    trim = options['trim']
    footprint = options['footprint']
    # flags
    scan = flags['s']
    shell_script_style = flags['g']

    if len(outfiles.split(",")) != len(methods.split(",")):
        grass.fatal(
            _("Number of outputs <%s> does not match number of methods <%s>" %
              (outfiles, methods)))

    # overwrite auf true setzen
    os.environ['GRASS_OVERWRITE'] = '1'

    # to hide non-error messages from subprocesses
    if grass.verbosity() <= 2:
        outdev = open(os.devnull, 'w')
    else:
        outdev = sys.stdout

    # use temporary region
    grass.use_temp_region()

    n, s, w, e, t, b = scan_extent(infile)

    # scan -s or shell_script_style -g:
    if scan:
        if not shell_script_style:
            grass.message(
                _("north: %s\nsouth: %s\nwest: %s\neast: %s\ntop: %s\nbottom: %s"
                  ) % (n, s, w, e, t, b))
        else:
            # shell script style: print to stdout
            print("n=%s s=%s w=%s e=%s t=%s b=%s" % (n, s, w, e, t, b))
    elif footprint:
        footprint_to_vectormap(infile, footprint)
    else:
        # get region with pdal
        #footprint_to_vectormap(infile, 'tiles')

        if raster_file:
            raster_reference = 'img' + str(os.getpid())
            grass.run_command('r.external',
                              input=raster_file,
                              flags='o',
                              output=raster_reference)
            result = grass.find_file(name=raster_reference, element='raster')
            if result[u'fullname'] == u'':
                raster_reference = raster_reference + '.1'
        # option 1: set region to extent of tiles while precisely aligning pixel
        # geometry to raster_reference (including both raster_reference and raster_file)
        if raster_reference:
            grass.run_command('g.region',
                              n=n,
                              s=s,
                              e=e,
                              w=w,
                              flags='g',
                              align=raster_reference)
        else:
            # option 2: change raster resolution to final resolution while best
            # effort aligning to pixel geometry
            grass.run_command('g.region',
                              n=n,
                              s=s,
                              e=e,
                              w=w,
                              flags='ap',
                              res=resolution)
        # generate PDAL pipeline
        # . pdal pipline laz2json (STDOUT) | r.in.xyz
        bn = os.path.basename(infile)
        infile_format = bn.split('.')[-1]
        # format_reader from https://pdal.io/stages/readers.html
        format_reader = ''
        if infile_format.lower() == 'laz' or infile_format.lower() == 'las':
            format_reader = 'readers.las'
        # pts: not tested
        elif infile_format.lower() == 'pts':
            format_reader = 'readers.pts'
        else:
            grass.run_command('g.remove',
                              flags='f',
                              type='vector',
                              name='tiles',
                              quiet=True)
            grass.fatal(_("Format .%s is not supported.." % infile_format))

        grass.message(_("Converting input file <%s>...") % infile)
        tmp_file_json = 'tmp_file_json_' + str(os.getpid())

        data = {}
        data['pipeline'] = []
        data['pipeline'].append({'type': format_reader, 'filename': infile})
        data['pipeline'].append({
            'type': 'writers.text',
            'format': 'csv',
            'order': 'X,Y,Z',
            'keep_unspecified': 'false',
            'filename': 'STDOUT',
            'quote_header': 'false'
        })
        with open(tmp_file_json, 'w') as f:
            json.dump(data, f)

        tmp_xyz = grass.tempfile()
        if tmp_xyz is None:
            grass.fatal("Unable to create temporary files")
        command_pdal1 = options['pdal_cmd'].split(' ')
        if options['pdal_cmd'] != 'pdal':
            v_index = None
            cmd_entries = options['pdal_cmd'].split(' ')
            for cmd_entry, num in zip(cmd_entries, range(len(cmd_entries))):
                if cmd_entry == '-v':
                    v_index = num
                    break
            mnt_vol = cmd_entries[v_index + 1].split(':')[1]
            tmp_file_json2 = os.path.join(mnt_vol, tmp_file_json)
        else:
            tmp_file_json2 = tmp_file_json
        command_pdal1.extend(['pipeline', '--input', tmp_file_json2])

        fh = open(tmp_xyz, 'wb')
        if grass.call(command_pdal1, stdout=fh) != 0:
            fh.close()
            # check to see if pdal pipeline executed properly
            print(command_pdal1)
            grass.fatal(_("pdal pipeline is broken..."))
        else:
            fh.close()

        for i in range(len(methods.split(","))):
            method = methods.split(",")[i]
            outfile = outfiles.split(",")[i]

            grass.message(_("Generating output raster map <%s>...") % outfile)

            command_pdal2 = [
                'r.in.xyz', 'input=' + tmp_xyz, 'output=' + outfile, 'skip=1',
                'separator=comma', 'method=' + method
            ]

            if zrange:
                command_pdal2.append('zrange=' + zrange)
            if zscale:
                command_pdal2.append('zscale=' + zscale)
            if output_type:
                command_pdal2.append('type=' + output_type)
            if percent:
                command_pdal2.append('percent=' + percent)
            if pth:
                command_pdal2.append('pth=' + pth)
            if trim:
                command_pdal2.append('trim=' + trim)

            if grass.call(command_pdal2, stdout=outdev) != 0:
                # check to see if r.in.xyz executed properly
                os.remove(tmp_xyz)
                grass.fatal(_("r.in.xyz is broken..."))

            # metadata
            empty_history = grass.tempfile()
            if empty_history is None:
                grass.fatal("Unable to create temporary files")
            f = open(empty_history, 'w')
            f.close()
            grass.run_command('r.support',
                              map=outfile,
                              source1=infile,
                              description='generated by r.in.pdal',
                              loadhistory=empty_history)
            grass.run_command('r.support',
                              map=outfile,
                              history=os.environ['CMDLINE'])
            os.remove(empty_history)

        # Cleanup
        grass.message(_("Cleaning up..."))
        grass.run_command('g.remove',
                          flags='f',
                          type='vector',
                          name='tiles',
                          quiet=True)
        if raster_file:
            grass.run_command('g.remove',
                              flags='f',
                              type='raster',
                              pattern=raster_reference[:-1] + '*',
                              quiet=True)
        os.remove(tmp_file_json)
        os.remove(tmp_xyz)
        grass.del_temp_region()
Esempio n. 23
0
def main(options, flags):
    configuration = options['configuration']    
    output = options['output']
    betriebid = options['betriebid']
    task = options['task']
    username = options['username']
    password = options['password']
    maxsoilloss = options['maxsoilloss']
        
    flag_b = flags['b'] #use newly defined barriers
    flag_g = flags['g'] #don't set region according to parcel data
    flag_i = flags['i'] #force reimport of base data
    flag_n = flags['n'] #don't import anything (offline mode)
    flag_c = flags['c'] #don't copy results to output raster map
    flag_s = flags['s'] #calculate statistics for results
    flag_v = flags['v'] #verbose
    flag_o = flags['o'] #overwrite

    
    #os.path.expanduser(path)
    env = gscript.parse_command("g.gisenv", flags='s')
    gisdbase = env['GISDBASE'].strip(";'").lstrip("'")
    location = env['LOCATION_NAME'].strip(";'").lstrip("'")
    mapset = env['MAPSET'].strip(";'").lstrip("'")
    
    try:
        gislock = os.path.join(gisdbase,location,mapset,'.gislock')
        os.remove(gislock)
    except OSError:
        pass

    gcommands = []
    #gcommands += ['g.version']
    #gcommands += ['g.mapset %s --q' %mapset]
    gcommands += ['db.connect -d --q']
    if flag_v:
        gcommands += ['g.gisenv']

    gexecute = 'g.gui.soilloss output='+output +' betriebid=' + betriebid \
        + ' task=' + task + ' maxsoilloss=' + maxsoilloss + ' username='******' password='******' -b'
    if flag_s: gexecute += ' -s'
    if flag_i: gexecute += ' -i'
    if flag_n: gexecute += ' -n'
    if flag_o: gexecute += ' --o'
    if gscript.overwrite(): gexecute += ' --o'
    if gscript.verbosity() > 2 or flag_v: gexecute += ' --v'
    else: gexecute += ' --q'

    gcommands += [gexecute]
    gcommands += ['exit']
    
    batchjob = os.getenv('HOME') + '/.qgis2/processing/grass7_batch_job.sh'
    with open(batchjob, 'w') as f:
        for s in gcommands:
            print s
            f.write(s + '\n')
        f.close()
        
    rclines = []
    rclines += ['MAPSET: ' + mapset]
    rclines += ['GISDBASE: ' + gisdbase]
    rclines += ['LOCATION_NAME: ' +  location]
    rclines += ['GUI: text']
    
    rcfile = os.getenv('HOME') + '/.grass7/rc'
    with open(rcfile, 'w') as f:
        for s in rclines:
            print s
            f.write(s + '\n')
        f.close()
        
    st = os.stat(batchjob)
    os.chmod(batchjob, st.st_mode | 0111)
    os.environ['GRASS_BATCH_JOB'] = batchjob
    del os.environ['LD_LIBRARY_PATH']
    del os.environ['GISBASE']
    
    command = ['grass70 ']  + [gisdbase + '/' \
        + location + '/' + mapset]
    subprocess.call(command, shell=True)
Esempio n. 24
0
def main():
    # parameters
    infile = options['input']
    raster_reference = options['raster_reference']
    raster_file = options['raster_file']
    outfile = options['output']
    resolution = options['resolution']
    method = options['method']
    zrange = options['zrange']
    zscale = options['zscale']
    output_type = options['type']
    percent = options['percent']
    pth = options['pth']
    trim = options['trim']
    footprint = options['footprint']
    # flags
    scan = flags['s']
    shell_script_style = flags['g']

    # overwrite auf true setzen
    os.environ['GRASS_OVERWRITE'] = '1'

    # to hide non-error messages from subprocesses
    if grass.verbosity() <= 2:
        outdev = open(os.devnull, 'w')
    else:
        outdev = sys.stdout

    # use temporary region
    grass.use_temp_region()

    # scan -s or shell_script_style -g:
    if scan:
        if not grass.find_program(
                options['pdal_cmd'].split(' ')[0],
                ' '.join(options['pdal_cmd'].split(' ')[1:]) +
                ' info --summary'):
            grass.fatal(
                _("The pdal program is not in the path " +
                  "and executable. Please install first"))
        command_scan = options['pdal_cmd'].split(' ')
        command_scan.extend(['info', '--summary', infile])

        tmp_scan = grass.tempfile()
        if tmp_scan is None:
            grass.fatal("Unable to create temporary files")
        fh = open(tmp_scan, 'wb')
        summary = True
        if grass.call(command_scan, stdout=fh) != 0:
            fh.close()
            command_scan = options['pdal_cmd'].split(' ')
            command_scan.extend(['info', infile])
            fh2 = open(tmp_scan, 'wb')
            if grass.call(command_scan, stdout=fh2) != 0:
                os.remove(tmp_scan)
                grass.fatal(
                    _("pdal cannot determine metadata " +
                      "for unsupported format of <%s>") % infile)
                fh2.close()
            else:
                fh2.close()
            summary = False
        else:
            fh.close()

        data = json.load(open(tmp_scan))
        if summary:
            str1 = u'summary'
            str2 = u'bounds'
            y_str = u'Y'
            x_str = u'X'
            z_str = u'Z'
            min_str = u'min'
            max_str = u'max'
            try:
                n = str(data[str1][str2][y_str][max_str])
                s = str(data[str1][str2][y_str][min_str])
                w = str(data[str1][str2][x_str][min_str])
                e = str(data[str1][str2][x_str][max_str])
                t = str(data[str1][str2][z_str][max_str])
                b = str(data[str1][str2][z_str][min_str])
            except:
                ymin_str = u'miny'
                xmin_str = u'minx'
                zmin_str = u'minz'
                ymax_str = u'maxy'
                xmax_str = u'maxx'
                zmax_str = u'maxz'
                n = str(data[str1][str2][ymax_str])
                s = str(data[str1][str2][ymin_str])
                w = str(data[str1][str2][xmin_str])
                e = str(data[str1][str2][xmax_str])
                t = str(data[str1][str2][zmax_str])
                b = str(data[str1][str2][zmin_str])
        else:
            str1 = u'stats'
            str2 = u'bbox'
            str3 = u'native'
            str4 = u'bbox'
            n = str(data[str1][str2][str3][str4][u'maxy'])
            s = str(data[str1][str2][str3][str4][u'miny'])
            w = str(data[str1][str2][str3][str4][u'minx'])
            e = str(data[str1][str2][str3][str4][u'maxx'])
            t = str(data[str1][str2][str3][str4][u'maxz'])
            b = str(data[str1][str2][str3][str4][u'minz'])
        if not shell_script_style:
            grass.message(
                _("north: %s\nsouth: %s\nwest: %s\neast: %s\ntop: %s\nbottom: %s"
                  ) % (n, s, w, e, t, b))
        else:
            grass.message(
                _("n=%s s=%s w=%s e=%s t=%s b=%s") % (n, s, w, e, t, b))
    elif footprint:
        footprint_to_vectormap(infile, footprint)
    else:
        # get region with pdal
        footprint_to_vectormap(infile, 'tiles')

        if raster_file:
            raster_reference = 'img' + str(os.getpid())
            grass.run_command('r.external',
                              input=raster_file,
                              flags='o',
                              output=raster_reference)
            result = grass.find_file(name=raster_reference, element='raster')
            if result[u'fullname'] == u'':
                raster_reference = raster_reference + '.1'
        # option 1: set region to extent of tiles while precisely aligning pixel
        # geometry to raster_reference (including both raster_reference and raster_file)
        if raster_reference:
            grass.run_command('g.region',
                              vector='tiles',
                              flags='g',
                              align=raster_reference)
        else:
            # option 2: change raster resolution to final resolution while best
            # effort aligning to pixel geometry
            grass.run_command('g.region',
                              vector='tiles',
                              flags='ap',
                              res=resolution)
        # generate PDAL pipeline
        # . pdal pipline laz2json (STDOUT) | r.in.xyz
        bn = os.path.basename(infile)
        infile_format = bn.split('.')[-1]
        # format_reader from https://pdal.io/stages/readers.html
        format_reader = ''
        if infile_format.lower() == 'laz' or infile_format.lower() == 'las':
            format_reader = 'readers.las'
        # pts: not tested
        elif infile_format.lower() == 'pts':
            format_reader = 'readers.pts'
        else:
            grass.run_command('g.remove',
                              flags='f',
                              type='vector',
                              name='tiles',
                              quiet=True)
            grass.fatal(_("Format .%s is not supported.." % infile_format))
        tmp_file_json = 'tmp_file_json_' + str(os.getpid())

        data = {}
        data['pipeline'] = []
        data['pipeline'].append({'type': format_reader, 'filename': infile})
        data['pipeline'].append({
            'type': 'writers.text',
            'format': 'csv',
            'order': 'X,Y,Z',
            'keep_unspecified': 'false',
            'filename': 'STDOUT',
            'quote_header': 'false'
        })
        with open(tmp_file_json, 'w') as f:
            json.dump(data, f)

        tmp_xyz = grass.tempfile()
        if tmp_xyz is None:
            grass.fatal("Unable to create temporary files")
        command_pdal1 = options['pdal_cmd'].split(' ')
        if options['pdal_cmd'] != 'pdal':
            v_index = None
            cmd_entries = options['pdal_cmd'].split(' ')
            for cmd_entry, num in zip(cmd_entries, range(len(cmd_entries))):
                if cmd_entry == '-v':
                    v_index = num
                    break
            mnt_vol = cmd_entries[v_index + 1].split(':')[1]
            tmp_file_json2 = os.path.join(mnt_vol, tmp_file_json)
        else:
            tmp_file_json2 = tmp_file_json
        command_pdal1.extend(['pipeline', '--input', tmp_file_json2])
        command_pdal2 = [
            'r.in.xyz', 'input=' + tmp_xyz, 'output=' + outfile, 'skip=1',
            'separator=comma', 'method=' + method
        ]

        if zrange:
            command_pdal2.append('zrange=' + zrange)
        if zscale:
            command_pdal2.append('zscale=' + zscale)
        if output_type:
            command_pdal2.append('type=' + output_type)
        if percent:
            command_pdal2.append('percent=' + percent)
        if pth:
            command_pdal2.append('pth=' + pth)
        if trim:
            command_pdal2.append('trim=' + trim)

        fh = open(tmp_xyz, 'wb')
        if grass.call(command_pdal1, stdout=fh) != 0:
            fh.close()
            # check to see if pdal pipeline executed properly
            grass.fatal(_("pdal pipeline is broken..."))
        else:
            fh.close()

        if grass.call(command_pdal2, stdout=outdev) != 0:
            # check to see if r.in.xyz executed properly
            os.remove(tmp_xyz)
            grass.fatal(_("r.in.xyz is broken..."))

        # metadata
        empty_history = grass.tempfile()
        if empty_history is None:
            grass.fatal("Unable to create temporary files")
        f = open(empty_history, 'w')
        f.close()
        grass.run_command('r.support',
                          map=outfile,
                          source1=infile,
                          description='generated by r.in.pdal',
                          loadhistory=empty_history)
        grass.run_command('r.support',
                          map=outfile,
                          history=os.environ['CMDLINE'])
        os.remove(empty_history)

        # Cleanup
        grass.message(_("Cleaning up..."))
        grass.run_command('g.remove',
                          flags='f',
                          type='vector',
                          name='tiles',
                          quiet=True)
        if raster_file:
            grass.run_command('g.remove',
                              flags='f',
                              type='raster',
                              pattern=raster_reference[:-1] + '*',
                              quiet=True)
        os.remove(tmp_file_json)
        os.remove(tmp_xyz)
        grass.message(_("Generating output raster map <%s>...") % outfile)
        grass.del_temp_region()
Esempio n. 25
0
def main():
    # lazy imports
    import grass.temporal as tgis
    from grass.pygrass.utils import copy as gcopy
    from grass.pygrass.messages import Messenger
    from grass.pygrass.vector import Vector

    # Get the options
    input = options["input"]
    output = options["output"]
    strds = options["strds"]
    where = options["where"]
    tempwhere = options["t_where"]

    if output and flags['u']:
        grass.fatal(_("Cannot combine 'output' option and 'u' flag"))
    elif not output and not flags['u']:
        grass.fatal(_("'output' option or 'u' flag must be given"))
    elif not output and flags['u']:
        grass.warning(
            _("Attribute table of vector {name} will be updated...").format(
                name=input))

    if where == "" or where == " " or where == "\n":
        where = None

    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False

    # Check the number of sample strds and the number of columns
    strds_names = strds.split(",")

    # Make sure the temporal database exists
    tgis.init()
    # We need a database interface
    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()

    samples = []

    first_strds = tgis.open_old_stds(strds_names[0], "strds", dbif)
    # Single space time raster dataset
    if len(strds_names) == 1:
        granu = first_strds.get_granularity()
        rows = first_strds.get_registered_maps(
            "name,mapset,start_time,end_time", tempwhere, "start_time", dbif)

        if not rows:
            dbif.close()
            grass.fatal(
                _("Space time raster dataset <%s> is empty") %
                first_strds.get_id())
        for row in rows:
            start = row["start_time"]
            end = row["end_time"]
            raster_maps = [
                row["name"] + "@" + row["mapset"],
            ]

            s = Sample(start, end, raster_maps, first_strds.get_name(), granu)
            samples.append(s)
    else:
        # Multiple space time raster datasets
        for name in strds_names[1:]:
            dataset = tgis.open_old_stds(name, "strds", dbif)
            if dataset.get_temporal_type() != first_strds.get_temporal_type():
                grass.fatal(
                    _(
                        "Temporal type of space time raster "
                        "datasets must be equal\n<%(a)s> of type "
                        "%(type_a)s do not match <%(b)s> of type "
                        "%(type_b)s" % {
                            "a": first_strds.get_id(),
                            "type_a": first_strds.get_temporal_type(),
                            "b": dataset.get_id(),
                            "type_b": dataset.get_temporal_type()
                        }))

        mapmatrizes = tgis.sample_stds_by_stds_topology(
            "strds", "strds", strds_names, strds_names[0], False, None,
            "equal", False, False)
        #TODO check granularity for multiple STRDS
        for i in range(len(mapmatrizes[0])):
            isvalid = True
            mapname_list = []
            for mapmatrix in mapmatrizes:

                entry = mapmatrix[i]

                if entry["samples"]:
                    sample = entry["samples"][0]
                    name = sample.get_id()
                    if name is None:
                        isvalid = False
                        break
                    else:
                        mapname_list.append(name)

            if isvalid:
                entry = mapmatrizes[0][i]
                map = entry["granule"]

                start, end = map.get_temporal_extent_as_tuple()
                s = Sample(start, end, mapname_list, name)
                samples.append(s)

    # Get the layer and database connections of the input vector
    if output:
        gcopy(input, output, 'vector')
    else:
        output = input

    msgr = Messenger()
    perc_curr = 0
    perc_tot = len(samples)
    pymap = Vector(output)
    try:
        pymap.open('r')
    except:
        dbif.close()
        grass.fatal(_("Unable to create vector map <%s>" % output))

    if len(pymap.dblinks) == 0:
        try:
            pymap.close()
            grass.run_command("v.db.addtable", map=output)
        except CalledModuleError:
            dbif.close()
            grass.fatal(
                _("Unable to add table <%s> to vector map <%s>" % output))
    if pymap.is_open():
        pymap.close()

    for sample in samples:
        raster_names = sample.raster_names
        # Call v.what.rast for each raster map

        for name in raster_names:
            coltype = "DOUBLE PRECISION"
            # Get raster map type
            raster_map = tgis.RasterDataset(name)
            raster_map.load()
            if raster_map.metadata.get_datatype() == "CELL":
                coltype = "INT"
            day = sample.printDay()
            column_name = "%s_%s" % (sample.strds_name, day)
            column_string = "%s %s" % (column_name, coltype)
            column_string.replace('.', '_')
            try:
                grass.run_command("v.db.addcolumn",
                                  map=output,
                                  column=column_string,
                                  overwrite=overwrite)
            except CalledModuleError:
                dbif.close()
                grass.fatal(
                    _("Unable to add column %s to vector map "
                      "<%s> ") % (column_string, output))
            try:
                grass.run_command("v.what.rast",
                                  map=output,
                                  raster=name,
                                  column=column_name,
                                  where=where,
                                  quiet=quiet)
            except CalledModuleError:
                dbif.close()
                grass.fatal(
                    _("Unable to run v.what.rast for vector map"
                      " <%s> and raster map <%s>") %
                    (output, str(raster_names)))

        msgr.percent(perc_curr, perc_tot, 1)
        perc_curr += 1

    dbif.close()
def main(options, flags):

    import model as modellib
    from config import ModelConfig

    try:
        imagesDir = options['images_directory']
        modelPath = options['model']
        classes = options['classes'].split(',')
        if options['band1']:
            band1 = options['band1'].split(',')
            band2 = options['band2'].split(',')
            band3 = options['band3'].split(',')
        else:
            band1 = list()
            band2 = list()
            band3 = list()
        outputType = options['output_type']
        if options['images_format']:
            extension = options['images_format']
            if options['images_format'][0] != '.':
                extension = '.{}'.format(extension)
        else:
            extension = ''

        # a directory where masks and georeferencing will be saved in case of
        # external images
        masksDir = gscript.core.tempfile().rsplit(os.sep, 1)[0]
    except KeyError:
        # GRASS parses keys and values as bytes instead of strings
        imagesDir = options[b'images_directory'].decode('utf-8')
        modelPath = options[b'model'].decode('utf-8')
        classes = options[b'classes'].decode('utf-8').split(',')
        if options[b'band1'].decode('utf-8'):
            band1 = options[b'band1'].decode('utf-8').split(',')
            band2 = options[b'band2'].decode('utf-8').split(',')
            band3 = options[b'band3'].decode('utf-8').split(',')
        else:
            band1 = list()
            band2 = list()
            band3 = list()
        outputType = options[b'output_type'].decode('utf-8')
        if options[b'images_format'].decode('utf-8'):
            extension = options[b'images_format'].decode('utf-8')
            if extension[0] != '.':
                extension = '.{}'.format(extension)
        else:
            extension = ''

        newFlags = dict()
        for flag, value in flags.items():
            newFlags.update({flag.decode('utf-8'): value})
        flags = newFlags

        # a directory where masks and georeferencing will be saved in case of
        # external images
        masksDir = gscript.core.tempfile().decode('utf-8').rsplit(os.sep, 1)[0]

    if len(band1) != len(band2) or len(band2) != len(band3):
        gscript.fatal('Length of band1, band2 and band3 must be equal.')

    # TODO: (3 different brands in case of lot of classes?)
    if len(classes) > 255:
        gscript.fatal('Too many classes. Must be less than 256.')

    if len(set(classes)) != len(classes):
        gscript.fatal('Two or more classes have the same name.')

    # used colour corresponds to class_id
    classesColours = range(len(classes) + 1)

    # Create model object in inference mode.
    config = ModelConfig(numClasses=len(classes) + 1)
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=modelPath,
                              config=config)

    model.load_weights(modelPath, by_name=True)

    masks = list()
    detectedClasses = list()

    # TODO: Use the whole list instead of iteration
    if len(band1) > 0:
        gscript.message('Detecting features in raster maps...')
        # using maps imported in GRASS
        mapsCount = len(band1)
        for i in range(mapsCount):
            gscript.percent(i + 1, mapsCount, 1)
            maskTitle = '{}_{}'.format(band1[i].split('.')[0], i)
            # load map into 3-band np.array
            gscript.run_command('g.region', raster=band1[i], quiet=True)
            bands = np.stack((garray.array(band1[i]), garray.array(
                band2[i]), garray.array(band3[i])), 2)

            # Run detection
            results = model.detect([bands], verbosity=gscript.verbosity())

            # Save results
            for r in results:
                parse_instances(
                    bands,
                    r['rois'],
                    r['masks'],
                    r['class_ids'],
                    # ['BG'] + [i for i in classes],
                    # r['scores'],
                    outputDir=masksDir,
                    which=outputType,
                    title=maskTitle,
                    colours=classesColours,
                    mList=masks,
                    cList=detectedClasses,
                    grassMap=True)

    if imagesDir:
        gscript.message('Detecting features in images from the directory...')
        for imageFile in [
                file for file in next(os.walk(imagesDir))[2]
                if os.path.splitext(file)[1] == extension
        ]:
            image = skimage.io.imread(os.path.join(imagesDir, imageFile))
            if image.shape[2] > 3:
                image = image[:, :, 0:3]
            source = gdal.Open(os.path.join(imagesDir, imageFile))

            sourceProj = source.GetProjection()
            sourceTrans = source.GetGeoTransform()

            # Run detection
            results = model.detect([image], verbosity=gscript.verbosity())

            # Save results
            for r in results:
                parse_instances(
                    image,
                    r['rois'],
                    r['masks'],
                    r['class_ids'],
                    # ['BG'] + [i for i in classes],
                    # r['scores'],
                    outputDir=masksDir,
                    which=outputType,
                    title=imageFile,
                    colours=classesColours,
                    proj=sourceProj,
                    trans=sourceTrans,
                    mList=masks,
                    cList=detectedClasses,
                    externalReferencing=flags['e'])

    if flags['e']:
        gscript.message('Masks detected. Georeferencing masks...')
        external_georeferencing(imagesDir, classes, masksDir, masks,
                                detectedClasses, extension)

    gscript.message('Converting masks to vectors...')
    masksString = ','.join(masks)
    for parsedClass in detectedClasses:
        gscript.message('Processing {} map...'.format(classes[parsedClass -
                                                              1]))
        i = 0
        for maskName in masks:
            gscript.percent(i, len(masks), 1)
            gscript.run_command('g.region', raster=maskName, quiet=True)
            gscript.run_command('r.mask',
                                raster=maskName,
                                maskcats=classesColours[parsedClass],
                                quiet=True)
            gscript.run_command('r.to.vect',
                                's',
                                input=maskName,
                                output=maskName,
                                type=outputType,
                                quiet=True)
            gscript.run_command('r.mask', 'r', quiet=True)
            i += 1

        gscript.run_command('v.patch',
                            input=masksString,
                            output=classes[parsedClass - 1])
        gscript.run_command('g.remove',
                            'f',
                            name=masksString,
                            type='vector',
                            quiet=True)
    gscript.run_command('g.remove',
                        'f',
                        name=masksString,
                        type='raster',
                        quiet=True)
Esempio n. 27
0
def main(options, flags):

    # Lazy import GDAL python bindings
    try:
        from osgeo import gdal, osr
    except ImportError as e:
        gscript.fatal(_("Module requires GDAL python bindings: {}").format(e))

    import model as modellib
    from config import ModelConfig

    try:
        imagesDir = options["images_directory"]
        modelPath = options["model"]
        classes = options["classes"].split(",")
        if options["band1"]:
            band1 = options["band1"].split(",")
            band2 = options["band2"].split(",")
            band3 = options["band3"].split(",")
        else:
            band1 = list()
            band2 = list()
            band3 = list()
        outputType = options["output_type"]
        if options["images_format"]:
            extension = options["images_format"]
            if options["images_format"][0] != ".":
                extension = ".{}".format(extension)
        else:
            extension = ""

        # a directory where masks and georeferencing will be saved in case of
        # external images
        masksDir = gscript.core.tempfile().rsplit(os.sep, 1)[0]
    except KeyError:
        # GRASS parses keys and values as bytes instead of strings
        imagesDir = options[b"images_directory"].decode("utf-8")
        modelPath = options[b"model"].decode("utf-8")
        classes = options[b"classes"].decode("utf-8").split(",")
        if options[b"band1"].decode("utf-8"):
            band1 = options[b"band1"].decode("utf-8").split(",")
            band2 = options[b"band2"].decode("utf-8").split(",")
            band3 = options[b"band3"].decode("utf-8").split(",")
        else:
            band1 = list()
            band2 = list()
            band3 = list()
        outputType = options[b"output_type"].decode("utf-8")
        if options[b"images_format"].decode("utf-8"):
            extension = options[b"images_format"].decode("utf-8")
            if extension[0] != ".":
                extension = ".{}".format(extension)
        else:
            extension = ""

        newFlags = dict()
        for flag, value in flags.items():
            newFlags.update({flag.decode("utf-8"): value})
        flags = newFlags

        # a directory where masks and georeferencing will be saved in case of
        # external images
        masksDir = gscript.core.tempfile().decode("utf-8").rsplit(os.sep, 1)[0]

    if len(band1) != len(band2) or len(band2) != len(band3):
        gscript.fatal("Length of band1, band2 and band3 must be equal.")

    # TODO: (3 different brands in case of lot of classes?)
    if len(classes) > 255:
        gscript.fatal("Too many classes. Must be less than 256.")

    if len(set(classes)) != len(classes):
        gscript.fatal("Two or more classes have the same name.")

    # used colour corresponds to class_id
    classesColours = range(len(classes) + 1)

    # Create model object in inference mode.
    config = ModelConfig(numClasses=len(classes) + 1)
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=modelPath,
                              config=config)

    model.load_weights(modelPath, by_name=True)

    masks = list()
    detectedClasses = list()

    # TODO: Use the whole list instead of iteration
    if len(band1) > 0:
        gscript.message("Detecting features in raster maps...")
        # using maps imported in GRASS
        mapsCount = len(band1)
        for i in range(mapsCount):
            gscript.percent(i + 1, mapsCount, 1)
            maskTitle = "{}_{}".format(band1[i].split(".")[0], i)
            # load map into 3-band np.array
            gscript.run_command("g.region", raster=band1[i], quiet=True)
            bands = np.stack(
                (
                    garray.array(band1[i]),
                    garray.array(band2[i]),
                    garray.array(band3[i]),
                ),
                2,
            )

            # Run detection
            results = model.detect([bands], verbosity=gscript.verbosity())

            # Save results
            for r in results:
                parse_instances(
                    bands,
                    r["rois"],
                    r["masks"],
                    r["class_ids"],
                    # ['BG'] + [i for i in classes],
                    # r['scores'],
                    outputDir=masksDir,
                    which=outputType,
                    title=maskTitle,
                    colours=classesColours,
                    mList=masks,
                    cList=detectedClasses,
                    grassMap=True,
                )

    if imagesDir:
        gscript.message("Detecting features in images from the directory...")
        for imageFile in [
                file for file in next(os.walk(imagesDir))[2]
                if os.path.splitext(file)[1] == extension
        ]:
            image = skimage.io.imread(os.path.join(imagesDir, imageFile))
            if image.shape[2] > 3:
                image = image[:, :, 0:3]
            source = gdal.Open(os.path.join(imagesDir, imageFile))

            sourceProj = source.GetProjection()
            sourceTrans = source.GetGeoTransform()

            # Run detection
            results = model.detect([image], verbosity=gscript.verbosity())

            # Save results
            for r in results:
                parse_instances(
                    image,
                    r["rois"],
                    r["masks"],
                    r["class_ids"],
                    # ['BG'] + [i for i in classes],
                    # r['scores'],
                    outputDir=masksDir,
                    which=outputType,
                    title=imageFile,
                    colours=classesColours,
                    proj=sourceProj,
                    trans=sourceTrans,
                    mList=masks,
                    cList=detectedClasses,
                    externalReferencing=flags["e"],
                )

    if flags["e"]:
        gscript.message("Masks detected. Georeferencing masks...")
        external_georeferencing(imagesDir, classes, masksDir, masks,
                                detectedClasses, extension)

    gscript.message("Converting masks to vectors...")
    masksString = ",".join(masks)
    for parsedClass in detectedClasses:
        gscript.message("Processing {} map...".format(classes[parsedClass -
                                                              1]))
        i = 0
        for maskName in masks:
            gscript.percent(i, len(masks), 1)
            gscript.run_command("g.region", raster=maskName, quiet=True)
            gscript.run_command(
                "r.mask",
                raster=maskName,
                maskcats=classesColours[parsedClass],
                quiet=True,
            )
            gscript.run_command(
                "r.to.vect",
                "s",
                input=maskName,
                output=maskName,
                type=outputType,
                quiet=True,
            )
            gscript.run_command("r.mask", "r", quiet=True)
            i += 1

        gscript.run_command("v.patch",
                            input=masksString,
                            output=classes[parsedClass - 1])
        gscript.run_command("g.remove",
                            "f",
                            name=masksString,
                            type="vector",
                            quiet=True)
    gscript.run_command("g.remove",
                        "f",
                        name=masksString,
                        type="raster",
                        quiet=True)
Esempio n. 28
0
def main():
    vector = options['map']
    table = options['table']
    layer = options['layer']
    columns = options['columns']
    key = options['key']
    
    # does map exist in CURRENT mapset?
    mapset = grass.gisenv()['MAPSET']
    if not grass.find_file(vector, element = 'vector', mapset = mapset)['file']:
        grass.fatal(_("Vector map <%s> not found in current mapset") % vector)
    
    map_name = vector.split('@')[0]
    
    if not table:
        if layer == '1':
            grass.verbose(_("Using vector map name as table name: <%s>") % map_name)
            table = map_name
        else:
            # to avoid tables with identical names on higher layers
            table = "%s_%s" % (map_name, layer)
            grass.verbose(_("Using vector map name extended by layer number as table name: <%s>") % table)
    else:
        grass.verbose(_("Using user specified table name: %s") % table)
    
    # check if DB parameters are set, and if not set them.
    grass.run_command('db.connect', flags = 'c')
    grass.verbose(_("Creating new DB connection based on default mapset settings..."))
    kv = grass.db_connection()
    database = kv['database']
    driver = kv['driver']
    schema = kv['schema']
    
    # maybe there is already a table linked to the selected layer?
    nuldev = file(os.devnull, 'w')
    try:
        grass.vector_db(map_name, stderr = nuldev)[int(layer)]
        grass.fatal(_("There is already a table linked to layer <%s>") % layer)
    except KeyError:
        pass
    
    # maybe there is already a table with that name?
    tables = grass.read_command('db.tables', flags = 'p', database = database, driver = driver,
                                stderr = nuldev)
    
    if not table in tables.splitlines():
        if columns:
            column_def = map(lambda x: x.strip().lower(), columns.strip().split(','))
        else:
            column_def = []
        
        # if not existing, create it:
        column_def_key = "%s integer" % key
        if column_def_key not in column_def:
            column_def.insert(0, column_def_key)
        column_def = ','.join(column_def)
        
        grass.verbose(_("Creating table with columns (%s)...") % column_def)
        
        sql = "CREATE TABLE %s (%s)" % (table, column_def)
        try:
            grass.run_command('db.execute',
                              database=database, driver=driver, sql=sql)
        except CalledModuleError:
            grass.fatal(_("Unable to create table <%s>") % table)

    # connect the map to the DB:
    if schema:
        table = '{schema}.{table}'.format(schema=schema, table=table)
    grass.run_command('v.db.connect', quiet = True,
                      map = map_name, database = database, driver = driver,
                      layer = layer, table = table, key = key)
    
    # finally we have to add cats into the attribute DB to make modules such as v.what.rast happy:
    # (creates new row for each vector line):
    grass.run_command('v.to.db', map = map_name, layer = layer,
                      option = 'cat', column = key, qlayer = layer)
    
    grass.verbose(_("Current attribute table links:"))
    if grass.verbosity() > 2:
        grass.run_command('v.db.connect', flags = 'p', map = map_name)
    
    # write cmd history:
    grass.vector_history(map_name)
    
    return 0
Esempio n. 29
0
def main():

    # Get the options
    type = options["type"]
    temporal_type = options["temporaltype"]
    columns = options["columns"]
    order = options["order"]
    where = options["where"]
    separator = gscript.separator(options["separator"])
    outpath = options["output"]
    colhead = flags['c']

    # Make sure the temporal database exists
    tgis.init()

    sp = tgis.dataset_factory(type, None)
    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()
    first = True

    if  gscript.verbosity() > 0 and not outpath:
        sys.stderr.write("----------------------------------------------\n")

    for ttype in temporal_type.split(","):
        if ttype == "absolute":
            time = "absolute time"
        else:
            time = "relative time"

        stds_list = tgis.get_dataset_list(type,  ttype,  columns,  where,  order, dbif=dbif)

        # Use the correct order of the mapsets, hence first the current mapset, then
        # alphabetic ordering
        mapsets = tgis.get_tgis_c_library_interface().available_mapsets()

        if outpath:
            outfile = open(outpath, 'w')

        # Print for each mapset separately
        for key in mapsets:
            if key in stds_list.keys():
                rows = stds_list[key]

                if rows:
                    if  gscript.verbosity() > 0 and not outpath:
                        if issubclass(sp.__class__,  tgis.AbstractMapDataset):
                            sys.stderr.write(_("Time stamped %s maps with %s available in mapset <%s>:\n")%\
                                                     (sp.get_type(),  time,  key))
                        else:
                            sys.stderr.write(_("Space time %s datasets with %s available in mapset <%s>:\n")%\
                                                     (sp.get_new_map_instance(None).get_type(),  time,  key))

                    # Print the column names if requested
                    if colhead == True and first == True:
                        output = ""
                        count = 0
                        for key in rows[0].keys():
                            if count > 0:
                                output += separator + str(key)
                            else:
                                output += str(key)
                            count += 1
                        if outpath:
                            outfile.write("{st}\n".format(st=output))
                        else:
                            print output
                        first = False

                    for row in rows:
                        output = ""
                        count = 0
                        for col in row:
                            if count > 0:
                                output += separator + str(col)
                            else:
                                output += str(col)
                            count += 1
                        if outpath:
                            outfile.write("{st}\n".format(st=output))
                        else:
                            print output
    if outpath:
        outfile.close()
    dbif.close()
Esempio n. 30
0
def main():
    # set the home path
    grass_env_file = None  # see check_shell()
    if sys.platform == "win32":
        grass_config_dirname = "GRASS7"
        grass_config_dir = os.path.join(os.getenv("APPDATA"),
                                        grass_config_dirname)
    else:
        grass_config_dirname = ".grass7"
        grass_config_dir = os.path.join(os.getenv("HOME"),
                                        grass_config_dirname)
    # configuration directory
    rlidir = os.path.join(grass_config_dir, "r.li")
    # check if GISBASE is set
    if "GISBASE" not in os.environ:
        # return an error advice
        grass.fatal(_("You must be in GRASS GIS to run this program."))

    # input raster map
    map_in = options["input"]
    # output raster map
    map_out = options["prefix"]
    # resolution of moving windows
    res = options["size"]
    # alpha value for r.renyi
    alpha = options["alpha"]
    # method to use
    methods = options["method"]
    # excluded method
    excludes = options["exclude"]

    resolution = checkValues(res)

    if alpha != "":
        alpha_value = checkValues(alpha, True)
    else:
        alpha_value = ""

    # check if ~/.r.li path exists
    if not os.path.exists(rlidir):
        # create ~/.grass7/r.li
        os.mkdir(rlidir)

    # set overwrite
    overwrite = grass.overwrite()

    quiet = True

    if grass.verbosity() > 2:
        quiet = False
    # if method and exclude option are not null return an error
    if methods != "simpson,shannon,pielou,renyi" and excludes != "":
        grass.fatal(
            _("You can either use 'method' or 'exclude' option but not both"))
    # calculate not excluded index
    elif excludes != "":
        excludes = excludes.split(",")
        checkAlpha(excludes, alpha_value, True)
        calculateE(rlidir, map_in, map_out, resolution, alpha_value, excludes,
                   quiet, overwrite)
    # calculate method
    elif methods != "":
        methods = methods.split(",")
        checkAlpha(methods, alpha_value)
        calculateM(rlidir, map_in, map_out, resolution, alpha_value, methods,
                   quiet, overwrite)
    # remove configuration files
    if not flags["t"]:
        removeConfFile(resolution, rlidir)
    grass.message(_("Done."))
Esempio n. 31
0
        'pfactor' : 'pfactor',
        'kfactor' : 'kfactor'
        }
    }    

#END OF USER SETTINGS
###DO NOT CHANGE ANYTHING AFTER THIS LINE !

import grass.script as gscript
from grass.pygrass.gis import Mapset
from grass.pygrass.gis.region import Region
from grass.pygrass.modules.shortcuts import raster as r, vector as v, general as g
from grass.pygrass.modules import Module

quiet = True
if gscript.verbosity() > 2:
    quiet=False

## define own modules in pygrass
rsoillossbare = Module("r.soilloss.bare")
rsoillossupdate = Module("r.soilloss.update")
rsoillossgrow = Module("r.soilloss.grow")
rsoillosscpmax = Module("r.soilloss.cpmax")
rsoillossreclass = Module("r.soilloss.reclass")
rsoillossstats = Module("r.soilloss.stats")
vinogr = Module("v.in.ogr")

def main(options, flags):
    config_name = options['configuration']
    params = owsConnections[config_name]