diff --git a/geonode/maps/models.py b/geonode/maps/models.py index bbc546865a0..ee0982f2d46 100644 --- a/geonode/maps/models.py +++ b/geonode/maps/models.py @@ -18,7 +18,7 @@ ######################################################################### import json import logging - +import math import itertools from deprecated import deprecated @@ -134,30 +134,36 @@ def compute_bbox(self, target_crs="EPSG:3857"): Compute bbox for maps by looping on all maplayers and getting the max bbox of all the datasets """ - bbox = bbox_utils.epsg_3857_area_of_use(target_crs="EPSG:3857") + epsg_bbox = bbox_utils.epsg_3857_area_of_use(target_crs="EPSG:3857") + bbox = [math.inf, -math.inf, math.inf, -math.inf] + for layer in self.maplayers.filter(visibility=True).order_by("order").iterator(): dataset = layer.dataset if dataset is not None: if dataset.ll_bbox_polygon: dataset_bbox = bbox_utils.clean_bbox(dataset.ll_bbox, target_crs) - elif ( - dataset.bbox[-1].upper() != "EPSG:3857" - and target_crs.upper() == "EPSG:3857" - and bbox_utils.exceeds_epsg3857_area_of_use(dataset.bbox) - ): + elif dataset.bbox: # handle exceeding the area of use of the default thumb's CRS dataset_bbox = bbox_utils.transform_bbox( bbox_utils.crop_to_3857_area_of_use(dataset.bbox), target_crs ) - else: - dataset_bbox = bbox_utils.transform_bbox(dataset.bbox, target_crs) - - bbox = [ - max(bbox[0], dataset_bbox[0]), - min(bbox[1], dataset_bbox[1]), - max(bbox[2], dataset_bbox[2]), - min(bbox[3], dataset_bbox[3]), - ] + + if dataset_bbox: + bbox = [ + min(bbox[0], dataset_bbox[0]), + max(bbox[1], dataset_bbox[1]), + min(bbox[2], dataset_bbox[2]), + max(bbox[3], dataset_bbox[3]), + ] + + if bbox[0] < epsg_bbox[0]: + bbox[0] = epsg_bbox[0] + if bbox[1] > epsg_bbox[1]: + bbox[1] = epsg_bbox[1] + if bbox[2] < epsg_bbox[2]: + bbox[2] = epsg_bbox[2] + if bbox[3] > epsg_bbox[3]: + bbox[3] = epsg_bbox[3] self.set_bbox_polygon([bbox[0], bbox[2], bbox[1], bbox[3]], target_crs) return bbox