Questions tagged [shapely]

Shapely is an open source Python package for construction and analysis of 2D planar geometric objects common to geographic information systems.

Filter by
Sorted by
Tagged with
44 votes
3 answers
40k views

Transforming Shapely Polygon and MultiPolygon objects

Is there an easy way of transforming Shapely objects (namely, Polygons and MultiPolygons) from one projection to another without having to dig around and extract coordinates by hand? In fact, I don'...
user avatar
40 votes
5 answers
68k views

How to write Shapely geometries to shapefiles?

Can someone demonstrate a simple way to write geometry data-structures from shapely into shapefiles? I am particularly interested in polygons with holes and linestrings. It would also be beneficial to ...
user avatar
32 votes
4 answers
68k views

Shapely not installing correctly

I am trying to run Shapely on a new machine, and tried both installing it from source, using the installer on its webpage, and using pip install shapely. Every reference I try to make from the library ...
user avatar
  • 1,368
31 votes
4 answers
37k views

Getting polygon areas using GeoPandas

Given a GeoPandas's GeoDataFrame containing a series of polygons, I would like to get the area in km sq of each feature in my list. This is a pretty common problem, and the usual suggested solution in ...
user avatar
30 votes
2 answers
7k views

Using OGR and Shapely more efficiently? [closed]

I'm looking for some suggestions about how to make my python code more efficient. Normally efficiency doesn't matter for me but I am now working with a text file of US locations with over 1.5 million ...
user avatar
  • 309
29 votes
3 answers
34k views

How to polygonize raster to shapely polygons

I am seeking an open-source python solution to convert raster to polygon (no ArcPy). I did know the GDAL function to convert raster to polygon, and here is the manual: http://pcjericks.github.io/py-...
user avatar
29 votes
8 answers
56k views

Returning lat and long of centroid point with GeoPandas

I would like to return the lat, lon or x, y from the point object in the following series as I plan to link them with an API I built that links OSM and NetworkX. The centroids will be added as new ...
user avatar
  • 8,442
28 votes
8 answers
28k views

Calculating area in km² for Polygon in WKT using Python

I have polygons in WKT that looks like this POLYGON ((24.8085317 46.8512821, 24.7986952 46.8574619, 24.8088238 46.8664741, 24.8155239 46.8576335, 24.8085317 46.8512821)) I would like to calculate the ...
user avatar
  • 441
22 votes
3 answers
20k views

What units are used by GeoPandas/Shapely area and distance functions?

it looks like a silly question, yet I can't find clear answer on that: what units geopandas/shapely use calculating distance/area between objects?
user avatar
  • 1,664
21 votes
4 answers
24k views

Welding individual line segments into one LineString using Shapely [closed]

I'm using Shapely in python and I'm given a MultiLineString with a bunch of Linestring objects. I can guarantee that all the LineString objects are simple lines with only 2 vertices and that they are ...
user avatar
  • 543
21 votes
3 answers
79k views

Check if a point falls within a multipolygon with Python

I have tried several examples of code using libraries such as shapefile, fiona, and ogr to attempt to check whether a point (x, y) falls within the boundaries of a multipolygon created with ArcMap (...
user avatar
  • 533
19 votes
4 answers
27k views

What is unit of Shapely length attribute?

I'm doing a very simple calculation of the length of a Polyline using shapely: from shapely.geometry import LineString ... xy_list = [map(float, e) for e in xy_intm] line = LineString(xy_list) s = '%...
user avatar
  • 2,436
19 votes
3 answers
16k views

Dissolving polygons based on attributes with Python (shapely, fiona)?

I have been trying to create a function that does basically the same thing that the QGIS "dissolve" function. I thought it would be super easy but well apparently not. So from what I have gathered ...
user avatar
19 votes
1 answer
10k views

Finding Nearest Line Segments to Point using shapely?

Background From a known Point, I require to establish the nearest surrounding "visible perimeter" against a table of MultiLineStrings, as shown on the diagram. I've searched this site with a number ...
user avatar
19 votes
1 answer
2k views

Calculating Focal Statistics for Special Neighborhood?

I'm looking to calculate focal statistics for each cell of a raster, within a neighborhood of a specified criteria. Background - I have three binary rasters, each representing a single vegetation ...
user avatar
  • 1,274
16 votes
1 answer
3k views

Why is Shapely's snapping (GEO snaps) not working as expected?

I'm trying to snap two lines to each other using Shapely/Geopandas but the result of the snapping is very odd. I tried : import geopandas as gpd from shapely.geometry import * from shapely.ops ...
user avatar
  • 615
15 votes
4 answers
6k views

Make shapefile from raster-bounds in Python

I have a raster that I opened with rasterio. When I do raster.bounds I get BoundingBox(left=399960.0, bottom=-3309780.0, right=609780.0, top=-3099960.0). My question is now, if there is any simple ...
user avatar
15 votes
3 answers
16k views

Understanding use of spatial indexes with RTree?

I'm having trouble understanding the use of spatial indexes with RTree. Example: I have 300 buffered points, and I need to know each buffer's intersection area with a polygon shapefile. The polygon ...
user avatar
  • 1,274
15 votes
2 answers
11k views

Polygon overlay with Shapely

I'm trying to capture all the non-overlapping polygons indicated below using Shapely (given polygons A,B & C). Moreover, I'm hoping to do so without iteration, testing for intersect etc. The ...
user avatar
  • 2,000
15 votes
2 answers
15k views

Geopandas Line Polygon Intersection

I'm trying to find where multiple lines intersect a polygon for two different geodataframes: from shapely.geometry import Polygon, LineString import geopandas as gpd polygon = Polygon([(0, 0), (1, 0)...
user avatar
  • 415
14 votes
1 answer
10k views

Returning percentage of area of polygon intersecting another polygon using shapely [closed]

Using the shapely library, suppose I have a shp file V and another shp file Z. For each polygon v in V, and for each polygon z in Z, I would like to know what percentage of the area of z falls within (...
user avatar
14 votes
2 answers
45k views

Creating Shapely LineString from two Points

If have two points, from which I want to create a straight LineString object: from shapely.geometry import Point, LineString A = Point(0,0) B = Point(1,1) The Shapely manual for LineString states: A ...
user avatar
  • 311
13 votes
2 answers
16k views

Reading postgis geometry with shapely

I am doing a small-scale workflow in ipython notebook with geopandas and shapely, pulling a bunch of geospatial data, sometimes from shapefiles, sometimes from postgis (where more expensive processing ...
user avatar
  • 1,857
13 votes
5 answers
35k views

GEOS and Shapely installation on Windows

When trying to install Shapely on my Windows 64bit computer, I cannot get the GEOS library to work. So far, I have run the OSGeo4W installer from which I installed GDAL (I believe the geos library ...
user avatar
13 votes
2 answers
21k views

Creating Shapely MultiPolygons from shapefile MultiPolygons

I'm reading a shapefile using Fiona: with fiona.open('data/boroughs/boroughs_n.shp') as source: mpolys = [p for p in source] candidate = polys[0]['geometry'] this gives me a dict containing a ...
user avatar
  • 422
13 votes
1 answer
14k views

Calculate distance from a Point within a Polygon to its edge

Let's say we have the following point and polygon: poly = Polygon([(0, 0), (2,8), (14, 10), (6,1)]) point = Point(4,4) Because point lies within poly, the distance from the first to the latter will ...
user avatar
  • 545
13 votes
5 answers
9k views

Python, shapely library: is it possible to do an affine operation on shape polygon?

I am trying to create a rotated rectangle in lat/lon coordinates. Basically, I have the coordinates of the center and rotation angle around the center. Is it possible to do using, shapely or gdal, ...
user avatar
  • 233
13 votes
1 answer
2k views

Counting features into intersections of Shapely Polygons

I have a geopandas GeoDataFrame containing hundreds of shapely Polygon and MultiPolygon geometries. The polygons overlap in lots of places. I'd like to make a new geometry that contains a count of how ...
user avatar
  • 231
12 votes
3 answers
8k views

Auto-select suitable UTM Zone based on Grid Intersection

I was wondering if the following approach a) would be considered valid and b) someone already did this in python. I have a python script where users can choose an area of analysis (anywhere on the ...
user avatar
  • 634
12 votes
1 answer
17k views

Converting Polygon to MultiPolygon with Shapely?

I'm writing code that loads data into a database. I have to do some validation/clean up of the data before inserting it. One of the requirements of the database is that this particular table only ...
user avatar
  • 1,679
12 votes
4 answers
17k views

using shapely: translating between Polygons and MultiPolygons

[EDIT: the solution to this was simply to use OGR to read shapefiles. See geographika's example.] In an ESRI shapefile, there is no distinction between Polygons and MultiPolygons. Furthermore, there ...
user avatar
12 votes
2 answers
18k views

Generating polygon representing rough 100km circle around latitude/longitude point using Python?

Given a latitude/longitude point anywhere on earth, I would like to assemble a polygon that roughly resembles a circle with a 100km radius around that point. It doesn't have to be pin-point accurate, ...
user avatar
12 votes
3 answers
17k views

Getting intersection of multiple polygons efficiently in Python

I would like to get the intersection of multiple polygons. Using Python's shapely package, I can find the intersection of two polygons using the intersection function. Is there a similar efficient ...
user avatar
  • 347
12 votes
1 answer
3k views

When validating geometries, what are "Nested shells"?

I am validating shapefiles polygon geometry using shapely, and have also attempted it using FME. Some polygons return the error "Nested Shells". I cannot find a definition of what the problem is, ...
user avatar
  • 1,151
12 votes
2 answers
4k views

Shapely with Rtree versus STRtree

I have a huge interrogation regarding the performance difference between Rtree and STRtree included in Shapely. Below is a short code where we create randomly 250 000 triangles (defined by 4 vertice) ...
user avatar
  • 583
12 votes
0 answers
2k views

Making polygon for every point in set using Voronoi diagram

I'm trying to make polygons from points, where each point will be represented by one polygon. I tried using Voronois, but the results are incomplete. While I more or less understand where this ...
user avatar
  • 579
11 votes
2 answers
14k views

Count the number of points in a (multi)polygon in shapely?

I'm using Python, Shapely and Fiona to process some shapefiles. Is there an easy way to count the number of points in a polygon/multipolygon in Shapely and Fiona?
user avatar
11 votes
2 answers
32k views

Converting Shapely MultiPolygon to Polygon: Technique doesn't always work

In order to get the exterior coordinates I need to convert a shapely MultiPolygon to a Polygon. I do it like this: if poly.geometry.type == 'Polygon': x, y = poly.geometry.exterior.xy elif poly....
user avatar
11 votes
1 answer
19k views

Split lines at points using Shapely

I'm trying to split a shapefile of geometry type 'line' at a shapefile of geometry type 'point. These points are snapped to the lines and are the start points/end points of other line shapefiles. ...
user avatar
  • 615
11 votes
2 answers
25k views

converting a list of shapely geometry to numpy array [closed]

How can I convert a shapely point geometry like following to numpy array? [<shapely.geometry.point.Point object at 0x000001CEF6978DA0>, <shapely.geometry.point.Point object at ...
user avatar
  • 571
11 votes
2 answers
4k views

Bowtie or hourglass polygon validity issue when self-crossing point is not defined

Using Python2.7 and shapely, let: import shapely coords = [(0, 0), (0, 2), (2, 0), (2, 2), (0, 0)] bowtie = shapely.geometry.Polygon(coords) bowtie.is_valid which gives this : Self-intersection ...
user avatar
  • 6,788
11 votes
1 answer
2k views

How to split two interscting polygons by the average linestring of the shared area (QGIS / python shapely)

I have two polygons, POLYGON((-5 -5,-5 5,5 5,5 -5,-5 -5)) POLYGON((1 1, 1 7, 7 7, 7 1, 1 1)) and wish to split them evenly. The result should be a split along the red marked line. I'm using ...
user avatar
  • 197
11 votes
1 answer
1k views

Python: Break linestring based on condition

I have a geopandas dataframe of a bunch of linestrings that have some data associated with each vertex/point: Point_x = (Lat, Lon, Time, ID, Data1, Data2, Data3) The points are converted to ...
user avatar
  • 1,211
10 votes
5 answers
12k views

Rounding all coordinates in shapely?

Following on from How can I add points to a LineString in shapely?, I'm hitting a problem where floating point representations mean objects aren't precisely where I expect them be after they are moved ...
user avatar
10 votes
2 answers
3k views

Invalid multipolygon of valid individual polygons

Could anyone please explain this to me? The polygons all have valid geometry but the resulting multipolygon somehow doesn't. The following script: from shapely.geometry import Polygon, MultiPolygon ...
user avatar
  • 3,302
10 votes
2 answers
8k views

Removing small polygon gaps in Shapely polygon?

I know this issue has been addressed before with different software, but how do I do it with Shapely? I have a polygon with a very small gap on the interior of the shape: Here is a close up: The ...
user avatar
  • 1,193
10 votes
2 answers
20k views

Creating a circle with radius in metres

I am using shapely and Python. How should I go about creating a circle using shapely whose radius is in meters? If i use sampleCircle = Point(1,1).buffer(1) print sampleCircle.area This returns ...
user avatar
10 votes
2 answers
10k views

Drawing ellipse with shapely

How do I draw an ellipse with shapely?
user avatar
  • 6,788
10 votes
1 answer
6k views

Snap points shapefile to line shapefile using shapely

I have 2 datasets: 1) a road network shapefile which has many polylines and 2) a bus stop shapefile which has many points representing bus stops. My points shapefile has CRS of EPSG 4326 and my line ...
user avatar
10 votes
2 answers
11k views

Shapely - convert polygons to lines?

Given a shapefile with polygons, how do I convert the polygons to individual lines instead? I know how to do this in QGIS, but I'm looking for a Shapely function which does the same thing.
user avatar

1
2 3 4 5
17