Beispiel #1
0
def _kappa_pixel(maps1, maps2, out, method, over):
    from grass.pygrass.raster.buffer import Buffer
    import sklearn
    rasterout = RasterRow(out, overwrite=over)
    rasterout.open('w','DCELL')
    array1 = maps1.values()[0]
    for row in range(len(array1)):
        newrow = Buffer((len(array1[row]),), mtype='DCELL')
        for col in range(len(array1[row])):
            vals1 = np.ndarray(len(maps1.values()))
            vals2 = np.ndarray(len(maps2.values()))
            x = 0
            for value in maps1.values():
                vals1[x] = value[row][col]
                x += 1
            x = 0
            for value in maps2.values():
                vals2[x] = value[row][col]
                x += 1
            if sklearn.__version__ >= '0.18':
                outval = sklearn.metrics.cohen_kappa_score(vals1, vals2,
                                                           weights=method)
            else:
                outval = sklearn.metrics.cohen_kappa_score(vals1, vals2)
            newrow[col] = outval
        rasterout.put_row(newrow)
    rasterout.close()
    return
Beispiel #2
0
 def segment2map(self):
     """Transform the segment file to a map.
     """
     row_buffer = Buffer((self._cols), self.mtype)
     for row in range(self._rows):
         row_buffer = self.segment.get_row(row, row_buffer)
         libraster.Rast_put_row(self._fd, row_buffer.p, self._gtype)
Beispiel #3
0
    def get_row(self, row, row_buffer=None):
        """This method returns the row using:

            * the read mode and
            * `rowcache` method

        :param row: the number of row to obtain
        :type row: int
        :param row_buffer: Specify the Buffer object that will be instantiate
        :type row_buffer: Buffer object

            >>> elev = RasterRowIO(test_raster_name)
            >>> elev.open('r')
            >>> for row in elev:
            ...     row
            Buffer([11, 21, 31, 41], dtype=int32)
            Buffer([12, 22, 32, 42], dtype=int32)
            Buffer([13, 23, 33, 43], dtype=int32)
            Buffer([14, 24, 34, 44], dtype=int32)

            >>> elev.close()

        """
        if row_buffer is None:
            row_buffer = Buffer((self._cols, ), self.mtype)
        rowio_buf = librowio.Rowio_get(ctypes.byref(self.rowio.c_rowio), row)
        ctypes.memmove(row_buffer.p, rowio_buf, self.rowio.row_size)
        return row_buffer
Beispiel #4
0
 def map2segment(self):
     """Transform an existing map to segment file.
     """
     row_buffer = Buffer((self._cols), self.mtype)
     for row in range(self._rows):
         libraster.Rast_get_row(self._fd, row_buffer.p, row, self._gtype)
         self.segment.put_row(row, row_buffer)
def filter(method, names, winsize, order, prefix, itercount, fit_up):

    current_mapset = grass.read_command('g.mapset', flags='p')
    current_mapset = current_mapset.strip()

    inputs = init_rasters(names)
    output_names = [prefix + name for name in names]
    outputs = init_rasters(output_names, mapset=current_mapset)
    try:
        open_rasters(outputs, write=True)
        open_rasters(inputs)

        reg = Region()
        for i in range(reg.rows):
            # import ipdb; ipdb.set_trace()
            row_data = np.array([_get_row_or_nan(r, i) for r in inputs])
            filtered_rows = _filter(method, row_data, winsize, order,
                                    itercount, fit_up)
            for map_num in range(len(outputs)):
                map = outputs[map_num]
                row = filtered_rows[map_num, :]
                buf = Buffer(row.shape, map.mtype, row)
                map.put_row(i, buf)
    finally:
        close_rasters(outputs)
        close_rasters(inputs)
Beispiel #6
0
    def get_row(self, row, row_buffer=None):
        """Return the row using the `segment.get_row` method

        :param row: specify the row number
        :type row: int
        :param row_buffer: specify the Buffer object that will be instantiate
        :type row_buffer: Buffer object

            >>> elev = RasterRowIO(test_raster_name)
            >>> elev.open('r')
            >>> for row in elev:
            ...     row
            Buffer([11, 21, 31, 41], dtype=int32)
            Buffer([12, 22, 32, 42], dtype=int32)
            Buffer([13, 23, 33, 43], dtype=int32)
            Buffer([14, 24, 34, 44], dtype=int32)

            >>> elev.close()


            >>> with RasterSegment(test_raster_name) as elev:
            ...     for row in elev:
            ...         row
            Buffer([11, 21, 31, 41], dtype=int32)
            Buffer([12, 22, 32, 42], dtype=int32)
            Buffer([13, 23, 33, 43], dtype=int32)
            Buffer([14, 24, 34, 44], dtype=int32)

        """
        if row_buffer is None:
            row_buffer = Buffer((self._cols), self.mtype)
        return self.segment.get_row(row, row_buffer)
Beispiel #7
0
def random_map(mapname, mtype, overwrite=True, factor=100):
    region = Region()
    random_map = RasterRow(mapname)
    random_map.open('w', mtype, overwrite)
    for _ in range(region.rows):
        row_buf = Buffer((region.cols, ), mtype,
                         buffer=(np.random.random(region.cols,) * factor).data)
        random_map.put_row(row_buf)
    random_map.close()
    return random_map
Beispiel #8
0
def numpy2raster(array, mtype, name):
    """Save a numpy array to a raster map"""
    reg = Region()
    if (reg.rows, reg.cols) != array.shape:
        msg = "Region and array are different: %r != %r"
        raise TypeError(msg % ((reg.rows, reg.cols), array.shape))
    with RasterRow(name, mode="w", mtype=mtype) as new:
        newrow = Buffer((array.shape[1],), mtype=mtype)
        for row in array:
            newrow[:] = row[:]
            new.put_row(newrow)
Beispiel #9
0
    def get_row(self, row, row_buffer=None):
        """Return the row using the `segment.get_row` method

        :param row: specify the row number
        :type row: int
        :param row_buffer: specify the Buffer object that will be instantiate
        :type row_buffer: Buffer object
        """
        if row_buffer is None:
            row_buffer = Buffer((self._cols), self.mtype)
        return self.segment.get_row(row, row_buffer)
Beispiel #10
0
    def _predict_multi(self, estimator, region, indexes, class_labels, height,
                       func, output, overwrite):
        # create and open rasters for writing if incremental reading
        if height is not None:
            dst = []

            for i, label in enumerate(class_labels):
                rastername = output + "_" + str(label)
                dst.append(RasterRow(rastername))
                dst[i].open("w", mtype="FCELL", overwrite=overwrite)

            # create data reader generator
            n_windows = len([i for i in self.row_windows(height=height)])

            data_gen = (
                (wi, self.read(rows=rows))
                for wi, rows in enumerate(self.row_windows(height=height)))

        # perform prediction
        try:
            if height is not None:
                for wi, arr in data_gen:
                    gs.percent(wi, n_windows, 1)
                    result = func(arr, estimator)
                    result = np.ma.filled(result, np.nan)

                    # write multiple features to GRASS GIS rasters
                    for i, arr_index in enumerate(indexes):
                        for row in range(result.shape[1]):
                            newrow = Buffer((region.cols, ), mtype="FCELL")
                            newrow[:] = result[arr_index, row, :]
                            dst[i].put_row(newrow)
            else:
                arr = self.read()
                result = func(arr, estimator)
                result = np.ma.filled(result, np.nan)

                for i, arr_index in enumerate(indexes):
                    numpy2raster(
                        result[arr_index, :, :],
                        mtype="FCELL",
                        rastname=rastername[i],
                        overwrite=overwrite,
                    )
        except:
            gs.fatal("Error in raster prediction")

        finally:
            if height is not None:
                for i in dst:
                    i.close()

        return RasterStack([i.name for i in dst])
Beispiel #11
0
 def _write(self, name, overwrite):
     """Write the numpy array into map
     """
     if not self.exist() or self.mode != 'r':
         self.flush()
         buff = Buffer(self[0].shape, mtype=self.mtype)
         with RasterRow(name,
                        self.mapset,
                        mode='w',
                        mtype=self.mtype,
                        overwrite=overwrite) as rst:
             for i in range(len(rst)):
                 buff[:] = self[i][:]
                 rst.put_row(buff[:])
         self.name = name
Beispiel #12
0
    def get_row(self, row, row_buffer=None):
        """This method returns the row using:

            * the read mode and
            * `rowcache` method

        :param row: the number of row to obtain
        :type row: int
        :param row_buffer: Specify the Buffer object that will be instantiate
        :type row_buffer: Buffer object
        """
        if row_buffer is None:
            row_buffer = Buffer((self._cols, ), self.mtype)
        rowio_buf = librowio.Rowio_get(ctypes.byref(self.rowio.c_rowio), row)
        ctypes.memmove(row_buffer.p, rowio_buf, self.rowio.row_size)
        return row_buffer
Beispiel #13
0
def createRast(name, matrix, inverse=False):
    """Create the new raster map using the output matrix of calculateOblique"""
    newscratch = RasterRow(name)
    newscratch.open('w', overwrite=True)
    try:
        for r in matrix:
            if inverse:
                r.reverse()
            newrow = Buffer((len(r), ), mtype='FCELL')
            for c in range(len(r)):
                newrow[c] = r[c]
            newscratch.put_row(newrow)
        newscratch.close()
        return True
    except:
        return False
Beispiel #14
0
def numpy2raster(array, mtype, rastname, overwrite=False):
    """Save a numpy array to a raster map

    :param obj array: a numpy array
    :param obj mtype: the datatype of array
    :param str rastername: the name of output map
    :param bool overwrite: True to overwrite existing map
    """
    reg = Region()
    if (reg.rows, reg.cols) != array.shape:
        msg = "Region and array are different: %r != %r"
        raise TypeError(msg % ((reg.rows, reg.cols), array.shape))
    with RasterRow(rastname, mode='w', mtype=mtype, overwrite=overwrite) as new:
        newrow = Buffer((array.shape[1],), mtype=mtype)
        for row in array:
            newrow[:] = row[:]
            new.put_row(newrow)
Beispiel #15
0
    def get_row(self, row, row_buffer=None):
        """Private method that return the row using the read mode
            call the `Rast_get_row` C function.

            :param row: the number of row to obtain
            :type row: int
            :param row_buffer: Buffer object instance with the right dim and type
            :type row_buffer: Buffer

            >>> elev = RasterRow(test_raster_name)
            >>> elev.open()
            >>> elev[0]
            Buffer([11, 21, 31, 41], dtype=int32)
            >>> elev.get_row(0)
            Buffer([11, 21, 31, 41], dtype=int32)

        """
        if row_buffer is None:
            row_buffer = Buffer((self._cols, ), self.mtype)
        libraster.Rast_get_row(self._fd, row_buffer.p, row, self._gtype)
        return row_buffer
Beispiel #16
0
def main():

    options, flags = gscript.parser()
    input = options['input']
    output = options['output']

    if (output is None or output == ""):
        gscript.error(_("[h.in] ERROR: output is a mandatory parameter."))
        exit()

    # Load HexASCII raster into memory
    hexASCII = HASC()
    try:
        hexASCII.loadFromFile(input)
    except (ValueError, IOError) as ex:
        gscript.error(
            _("[h.in] ERROR: Failed to load raster %s: %s" % (input, ex)))
        exit()

    # Set region (note that it is tricking GRASS to think it is a squared raster)
    gscript.run_command('g.region',
                        rows=hexASCII.nrows,
                        cols=hexASCII.ncols,
                        res=hexASCII.side)

    # Create RasterRow object and iterate trough rows
    newRast = raster.RasterRow(output)
    newRast.open('w', 'FCELL')
    for row in range(0, hexASCII.nrows):
        newRow = Buffer(shape=(1, hexASCII.ncols))  #, dtype=float, order='F')
        for col in range(0, hexASCII.ncols):
            newRow[0, col] = hexASCII.get(col, row)
        gscript.message(_("[h.in] DEBUG: Importing row: %s" % newRow))
        newRast.put_row(newRow)
    gscript.message(_("[h.in] DEBUG: Imported raster: %s" % (newRast)))

    # Close RasterRow to force its creation
    newRast.close()

    gscript.message(_("[h.in] SUCCESS: HexASCII raster imported."))
Beispiel #17
0
    def get_row(self, row, row_buffer=None):
        """Private method that return the row using the read mode
        call the `Rast_get_row` C function.

        :param row: the number of row to obtain
        :type row: int
        :param row_buffer: Buffer object instance with the right dim and type
        :type row_buffer: Buffer

        >>> elev = RasterRow('elevation')
        >>> elev.open()
        >>> elev[0]                 # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Buffer([ 141.99613953, 141.27848816,  141.37904358, ..., 58.40825272,
                 58.30711365,  58.18310547], dtype=float32)
        >>> elev.get_row(0)         # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
        Buffer([ 141.99613953, 141.27848816, 141.37904358, ..., 58.40825272,
                 58.30711365, 58.18310547], dtype=float32)

        """
        if row_buffer is None:
            row_buffer = Buffer((self._cols, ), self.mtype)
        libraster.Rast_get_row(self._fd, row_buffer.p, row, self._gtype)
        return row_buffer
Beispiel #18
0
#!/usr/bin/env python3

import numpy

from grass.pygrass.raster import RasterRow

newscratch = RasterRow('newscratch')
newscratch.open('w', overwrite=True)

# get computational region info
from grass.pygrass.gis.region import Region
reg = Region()

# import buffer and create empty row
from grass.pygrass.raster.buffer import Buffer
newrow = Buffer((reg.cols,), mtype='CELL')

# we create a raster to fill all the GRASS GIS region
for r in range(reg.rows):
    newrow[:] = numpy.random.random_integers(0, 1000, size=newrow.size)
    newscratch.put_row(newrow)
          
newscratch.close()
Beispiel #19
0
    def predict(self, estimator, output, height=None, overwrite=False):
        """Prediction method for RasterStack class

        Parameters
        ----------
        estimator : estimator object implementing 'fit'
            The object to use to fit the data.

        output : str
            Output name for prediction raster.

        height : int (opt).
            Number of raster rows to pass to estimator at one time. If not
            specified then the entire raster is read into memory.

        overwrite : bool (opt). Default is False
            Option to overwrite an existing raster.

        Returns
        -------
        RasterStack
        """

        reg = Region()
        func = self._pred_fun

        # determine dtype
        test_window = list(self.row_windows(height=1))[0]
        img = self.read(rows=test_window)
        result = func(img, estimator)

        try:
            np.finfo(result.dtype)
            mtype = "FCELL"
            nodata = np.nan
        except:
            mtype = "CELL"
            nodata = -2147483648

        # determine whether multi-target
        if result.shape[0] > 1:
            n_outputs = result.shape[result.ndim - 1]
        else:
            n_outputs = 1

        indexes = np.arange(0, n_outputs)

        # chose prediction function
        if len(indexes) == 1:
            func = self._pred_fun
        else:
            func = self._predfun_multioutput

        if len(indexes) > 1:
            result_stack = self._predict_multi(estimator, reg, indexes,
                                               indexes, height, func, output,
                                               overwrite)
        else:
            if height is not None:

                with RasterRow(output,
                               mode="w",
                               mtype=mtype,
                               overwrite=overwrite) as dst:
                    n_windows = len(
                        [i for i in self.row_windows(height=height)])

                    data_gen = ((wi, self.read(rows=rows))
                                for wi, rows in enumerate(
                                    self.row_windows(height=height)))

                    for wi, arr in data_gen:
                        gs.percent(wi, n_windows, 1)
                        result = func(arr, estimator)
                        result = np.ma.filled(result, nodata)

                        # writing data to GRASS raster row-by-row
                        for i in range(result.shape[1]):
                            newrow = Buffer((reg.cols, ), mtype=mtype)
                            newrow[:] = result[0, i, :]
                            dst.put_row(newrow)

            else:
                arr = self.read()
                result = func(arr, estimator)
                result = np.ma.filled(result, nodata)
                numpy2raster(result[0, :, :],
                             mtype=mtype,
                             rastname=output,
                             overwrite=overwrite)

            result_stack = RasterStack(output)

        return result_stack
Beispiel #20
0
def main():

    # Get the options
    inputs = options["inputs"]
    output = options["output"]
    basename = options["basename"]
    where = options["where"]
    pyfile = options["pyfile"]
    nrows = int(options["nrows"])

    input_name_list = inputs.split(",")

    input_strds: List[StrdsEntry] = []

    # Import the python code into the current function context
    code = open(pyfile, "r").read()
    projection_kv = gcore.parse_command("g.proj", flags="g")
    epsg_code = projection_kv["epsg"]

    tgis.init()
    mapset = gcore.gisenv()["MAPSET"]

    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()

    region = Region()
    num_input_maps = 0
    open_output_maps = []

    for input_name in input_name_list:
        sp = tgis.open_old_stds(input_name, "strds", dbif)
        map_list = sp.get_registered_maps_as_objects(where=where,
                                                     order="start_time",
                                                     dbif=dbif)

        if not map_list:
            dbif.close()
            gcore.fatal(_("Space time raster dataset <%s> is empty") % input)

        if nrows == 0:
            dbif.close()
            gcore.fatal(_("Number of rows for the udf must be greater 0."))

        num_input_maps = len(map_list)
        input_strds.append(
            StrdsEntry(dbif=dbif, strds=sp, map_list=map_list, region=region))

    for strds in input_strds:
        if len(strds.map_list) != num_input_maps:
            dbif.close()
            gcore.fatal(
                _("The number of maps in the input STRDS must be equal"))

    # Setup the input strds to compute the output maps and the resulting strds
    mtype = None
    for strds in input_strds:
        strds.setup()
        mtype = strds.mtype

    num_output_maps = count_resulting_maps(input_strds=input_strds,
                                           code=code,
                                           epsg_code=epsg_code)

    if num_output_maps == 1:
        output_map = RasterRow(name=basename)
        output_map.open(mode="w", mtype=mtype, overwrite=gcore.overwrite())
        open_output_maps.append(output_map)
    elif num_output_maps > 1:
        for index in range(num_output_maps):
            output_map = RasterRow(name=basename + f"_{index}", mapset=mapset)
            output_map.open(mode="w", mtype=mtype, overwrite=gcore.overwrite())
            open_output_maps.append(output_map)
    else:
        dbif.close()
        gcore.fatal(_("No result generated") % input)

    # Workaround because time reduction will remove the timestamp
    result_start_times = [datetime.now()]
    first = False

    # Read several rows for each map of each input strds and load them into the udf
    for index in range(0, region.rows, nrows):
        if index + nrows > region.rows:
            usable_rows = index + nrows - region.rows + 1
        else:
            usable_rows = nrows

        # Read all input strds as cubes
        datacubes = []
        for strds in input_strds:
            datacube = strds.to_datacube(index=index, usable_rows=usable_rows)
            datacubes.append(datacube)

        # Run the UDF code
        data = run_udf(code=code, epsg_code=epsg_code, datacube_list=datacubes)

        # Read only the first cube
        datacubes = data.get_datacube_list()
        first_cube_array: xarray.DataArray = datacubes[0].get_array()

        if first is False:
            if 't' in first_cube_array.coords:
                result_start_times = first_cube_array.coords['t']

        # Three dimensions
        if first_cube_array.ndim == 3:
            for count, slice in enumerate(first_cube_array):
                output_map = open_output_maps[count]
                # print(f"Write slice at index {index} \n{slice} for map {output_map.name}")
                for row in slice:
                    # Write the result into the output raster map
                    b = Buffer(shape=(region.cols, ), mtype=mtype)
                    b[:] = row[:]
                    output_map.put_row(b)
        # Two dimensions
        elif first_cube_array.ndim == 2:
            output_map = open_output_maps[0]
            # print(f"Write slice at index {index} \n{slice} for map {output_map.name}")
            for row in first_cube_array:
                # Write the result into the output raster map
                b = Buffer(shape=(region.cols, ), mtype=mtype)
                b[:] = row[:]
                output_map.put_row(b)

        first = True

    # Create new STRDS
    new_sp = open_new_stds(
        name=output,
        type="strds",
        temporaltype=input_strds[0].strds.get_temporal_type(),
        title="new STRDS",
        descr="New STRDS from UDF",
        semantic="UDF",
        overwrite=gcore.overwrite(),
        dbif=dbif)

    maps_to_register = []
    for count, output_map in enumerate(open_output_maps):
        output_map.close()
        print(output_map.fullname())
        rd = RasterDataset(output_map.fullname())
        if input_strds[0].strds.is_time_absolute():
            if hasattr(result_start_times, "data"):
                d = pandas.to_datetime(result_start_times.data[count])
            else:
                d = result_start_times[count]
            rd.set_absolute_time(start_time=d)
        elif input_strds[0].strds.is_time_relative():
            if hasattr(result_start_times, "data"):
                d = result_start_times.data[count]
            else:
                d = result_start_times[count]
            rd.set_relative_time(start_time=d, end_time=None, unit="seconds")
        rd.load()
        if rd.is_in_db(dbif=dbif):
            rd.update(dbif=dbif)
        else:
            rd.insert(dbif=dbif)
        maps_to_register.append(rd)
        rd.print_info()

    register_map_object_list(type="raster",
                             map_list=maps_to_register,
                             output_stds=new_sp,
                             dbif=dbif)

    dbif.close()