TopologicalError: The operation GEOSIntersection_r could not be performed

2024/9/30 21:19:23

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assembly constituency level. So I am following a pycon talk. Everything is working fine but I am getting error in get_intersection function.Error for that is TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f460250ce10>.

I have tried to use both pygeos and rtree. There were some links which said that problem is in pygeos. So,I used rtree.But of no avail.Please help Thanks in advance.

Code tried by me is

constituencies=gpd.GeoDataFrame.from_file('/content/AC_All_Final.shp')
districts=gpd.GeoDataFrame.from_file('/content/2001_Dist.shp')
districts['AREA'] = districts.geometry.area
constituencies['AREA'] = constituencies.geometry.area
merged = gpd.sjoin(districts, constituencies).reset_index().rename(columns={'index': 'index_left'})def get_intersection(row):left_geom = districts['geometry'][row['index_left']]right_geom = constituencies['geometry'][row['index_right']]return left_geom.intersection(right_geom)***Error is at this point***
merged['geometry'] = merged.apply(get_intersection, axis=1)
merged['AREA'] = merged.geometry.area
Error trace is given below:
TopologyException: Input geom 1 is invalid: Ring Self-intersection at or near point 77.852561819157373 14.546596140487276 at 77.852561819157373 14.546596140487276
---------------------------------------------------------------------------
TopologicalError                          Traceback (most recent call last)
<ipython-input-17-8123669e025c> in <module>()4     return left_geom.intersection(right_geom)5 
----> 6 merged['geometry'] = merged.apply(get_intersection, axis=1)7 merged['AREA'] = merged.geometry.area7 frames
/usr/local/lib/python3.6/dist-packages/shapely/topology.py in _check_topology(self, err, *geoms)36                     "The operation '%s' could not be performed. "37                     "Likely cause is invalidity of the geometry %s" % (
---> 38                         self.fn.__name__, repr(geom)))39         raise err40 TopologicalError: The operation 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0x7f460250ce10>
Answer

The error message tells you exactly what is going on. Some of your geometries are not valid, so you have to make them valid before doing your apply. The simple trick, which works in most of the cases is using buffer(0).

merged['geometry'] = merged.buffer(0)

Since the issue is with geometry validity and is raised by GEOS, it does not matter if you use shapely/rtree backend or pygeos.

https://en.xdnf.cn/q/71037.html

Related Q&A

Plot a function during debugging in Python

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function. In Python I can…

getting last n items from queue

everything I see is about lists but this is about events = queue.queue() which is a queue with objects that I want to extract, but how would I go about getting the last N elements from that queue?

Paramiko ValueError p must be exactly 1024, 2048, or 3072 bits long

I am trying to connect SFTP using Python script. Im unable to connect due to "p error".import paramiko client = paramiko.SSHClient() client.load_system_host_keys() client.connect(####.com, us…

WindowsError: [Error 5] Access is denied using urllib2

Im getting a "WindowsError: [Error 5] Access is denied" message when reading a website with urllib2. from urllib2 import urlopen, Request from bs4 import BeautifulSouphdr = {User-Agent: Mozil…

Send key combination with python

I want to be able to send the key combination SHIFT + CTRL + . (dot) using the following code:import win32com.client as comclt wsh= comclt.Dispatch("WScript.Shell") wsh.SendKeys() So far I wa…

Taking data from drop-down menu using flask

Im completely new to flask, and really am completely lost with how to approach this. Ive looked into other SO questions but I cant seem to get this working regardless. I have a form as such: <form…

How to get list_blobs to behave like gsutil

I would like to only get the first level of a fake folder structure on GCS.If I run e.g.:gsutil ls gs://gcp-public-data-sentinel-2/tiles/I get a list like this:gs://gcp-public-data-sentinel-2/tiles/01/…

How to avoid gcc warning in Python C extension when using Py_BEGIN_ALLOW_THREADS

The simplest way to manipulate the GIL in Python C extensions is to use the macros provided:my_awesome_C_function() {blah;Py_BEGIN_ALLOW_THREADS// do stuff that doesnt need the GILif (should_i_call_ba…

Pairwise operations (distance) on two lists in numpy

I have two lists of coordinates:l1 = [[x,y,z],[x,y,z],[x,y,z],[x,y,z],[x,y,z]] l2 = [[x,y,z],[x,y,z],[x,y,z]]I want to find the shortest pairwise distance between l1 and l2. Distance between two coordi…

Bad results when undistorting points using OpenCV in Python

Im having trouble undistorting points on an image taken with a calibrated camera using the Python bindings for OpenCV. The undistorted points have entirely different coordinates than the original point…