def polygon_2_raster(polygon: gpd.GeoDataFrame, rst_fn: str, out_fn: str, out_shape: List[int]) -> None: '''This function take in a geopandas object of polygons and converts them into a raster image based on the 'temp_rast' raster Input: polygon -- the geopandas object of polygons rst_fn -- the filename of the raster that will be the template upon which the polygons will be burned out_fn -- the output file name that the raster of the polygons will be written to out_shape -- a two element list of the x and y shape of the output raster image Output: ''' with rio.open(rst_fn, 'r') as rst: meta = rst.meta.copy() meta.update(compress='lzw') with rio.open(out_fn, 'w', **meta) as out: #we create a generator of geom, value pairs to use in the features.rasterize function shapes = ((feature['geometry'], 1) for feature in polygon.iterfeatures()) labels = features.rasterize(shapes=shapes, out_shape=out_shape, transform=meta['transform'], fill=0, dtype='uint16', all_touched=False) out.write(labels, indexes=1)
def test_geodataframe_iterfeatures(self): df = self.df.iloc[:1].copy() df.loc[0, "BoroName"] = np.nan # when containing missing values # null: ouput the missing entries as JSON null result = list(df.iterfeatures(na="null"))[0]["properties"] assert result["BoroName"] is None # drop: remove the property from the feature. result = list(df.iterfeatures(na="drop"))[0]["properties"] assert "BoroName" not in result.keys() # keep: output the missing entries as NaN result = list(df.iterfeatures(na="keep"))[0]["properties"] assert np.isnan(result["BoroName"]) # test for checking that the (non-null) features are python scalars and # not numpy scalars assert type(df.loc[0, "Shape_Leng"]) is np.float64 # null result = list(df.iterfeatures(na="null"))[0] assert type(result["properties"]["Shape_Leng"]) is float # drop result = list(df.iterfeatures(na="drop"))[0] assert type(result["properties"]["Shape_Leng"]) is float # keep result = list(df.iterfeatures(na="keep"))[0] assert type(result["properties"]["Shape_Leng"]) is float # when only having numerical columns df_only_numerical_cols = df[["Shape_Leng", "Shape_Area", "geometry"]] assert type(df_only_numerical_cols.loc[0, "Shape_Leng"]) is np.float64 # null result = list(df_only_numerical_cols.iterfeatures(na="null"))[0] assert type(result["properties"]["Shape_Leng"]) is float # drop result = list(df_only_numerical_cols.iterfeatures(na="drop"))[0] assert type(result["properties"]["Shape_Leng"]) is float # keep result = list(df_only_numerical_cols.iterfeatures(na="keep"))[0] assert type(result["properties"]["Shape_Leng"]) is float with pytest.raises( ValueError, match="GeoDataFrame cannot contain duplicated column names."): df_with_duplicate_columns = df[[ "Shape_Leng", "Shape_Leng", "Shape_Area", "geometry" ]] list(df_with_duplicate_columns.iterfeatures()) # geometry not set df = GeoDataFrame({ "values": [0, 1], "geom": [Point(0, 1), Point(1, 0)] }) with pytest.raises(AttributeError): list(df.iterfeatures())