Skip to content

geospatial_analysis

geospatial_analysis

This module contains end user functions for spatially identifying the relevant TS row and distance calculations

calculate_distance_to_fault(longitude: float, latitude: float) -> float

Calculates the distance from a latitude and longitude point to the nearest fault

Parameters:

Name Type Description Default
longitude float

longitude of the point of interest

required
latitude float

latitude of the point of interest

required

Returns:

Name Type Description
d float

distance to fault, rounded to nearest kilometre

Source code in nzssdt_2023/end_user_functions/geospatial_analysis.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def calculate_distance_to_fault(longitude: float, latitude: float) -> float:
    """Calculates the distance from a latitude and longitude point to the nearest fault

    Args:
        longitude: longitude of the point of interest
        latitude: latitude of the point of interest

    Returns:
        d: distance to fault, rounded to nearest kilometre
    """
    point_location = Point(longitude, latitude)
    latlon = gpd.GeoDataFrame(geometry=[point_location], crs="EPSG:4326")

    # convert to NZTM for distance calcs
    latlon_nztm = latlon.to_crs(epsg=2193)
    faults_nztm = FAULTS.to_crs(epsg=2193)

    # calculate minimum distance to fault
    d = round(
        latlon_nztm.geometry.apply(lambda x: faults_nztm.distance(x).min()) / 1000.0
    )

    return d[0]

identify_location_id(longitude: float, latitude: float) -> str

Identifies the TS location assigned to a latitute and longitude

Parameters:

Name Type Description Default
longitude float

longitude of the point of interest

required
latitude float

latitude of the point of interest

required

Returns:

Name Type Description
location_id str

name of the relevant TS location

Source code in nzssdt_2023/end_user_functions/geospatial_analysis.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def identify_location_id(longitude: float, latitude: float) -> str:
    """Identifies the TS location assigned to a latitute and longitude

    Args:
        longitude: longitude of the point of interest
        latitude: latitude of the point of interest

    Returns:
        location_id: name of the relevant TS location
    """

    # check whether point falls within New Zealand
    if sum(NZ_MAP.contains(Point(longitude, latitude))) > 0:

        # identify polygons that the point falls within
        point_location = Point(longitude, latitude)
        within_idx = POLYGONS.contains(point_location)

        # if point falls in a polygon
        if sum(within_idx) > 0:
            # confirm that it only falls in one polygon
            assert sum(within_idx) == 1, "Point falls within more than one polygon"
            location_id = POLYGONS[within_idx].index[0]

        # if point does not fall in a polygon
        else:
            # calculate distance to all grid points
            grid_dist = GRID_PTS.geometry.apply(
                lambda x: point_location.distance(x)
            ).round(4)
            # find the closest locations (ordered by northwest, NE, SW, SE)
            closest_idx = np.where(grid_dist == grid_dist.min())[0]
            # for equidistant points, take the first
            location_id = GRID_PTS.index[closest_idx[0]]

    else:
        location_id = "outside NZ"

    return location_id