coordinate-system – Creating an Image at Coordinates with Given Radius

coordinate systemmapnik

It seems, like a trivial task, but I struggle to understand where I am doing wrong.
I have a postgres db with maps, from which I create a mapnik::Map with whole map. Then, when I get query with coordinates and distance to border, I calculate the coordinates of left, bottom, right and top points using formulas to calculate destination point given distance and bearing from start point, then I use those to create mapnik::box2d and use it like this

whole_map_.zoom_to_box(box);
mapnik::image_32 im(CANVAS_HEIGHT, CANVAS_WIDTH);
mapnik::agg_renderer<mapnik::image_32> ren(whole_map_, im);
ren.apply();
  
mapnik::save_to_file(im, output_file);

However, the image that I get is not centered on coordinate and it is not using the actual box I am trying to use. In two examples, I am using coordinate (19.000000, 54.000000) and radiuses 30km and 120 km, this is what I expected to see, from QGis, I inserted points with coordinates I get from the formula above.

30 km distance
QGis 30
120 km distance
QGis 120

and the images I get are:

30 km distance
mapnik 30 km
120 km distance
mapnik 120 km

Easy to see, that image with 30 km radius should not have much water areas in it, perhaps only in top right corner, however is does on picture, the centering is off as well, but it is not that clear to see. Same goes for example with 120 km radius.

It seems, that mapnik is rendering pictures using the CANVAS_HEIGHT and CANVAS_WIDTH constants, rather then the provided box2d. How can I achieve the desired outcome?

Edit:
I fixed the centering part, I did mistake while rendering, I declared the map to be one size and outgoing picture to another, this produced such behavior. The last problem is can I cut off the edges, that are outside of the box that was calculated?

Best Answer

I solved both problems, the centering part was my mistake, I had different sizes for map and outgoing picture, which produced this shift. The projection part, I solved by googling a lot on very old forums, because there is no better option and I found this answer, which helped me understand, that xml file has not only source projection, but destination as well.

Also I don't have a link, but I found an example of how to transform mapnik::box2d from one projection to another. I did it like this

auto merc = mapnik::projection("+proj=merc");
auto longlat = mapnik::projection("+proj=longlat");
auto merc_bbox = mapnik::proj_transform(longlat, merc).forward(box);
box.re_center({2115070.325072196, 7170156.293999939});  // todo
whole_map_.zoom_to_box(box);

The re_center(...) part I still have to complete, but I am sure the formulas of converting latitude and longitude to merc projection are not hard to find. This two things produced image that I needed.

Related Question