Solved

How to dynamically set raster resolution based on line length values?


Badge

Hi there,

I've been toying a lot with rasters lately, but I've hit a bit of a snag. I need to create a raster from shape files that has a resolution based on another step that takes place in my workbench. I'll explain:

So first thing I have is a set of polyline geometries. In quite few steps (BoundingBoxAccumilator ->GeometryCoarcer ->Chopper ->LengthCalculator-> StatisticsCalculator) I know what the longest and shortest edge is of the bounding box of my geometry, by using the Minimum and Maximum based on the lengths calculated. Anyway, this is just to give some context, it is not the problem area. I did inspect this output and I do get for example Minimum:1000, Maximum 1500.

I also have a FeatureReader separately attached to the BoundingBoxAccumilator mentioned above, which calls a WMS layer and outputs a raster based on the inside of the bounding box. Naturally this will also have a Rows/Columns of 1000x1500 as it uses the same bounding box as my geometries.

Now the problem happens in my final step where I want to bring it all together. I now want to take the original polylines and rasterize them via the MapnikRasterizer. The catch is, I want to use the Minimum and Maximum values determined in the first step (by the StatisticsCalculator) to be used as the Columns and Rows values in the MapnikRasterizer, as I will be using the workbench on multiple separate geometries, and I want the values to always match the bounding box, and the WMS tile I'm calling.

I understand this is a long and confusing explanation, so here's an image of my workspace to make more sense of it all (EDGESHORT and EDGETALL is just Minimum and Maximum renamed:

When I do run the workbench I get the following error:

MapnikRasterizer(MapnikFactory): No values were specified for either both NUM_ROWS and NUM_COLUMNS, or SPACING. One or the other must be specified

A VERY IMPORTANT NOTE: In principal, my workspace works perfectly how I want it if I manually place 1000 and 1500 as the MapnikRasterizer's Columns and Rows. Then my output is a perfect raster of my WMS backdrop, and my Line geometries overlapping that, all at the same resolution. The issue is not there, but specifically on how to dynamically set the MapnikRasterizer's Columns and Rows based on whatever the bounding box extent is of my line geometries.

My aplogies for the wall of text, and thanks in advance for any help :)

icon

Best answer by daveatsafe 26 June 2018, 18:15

View original

2 replies

Userlevel 2
Badge +17

Hi @robbie_botha,

The first feature into the the MapnikRasterizer must have the EDGESHORT and EDGETALL attribute populated.

Rather than using the FeatureHolder, please add a Sorter right before the MapnikRasterizer. Create an attribute _sort on the bounding box and set it to 0. Create the same attribute on the other features and set it to 1. In the Sorter, sort by '_sort' ascending. This will ensure that the bounding box gets to the MapnikRasterizer first.

Badge

Hi @robbie_botha,

The first feature into the the MapnikRasterizer must have the EDGESHORT and EDGETALL attribute populated.

Rather than using the FeatureHolder, please add a Sorter right before the MapnikRasterizer. Create an attribute _sort on the bounding box and set it to 0. Create the same attribute on the other features and set it to 1. In the Sorter, sort by '_sort' ascending. This will ensure that the bounding box gets to the MapnikRasterizer first.

Hi @DaveAtSafe,

 

Thanks a lot, this works perfectly! One more question, now that everything is working as intended.

 

I'd like to add one more step, where I resize the image. Ideally, I'd most likely just identify the largest edge between the Rows and Columns, then resize that to 1000, and the smallest edge maintains the aspect ratio.

 

A few examples:

 

1500x600 = 1000x400

 

4000x3000 =1000x750

 

2000x8000 = 250x1000

 

 

This way my largest edge is always 1000, but the smaller edge scales to keep it's aspect ratio. Is there any way to do this? I see the RasterResampler can resize by percentage, but that doesn't quite solve my problem.

 

 

Thanks in advance,

 

 

Reply