Skip to content

Commit

Permalink
Fixing thumb generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kappu committed Oct 22, 2018
1 parent 1a472fc commit 497fb6d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
63 changes: 44 additions & 19 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,33 +1863,58 @@ def decimal_encode(bbox):
bbox_to_projection([float(coord) for coord in request_body['bbox']] + [request_body['srid'], ],
target_srid=4326)[:4])

# Fetch XYZ tiles
bounds = wgs84_bbox[0:4]

bounds[0] = _v(bounds[0], x=True, target_srid=4326)
bounds[2] = _v(bounds[2], x=True, target_srid=4326)

if 'zoom' in request_body:
zoom = request_body['zoom']
else:
zoom = bounds_to_zoom_level(bounds, width, height)

t_ll = mercantile.tile(bounds[0], bounds[1], zoom)
t_ur = mercantile.tile(bounds[2], bounds[3], zoom)
tiles = mercantile.tiles(bounds[0], bounds[1], bounds[2], bounds[3], zoom)
cols = {}
f_col = t_ll.x
for tile in tiles:
if tile.x in cols:
cols[tile.x].append(tile)
else:
cols[tile.x] = [tile]

ordered_cols = cols.values()
i = 0
while ordered_cols[i][0].x != f_col and i < len(ordered_cols):
i = i+1
ordered_cols = ordered_cols[i:] + ordered_cols[:i]

bounds_ll = mercantile.bounds(t_ll)
bounds_ur = mercantile.bounds(t_ur)

lat_res = abs(256 / (bounds_ur.north - bounds_ur.south))
lng_res = abs(256 / (bounds_ll.east - bounds_ll.west))
top = round(abs(bounds_ur.north - bounds[3]) * -lat_res)
left = round(abs(bounds_ll.west - bounds[0]) * -lng_res)

# Build Image Request Template
_img_request_template = "<div style='height:{height}px; width:{width}px;'>\
<div style='position: absolute; z-index: 749; \
<div style='position: absolute; top:{top}px; left:{left}px; z-index: 749; \
transform: translate3d(0px, 0px, 0px) scale3d(1, 1, 1);'> \
\n".format(height=height, width=width)
\n".format(height=height, width=width, top=top, left=left)

# Fetch XYZ tiles
bounds = wgs84_bbox[0:4]
zoom = bounds_to_zoom_level(bounds, width, height)

t_ll = mercantile.tile(_v(bounds[0], x=True), _v(bounds[1], x=False), zoom)
t_ur = mercantile.tile(_v(bounds[2], x=True), _v(bounds[3], x=False), zoom)
ratio = float(max(width, height)) / float(min(width, height))
y_offset = 1 if ratio >= 1.5 else 0
xmin, ymax = t_ll.x, t_ll.y+y_offset
xmax, ymin = t_ur.x, t_ur.y+y_offset

for xtile in range(xmin, xmax+1):
for ytile in range(ymin, ymax+1):
box = [(xtile-xmin)*256, (ytile-ymin)*255]
for col in range(0, len(ordered_cols)):
for row in range(0, len(ordered_cols[col])):
box = [col * 256, row * 256]
t = ordered_cols[col][row]
if smurl:
imgurl = smurl.format(z=zoom, x=xtile, y=ytile)
imgurl = smurl.format(z=t.z, x=t.x, y=t.y)
_img_request_template += _img_src_template.format(ogc_location=imgurl,
height=256, width=256,
left=box[0], top=box[1])

xy_bounds = mercantile.xy_bounds(mercantile.Tile(xtile, ytile, zoom))
xy_bounds = mercantile.xy_bounds(t)
params = {
'width': 256,
'height': 256,
Expand Down
2 changes: 2 additions & 0 deletions geonode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def bbox_to_wkt(x0, x1, y0, y1, srid="4326"):


def _v(coord, x, source_srid=4326, target_srid=3857):
if source_srid == 4326 and x:
coord = coord - (round(coord / 360.0) * 360.0)
if source_srid == 4326 and target_srid != 4326:
if x and coord >= 180.0:
return 179.0
Expand Down

0 comments on commit 497fb6d

Please sign in to comment.