1
-
2
1
import requests
3
2
4
3
API_URL = "https://api.data.gov.sg/v1/transport/traffic-images"
@@ -9,6 +8,7 @@ def __init__(self, url="No API URL"):
9
8
self .timestamp = None
10
9
self .api_status = "Unverified"
11
10
self .metadata = None
11
+ self .camera_id_array = []
12
12
13
13
# Get API response
14
14
response = requests .get (self .url )
@@ -24,6 +24,10 @@ def __init__(self, url="No API URL"):
24
24
print (f"The API status is: { self .api_status } " )
25
25
print (f"The API was called at: { self .timestamp } " )
26
26
27
+ for item in self .metadata ["items" ]:
28
+ for camera in item ["cameras" ]:
29
+ self .camera_id_array .append (camera ["camera_id" ])
30
+
27
31
def extract_image (self , camera_id ):
28
32
# Loop through the items and cameras to find the correct camera_id
29
33
for item in self .metadata ["items" ]:
@@ -33,6 +37,17 @@ def extract_image(self, camera_id):
33
37
# If camera ID is not found
34
38
return f"Camera ID { camera_id } not found."
35
39
40
+ def extract_latlon (self , camera_id ):
41
+ for item in self .metadata ["items" ]:
42
+ for camera in item ["cameras" ]:
43
+ if camera ["camera_id" ] == str (camera_id ):
44
+ longitude = camera ["location" ]["longitude" ]
45
+ latitude = camera ["location" ]["latitude" ]
46
+ return longitude , latitude # Return both longitude and latitude as a tuple
47
+ # If camera ID is not found
48
+ return f"Camera ID { camera_id } not found."
49
+
50
+
36
51
37
52
38
53
0 commit comments