taking the zoom factor into account

stable
David Decotigny 2009-08-26 15:19:06 +02:00 zatwierdzone przez david
rodzic eb4cc82469
commit 7a393b96c7
4 zmienionych plików z 37 dodań i 12 usunięć

Wyświetl plik

@ -49,3 +49,13 @@ class BoundingBox:
"""Return a new bbox of the same size + dlat/dlong added on the sides"""
return BoundingBox(self._lat1+dlat, self._long1 - dlong,
self._lat2-dlat, self._long2 + dlong)
def get_pixel_size_for_zoom_factor(self, zoom = 17):
"""Return the size in pixels (tuple width,height) needed to
render the bounding box at the given zoom factor"""
delta_lat = abs(self._lat1 - self._lat2)
delta_long = abs(self._long1 - self._long2)
pix_x = delta_long * (2 ** (zoom + 8)) / 360
pix_y = delta_lat * (2 ** (zoom + 8)) / 180
return (int(math.ceil(pix_x)),
int(math.ceil(pix_y)))

Wyświetl plik

@ -93,21 +93,27 @@ class MapCanvas:
"""
OSM in the background of a canvas used to draw grids and text.
"""
def __init__(self, mapfile_path, bbox, grwidth = 800, grheight = 600):
def __init__(self, mapfile_path, geographic_bbox, graph_bbox = None):
"""
@param mapfile_path (string) path the the osm.xml map file
@param envelope (BoundingBox) bounding box to render, in
@param geographic_bbox (BoundingBox) bounding box to render, in
latlong (4326) coordinates
@param grwidth/grwidth (int) graphical width/height of the
rendered area for raster output
@param graph_bbox (int) graphical width/height of the
rendered area for raster output (None = auto)
"""
self._projname = GLOBALS.MAIN_PROJECTION
self._proj = mapnik.Projection(self._projname)
self._envelope = mapnik.Envelope(bbox.get_top_left()[1],
bbox.get_top_left()[0],
bbox.get_bottom_right()[1],
bbox.get_bottom_right()[0])
self._map = mapnik.Map(grwidth, grheight, self._projname)
if graph_bbox is None:
graph_bbox = geographic_bbox.get_pixel_size_for_zoom_factor()
elif str(graph_bbox).startswith('zoom:'):
graph_bbox = geographic_bbox.get_pixel_size_for_zoom_factor(int(graph_bbox[5:]))
self._envelope = mapnik.Envelope(geographic_bbox.get_top_left()[1],
geographic_bbox.get_top_left()[0],
geographic_bbox.get_bottom_right()[1],
geographic_bbox.get_bottom_right()[0])
self._map = mapnik.Map(graph_bbox[0],
graph_bbox[1],
self._projname)
mapnik.load_map(self._map, mapfile_path)
# Keep geographic bounding box, ignoring one dimension of the

Wyświetl plik

@ -178,14 +178,14 @@ class OCitySMap:
return sorted(map(_humanize_street_label, sl),
lambda x, y: cmp(x[0].lower(), y[0].lower()))
def render_into_files(self, osm_map_file, out_filenames):
def render_into_files(self, osm_map_file, out_filenames, zoom_factor):
GRID_COLOR = '#8BB381'
l.debug('rendering from %s to %s...' % (osm_map_file, out_filenames))
g = self.griddesc.generate_shape_file('x.shp')
bbox = self.boundingbox.create_expanded(self.griddesc.height_square_angle/2.,
self.griddesc.width_square_angle/2.)
city = map_canvas.MapCanvas(osm_map_file, bbox)
city = map_canvas.MapCanvas(osm_map_file, bbox, zoom_factor)
city.add_shapefile(g.get_filepath(), GRID_COLOR)
l.debug('adding labels...')
for idx, label in enumerate(self.griddesc.vertical_labels):

Wyświetl plik

@ -23,6 +23,9 @@ def main():
nargs=3, metavar='NAME BBOX',
help='Specify a zoomed section by its named '
'bounding box.')
parser.add_option('-f', '--zoom-factor',
metavar='[0-18]', help='Zoom factor for the'
'rendering (default=16)', type='int', default =16)
parser.add_option('-x', '--osm-xml', dest='osm_xml', metavar='PATH',
help='Path to the osm.xml file')
@ -40,6 +43,11 @@ def main():
parser.error("Invalid path to the osm.xml file (%s)"
% options.osm_xml)
try:
options.zoom_factor = int(options.zoom_factor)
except ValueError:
parser.error("Invalid zoom factor: %s" % options.zoom_factor)
if not options.output:
options.output = ['citymap.svg']
@ -68,7 +76,8 @@ def main():
except KeyboardInterrupt:
sys.stderr.write(' Aborting.\n')
renderer.render_into_files(options.osm_xml, options.output)
renderer.render_into_files(options.osm_xml, options.output,
"zoom:%d" % options.zoom_factor)
return 0
if __name__ == '__main__':