Ejemplo n.º 1
0
def test_equivalent_masks():
    from numpy import nan
    X = numpy.array([
        1, 2, 3, nan, nan, 7,
        1, 2, 3, nan, nan, 7,
        1, 2, 3, nan, nan, nan,
        1, 2, 3, nan, nan, nan,
        1, 2, 3, nan, nan, 7,
    ])

    Y1 = X.copy()

    Y2 = numpy.array([
        1, 2, 3, nan, nan, 7,
        1, 2, 3, nan, nan, nan,
        1, 2, 3, nan, nan, nan,
        1, 2, 3, nan, nan, nan,
        1, 2, 3, nan, nan, 7,
    ])
    with raises(ValueError):
        validate.equivalent_masks(X, Y2)

    x, y = validate.equivalent_masks(X, Y1)
    nptest.assert_array_equal(X, x.data)
    nptest.assert_array_equal(Y1, y.data)
Ejemplo n.º 2
0
def write_points(X, Y, crs, outputfile, river=None, reach=0, elev=None):
    """ Saves grid-related attributes of a pygridgen.Gridgen object to a
    GIS file with geomtype = 'Point'.

    Parameters
    ----------
    X, Y : numpy (masked) arrays, same dimensions
        Attributes of the gridgen object representing the x- and y-coords.
    crs : string
        A geopandas/proj/fiona-compatible string describing the coordinate
        reference system of the x/y values.
    outputfile : string
        Path to the point-geometry GIS file to which the data will be written.
    river : optional string (default = None)
        The river to be listed in the GIS files's attribute table.
    reach : optional int (default = 0)
        The reach of the river to be listed in the GIS file's attribute
        table.
    elev : optional array or None (defauly)
        The elevation of the grid cells. Array dimensions must be 1 less than
        X and Y.

    Returns
    -------
    geopandas.GeoDataFrame

    """

    # check that X and Y are have the same shape, NaN cells
    X, Y = validate.equivalent_masks(X, Y)

    # check elev shape
    elev = validate.elev_or_mask(X, elev, 'elev', offset=0)

    # start writting or appending to the output
    row = 0
    geodata = []
    for ii in range(X.shape[1]):
        for jj in range(X.shape[0]):
            # check that nothing is masked (outside of the river)
            if not (X.mask[jj, ii]):
                row += 1

                # build the coords
                coords = (X[jj, ii], Y[jj, ii])

                # build the attributes
                record = OrderedDict(id=int(row),
                                     river=river,
                                     reach=reach,
                                     ii=int(ii + 2),
                                     jj=int(jj + 2),
                                     elev=float(elev[jj, ii]),
                                     ii_jj='{:02d}_{:02d}'.format(
                                         ii + 2, jj + 2),
                                     geometry=Point(coords))

                geodata.append(record)

    gdf = geopandas.GeoDataFrame(geodata, crs=crs, geometry='geometry')
    gdf.to_file(outputfile)
    return gdf
Ejemplo n.º 3
0
 def test_error(self):
     validate.equivalent_masks(self.X, self.Y2)
Ejemplo n.º 4
0
 def test_baseline(self):
     x, y = validate.equivalent_masks(self.X, self.Y1)
     nptest.assert_array_equal(self.X, x.data)
     nptest.assert_array_equal(self.Y1, y.data)
Ejemplo n.º 5
0
def savePointShapefile(X, Y, template, outputfile, mode="w", river=None, reach=0, elev=None):
    """ Saves grid-related attributes of a pygridgen.Gridgen object to a
    shapefile with geomtype = 'Point'.

    Parameters
    ----------
    X, Y : numpy (masked) arrays, same dimensions
        Attributes of the gridgen object representing the x- and y-coords.
    template : string
        Path to a template shapfiles with the desired schema.
    outputfile : string
        Path to the point shapefile to which the data will be written.
    mode : optional string (default = 'w')
        The mode with which `outputfile` will be written.
        (i.e., 'a' for append and 'w' for write)
    river : optional string (default = None)
        The river to be listed in the shapefile's attribute table.
    reach : optional int (default = 0)
        The reach of the river to be listed in the shapefile's attribute
        table.
    elev : optional array or None (defauly)
        The elevation of the grid cells. Array dimensions must be 1 less than
        X and Y.

    Returns
    -------
    None

    """

    # check that the `mode` is a valid value
    mode = validate.file_mode(mode)

    # check that X and Y are have the same shape, NaN cells
    X, Y = validate.equivalent_masks(X, Y)

    # check elev shape
    elev = validate.elev_or_mask(X, elev, "elev", offset=0)

    # load the template
    with fiona.open(template, "r") as src:
        src_driver = src.driver
        src_crs = src.crs
        src_schema = src.schema

    src_schema["geometry"] = "Point"

    # start writting or appending to the output
    with fiona.open(outputfile, mode, driver=src_driver, crs=src_crs, schema=src_schema) as out:
        row = 0
        for ii in range(X.shape[1]):
            for jj in range(X.shape[0]):
                # check that nothing is masked (outside of the river)
                if not (X.mask[jj, ii]):
                    row += 1

                    # build the coords
                    coords = (X[jj, ii], Y[jj, ii])

                    # build the attributes
                    props = OrderedDict(
                        id=int(row),
                        river=river,
                        reach=reach,
                        ii=int(ii + 2),
                        jj=int(jj + 2),
                        elev=float(elev[jj, ii]),
                        ii_jj="{:02d}_{:02d}".format(ii + 2, jj + 2),
                    )

                    # append to the output file
                    record = misc.make_record(row, coords, "Point", props)
                    out.write(record)