Search code examples
pythonloopstry-catchexcept

Python- Skip the line with error and process the next line


I have below code and I am trying to reverse geocode a file using overpy. While trying to do that I am facing couple of issues

1) To avoid Overpytoomanyrequests error I am using Try except, and each time there is a error the while loop starts from first, is there way to let the while loop resume from the line there is a error instead of starting from first line? 2) Also there are few data points which have lat values beyond 90, can we write a code to skip these lines with inconsistent data and process next line? I tried pass, continue and next all of these end up in infinite loops

import overpy
import pandas as pd
import numpy as np
import time
import sys

df = pd.read_csv("/home/runner/sample_v1.csv")
df.sort_values(by=['cvdt35_timestamp_s'],inplace=True)

api= overpy.Overpass()
box = 0.0005
queries = []
names = []
highways =[]
results = []
df['Name']=''
df['Highway'] =''

while True :
  try :
    for row in df.index:                                                                                             
        query = ('way["highway"~"."](around:15,{},{});(._;<;); out body;').format(df.gps_lat_dd.iloc[row], df.gps_lon_dd.iloc[row])
        queries.append(query)

    for i in range(len(queries)):
        query = queries[i]
        print(str(i)+', '+query)
        result = api.query(query)
        results.append(result)

    for result in results:
        n1 = []
        h1 = []
        for way in result.ways:
            name = way.tags.get("name", "n/a")
            n1.append(name)
            highway = way.tags.get("highway", "n/a")
            h1.append(highway)
        names.append(','.join(n1))
        highways.append(','.join(h1))
  except overpy.exception.OverpassTooManyRequests:
      time.sleep(10*6)
  except overpy.exception.OverpassBadRequest:
    next
    break

df['Name']=names
df['Highway']=highways


sample data


RecordID    GPS_Latitude_Min_dec    gps_lat_dd  gps_lon_dd
947 1.6383  167.077305  333.077305
948 1.6383  167.077305  333.077305
881 0.4332  42.30722    -83.18112667
882 0.4333  42.30722167 -83.18112667
883 0.433   42.30721667 -83.181125
884 0.433   42.30721667 -83.181125

The code should ignore first two lines and continue processing from 3rd. Thanks for your help in advance

edited as below


while True :
  try :
      for row in df.index:                                                                                            
          query = ('way["highway"~"."](around:15,{},{});(._;<;); out body;').format(df.gps_lat_dd.iloc[row], df.gps_lon_dd.iloc[row])
          queries.append(query)
      for i in range(len(queries)):
        query = queries[i]
        print(str(i)+', '+query)
        result = api.query(query)
        results.append(result)
  except overpy.exception.OverpassTooManyRequests:
    time.sleep(10*6)
    False
  except overpy.exception.OverpassBadRequest:
    False
    continue

    for result in results:
        n1 = []
        h1 = []
        for way in result.ways:
            name = way.tags.get("name", "n/a")
            n1.append(name)
            highway = way.tags.get("highway", "n/a")
            h1.append(highway)
            names.append(','.join(n1))
            highways.append(','.join(h1))

But still endless loops


Solution

  • You can capture your try/except block in the only place where it can actually hit an exception (where you use an api query).

    You break out of the while loop if you are done with that part and then only append real results.

    import overpy
    import pandas as pd
    import numpy as np
    import time
    import sys
    
    df = pd.read_csv("/home/runner/sample_v1.csv")
    df.sort_values(by=['cvdt35_timestamp_s'],inplace=True)
    
    api= overpy.Overpass()
    box = 0.0005
    queries = []
    names = []
    highways =[]
    results = []
    df['Name']=''
    df['Highway'] =''
    
    for row in df.index:                                                                                             
        query = ('way["highway"~"."](around:15,{},{});(._;<;); out body;').format(df.gps_lat_dd.iloc[row], df.gps_lon_dd.iloc[row])
        queries.append(query)
    
    for i in range(len(queries)):
        result = 0
        query = queries[i]
        print(str(i)+', '+query)
        while True:
            try:
                result = api.query(query)
                break
            except overpy.exception.OverpassTooManyRequests:
                time.sleep(10*6)
            except overpy.exception.OverpassBadRequest:
                break
        if result:
            results.append(result)
    
    for result in results:
        n1 = []
        h1 = []
        for way in result.ways:
            name = way.tags.get("name", "n/a")
            n1.append(name)
            highway = way.tags.get("highway", "n/a")
            h1.append(highway)
        names.append(','.join(n1))
        highways.append(','.join(h1))
    
    df['Name']=names
    df['Highway']=highways