def test_make_geocube__like_error_invalid_args(load_extra_kwargs): soil_attribute_list = [ "om_r", "sandtotal_r", "silttotal_r", "claytotal_r", "cec7_r", "ph1to1h2o_r", "dbthirdbar_r", "awc_r", ] with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: with pytest.raises(AssertionError): make_geocube( vector_data=os.path.join(TEST_INPUT_DATA_DIR, "soil_data_flat.geojson"), measurements=soil_attribute_list, like=xdc, fill=-9999.0, **load_extra_kwargs, )
def test_make_geocube__group_by_like_error_invalid_args(load_extra_kwargs): soil_attribute_list = [ "cokey", "mukey", "drclassdcd", "hzdept_r", "hzdepb_r", "sandtotal_r", "silttotal_r", "claytotal_r", ] with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_group.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: with pytest.raises(AssertionError): make_geocube( vector_data=os.path.join( TEST_INPUT_DATA_DIR, "soil_data_group.geojson" ), measurements=soil_attribute_list, like=xdc, group_by="hzdept_r", fill=-9999.0, **load_extra_kwargs, )
def test_make_geocube__no_resolution_error(): with pytest.raises(RuntimeError): make_geocube( vector_data=os.path.join(TEST_INPUT_DATA_DIR, "soil_data_flat.geojson"), measurements=["sandtotal_r"], output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), )
def test_make_geocube__group_by__no_resolution_error(): with pytest.raises(RuntimeError): make_geocube( vector_data=TEST_INPUT_DATA_DIR / "soil_data_group.geojson", measurements=["sandtotal_r"], output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), group_by="hzdept_r", fill=-9999.0, )
def test_make_geocube__group_by__categorical(input_geodata, tmpdir): input_geodata["soil_type"] = [ "sand", "bob", "clay", "sand", "silt", "clay", "sand", ] * 11 soil_attribute_list = ["sandtotal_r", "silttotal_r", "soil_type", "claytotal_r"] out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), group_by="hzdept_r", resolution=(-10, 10), categorical_enums={"soil_type": ("sand", "silt", "clay")}, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_group_categorical.nc")) ) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_group_categorical.nc"), autoclose=True, mask_and_scale=False, ) as xdc: xarray.testing.assert_allclose(out_grid, xdc) tmpdir.remove()
def write_raster(self, df): # TODO: https://github.com/corteva/geocube/blob/master/geocube/geo_utils/geobox.py#L79 df.rename(columns={'geom': 'geometry'}, inplace=True) cube = make_geocube(df, measurements=["elevation"], resolution=(-0.0001, 0.0001)) cube.elevation.rio.to_raster(self.filename, bigtiff=self.bigtiff)
def rasterize(layer, layer_type): if layer_type == 'Boundary': raster = make_geocube( vector_data=layer, measurements=['code'], resolution=(x_grid_step, y_grid_step), fill=0 # optional ) else: raster = make_geocube( vector_data=layer, measurements=['code'], resolution=(x_grid_step, y_grid_step), # fill=999 # optional ) return raster
def test_make_geocube__only_resolution(input_geodata, tmpdir): soil_attribute_list = [ "om_r", "sandtotal_r", "silttotal_r", "claytotal_r", "cec7_r", "ph1to1h2o_r", "dbthirdbar_r", "awc_r", ] out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, resolution=(-0.001, 0.001), fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( tmpdir.mkdir("make_geocube_soil") / "soil_grid_flat_original_crs.nc") # test output data with xarray.open_dataset( TEST_COMPARE_DATA_DIR / "soil_grid_flat_original_crs.nc", mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc)
def test_make_geocube__categorical(input_geodata, tmpdir): input_geodata["soil_type"] = [ "sand", "silt", "clay", "frank", "silt", "clay", "sand", ] out_grid = make_geocube( vector_data=input_geodata, output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), resolution=(-10, 10), categorical_enums={"soil_type": ("sand", "silt", "clay")}, fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_flat_categorical.nc")) ) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat_categorical.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc)
def test_make_geocube__like(input_geodata, tmpdir): soil_attribute_list = [ "om_r", "sandtotal_r", "silttotal_r", "claytotal_r", "cec7_r", "ph1to1h2o_r", "dbthirdbar_r", "awc_r", ] with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, like=xdc, fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_flat.nc")) ) xarray.testing.assert_allclose(out_grid, xdc)
def test_make_geocube(input_geodata, tmpdir): soil_attribute_list = [ "om_r", "sandtotal_r", "silttotal_r", "claytotal_r", "cec7_r", "ph1to1h2o_r", "dbthirdbar_r", "awc_r", ] out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), resolution=(-10, 10), fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf(str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_flat.nc"))) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc)
def test_make_geocube__group_by_only_resolution(input_geodata, tmpdir): soil_attribute_list = ["sandtotal_r", "silttotal_r", "claytotal_r"] out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, group_by="hzdept_r", resolution=(-0.001, 0.001), fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_grouped_original_crs.nc")) ) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_grouped_original_crs.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc) tmpdir.remove()
def test_make_geocube__group_by_like(input_geodata, tmpdir): soil_attribute_list = [ "cokey", "mukey", "drclassdcd", "hzdept_r", "hzdepb_r", "sandtotal_r", "silttotal_r", "claytotal_r", ] with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_group.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, group_by="hzdept_r", like=xdc, fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_group.nc")) ) xarray.testing.assert_allclose(out_grid, xdc) tmpdir.remove()
def test_make_geocube__group_by_convert_with_time(input_geodata, tmpdir): out_grid = make_geocube( vector_data=input_geodata, datetime_measurements=["test_time_attr"], resolution=(-0.00001, 0.00001), group_by="test_attr", fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_time").join("vector_data_group.nc")) ) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "vector_data_group.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc) assert out_grid.test_time_attr.attrs["units"] == "seconds from 1970-01-01T00:00:00" assert out_grid.test_time_attr.attrs["_FillValue"] == 0 tmpdir.remove()
def create_geocube_density_map(self,like=None): if like is None: minx,miny,maxx,maxy=self.polygon_df.total_bounds r = ((maxx - minx) / self.imsize[0]) print(r) self.geocube_resolution = (r,r) self.gc = make_geocube(self.polygon_df, [self.data_key],resolution=self.geocube_resolution) else: self.gc = make_geocube(self.polygon_df, [self.data_key],like=like) im = self.gc.to_array().data[0] im = np.nan_to_num(im,nan=self.mean_val) self.imsize = im.shape self.padded_im = np.pad(im,pad_width=self.pad_width,constant_values=self.mean_val) if self.gaussian_blur is not None: self.padded_im = gaussian_filter(self.padded_im,self.gaussian_blur) np.savetxt(self.density_map_fname,self.padded_im)
def test_make_geocube__group_by(input_geodata, tmpdir): soil_attribute_list = [ "cokey", "mukey", "drclassdcd", "hzdept_r", "hzdepb_r", "sandtotal_r", "silttotal_r", "claytotal_r", ] out_grid = make_geocube( vector_data=input_geodata, measurements=soil_attribute_list, output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), group_by="hzdept_r", resolution=(-10, 10), fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_group.nc")) ) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_group.nc"), mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc) tmpdir.remove()
def vector(input, output): from geocube.api.core import make_geocube grid = make_geocube( vector_data=merged_gpd, resolution=(100, 100), ) grid.present.rio.to_raster(output, compress="DEFLATE") return (output)
def test_make_geocube__new_bounds_crs(): utm_cube = make_geocube( vector_data=os.path.join(TEST_INPUT_DATA_DIR, "wgs84_geom.geojson"), output_crs="epsg:32614", resolution=(-1, 1), ) assert_almost_equal(utm_cube.id.rio.bounds(), (1665478.0, 7018306.0, 1665945.0, 7018509.0))
def get_numpy(feature, shapefile, resolution=0.1): grid = make_geocube( vector_data=shapefile, measurements=[feature], resolution=(-resolution, resolution), ) slide = grid[feature] return slide
def gdf_to_dataarray(gdf, crs, resolution): """ df should be a geodataframe with a geometry column crs in format {'init': 'epsg:4326'} resolution in format (0.000629777416967, -0.000629777416967) """ envelope = gdf.unary_union.envelope rasterizeable_aoi = gpd.GeoDataFrame(crs=crs, geometry=[envelope]) rasterizeable_aoi[ 'value'] = 1 # allows us to make non empty dataset, required for resampling return make_geocube(vector_data=rasterizeable_aoi, resolution=resolution)['value']
def _generate_ground_truth(w, h, crop_size, annotation_polygon: Polygon, pixel_annotation_value): """ :param w: :param h: :param crop_size: :param annotation_polygon: :param pixel_annotation_value: :return: """ patch_mask_polygon = Polygon([(w, h), (w + crop_size, h), (w + crop_size, h + crop_size), (w, h + crop_size)]) patch_mask_polygon = gpd.GeoSeries(patch_mask_polygon) annotation_polygon = gpd.GeoSeries(annotation_polygon) # Get the intersection of the `patch mask and an annotation # # 'patch_mask' would fed into GeoDataFrame as dataset. gdf_mask = gpd.GeoDataFrame({ 'geometry': patch_mask_polygon, 'patch_mask': pixel_annotation_value }) gdf_curr_annotation = gpd.GeoDataFrame({'geometry': annotation_polygon}) gdf_mask_curr_anno_diff = gpd.overlay(gdf_mask, gdf_curr_annotation, how='intersection') if not gdf_mask_curr_anno_diff.empty: # 'geom' work as boundary box mask_curr_anno_intersection_rasterized = \ make_geocube(vector_data=gdf_mask_curr_anno_diff, resolution=(1., 1.), geom=json.dumps(mapping(box(w, h, w+crop_size, h+crop_size))), fill=opt.pixel_anno_ignore) # TODO: refactor a transformation of geocube data to numpy array intersection_data = mask_curr_anno_intersection_rasterized.to_dict() intersection_data = intersection_data['data_vars']['patch_mask'][ 'data'] patch_ground_truth = np.array(intersection_data) return patch_ground_truth return np.full((crop_size, crop_size), pixel_annotation_value).astype(np.float)
def test_make_geocube__minimize_dtype(dtype, fill, expected_type, tmpdir): gdf = gpd.read_file(TEST_INPUT_DATA_DIR / "soil_data_flat.geojson") gdf["mask"] = 1 gdf["mask"] = gdf["mask"].astype(dtype) out_grid = make_geocube( vector_data=gdf, measurements=["mask"], output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), resolution=(-10, 10), fill=fill, ) assert out_grid.mask.dtype.name == expected_type # test writing to netCDF out_grid.to_netcdf( tmpdir.mkdir("make_geocube_soil") / "soil_grid_flat_mask.nc")
def test_make_geocube__custom_rasterize_function__filter_null( function, compare_name, tmpdir): input_geodata = os.path.join(TEST_INPUT_DATA_DIR, "point_with_null.geojson") out_grid = make_geocube( vector_data=input_geodata, resolution=(-0.00001, 0.00001), rasterize_function=function, ) # test writing to netCDF out_grid.to_netcdf(str(tmpdir.mkdir("geocube_custom").join(compare_name))) # test output data with xarray.open_dataset(os.path.join(TEST_COMPARE_DATA_DIR, compare_name), mask_and_scale=False) as xdc: xarray.testing.assert_allclose(out_grid, xdc, rtol=0.1, atol=0.1)
def __call__(self, abstract_geometry: AbstractGeometry): circles, box_coord_dim = abstract_geometry.get_geom_info() grid_size = self._calculate_grid_size(box_coord_dim) boxx = self._make_box(circles, box_coord_dim) vector_data = self._make_geopandas_dataframe(circles, boxx) raster_array = make_geocube(vector_data=vector_data, resolution=grid_size, fill=0) x = np.array(raster_array.x) y = np.array(raster_array.y) raster_image = np.array(raster_array.tags) return x, y, raster_image
def rasterize(src_vector, out_rst, res=30, column=None, verbose=False): """ Burns vector geometries into a raster. Args : src_vector (str) : path to the source vector shapefile out_rst (str, optional) : path to the output raster res (int, optional) : the resolution of the output raster. If none, the default landsat 7 30m res will be used column (str, optional) : the name of the column to use as value in the output raster. default ot the first one """ # apply the verbose option v_print = custom_print(verbose) # read the vector data gdf = gpd.read_file(src_vector).to_crs("EPSG:4326") # identify the column to be burn in the raster if not column: column = gdf.columns[0] # optimize dtype dtype = rio.dtypes.get_minimum_dtype(gdf[column]) # optimize the nodata value to meet the dtype fill = np.nan if np.issubdtype(dtype, np.integer): fill = 0 # convert the metric resolution into deg (as we work in EPSG 4326) # consider the equator approximation : 1° = 111 Km res = (res / 111) * (10**(-3)) out_grid = make_geocube(vector_data=gdf, measurements=[column], resolution=(-res, res), fill=fill) # write the column to raster file out_grid[column].rio.to_raster(out_rst, dtype=dtype) v_print( f'The vector geometries have been burn into the raster : {out_rst}') return
def make_geocube( output_file, vector_data, measurements, output_crs, resolution, # pylint: disable=too-many-arguments align, geom, like, fill, group_by, interpolate_na_method, ): # NOSONAR """ Utility to load vector data into the xarray raster format. """ if not resolution and isinstance(resolution, tuple): resolution = None if not align and isinstance(align, tuple): align = None if not measurements and isinstance(measurements, tuple): measurements = None if like is not None: like = xarray.open_dataset(like) try: gcds = core.make_geocube( vector_data=vector_data, measurements=measurements, output_crs=output_crs, resolution=resolution, align=align, geom=geom, like=like, fill=fill, group_by=group_by, interpolate_na_method=interpolate_na_method, ) finally: if like is not None: like.close() gcds.to_netcdf(output_file)
def fraction_raster(init_gdf, value, template_rst, out_rst): gdf = init_gdf.copy() gdf[f'{value}%'] = gdf[value] / gdf.sum(axis=1) * 100 with rasterio.open(template_rst) as src: gt = src.transform resX = gt[0] resY = -gt[4] cube = make_geocube( gdf, measurements=[f'{value}%'], resolution=(resX, resY), ) cube[f'{value}%'].rio.to_raster(out_rst) return
def test_make_geocube__no_measurements(input_geodata, tmpdir): out_grid = make_geocube( vector_data=input_geodata, output_crs=TEST_GARS_PROJ, geom=json.dumps(mapping(TEST_GARS_POLY)), resolution=(-10, 10), ) # test writing to netCDF out_grid.to_netcdf( str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_flat.nc"))) # test output data with xarray.open_dataset(os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat.nc"), mask_and_scale=False) as xdc: xarray.testing.assert_allclose(out_grid, xdc) tmpdir.remove()
def GeodataframeToTiff(geodata, lipascode, lipasname): """ This function turns minimum travel times of each travel method (10) into a TIFF-raster. Arguments: Function takes a geodataframe (made with TableJoiner()-function) as geodata, and sport facility code as lipascode and name as lipasname. Geodataframe has to have minimum travel times to sport facility in columns specified in attr_list. Function has been designed for spatial resolution of 250m x 250m (MetropAccess_YKR_grid). As a return user gets 10 tiff-files saved to output folder. """ # defining attributes that will be tranformed into TIFF-files attr_list = [ "min_t_bike_f", "min_t_bike_s", "min_t_pt_r_t", "min_t_pt_r_tt", "min_t_pt_m_t", "min_t_car_r", "min_t_car_m", "min_t_walk", "min_t_car_sl", "min_t_pt_m_tt" ] # creating a raster cube from geodataframe and selected attribute values cube = make_geocube(vector_data=geodata, measurements=attr_list, resolution=(250, -250)) # writing all cube.min_t_bike_f.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_bike_f_t.tiff") cube.min_t_pt_r_t.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_pt_r_t.tiff") cube.min_t_car_r.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_car_r_t.tiff") cube.min_t_pt_r_tt.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_pt_r_tt.tiff") cube.min_t_bike_s.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_bike_s_t.tiff") cube.min_t_pt_m_t.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_pt_m_t.tiff") cube.min_t_pt_m_tt.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_pt_m_tt.tiff") cube.min_t_car_sl.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_car_sl_t.tiff") cube.min_t_car_m.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_car_m_t.tiff") cube.min_t_walk.rio.to_raster("outputs/" + lipascode + "_" + lipasname + "_walk_f_t.tiff") print("Files saved.")
def test_make_geocube__custom_rasterize_function(function, compare_name, tmpdir): input_geodata = os.path.join(TEST_INPUT_DATA_DIR, "time_vector_data.geojson") out_grid = make_geocube( vector_data=input_geodata, measurements=["test_attr", "test_time_attr", "test_str_attr"], resolution=(-0.00001, 0.00001), rasterize_function=function, fill=-9999.0, ) # test writing to netCDF out_grid.to_netcdf(str(tmpdir.mkdir("geocube_custom").join(compare_name))) # test output data with xarray.open_dataset( os.path.join(TEST_COMPARE_DATA_DIR, compare_name), mask_and_scale=False, decode_coords="all", ) as xdc: xarray.testing.assert_allclose(out_grid, xdc, rtol=0.1, atol=0.1)