3

I recently manually built a tile server with this instruction -> https://switch2osm.org/serving-tiles/manually-building-a-tile-server-20-04-lts/. I downloaded location data for Russia from geofabrik.de and then loaded to database by osm2pgsql. Now all working nicely - Apache, mod_tile, renderd, Mapnik and PostgreSQL with PostGIS.

Now I want to create a program on Python for generating a PNG file with specific region (box). This region will be defined by coordinates (left and right corners of box). I write this code:

import mapnik

mapnik_xml = "openstreetmap-carto/mapnik.xml"
map_output = "region_map_mapnikXml.png"

# Create a map object
m = mapnik.Map(600,300)
mapnik.load_map(m, mapnik_xml)
bbox = mapnik.Box2d(45.23, 41.34, 46.23, 43.23)
m.zoom_to_box(bbox)
mapnik.render_to_file(m, map_output)
print(f"Rendered image to {map_output}")

But after executing this code created only blank file with background. I don't figure out why? Can you help me please.

Additionaly, if I run this code:

import mapnik

mapnik_xml = "openstreetmap-carto/mapnik.xml"
map_output = "world_map_mapnikXml.png"

# Create a map object
m = mapnik.Map(600,300)
mapnik.load_map(m, mapnik_xml)
m.zoom_all()
mapnik.render_to_file(m, map_output)
print(f"Rendered image to {map_output}")

in results, generated PNG file - world map with shape of mainlands and countries.

Why is this happening?

1 Answer 1

2

You may need to use a bbox with coordinates using EPSG 3857 because the OpenStreetMap Carto style use it e.g https://github.com/gravitystorm/openstreetmap-carto/blob/master/project.mml#L18

So, bbox = mapnik.Box2d(45.23, 41.34, 46.23, 43.23) should be instead bbox = mapnik.Box2d(5034980.57, 5062621.68, 5146300.06, 5347045.97)

You may want to use PyProj to calculate EPSG 3857 coordinates from longitude, latitude with

from pyproj import Proj, transform

inProj = Proj('epsg:4326')
outProj = Proj('epsg:3857')

x1,y1 = -105.150271116, 39.7278572773
x2,y2 = transform(inProj,outProj,x1,y1)
print(x2,y2)

PS: Proj recipe borrowed from Converting projected coordinates to lat/lon using Python? but doing the opposite operation (EPSG 4326 to EPSG 3857)

3
  • Thank you! I truly appreciate your help answer!
    – Kaanr
    Jul 15, 2020 at 5:43
  • Can you tell me or where I can read about it - what this string mean? ''' <Map background-color="#f2efe9" srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"> '''
    – Kaanr
    Jul 15, 2020 at 5:52
  • 1
    Go to epsg.io/3857 In section "Export", click Mapnik. This string is the Mapnik xml equivalent to declare your map is using background-color "#f2efe9" and srs (spatial reference system) "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"
    – ThomasG77
    Jul 15, 2020 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.