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?

Vince
  • 20,017
  • 15
  • 45
  • 64
Kaanr
  • 47
  • 5

1 Answers1

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)

Kaanr
  • 47
  • 5
ThomasG77
  • 30,725
  • 1
  • 53
  • 93
  • Thank you! I truly appreciate your help answer! – Kaanr Jul 15 '20 at 05:43
  • Can you tell me or where I can read about it - what this string mean? ''' ''' – Kaanr Jul 15 '20 at 05:52
  • 1
    Go to https://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 '20 at 06:35