#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Functions for vector data handling.
@author: Olli Nevalainen (Finnish Meteorological Institute)
"""
from typing import Tuple
import numpy as np
import pyproj
from shapely.geometry import Polygon
from shapely.ops import transform
[docs]
def expand_bounds(bbox_coordinates: list, amount: float) -> list:
"""Expand bounding box coordinates by amount.
Parameters
----------
bbox_coordinates : list
Bounding box coordinates [xmin, ymin, xmax, ymax].
amount : float
Amount to expand the bounding box.
Returns
-------
bbox_coordinates : list
Expanded bounding box coordinates [xmin, ymin, xmax, ymax].
"""
bbox_coordinates[0] = bbox_coordinates[0] - amount
bbox_coordinates[1] = bbox_coordinates[1] - amount
bbox_coordinates[2] = bbox_coordinates[2] + amount
bbox_coordinates[3] = bbox_coordinates[3] + amount
return bbox_coordinates
[docs]
def coordinate_arrays_from_profile(profile: dict) -> Tuple[np.ndarray, np.ndarray]:
"""Get coordinate arrays from rasterio profile.
Parameters
----------
profile : dict
Rasterio profile.
Returns
-------
xs : np.ndarray
Array of x coordinates.
ys : np.ndarray
Array of y coordinates.
"""
dx = profile["transform"].a
dy = profile["transform"].e
# upperleft corner coordinates
x_ul = profile["transform"].c
y_ul = profile["transform"].f
width = profile["width"]
height = profile["height"]
xs = np.array([x_ul + i * dx for i in range(width)])
ys = np.array([y_ul + i * dy for i in range(height)])
return xs, ys