Distance Module

Various functions to get distances and azimuth angles between two points etc.

Usage:

Get distance matrix for all turbines and met masts if a dict with location name and tuple(lat, long) is used

>>> locations = {
...    "Metmast": (-4.19, -38.08),
...    "WTG1": (-4.18523, -38.08384),
...    "WTG2": (-4.182794, -38.085799),
...    "WTG6": (-4.185351, -38.087376),
...    "WTG11": (-4.18908, -38.08262),
...    "WTG12": (-4.189967, -38.082009),
...    "WTG13": (-4.190953, -38.081391),
...    "WTG14": (-4.191046, -38.079538),
...    "WTG15": (-4.19211, -38.07853),
...    "WTG17": (-4.19395, -38.07708),
...    "WTG20": (-4.19679, -38.07462),
...    "WTG26": (-4.20256, -38.07288),
...    "WTG30": (-4.20724, -38.06941),
...    "WTG25": (-4.20965, -38.06663),
... }
>>> dist_matrix = distance_matrix_from_dict(locations)
farmnet.data.datasets.distance.azimuth_matrix_from_df(df: DataFrame, lat_col: str, long_col: str, name_col: str | None = None) DataFrame[source]

Compute an azimuth matrix between geographic points in a DataFrame.

The function calculates forward azimuths from one location to another using the WGS84 ellipsoid model via pyproj.Geod.inv.

Parameters:
  • df (pd.DataFrame) – Input DataFrame containing latitude and longitude coordinates.

  • lat_col (str) – Column name containing latitude values.

  • long_col (str) – Column name containing longitude values.

  • name_col (str or None) – Optional column name for row/column labels in the output matrix. If None, integer indices are used.

Returns:

A square DataFrame of azimuths (degrees) from each point to every other.

Return type:

pd.DataFrame

Example:

>>> import pandas as pd
>>> import pandas as pd
>>> df = pd.DataFrame({
...     'lat': [47.226624,  47.38454096, 47.499950],
...     'lon': [8.818437, 8.529927493,  8.737565],
...     'name': ['Rappi', 'Zuerich', 'Winterthur']
... })
>>> azimuth_matrix_from_df(df, 'lat', 'lon', 'name').round(1)
name        Rappi  Zuerich  Winterthur
name
Rappi       180.0    -51.1       -11.3
Zuerich     128.7    180.0        50.6
Winterthur  168.6   -129.3       180.0

Warning

Drop name??

farmnet.data.datasets.distance.azimuth_matrix_from_dict(locations: dict[str, tuple[float, float]]) DataFrame[source]

Compute a matrix of forward azimuths (initial compass bearings) between named geographic locations.

The function takes a dictionary of named locations with (latitude, longitude) coordinates and returns a square DataFrame containing the azimuths (in degrees) from each location to every other using the WGS84 ellipsoid via pyproj.Geod.inv. Azimuths are wrapped to [0, 360) and the diagonal is set to 0°.

Parameters:

locations (dict[str, tuple[float, float]]) – Dictionary where keys are location names and values are (latitude, longitude) tuples.

Returns:

A square DataFrame with azimuths (in degrees) between locations.

Return type:

pd.DataFrame

Example:

>>> locations = {
...     "Rappi": (47.226624, 8.818437),
...     "Zuerich": (47.38454096, 8.529927493),
...     "Winterthur": (47.499950, 8.737565)
... }
>>> azimuth_matrix_from_dict(locations).round(0)
            Rappi  Zuerich  Winterthur
Rappi         0.0    309.0       349.0
Zuerich     129.0      0.0        51.0
Winterthur  169.0    231.0         0.0
farmnet.data.datasets.distance.distance_matrix_from_df(df: DataFrame, lat_col: str, long_col: str, name_col: str | None = None) DataFrame[source]

Compute a distance matrix between points in a DataFrame using the Haversine formula.

The function takes a DataFrame with latitude and longitude columns and returns a square distance matrix as a pandas DataFrame. Distances are in kilometers.

Parameters:
  • df (pd.DataFrame) – Input DataFrame containing geographic coordinates.

  • lat_col (str) – Name of the column containing latitude values.

  • long_col (str) – Name of the column containing longitude values.

  • name_col (str or None) – Optional name of the column to use for row/column labels in the output matrix. If None, a numeric index is used.

Returns:

A square DataFrame representing the pairwise distance matrix (in kilometers).

Return type:

pd.DataFrame

Example

>>> import pandas as pd
>>> df = pd.DataFrame({
...     'lat': [47.226624,  47.38454096, 47.499950],
...     'lon': [8.818437, 8.529927493,  8.737565],
...     'name': ['Rappi', 'Zuerich', 'Winterthur']
... })
>>> distance_matrix_from_df(df, 'lat', 'lon', 'name').round(0)
name        Rappi  Zuerich  Winterthur
name
Rappi         0.0     28.0        31.0
Zuerich      28.0      0.0        20.0
Winterthur   31.0     20.0         0.0

Warning

Drop name??

farmnet.data.datasets.distance.distance_matrix_from_dict(locations: dict[str, tuple[float, float]]) DataFrame[source]

Compute a distance matrix between named geographic locations using the Haversine formula.

The function takes a dictionary of named locations with (latitude, longitude) coordinates and returns a square DataFrame representing pairwise distances in kilometers.

Parameters:

locations (dict[str, tuple[float, float]]) – Dictionary where keys are location names and values are (latitude, longitude) tuples.

Returns:

A square DataFrame with pairwise distances (in kilometers) between locations.

Return type:

pd.DataFrame

Example:

>>> locations = {
...     "Rappi": (47.226624, 8.818437),
...     "Zuerich": (47.38454096, 8.529927493),
...     "Winterthur": (47.499950, 8.737565)
... }
>>> distance_matrix_from_dict(locations).round(0)
            Rappi  Zuerich  Winterthur
Rappi         0.0     28.0        31.0
Zuerich      28.0      0.0        20.0
Winterthur   31.0     20.0         0.0
farmnet.data.datasets.distance.flatten(list_of_lists: list[list]) list[source]

Flatten a list of lists into a single list.

Parameters:

list_of_lists (list[list]) – A list containing sublists.

Returns:

A single flattened list with all elements from the sublists.

Return type:

list

Example:

>>> flatten([[1, 2], [3, 4], [5]])
[1, 2, 3, 4, 5]
>>> flatten([['a'], ['b', 'c'], []])
['a', 'b', 'c']
farmnet.data.datasets.distance.lat_lon_to_xy(locations: dict[str, tuple[float, float]]) list[tuple][source]

Convert a dictionary of (latitude, longitude) coordinates to normalized (x, y) Cartesian coordinates.

This function uses a WGS84 projection via pyproj to convert geographic coordinates into Cartesian coordinates, then normalizes them by dividing by the minimum x and y values.

Parameters:

locations (dict[str, tuple[float, float]]) – Dictionary where keys are location names and values are (latitude, longitude) tuples.

Returns:

A list of tuples in the format (location_name, x, y) with normalized coordinates.

Return type:

list[tuple]

Example:

>>> locations = {
...     "Rappi": (47.226624, 8.818437),
...     "Zuerich": (47.38454096, 8.529927493),
...     "Winterthur": (47.499950, 8.737565)
... }
>>> lat_lon_to_xy(locations)
[('Rappi', np.float64(0.03382320743485345), np.float64(0.0)), ('Zuerich', np.float64(0.0), np.float64(0.0033438121683226907)), ('Winterthur', np.float64(0.024342235871335882), np.float64(0.0057875405195171314))]
farmnet.data.datasets.distance.stack(li: list) list[list][source]

Convert a flat list into a list of single-item lists (i.e., “stack” each element).

Parameters:

li (list) – A flat list of elements.

Returns:

A list where each original element is wrapped in its own sublist.

Return type:

list[list]

Example:

>>> stack([1, 2, 3])
[[1], [2], [3]]
>>> stack(['a', 'b'])
[['a'], ['b']]