### Install Development Dependencies Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Install the project dependencies for development, including optional extras. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Install pre-commit Hook Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Installs the pre-commit hook for code quality checks. This command should be run after installing the pre-commit package. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### Install PyGeoIf using pip Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Install PyGeoIf from PyPI using pip. This command ensures you have the latest version available. ```bash pip install pygeoif ``` -------------------------------- ### Run Doctests Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Execute doctests embedded within the README.rst file to verify example code snippets. ```bash pytest --doctest-glob="README.rst" ``` -------------------------------- ### Create Geometry from Shapely Point Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Converts a shapely Point object to a pygeoif geometry object. Ensure shapely is installed and imported. ```pycon >>> from shapely.geometry import Point >>> from pygeoif import geometry, shape >>> shape(Point(0,0)) Point(0.0, 0.0) ``` -------------------------------- ### Get GeoJSON Interface Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Returns the GeoJSON-compatible dictionary representation of a geometry. ```APIDOC ## Get GeoJSON Interface ### Description Returns the `__geo_interface__` dictionary, which is compatible with GeoJSON specifications. ### Method N/A (Method call on a geometry object) ### Endpoint N/A ### Request Example N/A ### Response #### Success Response (200) - **geo_interface** (dict) - A dictionary representing the geometry in GeoJSON format. ``` -------------------------------- ### Create and Inspect Points Source: https://context7.com/cleder/pygeoif/llms.txt Demonstrates creating 2D and 3D points, accessing coordinates, and checking for empty geometry states. ```python from pygeoif import Point # Create a 2D point p = Point(1.0, -1.0) print(p) # POINT (1.0 -1.0) print(p.x) # 1.0 print(p.y) # -1.0 print(p.bounds) # (1.0, -1.0, 1.0, -1.0) print(p.wkt) # POINT (1.0 -1.0) print(p.__geo_interface__) # {'type': 'Point', 'bbox': (1.0, -1.0, 1.0, -1.0), 'coordinates': (1.0, -1.0)} # Create a 3D point p3d = Point(1.0, 2.0, 3.0) print(p3d) # POINT (1.0 2.0 3.0) print(p3d.z) # 3.0 print(p3d.has_z) # True print(p3d.wkt) # POINT Z (1.0 2.0 3.0) # Check if point is empty empty_point = Point(float('nan'), float('nan')) print(empty_point.is_empty) # True print(bool(empty_point)) # False ``` -------------------------------- ### Manage LinearRings Source: https://context7.com/cleder/pygeoif/llms.txt Illustrates creating closed rings, checking orientation, and calculating centroids. ```python from pygeoif import LinearRing # Create a linear ring (automatically closes if needed) ring = LinearRing([(0, 0), (0, 1), (1, 1), (1, 0)]) print(ring) # LINEARRING (0 0, 0 1, 1 1, 1 0, 0 0) print(ring.coords) # ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)) # Check orientation (counter-clockwise is positive area) print(ring.is_ccw) # True (counter-clockwise) # Get centroid (for 2D rings only) centroid = ring.centroid if centroid: print(f"Centroid: ({centroid.x}, {centroid.y})") # Centroid: (0.5, 0.5) # Create a clockwise ring cw_ring = LinearRing([(0, 0), (1, 0), (1, 1), (0, 1)]) print(cw_ring.is_ccw) # False (clockwise) ``` -------------------------------- ### Create and Iterate LineStrings Source: https://context7.com/cleder/pygeoif/llms.txt Shows how to construct LineStrings from coordinate tuples or existing Point objects and iterate over their vertices. ```python from pygeoif import LineString, Point # Create from coordinate tuples line = LineString([(0.0, 0.0), (1.0, 1.0), (2.0, 0.0)]) print(line) # LINESTRING (0.0 0.0, 1.0 1.0, 2.0 0.0) print(line.bounds) # (0.0, 0.0, 2.0, 1.0) print(line.coords) # ((0.0, 0.0), (1.0, 1.0), (2.0, 0.0)) print(line.wkt) # LINESTRING (0.0 0.0, 1.0 1.0, 2.0 0.0) # Create from Points p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(2, 0) line_from_points = LineString.from_points(p1, p2, p3) print(line_from_points) # LineString(((0, 0), (1, 1), (2, 0))) # Iterate over points in the line for point in line.geoms: print(f"Point: ({point.x}, {point.y})") # Get GeoJSON-like interface print(line.__geo_interface__) # {'type': 'LineString', 'bbox': (0.0, 0.0, 2.0, 1.0), 'coordinates': ((0.0, 0.0), (1.0, 1.0), (2.0, 0.0))} ``` -------------------------------- ### Construct and Inspect Geometries Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Demonstrates creating Point and LineString objects and accessing their GeoJSON interface and bounds. ```pycon >>> from pygeoif import geometry >>> p = geometry.Point(1,1) >>> p.__geo_interface__ {'type': 'Point', 'bbox': (1, 1, 1, 1), 'coordinates': (1, 1)} >>> print(p) POINT (1 1) >>> p Point(1, 1) >>> l = geometry.LineString([(0.0, 0.0), (1.0, 1.0)]) >>> l.bounds (0.0, 0.0, 1.0, 1.0) >>> print(l) LINESTRING (0.0 0.0, 1.0 1.0) ``` -------------------------------- ### Run pre-commit All Files Check Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Executes all pre-commit hooks on all files in the repository to ensure code consistency and quality. ```bash pre-commit run --all-files ``` -------------------------------- ### Create and Inspect a Point Geometry in PyGeoIf Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Demonstrates creating a Point object and accessing its GeoJSON-like interface and string representation. The `__geo_interface__` attribute provides a dictionary representation, while `print(p)` and `p` show the WKT and object representations, respectively. ```python from pygeoif import geometry p = geometry.Point(1,1) p.__geo_interface__ print(p) p ``` -------------------------------- ### Geometry Creation from Shapely Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Demonstrates how to create pygeoif geometries from shapely objects. ```APIDOC ## Geometry Creation from Shapely ### Description This example shows how to convert a shapely geometry object into a pygeoif geometry object using the `shape` function. ### Method N/A (Function call) ### Endpoint N/A ### Request Example ```python from shapely.geometry import Point from pygeoif import shape shape(Point(0,0)) ``` ### Response Example ``` Point(0.0, 0.0) ``` ``` -------------------------------- ### Run Unit and Static Tests Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Executes the unit tests and static analysis checks for the pygeoif project. This includes doctests embedded in the README. ```bash pytest tests pytest --doctest-glob="README.rst" ``` -------------------------------- ### Run Pytest Suite Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Execute the unit and static tests for the Pygeoif library using pytest. ```bash pytest tests ``` -------------------------------- ### Compare Features Source: https://context7.com/cleder/pygeoif/llms.txt Demonstrates comparing two Feature objects for equality. ```python feature2 = Feature(Point(1.0, -1.0), properties) print(feature == feature2) # True ``` -------------------------------- ### Create and Inspect a LineString Geometry in PyGeoIf Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Shows how to create a LineString from a list of coordinate tuples and access its bounds and string representation. The `bounds` attribute returns the minimum bounding box, and `print(l)` displays the Well-Known Text (WKT) format. ```python l = geometry.LineString([(0.0, 0.0), (1.0, 1.0)]) l.bounds print(l) ``` -------------------------------- ### Construct Polygons with Holes Source: https://context7.com/cleder/pygeoif/llms.txt Demonstrates creating simple polygons and polygons containing interior holes, with access to exterior and interior rings. ```python from pygeoif import Polygon, LinearRing # Create a simple polygon (square) coords = [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)] polygon = Polygon(coords) print(polygon) # Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)),) print(polygon.wkt) # POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0)) print(polygon.bounds) # (0, 0, 1, 1) # Create a polygon with a hole exterior = [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)] hole = [(2, 2), (2, 8), (8, 8), (8, 2), (2, 2)] polygon_with_hole = Polygon(exterior, holes=[hole]) print(polygon_with_hole.wkt) # POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0),(2 2, 2 8, 8 8, 8 2, 2 2)) # Access exterior and interior rings print(polygon_with_hole.exterior.coords) for interior in polygon_with_hole.interiors: print(f"Hole: {interior.coords}") ``` -------------------------------- ### Create a Point geometry Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Instantiate a Point object and access its coordinate attributes. ```pycon >>> from pygeoif import Point >>> p = Point(1.0, -1.0) >>> print(p) POINT (1.0 -1.0) >>> p.y -1.0 >>> p.x 1.0 ``` -------------------------------- ### Create a GeometryCollection Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Group multiple geometry instances into a single collection. ```pycon >>> from pygeoif import geometry >>> p = geometry.Point(1.0, -1.0) >>> p2 = geometry.Point(1.0, -1.0) >>> geoms = [p, p2] >>> c = geometry.GeometryCollection(geoms) >>> [geom for geom in geoms] [Point(1.0, -1.0), Point(1.0, -1.0)] ``` -------------------------------- ### Create a Feature Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Combine a geometry with a dictionary of user-defined properties. ```pycon >>> from pygeoif import Point, Feature >>> p = Point(1.0, -1.0) >>> props = {'Name': 'Sample Point', 'Other': 'Other Data'} >>> a = Feature(p, props) >>> a.properties {'Name': 'Sample Point', 'Other': 'Other Data'} >>> a.properties['Name'] 'Sample Point' ``` -------------------------------- ### Create and Use MultiLineString Source: https://context7.com/cleder/pygeoif/llms.txt Construct MultiLineString objects from coordinate sequences or LineString objects. Demonstrates WKT representation, iteration over constituent LineStrings, and GeoJSON interface. ```python from pygeoif import MultiLineString, LineString # Create from coordinate sequences mls = MultiLineString([ [(0, 0), (1, 1)], [(2, 2), (3, 3), (4, 2)] ]) print(mls) # MultiLineString((((0, 0), (1, 1)), ((2, 2), (3, 3), (4, 2)))) print(mls.wkt) # MULTILINESTRING ((0 0, 1 1),(2 2, 3 3, 4 2)) print(len(mls)) # 2 # Create from LineString objects line1 = LineString([(0, 0), (1, 1)]) line2 = LineString([(2, 2), (3, 3)]) mls_from_lines = MultiLineString.from_linestrings(line1, line2) # Iterate over LineStrings for linestring in mls.geoms: print(f"Line with {len(linestring.geoms)} points") # Get GeoJSON interface print(mls.__geo_interface__) # {'type': 'MultiLineString', 'bbox': (0, 0, 4, 3), 'coordinates': (((0, 0), (1, 1)), ((2, 2), (3, 3), (4, 2)))} ``` -------------------------------- ### Geometry Classes Overview Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Common attributes and methods available across all pygeoif geometry classes. ```APIDOC ## Geometry Attributes ### Attributes - **__geo_interface__** (interface) - Interface to GeoJSON. - **geom_type** (string) - Returns the Geometry Type. - **bounds** (tuple) - Returns (minx, miny, maxx, maxy) bounding box. - **wkt** (string) - Returns the Well Known Text representation. ### Methods - **convex_hull()** - Returns the smallest convex Polygon containing all points. ``` -------------------------------- ### Format Code with Ruff Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Uses Ruff to automatically format the pygeoif codebase according to defined style guidelines. ```bash ruff fmt pygeoif ``` -------------------------------- ### Create a FeatureCollection Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Aggregate multiple Feature instances into a collection. ```pycon >>> from pygeoif import Point, Feature, FeatureCollection >>> p = Point(1.0, -1.0) >>> props = {'Name': 'Sample Point', 'Other': 'Other Data'} >>> a = Feature(p, props) >>> p2 = Point(1.0, -1.0) >>> props2 = {'Name': 'Sample Point2', 'Other': 'Other Data2'} >>> b = Feature(p2, props2) >>> features = [a, b] >>> c = FeatureCollection(features) >>> [feature for feature in c] [Feature(Point(1.0, -1.0), {'Name': 'Sample Point', 'Other': 'Other Data'},...] ``` -------------------------------- ### Create Bounding Box Polygons Source: https://context7.com/cleder/pygeoif/llms.txt Generate rectangular polygons from bounding box coordinates, with configurable orientation. ```python from pygeoif.factories import box # Create a simple bounding box polygon bbox_polygon = box(0, 0, 10, 10) print(bbox_polygon.wkt) # POLYGON ((10 0, 10 10, 0 10, 0 0, 10 0)) print(bbox_polygon.bounds) # (0, 0, 10, 10) # Create with clockwise orientation cw_box = box(0, 0, 5, 5, ccw=False) print(cw_box.wkt) # POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0)) # Use for spatial queries minx, miny, maxx, maxy = -122.5, 37.5, -122.0, 38.0 san_francisco_bbox = box(minx, miny, maxx, maxy) print(san_francisco_bbox) ``` -------------------------------- ### Create and Use MultiPoint Source: https://context7.com/cleder/pygeoif/llms.txt Instantiate MultiPoint objects from coordinate tuples or Point objects. Supports deduplication of points with the `unique` parameter. Demonstrates iteration and accessing bounds. ```python from pygeoif import MultiPoint, Point # Create from coordinate tuples mp = MultiPoint([(0, 0), (1, 1), (2, 2)]) print(mp) # MultiPoint(((0, 0), (1, 1), (2, 2))) print(mp.wkt) # MULTIPOINT ((0 0), (1 1), (2 2)) print(len(mp)) # 3 # Create with unique points (removes duplicates) mp_unique = MultiPoint([(0, 0), (1, 1), (0, 0), (1, 1)], unique=True) print(len(mp_unique)) # 2 # Create from Point objects p1, p2, p3 = Point(0, 0), Point(1, 1), Point(2, 2) mp_from_points = MultiPoint.from_points(p1, p2, p3) # Iterate over points for point in mp.geoms: print(f"Point: ({point.x}, {point.y})") # Get bounds of all points print(mp.bounds) # (0, 0, 2, 2) ``` -------------------------------- ### pygeoif Geometry Classes Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Overview of pygeoif geometry classes and their common attributes. ```APIDOC ## pygeoif Geometry Classes All geometry classes implement the attribute: * ``__geo_interface__``: An interface to GeoJSON. All geometry classes implement the attributes: * ``geom_type``: Returns a string specifying the Geometry Type of the object. * ``bounds``: Returns a (minx, miny, maxx, maxy) tuple that bounds the object. * ``wkt``: Returns the 'Well Known Text' representation of the object. For two-dimensional geometries, the following methods are implemented: * ``convex_hull``: Returns a representation of the smallest convex Polygon containing all the points in the object unless the number of points in the object is less than three. For two points, the convex hull collapses to a LineString; for 1, a Point. For three-dimensional objects, only their projection in the xy plane is taken into consideration. Empty objects without coordinates return ``None`` for the convex_hull. ### Point A zero-dimensional geometry. Attributes: x, y, z : float Coordinate values Example: ```python >>> from pygeoif import Point >>> p = Point(1.0, -1.0) >>> print(p) POINT (1.0 -1.0) >>> p.y -1.0 >>> p.x 1.0 ``` ### LineString A one-dimensional figure comprising one or more line segments. Attributes: geoms : sequence A sequence of Points ### LinearRing A closed one-dimensional geometry comprising one or more line segments. ### Polygon A two-dimensional figure bounded by a linear ring. Attributes: exterior : LinearRing The ring which bounds the positive space of the polygon. interiors : sequence A sequence of rings which bound all existing holes. ### MultiPoint A collection of one or more points. Attributes: geoms : sequence A sequence of Points. ### MultiLineString A collection of one or more line strings. Attributes: geoms : sequence A sequence of LineStrings ### MultiPolygon A collection of one or more polygons. Attributes: geoms : sequence A sequence of `Polygon` instances ### GeometryCollection A heterogenous collection of geometries (Points, LineStrings, LinearRings and Polygons). Attributes: geoms : sequence A sequence of geometry instances Note: ``GEOMETRYCOLLECTION`` isn't supported by the Shapefile or GeoJSON format. Example: ```python >>> from pygeoif import geometry >>> p = geometry.Point(1.0, -1.0) >>> p2 = geometry.Point(1.0, -1.0) >>> geoms = [p, p2] >>> c = geometry.GeometryCollection(geoms) >>> [geom for geom in geoms] [Point(1.0, -1.0), Point(1.0, -1.0)] ``` ### Feature Aggregates a geometry instance with associated user-defined properties. Attributes: geometry : object A geometry instance properties : dict A dictionary linking field keys with values associated with the geometry instance Example: ```python >>> from pygeoif import Point, Feature >>> p = Point(1.0, -1.0) >>> props = {'Name': 'Sample Point', 'Other': 'Other Data'} >>> a = Feature(p, props) >>> a.properties {'Name': 'Sample Point', 'Other': 'Other Data'} >>> a.properties['Name'] 'Sample Point' ``` ### FeatureCollection A heterogenous collection of Features. Attributes: features: sequence A sequence of feature instances Example: ```python >>> from pygeoif import Point, Feature, FeatureCollection >>> p = Point(1.0, -1.0) >>> props = {'Name': 'Sample Point', 'Other': 'Other Data'} >>> a = Feature(p, props) >>> p2 = Point(1.0, -1.0) >>> props2 = {'Name': 'Sample Point2', 'Other': 'Other Data2'} >>> b = Feature(p2, props2) >>> features = [a, b] >>> c = FeatureCollection(features) >>> [feature for feature in c] [Feature(Point(1.0, -1.0), {'Name': 'Sample Point', 'Other': 'Other Data'},...)] ``` ``` -------------------------------- ### Create Geometry from WKT Source: https://github.com/cleder/pygeoif/blob/develop/README.rst API for creating geometry objects from Well-Known Text (WKT) representation. ```APIDOC ## Create Geometry from WKT ### Description Creates a geometry object from its Well-Known Text (WKT) representation using the `from_wkt` function. ### Method N/A (Function call) ### Endpoint N/A ### Parameters #### Query Parameters - **wkt_string** (string) - Required - The WKT string representing the geometry. ### Request Example ```python from pygeoif import from_wkt p = from_wkt('POINT (0 1)') print(p) ``` ### Response Example ``` POINT (0.0 1.0) ``` ``` -------------------------------- ### Manage FeatureCollections Source: https://context7.com/cleder/pygeoif/llms.txt Create and interact with collections of features, including iteration and GeoJSON interface access. ```python from pygeoif import Feature, FeatureCollection, Point, LineString # Create features f1 = Feature(Point(0, 0), {'name': 'Origin'}) f2 = Feature(Point(10, 10), {'name': 'Far Point'}) f3 = Feature(LineString([(0, 0), (5, 5)]), {'name': 'Line'}) # Create collection fc = FeatureCollection([f1, f2, f3]) print(len(fc)) # 3 print(fc.bounds) # (0, 0, 10, 10) # Iterate over features for feature in fc: print(f"{feature.properties['name']}: {feature.geometry.geom_type}") # Access via generator for feature in fc.features: print(feature.geometry) # Get GeoJSON-like interface geo_interface = fc.__geo_interface__ print(geo_interface['type']) # FeatureCollection print(len(geo_interface['features'])) # 3 ``` -------------------------------- ### Check Code with Ruff Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Uses Ruff to check the pygeoif codebase for linting errors and style issues. ```bash ruff check pygeoif ``` -------------------------------- ### Create and Use GeometryCollection Source: https://context7.com/cleder/pygeoif/llms.txt Construct a GeometryCollection with mixed geometry types, including nested collections. Note that GEOMETRYCOLLECTION is not supported by Shapefile or GeoJSON formats. Demonstrates WKT, iteration, and GeoJSON interface. ```python from pygeoif import GeometryCollection, Point, LineString, Polygon # Create a collection with mixed geometry types point = Point(0, 0) line = LineString([(1, 1), (2, 2)]) polygon = Polygon([(3, 3), (3, 4), (4, 4), (4, 3)]) gc = GeometryCollection([point, line, polygon]) print(gc) # GeometryCollection((Point(0, 0), LineString(((1, 1), (2, 2))), Polygon(((3, 3), (3, 4), (4, 4), (4, 3), (3, 3)),))) print(gc.wkt) # GEOMETRYCOLLECTION (POINT (0 0), LINESTRING (1 1, 2 2), POLYGON ((3 3, 3 4, 4 4, 4 3, 3 3))) print(len(gc)) # 3 # Iterate over geometries for geom in gc.geoms: print(f"{geom.geom_type}: {geom}") # Nested collections nested_gc = GeometryCollection([ gc, Point(10, 10) ]) print(nested_gc.wkt) # Get GeoJSON-like interface print(gc.__geo_interface__) # {'type': 'GeometryCollection', 'geometries': ({'type': 'Point', 'bbox': (0, 0, 0, 0), 'coordinates': (0, 0)}, ...)} ``` -------------------------------- ### pygeoif Functions Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Documentation for the 'shape' function. ```APIDOC ## shape Function ### Description Create a pygeoif feature from an object that provides the ``__geo_interface__`` or any GeoJSON compatible dictionary. ### Method Not applicable (function call) ### Endpoint Not applicable (function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python # Example usage would depend on having an object with __geo_interface__ or a GeoJSON dict # from pygeoif import shape # geojson_data = {...} # pygeoif_object = shape(geojson_data) ``` ### Response #### Success Response (200) - **pygeoif_object** (object) - A pygeoif geometry object created from the input. #### Response Example ```json { "example": "A pygeoif geometry object (e.g., Point, Polygon, etc.)" } ``` ``` -------------------------------- ### Create and Use MultiPolygon Source: https://context7.com/cleder/pygeoif/llms.txt Instantiate MultiPolygon objects from polygon coordinate sequences or Polygon objects. Supports polygons with holes. Demonstrates WKT representation, iteration, and bounds. ```python from pygeoif import MultiPolygon, Polygon # Create from polygon coordinate sequences mp = MultiPolygon([ ([(0, 0), (0, 1), (1, 1), (1, 0)],), # Simple polygon ([(2, 2), (2, 4), (4, 4), (4, 2)], [[(2.5, 2.5), (2.5, 3.5), (3.5, 3.5), (3.5, 2.5)]]) # With hole ]) print(mp.wkt) # MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)),((2 2, 2 4, 4 4, 4 2, 2 2),(2.5 2.5, 2.5 3.5, 3.5 3.5, 3.5 2.5, 2.5 2.5))) print(len(mp)) # 2 # Create from Polygon objects poly1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) poly2 = Polygon([(5, 5), (5, 6), (6, 6), (6, 5)]) mp_from_polys = MultiPolygon.from_polygons(poly1, poly2) # Iterate over polygons for polygon in mp.geoms: print(f"Polygon with exterior: {polygon.exterior.coords}") # Check bounds print(mp.bounds) # (0, 0, 4, 4) ``` -------------------------------- ### Create Geometry from WKT String Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Creates a pygeoif geometry object from its Well-Known Text (WKT) representation. The `from_wkt` function parses the string and returns the corresponding geometry. ```pycon >>> from pygeoif import from_wkt >>> p = from_wkt('POINT (0 1)') >>> print(p) POINT (0.0 1.0) ``` -------------------------------- ### Define Custom Spatial Reference System (SRS) Source: https://context7.com/cleder/pygeoif/llms.txt Defines a custom Spatial Reference System (SRS) with specific minimum and maximum XYZ bounds. This custom SRS can then be used with Hypothesis strategies to generate geometries within these defined boundaries. ```python from pygeoif.srs import Srs custom_srs = Srs( name="Custom", min_xyz=(0, 0, 0), max_xyz=(1000, 1000, 100) ) ``` -------------------------------- ### Test LineString Bounds with Hypothesis Source: https://context7.com/cleder/pygeoif/llms.txt Tests the bounds property of LineString objects generated by Hypothesis. Ensures that the minimum x is less than or equal to the maximum x, and the minimum y is less than or equal to the maximum y. ```python from pygeoif.testing import line_strings from hypothesis import given @given(line_strings(max_points=10)) def test_linestring_bounds(line): if not line.is_empty: bounds = line.bounds assert bounds[0] <= bounds[2] # minx <= maxx assert bounds[1] <= bounds[3] # miny <= maxy ``` -------------------------------- ### Create and Use Feature Source: https://context7.com/cleder/pygeoif/llms.txt Aggregate a geometry with user-defined properties and an optional feature ID. Features implement `__geo_interface__` for GeoJSON compatibility. Demonstrates creating features with and without IDs. ```python from pygeoif import Feature, Point, Polygon # Create a feature with properties point = Point(1.0, -1.0) properties = {'name': 'Sample Point', 'category': 'marker', 'value': 42} feature = Feature(point, properties) print(feature.geometry) # Point(1.0, -1.0) print(feature.properties) # {'name': 'Sample Point', 'category': 'marker', 'value': 42} print(feature.properties['name']) # Sample Point # Create a feature with an ID polygon = Polygon([(0, 0), (0, 10), (10, 10), (10, 0)]) feature_with_id = Feature(polygon, {'type': 'boundary'}, feature_id='boundary_001') print(feature_with_id.id) # boundary_001 # Get GeoJSON-like interface print(feature.__geo_interface__) ``` -------------------------------- ### Utility Functions Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Functions for creating geometries and calculating spatial properties. ```APIDOC ## shape(obj) - **Description**: Create a pygeoif feature from an object providing __geo_interface__ or a GeoJSON dictionary. ## from_wkt(wkt_string) - **Description**: Create a geometry from its WKT representation. ## signed_area(ring) - **Description**: Return the signed area enclosed by a ring. A value >= 0 indicates a counter-clockwise oriented ring. ``` -------------------------------- ### Force 2D Geometry Source: https://context7.com/cleder/pygeoif/llms.txt Converts geometries to 2D by dropping z-coordinates. Geometries already in 2D remain unchanged. ```python from pygeoif import Point, LineString, Polygon from pygeoif import force_2d # Convert 3D point to 2D point_3d = Point(1, 2, 3) point_2d = force_2d(point_3d) print(point_2d) # Point(1, 2) print(point_2d.has_z) # False # Convert 3D linestring to 2D line_3d = LineString([(0, 0, 0), (1, 1, 1), (2, 2, 2)]) line_2d = force_2d(line_3d) print(line_2d.coords) # ((0, 0), (1, 1), (2, 2)) # Already 2D geometry passes through unchanged point_already_2d = Point(5, 5) result = force_2d(point_already_2d) print(result) # Point(5, 5) ``` -------------------------------- ### Map Geometries to GeoJSON Source: https://context7.com/cleder/pygeoif/llms.txt Convert pygeoif geometries to GeoJSON-compatible dictionaries using the mapping function. ```python from pygeoif import mapping, Point, Polygon # Get mapping from Point point = Point(1, 2) geo_dict = mapping(point) print(geo_dict) # {'type': 'Point', 'bbox': (1, 2, 1, 2), 'coordinates': (1, 2)} # Get mapping from Polygon polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) poly_dict = mapping(polygon) print(poly_dict['type']) # Polygon print(poly_dict['coordinates']) # Convert to JSON import json json_str = json.dumps(geo_dict) print(json_str) # {"type": "Point", "bbox": [1, 2, 1, 2], "coordinates": [1, 2]} ``` -------------------------------- ### box Source: https://context7.com/cleder/pygeoif/llms.txt Creates a rectangular polygon from bounding box coordinates. ```APIDOC ## box ### Description Creates a rectangular Polygon geometry from min/max coordinates. ### Parameters - **minx** (float) - Required - Minimum X coordinate. - **miny** (float) - Required - Minimum Y coordinate. - **maxx** (float) - Required - Maximum X coordinate. - **maxy** (float) - Required - Maximum Y coordinate. - **ccw** (bool) - Optional - Counter-clockwise orientation (default True). ``` -------------------------------- ### Force 3D Geometry Source: https://context7.com/cleder/pygeoif/llms.txt Converts geometries to 3D by adding a z-coordinate, which defaults to 0 if not specified. ```python from pygeoif import Point, LineString from pygeoif import force_3d # Convert 2D point to 3D with default z=0 point_2d = Point(1, 2) point_3d = force_3d(point_2d) print(point_3d) # Point(1, 2, 0) print(point_3d.has_z) # True # Convert with custom z value elevated = force_3d(point_2d, z=100) print(elevated) # Point(1, 2, 100) # Convert 2D linestring to 3D line_2d = LineString([(0, 0), (1, 1), (2, 2)]) line_3d = force_3d(line_2d, z=50) print(line_3d.coords) # ((0, 0, 50), (1, 1, 50), (2, 2, 50)) # Already 3D geometry maintains original z values point_already_3d = Point(1, 2, 3) result = force_3d(point_already_3d) print(result) # Point(1, 2, 3) ``` -------------------------------- ### shape Source: https://context7.com/cleder/pygeoif/llms.txt Creates a pygeoif geometry from a GeoJSON-like dictionary or any object implementing __geo_interface__. ```APIDOC ## shape ### Description Factory function to convert GeoJSON-like dictionaries or objects with `__geo_interface__` into pygeoif geometry objects. ### Parameters - **obj** (dict/object) - Required - The GeoJSON dictionary or object to convert. ``` -------------------------------- ### Hypothesis Testing Strategies Source: https://context7.com/cleder/pygeoif/llms.txt Utilizes Hypothesis strategies to generate random geometries for property-based testing. ```python from hypothesis import given from pygeoif.hypothesis.strategies import ( points, line_strings, polygons, multi_points, geometry_collections, Srs, epsg4326 ) # Test with random points @given(points()) def test_point_has_coordinates(point): assert point.x is not None assert point.y is not None assert point.geom_type == 'Point' # Test with geographically bounded points (WGS84) @given(points(srs=epsg4326)) def test_point_in_valid_range(point): assert -180 <= point.x <= 180 assert -90 <= point.y <= 90 # Test with 3D points only @given(points(has_z=True)) def test_3d_point(point): assert point.has_z is True assert point.z is not None ``` -------------------------------- ### Test Polygon WKT Roundtrip with Hypothesis Source: https://context7.com/cleder/pygeoif/llms.txt Verifies that a Polygon geometry can be converted to its Well-Known Text (WKT) representation and then successfully parsed back into a Polygon object. This ensures WKT serialization and deserialization works correctly. ```python from pygeoif.testing import polygons from pygeoif import from_wkt from hypothesis import given @given(polygons(max_points=8, max_interiors=2)) def test_polygon_wkt_roundtrip(polygon): if not polygon.is_empty: wkt = polygon.wkt restored = from_wkt(wkt) assert restored.geom_type == 'Polygon' ``` -------------------------------- ### box Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Creates a rectangular polygon with a configurable normal vector. ```APIDOC ## box ### Description Return a rectangular polygon with configurable normal vector. ``` -------------------------------- ### Parse WKT Geometries Source: https://context7.com/cleder/pygeoif/llms.txt Convert Well-Known Text strings into pygeoif geometry objects. ```python from pygeoif import from_wkt # Parse point point = from_wkt('POINT (1 2)') print(point) # Point(1.0, 2.0) # Parse 3D point point_3d = from_wkt('POINT Z (1 2 3)') print(point_3d.z) # 3.0 # Parse linestring line = from_wkt('LINESTRING (0 0, 1 1, 2 0)') print(line.coords) # ((0.0, 0.0), (1.0, 1.0), (2.0, 0.0)) # Parse polygon with hole polygon = from_wkt('POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (2 2, 2 8, 8 8, 8 2, 2 2))') print(len(list(polygon.interiors))) # 1 # Parse multipoint mp = from_wkt('MULTIPOINT ((0 0), (1 1), (2 2))') print(len(mp)) # 3 # Parse multipolygon mpoly = from_wkt('MULTIPOLYGON (((0 0, 0 1, 1 1, 1 0, 0 0)), ((2 2, 2 3, 3 3, 3 2, 2 2)))') print(len(mpoly)) # 2 # Parse geometry collection gc = from_wkt('GEOMETRYCOLLECTION (POINT (0 0), LINESTRING (1 1, 2 2))') print(gc.geom_type) # GeometryCollection # Case insensitive parsing lower_case = from_wkt('point (5 5)') print(lower_case) # Point(5.0, 5.0) ``` -------------------------------- ### Create Geometries with shape Source: https://context7.com/cleder/pygeoif/llms.txt Factory function to create pygeoif geometries from GeoJSON dictionaries or objects implementing __geo_interface__. ```python from pygeoif import shape # Create from GeoJSON dictionary geojson_point = {'type': 'Point', 'coordinates': [1, 2]} point = shape(geojson_point) print(point) # Point(1, 2) geojson_polygon = { 'type': 'Polygon', 'coordinates': [[(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]] } polygon = shape(geojson_polygon) print(polygon.wkt) # POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0)) # Create from object with __geo_interface__ from pygeoif import Point p = Point(3, 4) p_copy = shape(p) print(p == p_copy) # True # Handle GeometryCollections gc_dict = { 'type': 'GeometryCollection', 'geometries': [ {'type': 'Point', 'coordinates': [0, 0]}, {'type': 'LineString', 'coordinates': [[1, 1], [2, 2]]} ] } gc = shape(gc_dict) print(gc.wkt) # GEOMETRYCOLLECTION (POINT (0 0), LINESTRING (1 1, 2 2)) ``` -------------------------------- ### orient Source: https://context7.com/cleder/pygeoif/llms.txt Returns a polygon with exterior and interior rings oriented consistently. ```APIDOC ## orient ### Description Ensures consistent ring orientation for polygons, typically counter-clockwise for exterior and clockwise for interior rings. ``` -------------------------------- ### orient Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Returns a copy of a polygon with exteriors and interiors in the specified orientation. ```APIDOC ## orient ### Description Returns a copy of a polygon with exteriors and interiors in the right orientation. ### Parameters - **ccw** (boolean) - Required - If True, the exterior will be in counterclockwise orientation and interiors in clockwise. If False, the reverse applies. ``` -------------------------------- ### Orient Polygons Source: https://context7.com/cleder/pygeoif/llms.txt Ensure consistent ring orientation for polygons, typically counter-clockwise for exterior and clockwise for holes. ```python from pygeoif import Polygon from pygeoif.factories import orient ``` -------------------------------- ### Feature and FeatureCollection Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Classes for aggregating geometry instances with user-defined properties. ```APIDOC ## Feature ### Attributes - **geometry** (instance) - A geometry instance. - **properties** (dict) - Dictionary linking field keys with values. ## FeatureCollection ### Attributes - **features** (sequence) - A sequence of feature instances. ``` -------------------------------- ### Type Check with Mypy Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Performs static type checking on the pygeoif codebase using Mypy to catch type errors. ```bash mypy pygeoif ``` -------------------------------- ### Calculate Signed Area Source: https://context7.com/cleder/pygeoif/llms.txt Determines the signed area of a ring to identify orientation, where positive is counter-clockwise and negative is clockwise. ```python from pygeoif.functions import signed_area # Counter-clockwise ring (positive area) ccw_ring = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)] area = signed_area(ccw_ring) print(f"CCW area: {area}") # CCW area: 1.0 # Clockwise ring (negative area) cw_ring = [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)] area = signed_area(cw_ring) print(f"CW area: {area}") # CW area: -1.0 # Use for orientation checking def is_counterclockwise(ring): return signed_area(ring) >= 0 print(is_counterclockwise(ccw_ring)) # True print(is_counterclockwise(cw_ring)) # False ``` -------------------------------- ### FeatureCollection Source: https://context7.com/cleder/pygeoif/llms.txt A collection of Feature objects with combined bounds and iteration support. ```APIDOC ## FeatureCollection ### Description A collection of Feature objects that supports iteration and provides combined bounds. ### Usage - Initialize with a list of Feature objects. - Access bounds via the `.bounds` property. - Iterate over features directly or via the `.features` generator. ``` -------------------------------- ### Create Rectangular Polygon Source: https://github.com/cleder/pygeoif/blob/develop/README.rst Generates a rectangular polygon with a configurable normal vector. ```APIDOC ## Create Rectangular Polygon ### Description Returns a rectangular polygon with a configurable normal vector. ### Method N/A (Function call) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) - **rectangular_polygon** (Polygon) - A rectangular polygon object. ``` -------------------------------- ### Create Polygon from LinearRings Source: https://context7.com/cleder/pygeoif/llms.txt Construct a Polygon object using LinearRing objects for the shell and any holes. This method is useful for defining complex polygonal shapes. ```python from pygeoif import LinearRing, Polygon shell = LinearRing([(0, 0), (0, 5), (5, 5), (5, 0)]) hole_ring = LinearRing([(1, 1), (1, 4), (4, 4), (4, 1)]) poly = Polygon.from_linear_rings(shell, hole_ring) print(poly.__geo_interface__) ``` -------------------------------- ### mapping Source: https://github.com/cleder/pygeoif/blob/develop/docs/overview.md Retrieves the __geo_interface__ dictionary representation of the object. ```APIDOC ## mapping ### Description Return the `__geo_interface__` dictionary. ```