1

After setting up a tile server accordingly this instruction https://switch2osm.org/serving-tiles/manually-building-a-tile-server-20-04-lts/ my offline tile server working nicely.

And after helping of ThomasG77 in this question How to create a map to PNG file with specific box coordinates by Mapnik? now I can generate PNG file with specific region (box) by this code on Python:

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(5034980.57, 5062621.68, 5146300.06, 5347045.97) m.zoom_to_box(bbox) mapnik.render_to_file(m, map_output) print(f"Rendered image to {map_output}")

But now I am facing the following problem: I need to generate PNG file of specific region with marker (something like small image e.g. green pin) in center of this region. How I can do that?

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

1 Answers1

3

You can find below a sample to do it.

import json
import mapnik

mapnik_xml = "openstreetmap-carto/mapnik.xml" map_output = "region_map_mapnikXml.png" epsg_3857 = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs" bbox_array = [5034980.57, 5062621.68, 5146300.06, 5347045.97]

Create a map object

m = mapnik.Map(600,300)

Uncomment if you want a standalone sample

m.srs = epsg_3857

Comment if you want a standalone sample without loading

the OpenStreetMap style

mapnik.load_map(m, mapnik_xml)

Create point style

s = mapnik.Style() # style object to hold rules r = mapnik.Rule() # rule object to hold symbolizers

Create symbolizer

point_sym = mapnik.MarkersSymbolizer() point_sym.fill = mapnik.Color('green') point_sym.allow_overlap = True point_sym.width = mapnik.Expression("20") point_sym.height = mapnik.Expression("20")

r.symbols.append(point_sym) # add the symbolizer to the rule object s.rules.append(r) m.append_style('center', s)

Create datasource

ds = mapnik.MemoryDatasource() f = mapnik.Feature(mapnik.Context(), 1)

f.geometry = f.geometry.from_wkt("POINT({} {})".format((bbox_array[0] + bbox_array[2]) / 2, (bbox_array[1] + bbox_array[3]) / 2)) ds.add_feature(f)

center_layer = mapnik.Layer('center_layer') center_layer.srs = epsg_3857 center_layer.datasource = ds center_layer.styles.append('center') m.layers.append(center_layer)

bbox = mapnik.Box2d(*bbox_array) m.zoom_to_box(bbox) mapnik.render_to_file(m, map_output) print(f"Rendered image to {map_output}")

ThomasG77
  • 30,725
  • 1
  • 53
  • 93
  • ThomasG77, thank you!!! Awesome! You are my savior:)) Can you tell me some resources about Mapnik and how to work with them, since I want to create hints to these markers, i.e. attach some information to these markers. Can you help me again? :) – Kaanr Jul 16 '20 at 06:27