def _distance_determine_aggregation_depth(self, valid_tile_tuples):
        """Uses distance between to recalibrate aggregation depth in tiles"""
        if len(valid_tile_tuples) < 2:
            return self.default_aggregation_depth

        lons = []
        lats = []
        gm = GlobalMercator()
        for tile, _ in valid_tile_tuples:
            geo_coords = gm.quadtree_to_geojson_lon_lat(tile)
            # Remember geojson ordering of the coordinates (lon, lat)
            lons.append(geo_coords[0])
            lats.append(geo_coords[1])

        max_distance = gm.distance_on_unit_sphere(
            min(lats),
            min(lons),
            max(lats),
            max(lons),
        )
        # Converts the maximum distance between points into a zoom level
        # appropriate for tile aggregation. Seems to work well.
        return gm.ZoomForPixelSize(max_distance) + 3
Example #2
0
 def get_geotile_scope(self, solr_tiles):
     """ find the most specific tile shared by the whole dataset """
     geo_tiles = []
     bound_list = []
     max_distance = 0
     lat_lon_min_max = [{
         'min': None,
         'max': None
     }, {
         'min': None,
         'max': None
     }]
     for tile_key in solr_tiles[::2]:
         if tile_key[:6] != '211111':
             # a bit of a hack to exclude display of
             # erroroneous data without spatial reference
             geo_tiles.append(tile_key)
             gm = GlobalMercator()
             bounds = gm.quadtree_to_lat_lon(tile_key)
             lat_lon_min_max = self.get_min_max(lat_lon_min_max, bounds)
             bound_list.append(bounds)
     if len(geo_tiles) > 0:
         test_tile = geo_tiles[0]  # we compare against the first tile
         tile_len = len(test_tile)  # size of the tile
         in_all_geotiles = True
         i = 0
         while (i < tile_len and in_all_geotiles):
             if i > 0:
                 test_val = str(test_tile[0:i])
             else:
                 test_val = str(test_tile[0])
             for geo_tile in geo_tiles:
                 if i > len(geo_tile):
                     in_all_geotiles = False
                 else:
                     if i > 0:
                         geo_tile_part = geo_tile[0:i]
                     else:
                         geo_tile_part = geo_tile[0]
                     if test_val != geo_tile_part:
                         in_all_geotiles = False
             if in_all_geotiles:
                 # ok! we have somthing that is still in all tiles
                 self.geotile_scope = test_val
                 i += 1
     if isinstance(self.geotile_scope, str):
         self.aggregation_depth += round(len(self.geotile_scope) * 1, 0)
         self.aggregation_depth = int(self.aggregation_depth)
         if self.aggregation_depth < 6:
             self.aggregation_depth = 6
     if self.aggregation_depth >= 6 and len(bound_list) >= 2:
         gm = GlobalMercator()
         max_distance = gm.distance_on_unit_sphere(
             lat_lon_min_max[0]['min'], lat_lon_min_max[1]['min'],
             lat_lon_min_max[0]['max'], lat_lon_min_max[1]['max'])
         if max_distance == 0:
             self.aggregation_depth = 10
         else:
             # converts the maximum distance between points into a zoom level
             # appropriate for tile aggregation. seems to work well.
             self.aggregation_depth = gm.ZoomForPixelSize(max_distance) + 3
             # print('now: ' + str(self.aggregation_depth) + ' for ' + str(max_distance))
             if self.aggregation_depth > self.max_depth:
                 self.aggregation_depth = self.max_depth
             if self.aggregation_depth < 6:
                 self.aggregation_depth = 6
     return self.geotile_scope