Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue #6843] Remove use of some magic numbers #6853

Merged
merged 6 commits into from
Jan 26, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions geonode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
SplitResult
)

MAX_EXTENT = 20037508.34
FULL_ROTATION_DEG = 360.0
HALF_ROTATION_DEG = 180.0
DEFAULT_TITLE = ""
DEFAULT_ABSTRACT = ""

Expand Down Expand Up @@ -332,8 +335,8 @@ def bbox_to_wkt(x0, x1, y0, y1, srid="4326", include_srid=True):


def _v(coord, x, source_srid=4326, target_srid=3857):
if source_srid == 4326 and x and abs(coord) != 180.0:
coord = coord - (round(coord / 360.0) * 360.0)
if source_srid == 4326 and x and abs(coord) != HALF_ROTATION_DEG:
coord -= (round(coord / FULL_ROTATION_DEG) * FULL_ROTATION_DEG)
if source_srid == 4326 and target_srid != 4326:
if x and float(coord) >= 179.999:
return 179.999
Expand Down Expand Up @@ -399,7 +402,7 @@ def bounds_to_zoom_level(bounds, width, height):
ZOOM_MAX = 21

def latRad(lat):
_sin = sin(lat * pi / 180.0)
_sin = sin(lat * pi / HALF_ROTATION_DEG)
if abs(_sin) != 1.0:
radX2 = log((1.0 + _sin) / (1.0 - _sin)) / 2.0
else:
Expand All @@ -416,7 +419,7 @@ def zoom(mapPx, worldPx, fraction):
sw = [float(bounds[0]), float(bounds[1])]
latFraction = (latRad(ne[1]) - latRad(sw[1])) / pi
lngDiff = ne[0] - sw[0]
lngFraction = ((lngDiff + 360.0) if (lngDiff < 0) else lngDiff) / 360.0
lngFraction = ((lngDiff + FULL_ROTATION_DEG) if lngDiff < 0 else lngDiff) / FULL_ROTATION_DEG
latZoom = zoom(float(height), WORLD_DIM['height'], latFraction)
lngZoom = zoom(float(width), WORLD_DIM['width'], lngFraction)
# ratio = float(max(width, height)) / float(min(width, height))
Expand Down Expand Up @@ -445,29 +448,29 @@ def forward_mercator(lonlat):

If the lat value is out of range, -inf will be returned as the y value
"""
x = lonlat[0] * 20037508.34 / 180
x = lonlat[0] * MAX_EXTENT / HALF_ROTATION_DEG
try:
# 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 = tan((90 + lonlat[1]) * pi / 360)
n = tan((90 + lonlat[1]) * pi / FULL_ROTATION_DEG)
except ValueError:
n = 0
if n <= 0:
y = float("-inf")
else:
y = log(n) / pi * 20037508.34
y = log(n) / pi * MAX_EXTENT
return (x, y)


def inverse_mercator(xy):
"""
Given coordinates in spherical mercator, return a lon,lat tuple.
"""
lon = (xy[0] / 20037508.34) * 180
lat = (xy[1] / 20037508.34) * 180
lat = 180 / pi * \
(2 * atan(exp(lat * pi / 180)) - pi / 2)
lon = (xy[0] / MAX_EXTENT) * HALF_ROTATION_DEG
lat = (xy[1] / MAX_EXTENT) * HALF_ROTATION_DEG
lat = HALF_ROTATION_DEG / pi * \
(2 * atan(exp(lat * pi / HALF_ROTATION_DEG)) - pi / 2)
return (lon, lat)


Expand Down Expand Up @@ -850,19 +853,20 @@ def _baselayer(lyr, order):
return DEFAULT_MAP_CONFIG, DEFAULT_BASE_LAYERS


max_extent = [-MAX_EXTENT, -MAX_EXTENT, MAX_EXTENT, MAX_EXTENT]
_viewer_projection_lookup = {
"EPSG:900913": {
"maxResolution": 156543.03390625,
"units": "m",
"maxExtent": [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
"maxExtent": max_extent,
},
"EPSG:3857": {
"maxResolution": 156543.03390625,
"units": "m",
"maxExtent": [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
"maxExtent": max_extent,
},
"EPSG:4326": {
"max_resolution": (180 - (-180)) / 256,
"max_resolution": FULL_ROTATION_DEG / 256,
"units": "degrees",
"maxExtent": [-180, -90, 180, 90]
}
Expand Down