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:
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:
- iterate yours arrays (no way around it)
- define a function that takes an individual array and returns a shapely polygon from the supplied inputs https://autogis-site.readthedocs.io/en/latest/notebooks/L1/geometric-objects.html From there I would:
- convert to a geopandas dataframe https://geopandas.org/en/stable/docs/reference/geodataframe.html
- convert the GeoDataFrame to GeoJSON https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.to_json.html
- pass the geojson to rasterio https://rasterio.readthedocs.io/en/latest/cli.html