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
32
votes
5answers
50k 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 ...
31
votes
3answers
23k 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'...
30
votes
2answers
6k 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 ...
28
votes
4answers
59k 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 ...
20
votes
2answers
19k 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-...
20
votes
2answers
20k views

Getting polygon areas using geopandas?

Given a geopandas 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 ...
18
votes
3answers
13k 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?
18
votes
1answer
8k 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 ...
18
votes
1answer
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 ...
17
votes
5answers
17k views

Area in KM from Polygon of coordinates

I have polygons from coordinates in (python shapely) 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)) ...
16
votes
3answers
12k 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 ...
15
votes
3answers
55k 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 (...
15
votes
3answers
12k 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 ...
15
votes
4answers
13k views

Weld individual line segments into one LineString using Shapely

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 ...
15
votes
2answers
8k 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 ...
14
votes
1answer
1k 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 ...
13
votes
6answers
29k views

GeoPandas: return lat and long of a centroid point

I would like to return the lat and lon or x and 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 ...
13
votes
1answer
1k 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 ...
12
votes
5answers
30k 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 ...
12
votes
2answers
11k 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 ...
12
votes
2answers
18k 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 = '%...
12
votes
2answers
17k 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 ...
12
votes
5answers
5k 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, ...
12
votes
4answers
16k 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 ...
12
votes
3answers
12k 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 ...
12
votes
1answer
9k 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)...
11
votes
2answers
26k 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 ...
11
votes
1answer
813 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 ...
10
votes
1answer
10k 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 ...
10
votes
1answer
8k 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 ...
10
votes
1answer
20k 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....
10
votes
1answer
5k 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 (...
10
votes
2answers
3k 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 ...
10
votes
3answers
3k views

Polygons across international dateline [-180..+180 longitude]

I'm trying to generate polygons for satellite orbital swaths. So far I have a method to generate two lines which represent the edge of each swath in [lat,long]. Some of the swath's cross the ...
10
votes
1answer
1k 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 ...
9
votes
1answer
14k views

Turning GeoDataFrame of x,y coordinates into Linestrings using GROUPBY?

I have a dataframe of X,Y coordinates that represent points along the paths taken by several different entities. Pseudo-data here, but it is roughly of the form: entity_id lat lon time 1001 ...
9
votes
3answers
6k 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 ...
9
votes
3answers
4k views

RTree spatial index does not result in faster intersection computation

I have some code that I am using to determine which Shapely Polygon/MultiPolygons intersect with a number of Shapely LineStrings. Through the answers to this question the code has gone from this: ...
9
votes
3answers
439 views

“Greedy” clipping lines with polygon

I am wishing to clip a set of polylines (black lines in image below) to the outer boundary of a polygon. Any voids within the polygon should be ignored. My ideal output are the dashed yellow lines. ...
9
votes
1answer
11k views

Line vs. Polygon Intersection Coordinates

I am working with Python, Shapely and Fiona... Is there any way where I can give in an input of a line shapefile and a polygon shapefile, and get a result of the intersection points and their ...
8
votes
2answers
8k views

Maximizing Code Performance for Shapely

I've written this code in order to calculate the percent cover of grassland within a national park, within 20km^2 of points of recorded occurrences for a species. It is designed to only consider the ...
8
votes
2answers
8k views

Does shapely within function identify inner holes?

I have a series of shapefiles that include both polygons and multipolygons. I am using ogr to extract them in wkb, and them I am using shapely to check if a point fall inside a polygon (or a ...
8
votes
2answers
6k views

Shapely Distance different from Geopy/Haversine

I am running a specific analysis where we use shapely to create buffers around points (store locations) and then check if other points (user locations) are present within that buffer value. When i ...
8
votes
1answer
6k views

Shapely polygon union results in strange artifacts of tiny, non-overlapping areas

I am not sure if this is a shapely bug or a problem with my polygons. I have a bunch of polygons that look like this: They are level-12 watersheds provided by HydroBasins, and are presumably non-...
8
votes
2answers
6k views

Drawing ellipse with shapely?

How do I draw an ellipse with shapely?
8
votes
2answers
10k views

Converting KML files for use with Python library Shapely

I am trying to get a KML-file (wijken.kml) into Shapely. The KML-file is validated against the proper XML schema, so I guess the input is correct. Routes I have tried: 1) Converting to WKT or WKB ...
8
votes
1answer
6k views

Buffer to linestring in shapely

I have a linestring in shapely which I'm converting into buffer. >>> from shapely.geometry import LineString >>> line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)]) &...
8
votes
1answer
2k 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, ...
8
votes
0answers
569 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) ...
7
votes
3answers
666 views

Getting geometries of hurricane's 'cone of uncertainty' using shapely?

I'm trying to plot hurricane forecast maps using python. I have several forecast positions derived from official advisories, and interpolate them into a smoothed curve, then draw a polygon of 'cone of ...

1
2 3 4 5
13