Skip to content
Advertisement

How to rasterize a batch of polygons in python

I have a batch of polygons which could be a NumPy array, torch tensor, or any other nd-array of shape (230_000, 3, 2). 230_000 is the number of polygons, 3 indicates it’s a triangle, 2 is its x and y coordinates.

I also have a batch of features of shape (230_000, 3, 3) (last is the color), it’s best the color of the features are interpolated during rasterization.

I am trying to find a way to rasterize all of them such that the output looks something like this:

Polygon Rasterization

There are not many rules to the algorithm. what is most important is filled polygons rasterized on an ndarray and is fast. the batch size will be hundreds of thousands of polygons. for loops will not help.

Advertisement

Answer

If you are set on creating a raster with python, I would check out Rasterio. If your main goal is to create the above visualization with Python, an easier way to go about it would be to use shapely and matplotlib. It also seems like your potentially missing a piece–you also need a function that generates shapes. You mention:

230_000 is the number of polygons, 3 indicates it’s a triangle, 2 is its x and y coordinates

2 is the x and y coordinates of what point? more information is necessary to understand the type of shapes you need to generate.

AFAIK, what you need to do is:

Advertisement