Skip to content

Commit

Permalink
Add script for rendering tests with upstream style
Browse files Browse the repository at this point in the history
  • Loading branch information
giggls committed Jan 16, 2025
1 parent 8de9615 commit db1b5dc
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ localconfig.js
# Generated at runtime by Python.
*.pyc
**/*.pyc

testoutput
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
DBSCHEME = "de"

MAPNIK_API = 3.0.15

TEMPFILE := $(shell mktemp -u)

XMLSTYLE := osm.xml

TO := ./testoutput

# This target will render test tiles for l10n stuff
test: $(TO)/test-l10n1.png $(TO)/test-l10n2.png $(TO)/test-l10n3.png $(TO)/test-l10n4.png $(TO)/test-l10n5.png

# l10n (country-names)
$(TO)/test-l10n1.png: $(XMLSTYLE)
mkdir -p $(TO)
./scripts/render_single_tile.py -t -s $(XMLSTYLE) -o $@ -u /3/5/3.png

# l10n (latin + asian scripts)
$(TO)/test-l10n2.png: $(XMLSTYLE)
mkdir -p $(TO)
./scripts/render_single_tile.py -t -s $(XMLSTYLE) -o $@ -u /10/792/483.png

# l10n station (thai + latin)
$(TO)/test-l10n3.png: $(XMLSTYLE)
mkdir -p $(TO)
./scripts/render_single_tile.py -t -s $(XMLSTYLE) -o $@ -u /14/12777/7562.png

# l10n Seoul in Latin and korean script
$(TO)/test-l10n4.png: $(XMLSTYLE)
mkdir -p $(TO)
./scripts/render_single_tile.py -t -s $(XMLSTYLE) -o $@ -u /10/873/396.png

# l10n near east
$(TO)/test-l10n5.png: $(XMLSTYLE)
mkdir -p $(TO)
./scripts/render_single_tile.py -t -s $(XMLSTYLE) -o $@ -u /10/612/416.png

clean:
rm -f $(TO)/test-*.png
105 changes: 105 additions & 0 deletions scripts/render_single_tile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/python3
#
# render a single tile by either specifying an URL, z,x,y or lat/lon
#
# (c) 2016 Sven Geggus <[email protected]>

import argparse
import sys
import math
import os
import time

def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 2.0 ** zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)


def TileToMeters(tx, ty, zoom):
initialResolution = 20037508.342789244 * 2.0 / 256.0
originShift = 20037508.342789244
tileSize = 256.0
zoom2 = (2.0**zoom)
res = initialResolution / zoom2
mx = (res*tileSize*(tx+1))-originShift
my = (res*tileSize*(zoom2-ty))-originShift
return mx, my

def TileToBBox(x,y,z):
x1,y1=TileToMeters(x-1,y+1,z)
x2,y2=TileToMeters(x,y,z)
return x1,y1,x2,y2

if __name__ == "__main__":

parser = argparse.ArgumentParser(description='Render a single tile using Mapnik')
parser.add_argument('-s', '--stylefile', default='osm.xml', help='XML Mapnik style file')
parser.add_argument('-o', '--outputfile', help='output filename default=prefix-zval-xval-yval.png, "-" for stdout')
parser.add_argument('-p', '--prefix', default='mapnik')
parser.add_argument('-t', '--time', help='Output rendering time', default=False, action='store_true')
parser.add_argument('-f', '--custom-fonts-dir', help='custom font directory', default='/etc/mapnik-osm-data/fonts/')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-c', '--lonlat', nargs=3, help='zoom lon lat Position of tile')
group.add_argument('-m', '--zxy', nargs=3, help='z x y Name of Tile')
group.add_argument('-u', '--url', help='Tile URL from which z,x and y will be extracted')

args = parser.parse_args()

if os.path.exists(args.stylefile):
mapfile=args.stylefile
else:
sys.stderr.write("file %s not found\n" % args.stylefile);
sys.exit(1)

if args.url:
import re
# our URI is in the form http://<server>/<something>/<z>/<x>/<y>.<ext>
# we are only interested in /<z>/<x>/<y>.<ext>
regex='/([0-9]+)/([0-9]+)/([0-9]+)\.(...)'
res=re.findall(regex,args.url)
z = int(res[0][0])
x = int(res[0][1])
y = int(res[0][2])

if args.lonlat:
z=int(args.lonlat[0])
lonx=float(args.lonlat[1])
laty=float(args.lonlat[2])
x,y = deg2num(lonx, laty, z)

if args.zxy:
z=int(args.zxy[0])
x=int(args.zxy[1])
y=int(args.zxy[2])

if args.time:
start = time.time()

import mapnik
mapnik.register_fonts(args.custom_fonts_dir)
m = mapnik.Map(256, 256)
mapnik.load_map(m, mapfile)
bba=TileToBBox(x,y,z)
bbox=mapnik.Box2d(bba[0],bba[1],bba[2],bba[3])
m.zoom_to_box(bbox)
im = mapnik.Image(256, 256)
mapnik.render(m, im)
if args.outputfile:
ofile=args.outputfile
else:
ofile=args.prefix+'-'+str(z)+'-'+str(x)+'-'+str(y)+'.png'

if ofile != '-':
f = open(ofile, 'wb')
f.write(im.tostring('png'));
f.close()
else:
sys.stdout.write(im.tostring('png'));
if args.time:
end = time.time()
print("rendering time: %d seconds" % (end - start))


0 comments on commit db1b5dc

Please sign in to comment.