Skip to content

Commit

Permalink
changed output projection in rendering code
Browse files Browse the repository at this point in the history
  • Loading branch information
bnordgren committed Dec 9, 2016
1 parent 20916ba commit bf5190b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
42 changes: 24 additions & 18 deletions coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,23 @@ def envPixelToLL(self, env, zoom):
GOOGLE_PROJECTION = GoogleProjection()
LATLONG_PROJECTION = Projection(LATLONG_PROJECTION_DEF)
MERCATOR_PROJECTION = Projection(MERCATOR_PROJECTION_DEF)
MT_STATE_PROJECTION = Projection(MT_STATE_PROJECTION_DEF)

# Use this projection for the map:
OUTPUT_PROJECTION = MT_STATE_PROJECTION


##### Geographic coordinate transformation

def LLToMerc(coord):
def LLToOutput(coord):
"""Converts a Coord(lon,lat) or Box2d(l,b,r,t) to
OSM Mercator (x,y)."""
return MERCATOR_PROJECTION.forward(coord)
(x,y) coordinates in the selected map projection."""
return OUTPUT_PROJECTION.forward(coord)

def mercToLL(coord):
"""Converts an OSM Mercator Coord(x,y) or Box2d(l,b,r,t)
to (lon,lat)."""
return MERCATOR_PROJECTION.inverse(coord)
def outputToLL(coord):
"""Converts a Coord(x,y) or Box2d(l,b,r,t) in the
selected map projection to (lon,lat)."""
return OUTPUT_PROJECTION.inverse(coord)

def LLToPixel(coord, z):
"""Converts a Coord(lon,lat) or Box2d(l,b,r,t) to
Expand All @@ -103,21 +107,23 @@ def pixelToLL(coord, z):
else:
return GOOGLE_PROJECTION.envPixelToLL(coord, z)

def pixelToMerc(coord, z):
def pixelToOutput(coord, z):
"""Converts an OSM pixel Coord(x,y) or Box2d(l,b,r,t)
to OSM Mercator (x,y) at the specified zoom level."""
to (x,y) in the selected map projection at the specified
zoom level."""
# No direct transformation. Use px->ll->merc.
if isinstance(coord, Coord):
ll = GOOGLE_PROJECTION.pixelToLL(coord, z)
else:
ll = GOOGLE_PROJECTION.envPixelToLL(coord, z)
return MERCATOR_PROJECTION.forward(ll)
return OUTPUT_PROJECTION.forward(ll)

def mercToPixel(coord, z):
"""Converts an OSM Mercator Coord(x,y) or Box2d(l,b,r,t)
to OSM pixel coordinates (x,y) at the specified zoom level."""
def outputToPixel(coord, z):
"""Converts a Coord(x,y) or Box2d(l,b,r,t) in the
specified map projection to OSM pixel coordinates
(x,y) at the specified zoom level."""
# No direct transformation. Use merc->ll->pixel.
ll = MERCATOR_PROJECTION.inverse(coord)
ll = OUTPUT_PROJECTION.inverse(coord)
if isinstance(coord, Coord):
return GOOGLE_PROJECTION.LLToPixel(ll, z)
else:
Expand All @@ -144,10 +150,10 @@ def getLLTileEnv(z, x, y, ntiles = 1, includeBorder = True):
tile coordinates."""
return pixelToLL(getPixelTileEnv(x, y, ntiles, includeBorder), z)

def getMercTileEnv(z, x, y, ntiles = 1, includeBorder = True):
"""Returns the OSM Mercator Box2d for the tile(s) at the specified
tile coordinates."""
return pixelToMerc(getPixelTileEnv(x, y, ntiles, includeBorder), z)
def getOutputTileEnv(z, x, y, ntiles = 1, includeBorder = True):
"""Returns the Box2d in output map projection for the tile(s)
at the specified tile coordinates."""
return pixelToOutput(getPixelTileEnv(x, y, ntiles, includeBorder), z)

def getTileAtLL(coord, z, ntiles = 1):
"""Returns the OSM tile coordinates (x, y) at the specified
Expand Down
16 changes: 8 additions & 8 deletions toposm.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,17 @@ def renderMetatileLayer(name, z, x, y, ntiles, map):
console.debugMessage(' Using cached: ' + name)
return mapnik.Image.open(getCachedMetaTilePath(name, z, x, y, 'png'))
console.debugMessage(' Rendering layer: ' + name)
env = getMercTileEnv(z, x, y, ntiles, True)
env = getOutputTileEnv(z, x, y, ntiles, True)
tilesize = getTileSize(ntiles, True)
image = renderLayerMerc(name, env, tilesize, tilesize, map)
image = renderLayerProjected(name, env, tilesize, tilesize, map)
if name in CACHE_LAYERS:
ensureDirExists(getCachedMetaTileDir(name, z, x))
image.save(getCachedMetaTilePath(name, z, x, y, 'png'))
return image

def renderLayerMerc(name, env, xsize, ysize, map):
"""Renders the specified layer to an image. ENV must be in spherical
mercator projection."""
def renderLayerProjected(name, env, xsize, ysize, map):
"""Renders the specified layer to an image. ENV must be in the
output map projection"""
map.zoom_to_box(env)
if USE_CAIRO and name in CAIRO_LAYERS:
assert mapnik.has_cairo()
Expand All @@ -278,7 +278,7 @@ def renderLayerMerc(name, env, xsize, ysize, map):
return image

def renderLayerLL(name, env, xsize, ysize, map):
return renderLayerMerc(name, LLToMerc(env), xsize, ysize, map)
return renderLayerProjected(name, LLToOutput(env), xsize, ysize, map)

def saveTiles(z, x, y, ntiles, mapname, image, suffix = 'png', imgtype = None):
"""Saves the individual tiles from a metatile image."""
Expand Down Expand Up @@ -355,10 +355,10 @@ def renderToPdf(envLL, filename, sizex, sizey):
localfilename = basefilename + '_' + mapname + '.pdf';
file = open(localfilename, 'wb')
surface = cairo.PDFSurface(file.name, sizex, sizey)
envMerc = LLToMerc(envLL)
envOutput = LLToOutput(envLL)
map = mapnik.Map(sizex, sizey)
mapnik.load_map(map, mapname + ".xml")
map.zoom_to_box(envMerc)
map.zoom_to_box(envOutput)
mapnik.render(map, surface)
surface.finish()
file.close()
Expand Down

0 comments on commit bf5190b

Please sign in to comment.