3

-This is a Python in general question.

I am having topology errors very often, they are very small and i would like to proceed to intersection even though they are there. Is it possible to make an exception for this particular :

TopologyException: found non-noded intersection between LINESTRING (229971 4.39944e+06, 229971 4.39944e+06) and LINESTRING (229971 4.39944e+06, 229971 4.39944e+06) at 229971.08857010119 4399438.0928708706 and make it complete the process anyway? I am using Python and Geopandas.

Also sometimes in intersection it says:

 TopologicalError: This operation could not be performed. Reason: unknown

is it possible for any line like this:inte_s=gpd.overlay(data2,asttom,how='intersection') that is causing this Error to create an exception and make it perform the calculation by being unfazed from the error that would show up? What code would do that in this line where the intersection is done?

a9e1
  • 83
  • 6

3 Answers3

2

Yes, you can skip or handle any exception in python using try/except blocks. In your case it would be:

try:
    inte_s=gpd.overlay(data2,asttom,how='intersection')
except (TopologicalError, TopologyException):
    #skipping error here
    pass
running.t
  • 5,329
  • 3
  • 32
  • 50
  • if you need something not specific and simply any error you should use:`except: pass` only? – a9e1 Jul 03 '18 at 06:09
  • I tried what you wrote and it still produced the error with the message and everything.What is going on? – a9e1 Jul 03 '18 at 06:55
  • @a9e1: For handling/skipping exception of (almost) any type you can use `try: ... except: pass` – running.t Jul 03 '18 at 08:15
  • Probably exception is raised from some other part of code, not the one you put into try/except clause. Please try reading exception trace to find out what part of code produces an exception. – running.t Jul 03 '18 at 08:16
  • I did, it raises the TopologyException that was filtered.Maybe it raised it but was not affected by it?Maybe. – a9e1 Jul 03 '18 at 10:14
  • It doesn't work : NameError: global name 'TopologyException' is not defined. Where are defined these exceptions in shapely ? – Eric H. Jan 31 '19 at 06:54
  • @EricH. I have no idea, This is not the scope of this question (because it's obvious you have to import names you are using) and I don't even know the library you both are using. Have you considered looking at documentation? – running.t Jan 31 '19 at 09:32
  • In fact, from https://discuss.python.org/t/shapely-intersection-topologyexception/8668 , TopologyException is part of the message. TopologicalError is enough : from shapely.errors import TopologicalError – Eric H. Apr 07 '22 at 07:27
2

TopologicalError needs to imported first, then use a try...except statement.

from shapely.geos import TopologicalError

try:
    # your code that may raise the exception
except TopologicalError:
    # alternative code if the exception is raised


Istopopoki
  • 1,584
  • 1
  • 13
  • 20
1

One way you can handle errors in Python is using a try and except statement.

Here is a sample piece of code that I use often when testing. You can place the operation that might fail in the try part of the block, and even if it does throw an exception, your whole script will not be broken.

try:
    #operation that may fail
except Exception as e:
    print('something went wrong: ' + e)
    # what you want to do if the operation does fail

The above code is a general form for a try/except statement. If you are looking to catch a specific error, you're looking for something like this SO answer.

BenG
  • 304
  • 1
  • 11