1

How can I convert my Shapely polygon to a numpy pixel array representing a boolean mask, with True for where the polygon is?

1 Answer 1

4

If you mean rasterizing your polygon, I recommend geocube

Here is a simple example assuming the data is in the WGS 84 projection:

import geopandas as gpd

from geocube.api.core import make_geocube

gdf = gpd.GeoDataFrame({"mask": [1]}, geometry=[shapely_geom], crs="EPSG:4326")
cube = make_geocube(gdf, resolution=(-0.001, 0.001), fill=0)
arr_mask = cube.mask.values == 1

Not the answer you're looking for? Browse other questions tagged or ask your own question.