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
def make_geotile_facet_options(self, solr_json): """Makes geographic tile facets from a solr_json response""" geotile_path_keys = (configs.FACETS_SOLR_ROOT_PATH_KEYS + ['discovery_geotile']) geotile_val_count_list = utilities.get_dict_path_value( geotile_path_keys, solr_json, default=[]) if not len(geotile_val_count_list): return None # Make the list of tile, count tuples. options_tuples = utilities.get_facet_value_count_tuples( geotile_val_count_list) if not len(options_tuples): return None valid_tile_tuples = self._make_valid_options_tile_tuples( options_tuples) if not len(valid_tile_tuples): # None of the chronological tiles are valid # given the query requirements. return None # Determine the aggregation depth needed to group geotiles # together into a reasonable number of options. self._get_tile_aggregation_depth(valid_tile_tuples) # Determine the min tile depth. We need to return this to # the client so the client knows not to over-zoom. tile_lens = [len(tile) for tile, _ in valid_tile_tuples] self.min_depth = min(tile_lens) # Get the client's requested feature type for the geotile # facets. feature_type = utilities.get_request_param_value( self.request_dict, param='geo-facet-type', default=self.default_tile_feature_type, as_list=False, solr_escape=False, ) if feature_type not in self.valid_tile_feature_types: # If the requested feature type is not in the # valid list of feature types, just use the default. feature_type = self.default_tile_feature_type aggregate_tiles = {} for tile, count in valid_tile_tuples: # Now aggregate the tiles. trim_tile_key = tile[:self.default_aggregation_depth] if trim_tile_key not in aggregate_tiles: # Make the aggregate tile with a count # of zero aggregate_tiles[trim_tile_key] = 0 aggregate_tiles[trim_tile_key] += count options = [] for tile, count in aggregate_tiles.items(): sl = SearchLinks(request_dict=copy.deepcopy(self.request_dict), base_search_url=self.base_search_url) # Remove non search related params. sl.remove_non_query_params() # Update the request dict for this facet option. sl.replace_param_value( 'disc-geotile', match_old_value=None, new_value=tile, ) urls = sl.make_urls_from_request_dict() if urls['html'] == self.current_filters_url: # The new URL matches our current filter # url, so don't add this facet option. continue option = LastUpdatedOrderedDict() option['id'] = urls['html'] option['json'] = urls['json'] option['count'] = count option['type'] = 'Feature' option['category'] = 'oc-api:geo-facet' # Add some general chronology information to the # geospatial tile. option = self._add_when_object_to_feature_option( tile, option, ) gm = GlobalMercator() if feature_type == 'Polygon': # Get polygon coordinates (a list of lists) geo_coords = gm.quadtree_to_geojson_poly_coords(tile) elif feature_type == 'Point': # Get point coordinates (a list of lon,lat values) geo_coords = gm.quadtree_to_geojson_lon_lat(tile) else: # We shouldn't be here! continue # Add the geometry object to the facet option. geometry = LastUpdatedOrderedDict() geometry['id'] = '#geo-disc-tile-geom-{}'.format(tile) geometry['type'] = feature_type geometry['coordinates'] = geo_coords option['geometry'] = geometry properties = LastUpdatedOrderedDict() properties['id'] = '#geo-disc-tile-{}'.format(tile) properties['href'] = option['id'] properties['label'] = 'Discovery region ({})'.format( (len(options) + 1)) properties['feature-type'] = 'discovery region (facet)' properties['count'] = count properties['early bce/ce'] = self.min_date properties['late bce/ce'] = self.max_date option['properties'] = properties options.append(option) return options