This repository was archived by the owner on Jul 26, 2018. It is now read-only.
forked from makinacorpus/osm-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender-raster.sh
More file actions
executable file
·113 lines (86 loc) · 3.59 KB
/
render-raster.sh
File metadata and controls
executable file
·113 lines (86 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
import os
import math
import re
import logging
import optparse
import urllib
try:
from mapnik import *
except ImportError:
from mapnik2 import *
OSM_MIRROR_CONF = '/etc/default/openstreetmap-conf'
"""
Configuration
Read from command-line arguments, with defaults coming from OSM_MIRROR_CONF
"""
def readconf():
config = open(OSM_MIRROR_CONF).readlines()
config = [line.strip().replace('"', '') for line in config]
return dict([re.split(r'\s*=\s*', line) for line in config])
config = readconf()
parser = optparse.OptionParser(usage="""%prog <stylename> <filename> [options]""")
parser.add_option('-e', '--extent', dest='extent', nargs=4,
help='Geographical bounding box. Two long lat pairs e.g. 2.04 43.88 2.22 43.98 (Albi)',
type='float',
default=[float(v) for v in config['EXTENT'].split(',')],
action='store')
parser.add_option('-s', '--scale', dest='scale',
help='Scale as integer (e.g. 25000 for 1:25000)',
type='int',
default=25000,
action='store')
parser.add_option('-p', '--projection', dest='projection',
help='Projection SRID',
type='int',
default=2154,
action='store')
(options, args) = parser.parse_args()
style = args[0]
output = '%s.png' % args[1]
stylefile = os.path.join('styles', style, '%s.xml' % style)
extension = 'png'
output_tif = output.replace('.png', '.tif')
pixels_per_m = 100.0 / 0.028 # 1 pixel = 0.28mm
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="\033[92m%(message)s\033[0m")
def get_spatial_reference(code):
if code == 2154:
return "+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
url = 'http://spatialreference.org/ref/epsg/%s/proj4/' % code
logger.info('Download spatial reference for %s' % code)
resp = urllib.urlopen(url).read()
return resp
def main():
global options
wgs84 = Projection('+proj=latlong +datum=WGS84')
srs = get_spatial_reference(options.projection)
projection = Projection(srs)
env = ProjTransform(wgs84, projection)
bbox = env.forward(Box2d(*options.extent))
width_m = bbox.width() / options.scale
height_m = bbox.height() / options.scale
width_pix = int(math.ceil(pixels_per_m * width_m))
height_pix = int(math.ceil(pixels_per_m * height_m))
map = Map(width_pix, height_pix)
load_map(map, stylefile)
map.srs = srs
map.zoom_to_box(bbox)
logger.info('Extent size: %sm x %sm' % (bbox.width(), bbox.height()))
logger.info('Scale: 1:%s' % options.scale)
logger.info('Scaled size: %sm x %sm' % (width_m, height_m))
logger.info('Image size: %s x %s' % (width_pix, height_pix))
logger.info('Map extent: %s' % map.envelope())
logger.info('Map center: %s' % map.envelope().center())
logger.info('At current scale of %s...' % map.scale())
logger.info('Map scale denominator: %s' % map.scale_denominator())
render_to_file(map, output, extension)
base_cmd = 'gdal_translate %s %s -a_srs "%s" %s %s'
georeference = '-a_ullr %s %s %s %s' % (bbox.minx, bbox.maxy,
bbox.maxx, bbox.miny)
opts = '-co "TFW=YES" -co compress=LZW'
cmd = base_cmd % (output, output_tif, srs, georeference, opts)
os.system(cmd)
logger.info("GeoTIFF '%s' created with projection EPSG:%s." % (output_tif, options.projection))
if __name__ == '__main__':
main()