def find_distinct_bounding_boxes_in_polygon()

in data-access/nexustiles/backends/nexusproto/dao/ElasticsearchProxy.py [0:0]


    def find_distinct_bounding_boxes_in_polygon(self, bounding_polygon, ds, start_time=0, end_time=-1, **kwargs):
        
        tile_max_lat = bounding_polygon.bounds[3]
        tile_min_lon = bounding_polygon.bounds[0]
        tile_min_lat = bounding_polygon.bounds[1]
        tile_max_lon = bounding_polygon.bounds[2]

        params = {
            "size": 0,
            "query": {
                "bool": {
                    "filter": [
                        {
                            "term": {
                                "dataset_s": {
                                    "value": ds
                                }
                            }
                        },
                        {
                            "geo_shape": {
                                "geo": {
                                    "shape": {
                                        "type": "envelope",
                                        "coordinates": [[tile_min_lon, tile_max_lat], [tile_max_lon, tile_min_lat]]
                                    },
                                    "relation": "intersects"
                                }
                            }
                        }
                    ]
                }
            },
            "aggs": {
                "distinct_bounding_boxes": {
                    "composite": {
                        "size": 100,
                        "sources": [
                            {
                                "bounding_box": {
                                    "terms": {
                                        "script": {
                                            "source": "String.valueOf(doc['tile_min_lon'].value) + ', ' + String.valueOf(doc['tile_max_lon'].value) + ', ' + String.valueOf(doc['tile_min_lat'].value) + ', ' + String.valueOf(doc['tile_max_lat'].value)",
                                            "lang": "painless"
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
                            
        if 0 < start_time <= end_time:
            params["query"]["bool"]["should"] = self.get_formatted_time_clause(start_time, end_time)
            params["query"]["bool"]["minimum_should_match"] = 1

        self._merge_kwargs(params, **kwargs)
        aggregations = self.do_aggregation_all(params, 'distinct_bounding_boxes')
        distinct_bounds = []
        for agg in aggregations:   
            coords = agg['key']['bounding_box'].split(',')
            min_lon = round(float(coords[0]), 2)
            max_lon = round(float(coords[1]), 2)
            min_lat = round(float(coords[2]), 2)
            max_lat = round(float(coords[3]), 2)
            polygon = 'POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' % (min_lon, max_lat, min_lon, min_lat, max_lon, min_lat, max_lon, max_lat, min_lon, max_lat)
            distinct_bounds.append(wkt.loads(polygon).bounds)
        
        return distinct_bounds