Last active
August 30, 2023 06:41
-
-
Save dertajora/d96a4f8ee539c2f8b699103dc01e3e08 to your computer and use it in GitHub Desktop.
Grenmiles prototype
This file contains hidden or 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
from datetime import datetime | |
from shapely.geometry import Point | |
from shapely.geometry.polygon import Polygon | |
# data trip of users | |
# id, driverId, userId, amount, rideType, latOrigin, longOrigin, latDestination, longDestination, distance, createdAt, finishedAt | |
trips = [ | |
[1,"b83c50be-4b17-405a-8885-f46b1cb12beb","0911d670-cb99-4000-9cae-c082c1c98c95", 12000, 1, -6.2812861, 107.0149711, 6.246310, 107.018128, 2.3, "2023-08-20 10:10:10", "2023-08-20 10:25:10"], | |
[2,"x123dda-4we7-ss5a-9985-xxeweb12beb","0911d670-cb99-4000-9cae-c082c1c98c95", 15000, 1, -6.2023535,106.8150157, -6.2273916,106.8043289, 1.5, "2023-08-20 11:10:10", "2023-08-20 11:45:10"], | |
[3,"d312fabd-2f65-493b-93d0-67a1a31d4e6e","0911d670-cb99-4000-9cae-c082c1c98c95", 35000, 2, -6.2273916,106.8043289, -6.2271669,106.7945101, 2.9, "2023-08-20 18:10:10", "2023-08-20 18:30:10"], | |
[4,"b83c50be-4b17-405a-8885-f46b1cb12beb","2f1e443f-3b92-4939-909f-7093dfe2d8b6", 15000, 1, -6.1951851,106.816837, -6.2124811,106.8182985, 1.8, "2023-08-20 15:10:10", "2023-08-20 15:30:10"], | |
] | |
# list of public transport area | |
# id, name, latPolygon, longPolygon, type, createdAt | |
publicTransportArea = [ | |
[1,"Sudirman Station", 3, [-6.2017039,-6.2023226,-6.2039865,-6.2035385,-6.2017039], [106.8219668,106.8256790,106.8253356,106.8216235,106.8219668] ,"2023-05-20 10:10:10"], | |
[2, "Bekasi Station",3, [-6.2475420,-6.2453876,-6.2463208,-6.2485872,-6.2475420], [107.0161916,107.0170553,107.0199735,107.0189167,107.0161916],"2023-05-20 10:10:10"], | |
] | |
potentialTrip = {} | |
polygonPublicTransport = {} | |
# define | |
for area in publicTransportArea: | |
tempLocation = [] | |
for i in range(len(area[3])): | |
tempLocation.append((area[3][i],area[4][i])) | |
polygonPublicTransport[area[0]] = { | |
"id" : area[0], | |
"name": area[1], | |
"type": area[2], | |
"polygon": Polygon(tempLocation) | |
} | |
# check if location is public transport or not | |
# return statusIfAroundPublicTransport, areaID, areaType | |
def checkIsPublicTransport(latitude, longitude): | |
point = Point(latitude, longitude) | |
# for area in publicTransportArea: | |
for data in polygonPublicTransport: | |
result = polygonPublicTransport[data]["polygon"].contains(point) | |
return True, polygonPublicTransport[data]["id"], polygonPublicTransport[data]["type"] | |
return False, 0, 0 | |
##return in minutes | |
def timeDiff(start,end): | |
start = datetime.strptime(start,"%Y-%m-%d %H:%M:%S") | |
end = datetime.strptime(end,"%Y-%m-%d %H:%M:%S") | |
return (end - start).total_seconds() / 60 | |
# calculated based on estimated emission per vehicle | |
# return number | |
def calculateEmission(distance, typeVehicle): | |
emission = 0 | |
# car | |
if typeVehicle == 1: | |
emission = 1.1 | |
# bike | |
elif typeVehicle == 2: | |
emission = 0.5 | |
# train | |
elif typeVehicle == 4: | |
emission = 0.8 | |
# bus | |
elif typeVehicle == 5: | |
emission = 0.7 | |
return emission * distance | |
for trip in trips: | |
if trip[2] not in potentialTrip: | |
# initial data | |
potentialTrip[trip[2]] = {"id": trip[2] , "trips": [trip], "greenJourneys": [] } | |
else: | |
potentialTrip[trip[2]]["trips"].append(trip) | |
for userData in potentialTrip: | |
totalTrip = len(potentialTrip[userData]["trips"]) | |
# not commuting | |
if totalTrip <= 1: | |
continue | |
for x in range(totalTrip - 1): | |
latOrigin = potentialTrip[userData]["trips"][x][5] | |
longOrigin = potentialTrip[userData]["trips"][x][6] | |
latDestination = potentialTrip[userData]["trips"][x][7] | |
longDestination = potentialTrip[userData]["trips"][x][8] | |
nextLatOrigin = potentialTrip[userData]["trips"][x+1][5] | |
nextLongOrigin = potentialTrip[userData]["trips"][x+1][6] | |
nextLatDestination = potentialTrip[userData]["trips"][x+1][7] | |
nextLongDestination = potentialTrip[userData]["trips"][x+1][8] | |
startTimeFirstTrip = potentialTrip[userData]["trips"][x][10] | |
startTimeNextTrip = potentialTrip[userData]["trips"][x+1][10] | |
statusOriginTrip, originStationID, originType = checkIsPublicTransport(latDestination, longDestination) | |
if statusOriginTrip: | |
statusNextTrip, nextStationID, nextType = checkIsPublicTransport(nextLatOrigin, nextLongOrigin) | |
else: | |
continue | |
# 90 minutes because of average commuting people in Jakarta is maximum 75 minutes, plus buffer 15 minutes | |
# source : https://www.lpem.org/wp-content/uploads/2020/08/WP-LPEM-054-Exploring_the_Changes_of_Commuting_Patterns.pdf | |
# elgible green journey | |
if statusNextTrip and timeDiff(startTimeFirstTrip, startTimeNextTrip) <= 90 and nextStationID == originStationID: | |
# calculate miles point for user | |
emissionFirstMile = calculateEmission(potentialTrip[userData]["trips"][x][5]) | |
emissionPublicTransport = calculateEmission(potentialTrip[userData]["trips"][x][5]) | |
emissionLastMile = = calculateEmission(potentialTrip[userData]["trips"][x][5]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment