Skip to content

Commit

Permalink
Cleanup 1
Browse files Browse the repository at this point in the history
Increasing specificity for prints; removing unnecessary prints; renaming certain variables so they're more meaningful.
  • Loading branch information
nataliejschultz committed Jun 17, 2024
1 parent 06e0a40 commit 5139a39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
19 changes: 9 additions & 10 deletions emission/individual_tests/TestOverpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@

class OverpassTest(unittest.TestCase):
def setUp(self):
loglevel = logging.DEBUG
logging.basicConfig(level=loglevel)
# Un-comment the two lines below to print debug logs.
# loglevel = logging.DEBUG
# logging.basicConfig(level=loglevel)
sample_data = '[out:json][bbox];way[amenity=parking];out;&bbox=-122.1111238,37.4142118,-122.1055791,37.4187945'
call_base = 'api/interpreter?data='
self.de_url_base = 'https://lz4.overpass-api.de/'+ call_base + sample_data
self.public_url_base = 'https://lz4.overpass-api.de/'+ call_base + sample_data
self.gfbk_url_base = 'https://overpass.geofabrik.de/' + OVERPASS_KEY + '/' + call_base + sample_data

def test_overpass(self):
r_gfbk = requests.get(self.gfbk_url_base)
r_de = requests.get(self.de_url_base)
r_public = requests.get(self.public_url_base)

if r_gfbk.status_code == 200 and r_de.status_code == 200:
if r_gfbk.status_code == 200 and r_public.status_code == 200:
print("requests successful!")
r_gfbk_len, r_de_len = len(r_gfbk.json()), len(r_de.json())
self.assertEqual(r_gfbk_len, r_de_len)
r_gfbk_len, r_public_len = len(r_gfbk.json()), len(r_public.json())
self.assertEqual(r_gfbk_len, r_public_len)
else:
print("status_gfbk", r_gfbk.status_code, type(r_gfbk.status_code), "status_de", r_de.status_code)
print("status_gfbk", r_gfbk.status_code, type(r_gfbk.status_code), "status_public", r_public.status_code)

#Test utilizes the functions get_stops_near, get_public_transit_stops, and make_request_and_catch.
def test_get_stops_near(self):
actual_result = enetm.get_stops_near(loc1, 150.0)[0]['routes'][0]['tags']
print("ACTUAL_RESULT:", actual_result, type(actual_result))
expected_result = {'from': 'National Renewable Energy Lab', 'name': 'RTD Route 125: Red Rocks College', 'network': 'RTD', 'network:wikidata': 'Q7309183', 'network:wikipedia': 'en:Regional Transportation District', 'operator': 'Regional Transportation District', 'public_transport:version': '1', 'ref': '125', 'route': 'bus', 'to': 'Red Rocks College', 'type': 'route'}
self.assertEqual(expected_result, actual_result)

Expand All @@ -54,7 +54,6 @@ def test_get_predicted_transit_mode(self):
stop1 = enetm.get_stops_near(loc2, 400.0)
stop2 = enetm.get_stops_near(loc3, 400.0)
actual_result = enetm.get_predicted_transit_mode(stop1, stop2)
print("ACTUAL TRANSIT MODE: ", actual_result)
expected_result = ['train', 'train']
self.assertEqual(actual_result, expected_result)

Expand Down
9 changes: 1 addition & 8 deletions emission/net/ext_service/transit_matching/match_stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
url = 'https://overpass.geofabrik.de/' + OVERPASS_KEY + '/'
print("overpass configured")
except:
print("overpass not configured, falling back to default overleaf.de")
print("overpass not configured, falling back to public overpass api")
url = "https://lz4.overpass-api.de/"

try:
Expand All @@ -27,8 +27,6 @@
def make_request_and_catch(overpass_query):
try:
response = requests.post(url + "api/interpreter", data=overpass_query)
print("*********RESPONSE:" , response.request.url)
("*********RESPONSE header:" , response.request.headers)

except requests.exceptions.ChunkedEncodingError as e:
logging.info("ChunkedEncodingError while creating request %s" % (e))
Expand Down Expand Up @@ -75,7 +73,6 @@ def get_public_transit_stops(min_lat, min_lon, max_lat, max_lon):
logging.debug("bbox_string = %s" % bbox_string)
overpass_public_transit_query_template = query_string
overpass_query = overpass_public_transit_query_template.format(bbox=bbox_string)
print("*************QUERY:", overpass_query)
call_return = RETRY
retry_count = 0
while call_return == RETRY:
Expand All @@ -89,7 +86,6 @@ def get_public_transit_stops(min_lat, min_lon, max_lat, max_lon):

logging.info(f"after all retries, retry_count = {retry_count}, call_return = {'RETRY' if call_return == RETRY else len(call_return)}...")
all_results = call_return
print("**********ALL_RESULTS: ", all_results)

relations = [ad.AttrDict(r) for r in all_results if r["type"] == "relation" and r["tags"]["type"] == "route"]
logging.debug("Found %d relations with ids %s" % (len(relations), [r["id"] for r in relations]))
Expand All @@ -112,7 +108,6 @@ def get_public_transit_stops(min_lat, min_lon, max_lat, max_lon):
rel_nodes_ids = rel_map[relation["id"]]
if stop.id in rel_nodes_ids:
stop["routes"].append({"id": relation["id"], "tags": relation["tags"]})
print("***********TRANSIT STOPS: ", stops)
return stops

# https://gis.stackexchange.com/a/19761
Expand All @@ -127,7 +122,6 @@ def get_stops_near(loc, distance_in_meters):
lon = loc[COORDS][0]
lat = loc[COORDS][1]
stops = get_public_transit_stops(lat - bbox_delta, lon - bbox_delta, lat + bbox_delta, lon + bbox_delta)
print("********STOPS NEAR!: ", stops)
logging.debug("Found %d stops" % len(stops))
for i, stop in enumerate(stops):
logging.debug("STOP %d: %s" % (i, stop))
Expand All @@ -144,7 +138,6 @@ def get_predicted_transit_mode(start_stops, end_stops):
p_end_routes = list(itertools.chain.from_iterable([extract_routes(s) for s in end_stops]))

rel_id_matches = get_rel_id_match(p_start_routes, p_end_routes)
print("**********ID_MATCHES: ", rel_id_matches)
logging.debug("len(start_routes) = %d, len(end_routes) = %d, len(rel_id_matches) = %d" %
(len(p_start_routes), len(p_end_routes), len(rel_id_matches)))
if len(rel_id_matches) > 0:
Expand Down

0 comments on commit 5139a39

Please sign in to comment.