Geographic Information Systems Asked by Kaanr on November 1, 2021
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?
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)
Answered by ThomasG77 on November 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP