structure updates
This commit is contained in:
127
tech_docs/python/Shapely.md
Normal file
127
tech_docs/python/Shapely.md
Normal file
@@ -0,0 +1,127 @@
|
||||
For high-performance geometric and spatial analysis, `Shapely` is a key Python library. It manipulates and analyzes planar geometric objects, building on the robust, C-based GEOS library for operations like: formation, manipulation, analysis, and visualization of complex geometric shapes. Here’s a concise reference guide for common use cases with `Shapely`:
|
||||
|
||||
# `Shapely` Reference Guide
|
||||
|
||||
## Installation
|
||||
```
|
||||
pip install shapely
|
||||
```
|
||||
Note: Ensure you have GEOS library or install `Shapely` with binary dependencies included if you're on Windows or Mac.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
### Importing Shapely
|
||||
```python
|
||||
from shapely.geometry import Point, LineString, Polygon
|
||||
```
|
||||
|
||||
### Creating Geometric Objects
|
||||
|
||||
#### Points
|
||||
```python
|
||||
# Create a point
|
||||
point = Point(0, 0)
|
||||
|
||||
# Create a point with Z dimension
|
||||
point_z = Point(0, 0, 0)
|
||||
```
|
||||
|
||||
#### Lines
|
||||
```python
|
||||
# Create a line from points
|
||||
line = LineString([(0, 0), (1, 1), (2, 2)])
|
||||
|
||||
# Accessing line's coordinates
|
||||
coords = list(line.coords)
|
||||
```
|
||||
|
||||
#### Polygons
|
||||
```python
|
||||
# Create a polygon
|
||||
polygon = Polygon([(0, 0), (1, 1), (1, 0)])
|
||||
|
||||
# Polygon with hole
|
||||
exterior = [(0, 0), (2, 0), (2, 2), (0, 2)]
|
||||
hole = [(0.5, 0.5), (1.5, 0.5), (1.5, 1.5)]
|
||||
polygon_with_hole = Polygon(shell=exterior, holes=[hole])
|
||||
```
|
||||
|
||||
## Operations
|
||||
|
||||
### Geometric Operations
|
||||
```python
|
||||
# Buffer - create a buffer around the geometry
|
||||
buffered_point = point.buffer(1.0)
|
||||
buffered_line = line.buffer(0.5)
|
||||
|
||||
# Intersection
|
||||
intersection = polygon.intersection(buffered_line)
|
||||
|
||||
# Union
|
||||
union = polygon.union(buffered_line)
|
||||
```
|
||||
|
||||
### Geometric Properties
|
||||
```python
|
||||
# Area
|
||||
area = polygon.area
|
||||
|
||||
# Length
|
||||
length = line.length
|
||||
|
||||
# Boundary
|
||||
boundary = polygon.boundary
|
||||
```
|
||||
|
||||
### Relationship Tests
|
||||
```python
|
||||
# Check if geometries intersect
|
||||
intersects = polygon.intersects(line)
|
||||
|
||||
# Check if a geometry contains another
|
||||
contains = polygon.contains(point)
|
||||
|
||||
# Check if a geometry is within another
|
||||
within = point.within(polygon)
|
||||
```
|
||||
|
||||
## Spatial Analysis
|
||||
|
||||
### Distance Calculation
|
||||
```python
|
||||
# Distance between two geometries
|
||||
distance = point.distance(line)
|
||||
```
|
||||
|
||||
### Convex Hull
|
||||
```python
|
||||
# Create a convex hull around geometry collection
|
||||
hull = polygon.convex_hull
|
||||
```
|
||||
|
||||
## Advanced Features
|
||||
|
||||
### Coordinate Systems and Projections
|
||||
Shapely does not directly handle coordinate system transformations or projections. It's focused on the geometric operations. For handling projections and transformations, integrating with libraries like `pyproj` is common.
|
||||
|
||||
### Integrating with Other Libraries
|
||||
Shapely geometries can be easily converted to and from formats used by libraries like `GeoPandas` for geospatial analysis and `matplotlib` for plotting.
|
||||
|
||||
## Visualizing Geometries
|
||||
While Shapely itself does not provide visualization capabilities, geometries can be visualized using `matplotlib` or integrated with `GeoPandas` for advanced geospatial visualizations.
|
||||
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
from shapely.geometry import Point
|
||||
|
||||
point = Point(0.5, 0.5)
|
||||
x, y = point.x, point.y
|
||||
|
||||
plt.plot(x, y, marker='o', color='red')
|
||||
plt.show()
|
||||
```
|
||||
|
||||
`Shapely` is fundamental for geometric operations in Python, allowing for complex shape analysis and manipulation with straightforward syntax. This guide covers basic geometric creations and operations, but `Shapely` supports a rich set of functionalities that cater to advanced geospatial data manipulation needs.
|
||||
|
||||
|
||||
Shapely’s ability to accurately represent and manipulate spatial data makes it an invaluable tool in the field of geospatial analysis, urban planning, and GIS (Geographic Information Systems) development, facilitating sophisticated spatial operations and analyses.
|
||||
Reference in New Issue
Block a user