Ora

What is shapely Python?

Published in Python Geometry Library 4 mins read

What is Shapely Python?

Shapely is a powerful Python library designed specifically for working with geometric objects in a planar coordinate system. It provides an intuitive and robust way to create, manipulate, and analyze basic geometric shapes such as points, lines, and polygons, making it an essential tool for spatial analysis and geographic information system (GIS) applications.


Core Functionality of Shapely

At its heart, Shapely allows developers to perform various operations and predicates on geometric data. You can easily define and manage complex spatial relationships through its API.

1. Creating Geometric Objects

Shapely simplifies the instantiation of fundamental geometric primitives.

  • Points: Represent a single location in space.
  • LineStrings: Represent a sequence of connected points, forming a line.
  • Polygons: Represent a region of space, defined by an exterior boundary and potentially multiple interior holes.
from shapely.geometry import Point, LineString, Polygon

# Create a Point
point = Point(1, 2)
print(f"Point: {point}")

# Create a LineString
line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1)])
print(f"LineString: {line}")

# Create a Polygon
polygon = Polygon([(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)])
print(f"Polygon: {polygon}")

2. Geometric Predicates (Relationships)

One of Shapely's key strengths lies in its ability to determine relationships between different geometries. We can check the relationships between two geometries, such as:

  • intersects(): Do the geometries share any common points?
  • contains(): Does one geometry completely contain another?
  • within(): Is one geometry entirely within another?
  • touches(): Do the geometries touch at their boundaries but not overlap?
  • crosses(): Do the interiors of the geometries intersect?
  • overlaps(): Do the geometries partially overlap?
  • equals(): Are the geometries topologically equal?
  • disjoint(): Do the geometries have no points in common?

Example:

from shapely.geometry import Point, Polygon

point_a = Point(1, 1)
point_b = Point(3, 3)
polygon_test = Polygon([(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)])

print(f"Is point_a within polygon_test? {point_a.within(polygon_test)}") # Output: True
print(f"Is point_b within polygon_test? {point_b.within(polygon_test)}") # Output: False
print(f"Does point_a intersect polygon_test? {point_a.intersects(polygon_test)}") # Output: True

3. Geometric Operations

Shapely offers a wide array of operations to modify or combine geometries:

  • union(): Combines two or more geometries into a single geometry.
  • intersection(): Returns the common part of two geometries.
  • difference(): Returns the part of one geometry that is not shared with another.
  • buffer(): Creates a polygon representing the area within a specified distance from a geometry.
  • centroid: Calculates the geometric center of a geometry.
  • convex_hull: Returns the smallest convex polygon that encloses all points in a geometry.
  • simplify(): Reduces the number of vertices in a geometry while maintaining its general shape.

4. Measurements and Properties

You can also retrieve various properties and measurements from geometric objects:

  • area: For polygons, calculates the area.
  • length: For LineStrings, calculates the length. For Polygons, calculates the perimeter.
  • distance(): Calculates the shortest distance between two geometries.
  • bounds: Returns the minimum bounding box (min_x, min_y, max_x, max_y).

Why Use Shapely?

Shapely is fundamental for tasks requiring precise handling of spatial data without direct interaction with a spatial database.

Feature Area Description
Spatial Analysis Performing proximity analysis, overlay operations, and determining spatial relationships.
Data Cleaning Validating geometries, fixing topological errors, and simplifying complex shapes.
GIS Applications Building custom mapping tools, processing geographic data, and integrating with other spatial libraries.
Data Visualization Preparing geometric data for plotting with libraries like Matplotlib or Folium.

Integration with Other Libraries

Shapely often serves as a foundational component within a larger Python spatial data ecosystem. It is extensively used by libraries like GeoPandas, which extends Pandas DataFrames to include a geometry column, enabling powerful spatial analysis on tabular data. It also works well with Fiona, a library for reading and writing geographic data formats.

For more in-depth information and detailed examples, refer to the official Shapely documentation.