quadtree.py 14.4 KB
Newer Older
Joseph Siddons's avatar
Joseph Siddons committed
1 2 3 4 5
"""
Constuctors for QuadTree classes that can decrease the number of comparisons
for detecting nearby records for example
"""

6
from dataclasses import dataclass
Joseph Siddons's avatar
Joseph Siddons committed
7
from datetime import datetime
8
from .distance_metrics import haversine, destination
9
from .utils import LatitudeError
10
from math import degrees, sqrt
Joseph Siddons's avatar
Joseph Siddons committed
11 12 13 14 15 16


class Record:
    """
    ICOADS Record class

17 18 19 20 21 22
    This is a simple instance of an ICOARDS record, it requires position data.
    It can optionally include datetime, a UID, and extra data passed as
    keyword arguments.

    Equality is checked only on the required fields + UID if it is specified.

Joseph Siddons's avatar
Joseph Siddons committed
23 24
    Parameters
    ----------
25
    lon : float
Joseph Siddons's avatar
Joseph Siddons committed
26
        Horizontal coordinate
27
    lat : float
Joseph Siddons's avatar
Joseph Siddons committed
28 29 30 31 32
        Vertical coordinate
    datetime : datetime | None
        Datetime of the record
    uid : str | None
        Unique Identifier
33 34 35 36 37
    fix_lon : bool
        Force longitude to -180, 180
    **data
        Additional data passed to the Record for use by other functions or
        classes.
Joseph Siddons's avatar
Joseph Siddons committed
38 39 40 41 42 43 44 45
    """

    def __init__(
        self,
        lon: float,
        lat: float,
        datetime: datetime | None = None,
        uid: str | None = None,
46
        fix_lon: bool = True,
Joseph Siddons's avatar
Joseph Siddons committed
47 48 49
        **data,
    ) -> None:
        self.lon = lon
50 51 52 53 54 55 56
        if fix_lon:
            # Move lon to -180, 180
            self.lon = ((self.lon + 540) % 360) - 180
        if lat < -90 or lat > 90:
            raise LatitudeError(
                "Expected latitude value to be between -90 and 90 degrees"
            )
Joseph Siddons's avatar
Joseph Siddons committed
57 58 59 60 61 62 63 64
        self.lat = lat
        self.datetime = datetime
        self.uid = uid
        for var, val in data.items():
            setattr(self, var, val)
        return None

    def __str__(self) -> str:
65
        return f"Record(lon = {self.lon}, lat = {self.lat}, datetime = {self.datetime}, uid = {self.uid})"
Joseph Siddons's avatar
Joseph Siddons committed
66 67

    def __eq__(self, other: object) -> bool:
68 69 70 71
        if not isinstance(other, Record):
            return False
        if self.uid and other.uid:
            return self.uid == other.uid
Joseph Siddons's avatar
Joseph Siddons committed
72
        return (
73
            self.lon == other.lon
Joseph Siddons's avatar
Joseph Siddons committed
74 75 76 77 78
            and self.lat == other.lat
            and self.datetime == other.datetime
            and (not (self.uid or other.uid) or self.uid == other.uid)
        )

79 80 81 82 83 84
    def distance(self, other: object) -> float:
        """Compute the Haversine distance to another Record"""
        if not isinstance(other, Record):
            raise TypeError("Argument other must be an instance of Record")
        return haversine(self.lon, self.lat, other.lon, other.lat)

Joseph Siddons's avatar
Joseph Siddons committed
85

86
@dataclass
Joseph Siddons's avatar
Joseph Siddons committed
87 88 89 90 91 92
class Rectangle:
    """
    A simple Rectangle class

    Parameters
    ----------
93
    lon : float
Joseph Siddons's avatar
Joseph Siddons committed
94
        Horizontal centre of the rectangle
95
    lat : float
Joseph Siddons's avatar
Joseph Siddons committed
96
        Vertical centre of the rectangle
97
    lon_range : float
Joseph Siddons's avatar
Joseph Siddons committed
98
        Width of the rectangle
99
    lat_range : float
Joseph Siddons's avatar
Joseph Siddons committed
100 101 102
        Height of the rectangle
    """

103 104 105 106
    lon: float
    lat: float
    lon_range: float
    lat_range: float
Joseph Siddons's avatar
Joseph Siddons committed
107

108 109 110 111 112 113 114 115
    def __post_init__(self):
        if self.lon > 180:
            self.lon -= 360
        if self.lat > 90 or self.lat < -90:
            raise LatitudeError(
                f"Central latitude value out of range {self.lat}, "
                + "should be between -90, 90 degrees"
            )
Joseph Siddons's avatar
Joseph Siddons committed
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    @property
    def west(self) -> float:
        """Western boundary of the Rectangle"""
        return (((self.lon - self.lon_range / 2) + 540) % 360) - 180

    @property
    def east(self) -> float:
        """Eastern boundary of the Rectangle"""
        return (((self.lon + self.lon_range / 2) + 540) % 360) - 180

    @property
    def north(self) -> float:
        """Northern boundary of the Rectangle"""
        north = self.lat + self.lat_range / 2
        if north > 90:
            raise LatitudeError(
                "Rectangle crosses north pole - Use two Rectangles"
            )
        return north

    @property
    def south(self) -> float:
        """Southern boundary of the Rectangle"""
        south = self.lat - self.lat_range / 2
        if south < -90:
            raise LatitudeError(
                "Rectangle crosses south pole - Use two Rectangles"
            )
        return south

    @property
    def edge_dist(self) -> float:
        """Approximate maximum distance from the centre to an edge"""
        corner_dist = max(
            haversine(self.lon, self.lat, self.east, self.north),
            haversine(self.lon, self.lat, self.east, self.south),
Joseph Siddons's avatar
Joseph Siddons committed
153
        )
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        if self.east * self.west < 0:
            corner_dist = max(
                corner_dist,
                haversine(self.lon, self.lat, self.east, 0),
            )
        return corner_dist

    def _test_east_west(self, lon: float) -> bool:
        if self.lon_range >= 360:
            # Rectangle encircles earth
            return True
        if self.east > self.lon and self.west < self.lon:
            return lon <= self.east and lon >= self.west
        if self.east < self.lon:
            return not (lon > self.east and lon < self.west)
        if self.west > self.lon:
            return not (lon < self.east and lon > self.west)
        return False

    def _test_north_south(self, lat: float) -> bool:
        return lat <= self.north and lat >= self.south
Joseph Siddons's avatar
Joseph Siddons committed
175 176 177

    def contains(self, point: Record) -> bool:
        """Test if a point is contained within the Rectangle"""
178 179
        return self._test_north_south(point.lat) and self._test_east_west(
            point.lon
Joseph Siddons's avatar
Joseph Siddons committed
180 181 182 183
        )

    def intersects(self, other: object) -> bool:
        """Test if another Rectangle object intersects this Rectangle"""
184 185 186 187 188 189 190 191 192 193 194 195 196
        if not isinstance(other, Rectangle):
            raise TypeError(
                f"other must be a Rectangle class, got {type(other)}"
            )
        if other.south > self.north:
            # Other is fully north of self
            return False
        if other.north < self.south:
            # Other is fully south of self
            return False
        # Handle east / west edges
        return self._test_east_west(other.west) or self._test_east_west(
            other.east
Joseph Siddons's avatar
Joseph Siddons committed
197 198 199 200 201 202 203 204 205 206 207
        )

    def nearby(
        self,
        point: Record,
        dist: float,
    ) -> bool:
        """Check if point is nearby the Rectangle"""
        # QUESTION: Is this sufficient? Possibly it is overkill
        return (
            haversine(self.lon, self.lat, point.lon, point.lat)
208
            <= dist + self.edge_dist
Joseph Siddons's avatar
Joseph Siddons committed
209 210 211
        )


212
class Ellipse:
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    """
    A simple Ellipse Class for an ellipse on the surface of a sphere.

    Parameters
    ----------
    lon : float
        Horizontal centre of the ellipse
    lat : float
        Vertical centre of the ellipse
    a : float
        Length of the semi-major axis
    b : float
        Length of the semi-minor axis
    theta : float
        Angle of the semi-major axis from horizontal anti-clockwise in radians
    """
229 230 231 232 233

    def __init__(
        self,
        lon: float,
        lat: float,
234 235
        a: float,
        b: float,
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        theta: float,
    ) -> None:
        self.a = a
        self.b = b
        self.lon = lon
        self.lat = lat
        # theta is anti-clockwise angle from horizontal in radians
        self.theta = theta
        # bearing is angle clockwise from north in degrees
        self.bearing = (90 - degrees(self.theta)) % 360

        a2 = self.a * self.a
        b2 = self.b * self.b

        self.c = sqrt(a2 - b2)
        self.p1_lon, self.p1_lat = destination(
            self.lon,
            self.lat,
            self.bearing,
            self.c,
        )
        self.p2_lon, self.p2_lat = destination(
            self.lon,
            self.lat,
260
            (self.bearing - 180) % 360,
261 262 263 264 265 266 267 268 269 270 271 272 273 274
            self.c,
        )

    def contains(self, point: Record) -> bool:
        """Test if a point is contained within the Ellipse"""
        return (
            haversine(self.p1_lon, self.p1_lat, point.lon, point.lat)
            + haversine(self.p2_lon, self.p2_lat, point.lon, point.lat)
        ) <= 2 * self.a

    def nearby_rect(self, rect: Rectangle) -> bool:
        """Test if a rectangle is near to the Ellipse"""
        return (
            haversine(self.p1_lon, self.p1_lat, rect.lon, rect.lat)
275
            <= rect.edge_dist + self.a
276
            and haversine(self.p2_lon, self.p2_lat, rect.lon, rect.lat)
277
            <= rect.edge_dist + self.a
278 279 280
        )


Joseph Siddons's avatar
Joseph Siddons committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
class QuadTree:
    """
    A Simple QuadTree class for PyCOADS

    Parameters
    ----------
    boundary : Rectangle
        The bounding Rectangle of the QuadTree
    capacity : int
        The capacity of each cell, if max_depth is set then a cell at the
        maximum depth may contain more points than the capacity.
    depth : int
        The current depth of the cell. Initialises to zero if unset.
    max_depth : int | None
        The maximum depth of the QuadTree. If set, this can override the
        capacity for cells at the maximum depth.
    """

    def __init__(
        self,
        boundary: Rectangle,
        capacity: int = 5,
        depth: int = 0,
        max_depth: int | None = None,
    ) -> None:
        self.boundary = boundary
        self.capacity = capacity
        self.depth = depth
        self.max_depth = max_depth
        self.points: list[Record] = list()
        self.divided: bool = False
        return None

    def __str__(self) -> str:
        indent = "    " * self.depth
        out = f"{indent}QuadTree:\n"
        out += f"{indent}- boundary: {self.boundary}\n"
        out += f"{indent}- capacity: {self.capacity}\n"
        out += f"{indent}- depth: {self.depth}\n"
        if self.max_depth:
            out += f"{indent}- max_depth: {self.max_depth}\n"
        out += f"{indent}- contents: {self.points}\n"
        if self.divided:
            out += f"{indent}- with children:\n"
            out += f"{self.northwest}"
            out += f"{self.northeast}"
            out += f"{self.southwest}"
            out += f"{self.southeast}"
        return out

    def divide(self):
        """Divide the QuadTree"""
        self.northwest = QuadTree(
            Rectangle(
                self.boundary.lon - self.boundary.lon_range / 4,
                self.boundary.lat + self.boundary.lat_range / 4,
                self.boundary.lon_range / 2,
                self.boundary.lat_range / 2,
            ),
            capacity=self.capacity,
            depth=self.depth + 1,
            max_depth=self.max_depth,
        )
        self.northeast = QuadTree(
            Rectangle(
                self.boundary.lon + self.boundary.lon_range / 4,
                self.boundary.lat + self.boundary.lat_range / 4,
                self.boundary.lon_range / 2,
                self.boundary.lat_range / 2,
            ),
            capacity=self.capacity,
            depth=self.depth + 1,
            max_depth=self.max_depth,
        )
        self.southwest = QuadTree(
            Rectangle(
                self.boundary.lon - self.boundary.lon_range / 4,
                self.boundary.lat - self.boundary.lat_range / 4,
                self.boundary.lon_range / 2,
                self.boundary.lat_range / 2,
            ),
            capacity=self.capacity,
            depth=self.depth + 1,
            max_depth=self.max_depth,
        )
        self.southeast = QuadTree(
            Rectangle(
                self.boundary.lon + self.boundary.lon_range / 4,
                self.boundary.lat - self.boundary.lat_range / 4,
                self.boundary.lon_range / 2,
                self.boundary.lat_range / 2,
            ),
            capacity=self.capacity,
            depth=self.depth + 1,
            max_depth=self.max_depth,
        )
        self.divided = True

    def insert(self, point: Record) -> bool:
        """Insert a point into the QuadTree"""
        if not self.boundary.contains(point):
            return False
        elif self.max_depth and self.depth == self.max_depth:
            self.points.append(point)
            return True
        elif len(self.points) < self.capacity:
            self.points.append(point)
            return True
        else:
            if not self.divided:
                self.divide()
            if self.northwest.insert(point):
                return True
            elif self.northeast.insert(point):
                return True
            elif self.southwest.insert(point):
                return True
            elif self.southeast.insert(point):
                return True
            return False

    def query(
        self,
        rect: Rectangle,
        points: list[Record] | None = None,
    ) -> list[Record]:
        """Get points that fall in a rectangle"""
        if not points:
            points = list()
        if not self.boundary.intersects(rect):
            return points

        for point in self.points:
            if rect.contains(point):
                points.append(point)

        if self.divided:
            points = self.northwest.query(rect, points)
            points = self.northeast.query(rect, points)
            points = self.southwest.query(rect, points)
            points = self.southeast.query(rect, points)

        return points

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    def query_ellipse(
        self,
        ellipse: Ellipse,
        points: list[Record] | None = None,
    ) -> list[Record]:
        """Get points that fall in an ellipse."""
        if not points:
            points = list()
        if not ellipse.nearby_rect(self.boundary):
            return points

        for point in self.points:
            if ellipse.contains(point):
                points.append(point)

        if self.divided:
            points = self.northwest.query_ellipse(ellipse, points)
            points = self.northeast.query_ellipse(ellipse, points)
            points = self.southwest.query_ellipse(ellipse, points)
            points = self.southeast.query_ellipse(ellipse, points)

        return points

Joseph Siddons's avatar
Joseph Siddons committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
    def nearby_points(
        self,
        point: Record,
        dist: float,
        points: list[Record] | None = None,
    ) -> list[Record]:
        """Get all points that are nearby another point"""
        if not points:
            points = list()
        if not self.boundary.nearby(point, dist):
            return points

        for test_point in self.points:
            if (
                haversine(point.lon, point.lat, test_point.lon, test_point.lat)
                <= dist
            ):
                points.append(test_point)

        if self.divided:
            points = self.northwest.nearby_points(point, dist, points)
            points = self.northeast.nearby_points(point, dist, points)
            points = self.southwest.nearby_points(point, dist, points)
            points = self.southeast.nearby_points(point, dist, points)

        return points