예제 #1
0
def update_existing_locations(
    vial_http: urllib3.connectionpool.ConnectionPool,
    locations: rtree.index.Index,
    source_ids: Iterable[str],
) -> None:
    """Updates rtree index with locations with source ids"""
    for chunked_ids in misc.batch(source_ids, 20):
        updated_locations = search_locations(vial_http, idref=list(chunked_ids))

        for loc in updated_locations:
            if loc.is_valid and loc.get("geometry"):
                locations.insert(_generate_index_row(loc))
예제 #2
0
    def _find_nearest_within_in_index(
        self, p: complex, max_dist: float, index: rtree.index.Index
    ) -> Tuple[Optional[int], Optional[float]]:
        """Find nearest in specific index. Return (idx, dist) tuple, both of which can be None.
        """

        # query the index while discarding anything that is no longer available
        items = [
            item
            for item in index.intersection(
                [p.real - max_dist, p.imag - max_dist, p.real + max_dist, p.imag + max_dist],
                objects=True,
            )
            if self.available[item.id]
        ]
        if len(items) == 0:
            return None, 0

        # we want the closest item, and we want it only if it's not too far
        item = min(items, key=lambda it: self._item_distance(p, it))
        dist = self._item_distance(p, item)
        if dist > max_dist:
            return None, 0

        return item.id, dist
예제 #3
0
    def _find_nearest_in_index(
        self, p: complex, index: rtree.index.Index
    ) -> Tuple[Optional[int], float]:
        """Check the N nearest lines, hopefully find one that is active."""

        for item in index.nearest((p.real, p.imag) * 2, 100, objects=True):
            if self.available[item.id]:
                return item.id, self._item_distance(p, item)

        return None, 0.0
예제 #4
0
def _find_candidates(
    source: location.NormalizedLocation,
    existing: rtree.index.Index,
    candidate_distance: float,
) -> Iterator[dict]:
    """Return a slice of existing locations"""
    src_point = shapely.geometry.Point(
        source.location.longitude,
        source.location.latitude,
    )

    search_bounds = src_point.buffer(candidate_distance).bounds

    yield from existing.intersection(search_bounds, objects="raw")
예제 #5
0
def _find_candidates(
    source: schema.NormalizedLocation,
    existing: rtree.index.Index,
) -> Iterator[dict]:
    """Return a slice of existing locations"""
    src_point = shapely.geometry.Point(
        source.location.longitude,
        source.location.latitude,
    )

    search_bounds = src_point.buffer(CANDIDATE_DEGREES_DISTANCE).bounds

    result = existing.intersection(search_bounds, objects=True)
    for row in result:
        yield row.object
예제 #6
0
def append_cells_to_spatial_tree(tree: rtree.index.Index, cells: List,
                                 idToNum: Dict):
    for element in cells:
        tree.insert(idToNum[element.get_feature_id()],
                    element.get_bounding_box(),
                    obj=element)