Skip to content

Commit

Permalink
- thumb improvs
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani committed Oct 18, 2018
1 parent 1cc9b7d commit 2c0674b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
26 changes: 17 additions & 9 deletions geonode/geoserver/helpers.py
Expand Up @@ -1783,7 +1783,7 @@ def _render_thumbnail(req_body, width=240, height=180):
return content


def _prepare_thumbnail_body_from_opts(request_body):
def _prepare_thumbnail_body_from_opts(request_body, request=None):
import mercantile
from geonode.utils import (_v,
bbox_to_projection,
Expand Down Expand Up @@ -1847,6 +1847,10 @@ def decimal_encode(bbox):
'format': wms_format,
# 'TIME': '-99999999999-01-01T00:00:00.0Z/99999999999-01-01T00:00:00.0Z'
}

if request and 'access_token' in request.session:
params['access_token'] = request.session['access_token']

_p = "&".join("%s=%s" % item for item in params.items())

import posixpath
Expand All @@ -1860,18 +1864,21 @@ def decimal_encode(bbox):
target_srid=4326)[:4])

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

# 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, truncate=True)
t_ur = mercantile.tile(_v(bounds[2], x=True), _v(bounds[3], x=False), zoom, truncate=True)
xmin, ymax = t_ll.x, t_ll.y
xmax, ymin = t_ur.x, t_ur.y
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):
Expand All @@ -1889,15 +1896,16 @@ def decimal_encode(bbox):
'transparent': True,
'bbox': ",".join([str(xy_bounds.left), str(xy_bounds.bottom),
str(xy_bounds.right), str(xy_bounds.top)]),
'crs': 'EPSG:3857'
'crs': 'EPSG:3857',

}
_p = "&".join("%s=%s" % item for item in params.items())

_img_request_template += \
_img_src_template.format(ogc_location=(thumbnail_create_url + '&' + _p),
height=256, width=256,
left=box[0], top=box[1])
_img_request_template += "</div>"
_img_request_template += "</div></div>"
image = _render_thumbnail(_img_request_template, width=width, height=height)
return image

Expand Down
3 changes: 2 additions & 1 deletion geonode/layers/views.py
Expand Up @@ -1387,7 +1387,8 @@ def layer_thumbnail(request, layername):
else:
image = None
try:
image = _prepare_thumbnail_body_from_opts(request.body)
image = _prepare_thumbnail_body_from_opts(request.body,
request=request)
except BaseException:
image = _render_thumbnail(request.body)

Expand Down
3 changes: 2 additions & 1 deletion geonode/maps/views.py
Expand Up @@ -1359,7 +1359,8 @@ def map_thumbnail(request, mapid):
try:
image = None
try:
image = _prepare_thumbnail_body_from_opts(request.body)
image = _prepare_thumbnail_body_from_opts(request.body,
request=request)
except BaseException:
image = _render_thumbnail(request.body)

Expand Down
31 changes: 17 additions & 14 deletions geonode/utils.py
Expand Up @@ -23,7 +23,6 @@
import copy
import datetime
import logging
import math
import os
import re
import uuid
Expand All @@ -41,6 +40,7 @@
import weakref
import traceback

from math import atan, exp, log, pi, sin, tan, floor
from contextlib import closing
from zipfile import ZipFile, is_zipfile, ZIP_DEFLATED
from StringIO import StringIO
Expand Down Expand Up @@ -240,31 +240,34 @@ def bbox_to_projection(native_bbox, target_srid=4326):


def bounds_to_zoom_level(bounds, width, height):
WORLD_DIM = {'height': float(256), 'width': float(256)}
WORLD_DIM = {'height': 256., 'width': 256.}
ZOOM_MAX = 21

def latRad(lat):
sin = math.sin(lat * math.pi / 180.0)
if abs(sin) != 1.0:
radX2 = math.log((1.0 + sin) / (1.0 - sin)) / 2.0
_sin = sin(lat * pi / 180.0)
if abs(_sin) != 1.0:
radX2 = log((1.0 + _sin) / (1.0 - _sin)) / 2.0
else:
radX2 = math.log(1.0) / 2.0
return max(min(radX2, math.pi), -math.pi) / 2.0
radX2 = log(1.0) / 2.0
return max(min(radX2, pi), -pi) / 2.0

def zoom(mapPx, worldPx, fraction):
try:
return math.floor(math.log(mapPx / worldPx / fraction) / math.log(2.0))
return floor(log(mapPx / worldPx / fraction) / log(2.0))
except BaseException:
return 0

ne = [float(bounds[2]), float(bounds[3])]
sw = [float(bounds[0]), float(bounds[1])]
latFraction = (latRad(ne[1]) - latRad(sw[1])) / math.pi
latFraction = (latRad(ne[1]) - latRad(sw[1])) / pi
lngDiff = ne[0] - sw[0]
lngFraction = ((lngDiff + 360.0) if (lngDiff < 0) else lngDiff) / 360.0
latZoom = zoom(float(height), WORLD_DIM['height'], latFraction)
lngZoom = zoom(float(width), WORLD_DIM['width'], lngFraction)
zoom = int(min(latZoom, lngZoom, ZOOM_MAX) - 1)
ratio = float(max(width, height)) / float(min(width, height))
z_offset = 0 if ratio >= 1.5 else -1
zoom = int(max(latZoom, lngZoom) + z_offset)
zoom = int(min(zoom, ZOOM_MAX))
return max(zoom, 0)


Expand All @@ -291,13 +294,13 @@ def forward_mercator(lonlat):
# With data sets that only have one point the value of this
# expression becomes negative infinity. In order to continue,
# we wrap this in a try catch block.
n = math.tan((90 + lonlat[1]) * math.pi / 360)
n = tan((90 + lonlat[1]) * pi / 360)
except ValueError:
n = 0
if n <= 0:
y = float("-inf")
else:
y = math.log(n) / math.pi * 20037508.34
y = log(n) / pi * 20037508.34
return (x, y)


Expand All @@ -307,8 +310,8 @@ def inverse_mercator(xy):
"""
lon = (xy[0] / 20037508.34) * 180
lat = (xy[1] / 20037508.34) * 180
lat = 180 / math.pi * \
(2 * math.atan(math.exp(lat * math.pi / 180)) - math.pi / 2)
lat = 180 / pi * \
(2 * atan(exp(lat * pi / 180)) - pi / 2)
return (lon, lat)


Expand Down

0 comments on commit 2c0674b

Please sign in to comment.