Created
August 25, 2019 08:14
-
-
Save nivleshc/a07aa79cfdf573158a3ba466c9ae9a59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_feed(feed, today): | |
global dict_pushpinLabel_to_desc_map | |
global dict_vehicleId_to_pushpinLabel_map | |
global lastpushpinLabel | |
global pushpin_url | |
global total_vehicles_found | |
#we will use the python module 'time' to convert epoch time (this is what gtfsr timestamps are in) to local time | |
#set the timezone for time | |
os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0' | |
time.tzset() | |
print(f'timezone set to {time.tzname}') | |
num_pushpin_assigned = 0 | |
total_feed_entity = len(feed.entity) | |
total_vehicles_found += total_feed_entity | |
print('Total feed.entity:',total_feed_entity," Total Vehicles Found:",total_vehicles_found) | |
#As we are using Bing maps, there is a limit to the number of pushpins we can specify for our map. If we | |
#reach this limit, we will break out of the loop below as there is no benefit for continuing on processing | |
for entity in feed.entity: | |
if num_pushpin_assigned >= maps_bing_pushpin_limit: | |
break #we haev exceeded the number of pushpins that can be used with Bing Maps. Exit loop | |
tripupdatetimestamp_autz = time.ctime(entity.vehicle.timestamp) | |
vehicleId = entity.vehicle.vehicle.id | |
#we need to make sure that the fields used for dynamodb keys are not null. If they are then skip this record. Currently these are TripDate which is | |
#todays date and vehicleId. So just check vehicleId for being not null and TripDate won't be null. | |
if (vehicleId): #only go ahead if vehicleId is present. Otherwise just print that vehicleId for this record is missing | |
#lets find out if this vehicle already has a pushpinLabel assigned for today | |
if vehicleId in dict_vehicleId_to_pushpinLabel_map.keys(): | |
#this vehicle already has a pushpinLabel. Get the label | |
pushpinLabel = dict_vehicleId_to_pushpinLabel_map[vehicleId]['label'] | |
else: | |
#this vehicle doesn't have any pushpinLabel already assigned. Generate a new pushpinLabel for it | |
lastpushpinLabel += 1 #increment the lastpushpinLabel so that it now points to a new number | |
pushpinLabel = lastpushpinLabel | |
tripId = entity.vehicle.trip.trip_id | |
#the pushpinLabelDesc will be set to the vehicle's label. There have been instances where I noticed the vehicle's label is missing/null. | |
#In these cases, set the pushpinLabelDesc to TripId | |
if not entity.vehicle.vehicle.label: | |
pushpinLabelDesc = tripId | |
else: | |
pushpinLabelDesc = entity.vehicle.vehicle.label | |
#since this vehicle had not been previously assigned a pushpinLabel for today, add its details to the two dict | |
inDynamodbTable = False #this item has not been read from or written to dynamodb yet | |
latitude = entity.vehicle.position.latitude | |
longitude = entity.vehicle.position.longitude | |
dict_pushpinLabel_to_desc_map[str(pushpinLabel)] = {"desc": str(pushpinLabelDesc),"vehicleId": str(vehicleId),"inDynamodbTable":inDynamodbTable,"latitude":latitude,"longitude":longitude,"tripId":tripId} | |
dict_vehicleId_to_pushpinLabel_map[str(vehicleId)] = {"label": str(pushpinLabel), "desc": str(pushpinLabelDesc)} | |
#this vehicle will be displayed on the map. Update dict_pushpinLabel_to_desc_map for this vehicle's entry so that when the legend is | |
#generated, it will be coloured differently to show that it is currently displayed on the map | |
dict_pushpinLabel_to_desc_map[str(pushpinLabel)]['isDisplayedOnMap'] = True | |
#add this vehicle's details to the pushpin url | |
pushpin_url += "pp=" + str(entity.vehicle.position.latitude) + "," + str(entity.vehicle.position.longitude) | |
#based on how long ago the laststopid timestamp is, calculate if the vehicle is active or inactive and respectively assign the icon | |
if (datetime.datetime.now() - datetime.datetime.strptime(tripupdatetimestamp_autz,'%a %b %d %H:%M:%S %Y')) > datetime.timedelta(minutes=inactive_vehicle_datetime_limit): | |
#this vehicle is inactive | |
pushpin_url += ";" + maps_inactive_vehicle_icon + ";" | |
else: | |
pushpin_url += ";" + maps_active_vehicle_icon + ";" | |
pushpin_url += str(pushpinLabel) + "&" | |
num_pushpin_assigned += 1 #increment the counter that denotes the number of pushpins added to the pushpin url | |
else: | |
print("process_feed:MissingVehicleId:VehicleWillBeSkipped:TripId:", entity.vehicle.trip.trip_id, " VehicleLabel:", entity.vehicle.vehicle.label," Latitude: ", entity.vehicle.position.latitude, " Longitude: ", entity.vehicle.position.longitude) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment