xrspatial.zonal.suggest_zonal_canvas

xrspatial.zonal.suggest_zonal_canvas(smallest_area: Union[int, float], x_range: Union[tuple, list], y_range: Union[tuple, list], crs: str = 'Mercator', min_pixels: int = 25)tuple[source]

Given a coordinate reference system (crs), a set of polygons with corresponding x range and y range, calculate the height and width of canvas so that the smallest polygon (polygon with smallest area) is rasterized with at least min pixels.

Currently, we assume that the smallest polygon does not intersect others. One should note that a polygon can have different shapes when it is rasterized in canvases of different size. Thus, we cannot 100% guarantee the actual number of pixels after rasterization. It is recommended to add an additional of 5% to @min_pixels parameter.

x_range: tuple or list of 2 numeric elements,

The full x extent of the polygon GeoDataFrame.

y_range: tuple or list of 2 numeric elements,

The full y extent of the polygon GeoDataFrame.

smallest_area: numeric (float, int)

Area of the smallest polygon.

crs: str (default = ‘Mercator)

Name of the coordinate reference system.

min_pixels: int (default = 25)

Expected number of pixels of the polygon with smallest area when the whole dataframe is rasterized.

height, width: int, int

height and width of the canvas in pixel space

Imports >>> from spatialpandas import GeoDataFrame >>> import geopandas as gpd >>> import datashader as ds

>>> df = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
>>> df = df.to_crs("EPSG:3857")
>>> df = df[df.continent != 'Antarctica']
>>> df['id'] = [i for i in range(len(df.index))]
>>> xmin, ymin, xmax, ymax = (df.bounds.minx.min(), df.bounds.miny.min(),
… df.bounds.maxx.max(), df.bounds.maxy.max())
>>> x_range = (xmin, xmax)
>>> y_range = (ymin, ymax)
>>> smallest_area = df.area.min()
>>> min_pixels = 20
>>> height, width = suggest_zonal_canvas(x_range=x_range, y_range=y_range,

… smallest_area=smallest_area, … crs=’Mercator’, … min_pixels=min_pixels)

>>> cvs = ds.Canvas(x_range=x_range, y_range=y_range,
… plot_height=height, plot_width=width)
>>> spatial_df = GeoDataFrame(df, geometry='geometry')
>>> agg = cvs.polygons(spatial_df, 'geometry', agg=ds.max('id'))
>>> min_poly_id = df.area.argmin()
>>> actual_min_pixels = len(np.where(agg.data==min_poly_id)[0])