def test_make_record(geom, geomtype, error, as_array): props = {'prop1': 'this string', 'prop2': 3.1415} expected_geoms = { 'point': { 'geometry': { 'type': 'Point', 'coordinates': [1, 2] }, 'id': 1, 'properties': props }, 'linestring': { 'geometry': { 'type': 'LineString', 'coordinates': [[[1, 2], [5, 6], [5, 2]]] }, 'id': 1, 'properties': props }, 'polygon': { 'geometry': { 'type': 'Polygon', 'coordinates': [[[1, 2], [5, 6], [5, 2]]] }, 'id': 1, 'properties': props } } if as_array: geom = numpy.array(geom) with utils.raises(error): record = misc.make_record(1, geom, geomtype, props) assert record == expected_geoms[geomtype.lower()]
def row2record(row, outfile): coords = (float(row.x), float(row.y)) props = OrderedDict( id=int(row.name), river=river, reach=reach, ii=int(row.i), jj=int(row.j), elev=0, ii_jj="{:03d}_{:03d}".format(int(row.i), int(row.j)), ) record = misc.make_record(int(row.name), coords, "Point", props) try: outfile.write(record) return 1 except: # pragma: no cover return 0
def saveGridShapefile(X, Y, mask, template, outputfile, mode, river=None, reach=0, elev=None, triangles=False): """ Saves a shapefile of quadrilaterals representing grid cells. Parameters ---------- X, Y : numpy (masked) arrays, same dimensions Attributes of the gridgen object representing the x- and y-coords. mask : numpy array or None Array describing which cells to mask (exclude) from the output. Shape should be N-1 by M-1, where N and M are the dimensions of `X` and `Y`. 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 : string 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. Shape should be N-1 by M-1, where N and M are the dimensions of `X` and `Y` (like `mask`). triangles : optional bool (default = False) If True, triangles can be included Returns ------- None """ # check that `mode` is valid mode = validate.file_mode(mode) # check X, Y shapes Y = validate.elev_or_mask(X, Y, "Y", offset=0) # check elev shape elev = validate.elev_or_mask(X, elev, "elev", offset=0) # check the mask shape mask = validate.elev_or_mask(X, mask, "mask", offset=1) X = np.ma.masked_invalid(X) Y = np.ma.masked_invalid(Y) ny, nx = X.shape # 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"] = "Polygon" # 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(nx - 1): for jj in range(ny - 1): if not (np.any(X.mask[jj : jj + 2, ii : ii + 2]) or mask[jj, ii]): row += 1 Z = elev[jj, ii] # build the array or coordinates coords = misc.make_poly_coords( xarr=X[jj : jj + 2, ii : ii + 2], yarr=Y[jj : jj + 2, ii : ii + 2], zpnt=Z, triangles=triangles ) # build the attributes props = OrderedDict( id=row, river=river, reach=reach, ii=ii + 2, jj=jj + 2, elev=Z, ii_jj="{:02d}_{:02d}".format(ii + 2, jj + 2), ) # append to file is coordinates are not masked # (masked = beyond the river boundary) if coords is not None: record = misc.make_record(row, coords, "Polygon", props) out.write(record)
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)
def test_bad_geom(self): misc.make_record(1, self.non_point_array, 'Circle', self.props)
def test_polygon_array(self): record = misc.make_record(1, self.non_point_array, 'Polygon', self.props) nt.assert_dict_equal(record, self.known_polygon)
def test_line_array(self): record = misc.make_record(1, self.non_point_array, 'LineString', self.props) nt.assert_dict_equal(record, self.known_line)
def test_point(self): record = misc.make_record(1, self.point, 'Point', self.props) nt.assert_dict_equal(record, self.known_point)