Cropping raster data

Hi,

I need to crop geo raster data using a polygon. What are the ways I have to follow?

Regards

I'm not familiar with Rust geo libraries so I can't tell you what to use, but here is a general strategy for the purpose:

  1. Transform the polygon into the coordinate system of the raster, if it is not already. (Note that in the general case, this may require introducing additional vertices; changes of projection do not preserve the straightness of edges.)
  2. Using a vector graphics renderer, paint the polygon onto a blank raster image that fits it.
  3. For each pixel in that image, if it is not transparent, replace its color with the color from the corresponding pixel of the original data image.

Depending on the vector graphics library you use, it may also have a built-in way to fill a shape with an image, without needing a separate step 3. Look at what options you have available there.

The key part is that once you have brought your two data sources into the same coordinate system, you don't need geo-specific algorithms, just regular graphics algorithms.

1 Like

Thank you for your interest,
Question is, when I rasterize the polygon, how can I push the data to newly created raster map, which is coming from original raster map?
Setting color of new raster will be dependent on raster values

Regards

That depends entirely on what the library you use for the rasterization wants as input. It might be as simple as a Vec<u8> of pixels or it might be a specific image buffer type.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.