diff --git a/Bounding Box Transformation.ipynb b/Bounding Box Transformation.ipynb new file mode 100644 index 0000000..7bccaa1 --- /dev/null +++ b/Bounding Box Transformation.ipynb @@ -0,0 +1,107 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "d7a281f3", + "metadata": {}, + "outputs": [], + "source": [ + "from rasters import BBox" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5bde08f4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "BBox(xmin=-118.753625032771, ymin=36.5914562284022, xmax=-118.331319677407, ymax=37.1398151261614, crs=\"EPSG:4326\")" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbox_latlon = BBox(xmin=-118.753625032771, ymin=36.5914562284022, xmax=-118.331319677407, ymax=37.1398151261614, crs=\"EPSG:4326\")\n", + "bbox_latlon" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "63bb94d8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "POINT (-118.54247235508899 36.8656356772818)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbox_latlon.centroid" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c025cf4b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'+proj=utm +zone=11 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbox_latlon.local_UTM_proj4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27e13506", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "rasters", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/rasters/bbox.py b/rasters/bbox.py index 46f6700..4672b3b 100644 --- a/rasters/bbox.py +++ b/rasters/bbox.py @@ -4,6 +4,7 @@ from typing import List, Union from .CRS import CRS, WGS84 +from .point import Point from .polygon import Polygon from .spatial_geometry import SpatialGeometry @@ -212,6 +213,20 @@ def buffer(self, buffer) -> BBox: crs=self.crs ) + @property + def centroid(self) -> Point: + """ + Calculates the centroid of the BBox. + + Returns: + Point: The centroid of the BBox. + """ + return Point( + x=(self.x_min + self.x_max) / 2, + y=(self.y_min + self.y_max) / 2, + crs=self.crs + ) + @property def bbox(self) -> BBox: """ diff --git a/rasters/point.py b/rasters/point.py index f815e64..357d242 100644 --- a/rasters/point.py +++ b/rasters/point.py @@ -8,8 +8,8 @@ from rasters.wrap_geometry import wrap_geometry from .CRS import CRS, WGS84 -from .bbox import BBox -from .polygon import Polygon +# from .bbox import BBox +# from .polygon import Polygon from .vector_geometry import VectorGeometry, SingleVectorGeometry @@ -34,18 +34,21 @@ class Point(SingleVectorGeometry): >>> print(point.x, point.y, point.crs) 10 20 WGS84 """ - def __init__(self, *args, crs: Union[CRS, str] = WGS84): - if isinstance(args[0], Point): + def __init__(self, *args, crs: Union[CRS, str] = WGS84, x: float = None, y: float = None): + if len(args) > 0 and isinstance(args[0], Point): # If the first argument is a Point, extract geometry and crs geometry = args[0].geometry crs = args[0].crs + elif x is not None and y is not None: + # If x and y are provided as keyword arguments + geometry = shapely.geometry.Point(x, y) else: - # Otherwise, create a new shapely Point from the arguments + # Otherwise, create a new shapely Point from the positional arguments geometry = shapely.geometry.Point(*args) - + # Initialize the base VectorGeometry class VectorGeometry.__init__(self, crs=crs) - + self.geometry = geometry @property @@ -101,7 +104,7 @@ def buffer( cap_style=CAP_STYLE.round, join_style=JOIN_STYLE.round, mitre_limit=5.0, - single_sided=False) -> Polygon: + single_sided=False) -> "Polygon": """ Creates a buffer polygon around the point. @@ -117,6 +120,8 @@ def buffer( Returns: Polygon: The buffer polygon. """ + from .polygon import Polygon + # Create the buffer using shapely's buffer method buffer_geom = shapely.geometry.Point.buffer( self.geometry, @@ -132,13 +137,14 @@ def buffer( return Polygon(buffer_geom, crs=self.crs) @property - def bbox(self) -> BBox: + def bbox(self) -> "BBox": """ Returns the bounding box of the point. - + Returns: BBox: The bounding box of the point. """ + from .bbox import BBox return BBox(self.x, self.y, self.x, self.y, crs=self.crs) def distance(self, other: Point) -> float: