def _make_valid_options_tile_tuples(self, options_tuples):
     """Makes a list of valid tile_dicts from a list of options_tuples"""
     valid_tile_tuples = []
     for tile, count in options_tuples:
         if tile.startswith(NULL_ISLAND_TILE_ROOT):
             # Skip, because the tile is almost certainly reflecting
             # bad data.
             logger.warn('Found {} bad geotile records'.format(count))
             # So skip.
             continue
         # Parse the tile to get the lon, lat list.
         gm = GlobalMercator()
         geo_coords = gm.quadtree_to_geojson_poly_coords(tile)
         if not isinstance(geo_coords, list):
             # Not a valid data for some awful reason.
             logger.warn('Found bad {} geotile with {} records'.format(
                 tile,
                 count,
             ))
             continue
         valid_tile_tuples.append((
             tile,
             count,
         ))
     return valid_tile_tuples
 def process_solr_tiles(self, solr_tiles):
     """ processes the solr_json 
         discovery geo tiles,
         aggregating to a certain
         depth
     """
     # first aggregate counts for tile that belong togther
     aggregate_tiles = self.aggregate_spatial_tiles(solr_tiles)
     # now generate GeoJSON for each tile region
     # print('Total tiles: ' + str(t) + ' reduced to ' + str(len(aggregate_tiles)))
     i = 0
     for tile_key, aggregate_count in aggregate_tiles.items():
         i += 1
         add_region = True
         fl = FilterLinks()
         fl.base_request_json = self.filter_request_dict_json
         fl.spatial_context = self.spatial_context
         new_rparams = fl.add_to_request('disc-geotile',
                                         tile_key)
         record = LastUpdatedOrderedDict()
         record['id'] = fl.make_request_url(new_rparams)
         record['json'] = fl.make_request_url(new_rparams, '.json')
         record['count'] = aggregate_count
         record['type'] = 'Feature'
         record['category'] = 'oc-api:geo-facet'
         if self.min_date is not False \
            and self.max_date is not False:
             when = LastUpdatedOrderedDict()
             when['id'] = '#event-' + tile_key
             when['type'] = 'oc-gen:formation-use-life'
             # convert numeric to GeoJSON-LD ISO 8601
             when['start'] = ISOyears().make_iso_from_float(self.min_date)
             when['stop'] = ISOyears().make_iso_from_float(self.max_date)
             record['when'] = when
         gm = GlobalMercator()
         geo_coords = gm.quadtree_to_geojson_poly_coords(tile_key)
         geometry = LastUpdatedOrderedDict()
         geometry['id'] = '#geo-disc-tile-geom-' + tile_key
         geometry['type'] = 'Polygon'
         geometry['coordinates'] = geo_coords
         record['geometry'] = geometry
         properties = LastUpdatedOrderedDict()
         properties['id'] = '#geo-disc-tile-' + tile_key
         properties['href'] = record['id']
         properties['label'] = 'Discovery region (' + str(i) + ')'
         properties['feature-type'] = 'discovery region (facet)'
         properties['count'] = aggregate_count
         properties['early bce/ce'] = self.min_date
         properties['late bce/ce'] = self.max_date
         record['properties'] = properties
         if len(tile_key) >= 6:
             if tile_key[:6] == '211111':
                 # no bad coordinates (off 0, 0 coast of Africa)
                 add_region = False  # don't display items without coordinates
         if add_region:
             self.geojson_regions.append(record)
Esempio n. 3
0
 def process_solr_tiles(self, solr_tiles):
     """ processes the solr_json 
         discovery geo tiles,
         aggregating to a certain
         depth
     """
     # first aggregate counts for tile that belong togther
     aggregate_tiles = self.aggregate_spatial_tiles(solr_tiles)
     # now generate GeoJSON for each tile region
     # print('Total tiles: ' + str(t) + ' reduced to ' + str(len(aggregate_tiles)))
     i = 0
     for tile_key, aggregate_count in aggregate_tiles.items():
         i += 1
         add_region = True
         fl = FilterLinks()
         fl.base_request_json = self.filter_request_dict_json
         fl.spatial_context = self.spatial_context
         new_rparams = fl.add_to_request('disc-geotile', tile_key)
         record = LastUpdatedOrderedDict()
         record['id'] = fl.make_request_url(new_rparams)
         record['json'] = fl.make_request_url(new_rparams, '.json')
         record['count'] = aggregate_count
         record['type'] = 'Feature'
         record['category'] = 'oc-api:geo-facet'
         if self.min_date is not False \
            and self.max_date is not False:
             when = LastUpdatedOrderedDict()
             when['id'] = '#event-' + tile_key
             when['type'] = 'oc-gen:formation-use-life'
             # convert numeric to GeoJSON-LD ISO 8601
             when['start'] = ISOyears().make_iso_from_float(self.min_date)
             when['stop'] = ISOyears().make_iso_from_float(self.max_date)
             record['when'] = when
         gm = GlobalMercator()
         geo_coords = gm.quadtree_to_geojson_poly_coords(tile_key)
         geometry = LastUpdatedOrderedDict()
         geometry['id'] = '#geo-disc-tile-geom-' + tile_key
         geometry['type'] = 'Polygon'
         geometry['coordinates'] = geo_coords
         record['geometry'] = geometry
         properties = LastUpdatedOrderedDict()
         properties['id'] = '#geo-disc-tile-' + tile_key
         properties['href'] = record['id']
         properties['label'] = 'Discovery region (' + str(i) + ')'
         properties['feature-type'] = 'discovery region (facet)'
         properties['count'] = aggregate_count
         properties['early bce/ce'] = self.min_date
         properties['late bce/ce'] = self.max_date
         record['properties'] = properties
         if len(tile_key) >= 6:
             if tile_key[:6] == '211111':
                 # no bad coordinates (off 0, 0 coast of Africa)
                 add_region = False  # don't display items without coordinates
         if add_region:
             self.geojson_regions.append(record)
    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