70
votes
Transforming Shapely Polygon and MultiPolygon objects
While shapely doesn't natively understand coordinate systems,
shapely.ops.transform() can do that along with pyproj. If pyproj.Proj can understand your both of your coordinate systems, then it can be ...
49
votes
Accepted
How to polygonize raster to shapely polygons
Use rasterio of Sean Gillies. It can be easily combined with Fiona (read and write shapefiles) and shapely of the same author.
In the script rasterio_polygonize.py
the beginning is
import rasterio
...
32
votes
Accepted
Welding individual line segments into one LineString using Shapely
You can use shapely's ops.linemerge to accomplish this:
from shapely import geometry, ops
# create three lines
line_a = geometry.LineString([[0,0], [1,1]])
line_b = geometry.LineString([[1,1], [1,0]]...
31
votes
Accepted
Check if a point falls within a multipolygon with Python
Shapefiles have no type MultiPolygon (type = Polygon), but they support them anyway (all rings are stored in one feature = list of polygons, look at Converting huge multipolygon to polygons)
The ...
30
votes
Accepted
Calculating area in km² for Polygon in WKT using Python
It wasn't readily apparent to me how to use @sgillies answer, so here is a more verbose version:
import pyproj
import shapely
import shapely.ops as ops
from shapely.geometry.polygon import Polygon
...
30
votes
Accepted
Getting polygon areas using GeoPandas
If the crs of the GeoDataFrame is known (EPSG:4326 unit=degree, here), you don't need Shapely, nor pyproj in your script because GeoPandas uses them).
import geopandas as gpd
test = gpd.read_file("...
29
votes
Returning lat and long of centroid point with GeoPandas
Ran into this problem myself. If you want the x and y as separate GeoDataFrame columns, then this works nicely:
gdf["x"] = gdf.centroid.map(lambda p: p.x)
gdf["y"] = gdf.centroid.map(lambda p: p.y)
...
25
votes
Reading postgis geometry with shapely
The default format for PostGIS geometry is hex-encoded WKB (Well-Known Binary). Shapely has the ability to convert this format into shapely geometry object with its wkb module:
from shapely import ...
25
votes
Transforming Shapely Polygon and MultiPolygon objects
If you're using pyproj2, it's much easier to use a Transformer. Here's an example:
import pyproj
from shapely.ops import transform
project = pyproj.Transformer.from_proj(
pyproj.Proj(init='epsg:...
24
votes
Accepted
Dissolving polygons based on attributes with Python (shapely, fiona)?
The question is about Fiona and Shapely and the other answer using GeoPandas requires to also know Pandas. Moreover GeoPandas uses Fiona to read/write shapefiles.
I do not question here the utility ...
23
votes
Accepted
Converting Polygon to MultiPolygon with Shapely?
You can wrap the polygon in a list and pass that as an argument to the MultiPolygon constructor.
Demo:
from shapely.geometry.multipolygon import MultiPolygon
from shapely import wkt
p = wkt.loads(u'...
21
votes
Accepted
What units are used by GeoPandas/Shapely area and distance functions?
Shapely uses a cartesian plane system for computing geometries (distance = euclidean distance)
Shapely does not support coordinate system transformations. All operations on two or more features ...
21
votes
Accepted
Returning percentage of area of polygon intersecting another polygon using shapely
Next code:
import fiona
from shapely.geometry import shape
path1 = '/home/zeito/pyqgis_data/polygon1.shp'
path2 = '/home/zeito/pyqgis_data/polygon8.shp'
polygon1 = fiona.open(path1)
polygon8 = ...
21
votes
Accepted
Looking for GeoPandas QGIS equivalent of ArcGIS "Select by location" tool
If poly is a GeoDataFrame with a single geometry, extract this:
polygon = poly.geometry[0]
Then, you can use the within method to check which points are within the polygon:
points.within(polygon)
...
20
votes
Calculating area in km² for Polygon in WKT using Python
It looks like your coordinates are longitude and latitude, yes? Use Shapely's shapely.ops.transform function to transform the polygon to projected equal area coordinates and then take the area.
...
19
votes
Accepted
Turning GeoDataFrame of x,y coordinates into Linestrings using GROUPBY?
I think I found an interim solution, which I'm posting in case it's useful for anyone:
import pandas as pd
import numpy as np
from geopandas import GeoDataFrame
from shapely.geometry import Point, ...
18
votes
Accepted
Intersect Shapefiles using Shapely
While I'm a big user of both shapely and fiona, I wouldn't go this approach. This is a task of writing an effective SQL statement.
Using ogr2ogr with an SQLITE dialect, you can process this from a ...
18
votes
Accepted
Calculate distance from a Point within a Polygon to its edge
Compare the point to the polygon's exterior ring:
poly.exterior.distance(point)
17
votes
Accepted
Returning lat and long of centroid point with GeoPandas
Leaving the rest below, but the main thing was accessing the geometry properly. If iterating over rows, e.g. for index, row in zones.iterrows(): you can simply use row.geometry.centroid.x and row....
17
votes
Make shapefile from raster-bounds in Python
With shapely box and GeoPandas
import rasterio as rio
ra = rio.open("raster.tif")
bounds = ra.bounds
Convert bounds to shapely geometry
from shapely.geometry import box
geom = box(*bounds)
print(...
16
votes
Accepted
Intersecting two shapefiles from Python or command line?
The question is about Shapely and Fiona in pure Python without QGIS ("using command line and/or shapely/fiona").
A solution is
from shapely import shape, mapping
import fiona
# schema of the new ...
16
votes
Accepted
Converting Shapely MultiPolygon to Polygon: Technique doesn't always work
You need to understand the Shapely binary predicates:
1) If the two polygons intersects the result of union or unary_union (in red) is a Polygon therefore you can computes the exterior
2) If the two ...
16
votes
Accepted
Rounding all coordinates in shapely?
Shapely and GEOS cannot reduce precision (floating-point precision problem) but you can use other functions as numpy.round() or round(),with the GeoJSON format.
For polygons
from shapely.geometry ...
16
votes
Rounding all coordinates in shapely?
There are a few instances where @gene's answer does not work.
For example, the using the overprecise value -73.92391000000001
geojson = {'type': 'Polygon', 'coordinates': [[[-73.92391, 41.31064], [-...
16
votes
Auto-select suitable UTM Zone based on Grid Intersection
There's the utm package, a bidirectional UTM-WGS84 converter for python.
It's able to convert a (latitude, longitude) tuple into an UTM coordinate:
import utm
utm.from_latlon(51.2, 7.5)
>>> (...
16
votes
Accepted
Removing small holes from the polygon
You can use the following example script. The script removes all holes from a polygon whose area is greater than eps.
from shapely import wkt
from shapely.geometry import Polygon
# sample polygon
...
15
votes
Accepted
Removing small polygon gaps in Shapely polygon?
The thing you are looking at is a sliver geometry. Similar to @sgillies's answer, except use a few buffer parameters to control the chiselled geometry shape:
import json
from shapely.geometry import ...
15
votes
Creating a circle with radius in metres
By design Shapely is unaware of coordinate reference systems or units. To use it to solve real world problems, you must learn to transform longitude and latitude to an approximately planar local ...
15
votes
Convert shapely polygon coordinates
To change projections with Fiona, use the pyproj module.
Example with a point shapefile (you can simplify the algorithm):
from pyproj import Proj, transform
import fiona
from fiona.crs import ...
15
votes
Accepted
Drawing ellipse with shapely
Answer
First, define a circle of radius 1.
Then define an ellipse along x and y axis.
Rotate the ellipse.
Finally, actually draw the ellipse.
Code
from matplotlib import pyplot
from shapely.geometry....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shapely × 807python × 581
geopandas × 213
fiona × 118
shapefile × 84
polygon × 61
intersection × 55
linestring × 53
geometry × 42
coordinate-system × 39
gdal × 31
postgis × 30
qgis × 29
geojson × 28
ogr × 28
python-2.7 × 24
pyproj × 24
pandas × 21
rasterio × 20
pyqgis × 18
geos × 18
point × 17
buffer × 16
distance × 15
coordinates × 14