Skip to content

Commit

Permalink
Work around latest projection issues with new proj library and mapnik
Browse files Browse the repository at this point in the history
  • Loading branch information
hholzgra committed Mar 27, 2024
1 parent 7f55ad3 commit c72bd43
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
21 changes: 15 additions & 6 deletions ocitysmap/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
"Mapnik module version %s is too old, see ocitysmap's INSTALL " \
"for more details." % mapnik.mapnik_version_string()

_MAPNIK_PROJECTION = "+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 +over"


EARTH_RADIUS = 6370986 # meters

def dd2dms(value):
Expand All @@ -49,6 +44,20 @@ def dd2dms(value):

return (degrees, minutes, seconds)

def get_proj_transformation():
try:
# new Proj library versions (e.g. v9 on Debian 12)
proj_wgs84 = mapnik.Projection("epsg:4326")
proj_google = mapnik.Projection("epsg:3857")
except:
# old Proj libraray versions (e.g. v7 on Debian 11)
proj_wgs84 = mapnik.Projection("+init=epsg:4326")
proj_google = mapnik.Projection( "+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 +over")

return mapnik.ProjTransform(proj_wgs84, proj_google)

class Point:
def __init__(self, lat, long_):
self._lat, self._long = float(lat), float(long_)
Expand Down Expand Up @@ -299,7 +308,7 @@ def to_mercator(self):
self.get_top_left()[0],
self.get_bottom_right()[1],
self.get_bottom_right()[0])
_proj = mapnik.Projection(_MAPNIK_PROJECTION)
_proj = get_proj_transformation()
bottom_left = _proj.forward(mapnik.Coord(envelope.minx, envelope.miny))
top_right = _proj.forward(mapnik.Coord(envelope.maxx, envelope.maxy))
top_left = mapnik.Coord(bottom_left.x, top_right.y)
Expand Down
7 changes: 3 additions & 4 deletions ocitysmap/layoutlib/multi_page_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ def __init__(self, db, rc, tmpdir, dpi, file_prefix):
OVERLAP_MARGIN_MM = 20

# Convert the original Bounding box into Mercator meters
self._proj = mapnik.Projection(coords._MAPNIK_PROJECTION)
self._proj = coords.get_proj_transformation()
orig_envelope = self._project_envelope(self.rc.bounding_box)


while True:
# Extend the bounding box to take into account the lost outer
# margin
Expand Down Expand Up @@ -593,8 +592,8 @@ def _inverse_envelope(self, envelope):
Inverse the given cartesian envelope (in 3587) back to a 4326
bounding box.
"""
c0 = self._proj.inverse(mapnik.Coord(envelope.minx, envelope.miny))
c1 = self._proj.inverse(mapnik.Coord(envelope.maxx, envelope.maxy))
c0 = self._proj.backward(mapnik.Coord(envelope.minx, envelope.miny))
c1 = self._proj.backward(mapnik.Coord(envelope.maxx, envelope.maxy))
return coords.BoundingBox(c0.y, c0.x, c1.y, c1.x)

def _prepare_page(self, ctx):
Expand Down
6 changes: 3 additions & 3 deletions ocitysmap/maplib/map_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, stylesheet, bounding_box, _width, _height, dpi=72.0,
"""

self._style_name = stylesheet.name
self._proj = mapnik.Projection(_MAPNIK_PROJECTION)
self._proj = ocitysmap.coords.get_proj_transformation()
self._dpi = dpi

# This is where the magic of the map canvas happens. Given an original
Expand Down Expand Up @@ -211,8 +211,8 @@ def _project_envelope(self, bbox):
def _inverse_envelope(self, envelope):
"""Inverse the given cartesian envelope (in 3587) back to a 4326
bounding box."""
c0 = self._proj.inverse(mapnik.Coord(envelope.minx, envelope.miny))
c1 = self._proj.inverse(mapnik.Coord(envelope.maxx, envelope.maxy))
c0 = self._proj.backward(mapnik.Coord(envelope.minx, envelope.miny))
c1 = self._proj.backward(mapnik.Coord(envelope.maxx, envelope.maxy))
return ocitysmap.coords.BoundingBox(c0.y, c0.x, c1.y, c1.x)

if __name__ == '__main__':
Expand Down

0 comments on commit c72bd43

Please sign in to comment.