Ejemplo n.º 1
0
        def traverse_to(level, key):
            page = self.db.get(key)
            min_lat, min_lon, max_lat, max_lon, pickled_ids = page.split(":")
            page_bounds = BBox(
                min_lat=float(min_lat), min_lon=float(min_lon), max_lat=float(max_lat), max_lon=float(max_lon)
            )

            ids = pickle.loads(pickled_ids)

            if level == 0:  # We're at the best leaf, add it
                if node.id in ids:
                    print "Warning: not adding duplicate id to spatial index"
                    return

                    # Get the potentially expanded bounds
                rect = Rectangle()
                rect.node_ids = ids
                rect.min_lat = float(min_lat)
                rect.min_lon = float(min_lon)
                rect.max_lat = float(max_lat)
                rect.max_lon = float(max_lon)
                rect.include(node.lat, node.lon, node.id)

                # Update the record
                self.db.put(key, str(rect))
            else:  # We're at an intermediate node, keep going
                best = None
                for id in ids:
                    page = self.db.get(id)
                    min_lat, min_lon, max_lat, max_lon, pickled_ids = page.split(":")

                    rect = Rectangle()
                    rect.node_ids = pickle.loads(pickled_ids)
                    rect.min_lat = float(min_lat)
                    rect.min_lon = float(min_lon)
                    rect.max_lat = float(max_lat)
                    rect.max_lon = float(max_lon)
                    area_before = rect.area()

                    rect.include(node.lat, node.lon)
                    area_after = rect.area()

                    growth = area_after - area_before
                    if not best or growth < best[1] or (growth == best[1] and area_after < best[2]):
                        best = (id, growth, area_after, rect)

                if best[1] > 1e-8:  # This rectangle needs to grow
                    self.db.put(best[0], str(best[3]))
                traverse_to(level - 1, best[0])
Ejemplo n.º 2
0
        def traverse_to(level, key):
            page = self.db.get(key)
            min_lat, min_lon, max_lat, max_lon, pickled_ids = page.split(":")
            page_bounds = BBox(
                min_lat=float(min_lat), min_lon=float(min_lon), max_lat=float(max_lat), max_lon=float(max_lon)
            )

            if not within(page_bounds, node):
                return

            ids = pickle.loads(pickled_ids)

            if level == 0:  # We're at a leaf that intersects, done
                if node.id in ids:
                    ids.remove(node.id)

                    rect = Rectangle()
                    rect.node_ids = ids
                    rect.min_lat = float(min_lat)
                    rect.min_lon = float(min_lon)
                    rect.max_lat = float(max_lat)
                    rect.max_lon = float(max_lon)

                    self.db.put(key, str(rect))
            else:  # We're at an intermediate node, keep going
                for id in ids:
                    traverse_to(level - 1, id)