77import sys
88sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ),'..' ,'pyloghub' )))
99from save_to_platform import save_scenario_check ,create_button
10- from input_data_validation import convert_dates ,validate_and_convert_data_types
10+ from input_data_validation import convert_dates ,validate_and_convert_data_types , convert_to_float , convert_df_to_dict_excluding_nan
1111from sending_requests import post_method ,create_headers ,create_url ,get_workspace_entities
1212
1313def forward_freight_shipment_emissions_road (addresses :pd .DataFrame ,parameters :dict ,api_key :str ,save_scenario = {},show_buttons = False )-> Optional [Tuple [pd .DataFrame ,pd .DataFrame ]]:
@@ -33,11 +33,12 @@ def forward_freight_shipment_emissions_road(addresses: pd.DataFrame, parameters:
3333 - toCity (str): City name of the address to which a shipment should arrive at.
3434 - toStreet (str): Street name with house number of the address to which a shipment should arrive at.
3535 - isRefrigirated (str): A YES/NO option that specifies whether the content being transferred through the shipments is refrigirated or not. If not specified will be taken as NO.
36+ -distance: Distance between sender and recipient. It will be calculated if not provided.
3637 - weight (number): The weight of the shipment.
3738
3839 parameters (dict): A dictionary containing parameters:
3940 - vehicleType: "van(0-3.5)", "truck", "truckUrbanTruck", "truckMGV", "truckHGV", "truckRigid(3.5-7.5)", "truckRigid(7.5-12)", "truckRigid(12-20)" , "truckRigid(20-26)", "truckRigid(26-32)", "truckArticulated(3.5-34)", "truckArticulated(34-40)", "truckArticulated(40-44)", "truckArticulated(44-60)", "truckArticulated(60-72)", "truckGeneral", "truckAutoCarrier", "truckDray", "truckExpedited", "truckFlatbed", "truckHeavybulk", "truckLTL", "truckMixed", "truckMoving", "truckPackage", "truckSpecialized", "truckTanker" or "truckTL"
40- - fuelType: enum "diesel", "petrol", "hybrid", " CNG", "LPG", "pluginHybrid ", "electricity" or "other"
41+ - fuelType: enum "diesel", "petrol", "CNG", "LPG", "electricity" or "other"
4142 - weightUnit: enum "kilograms" or "lbs"
4243 - emissionStandard: enum "EURO_5" or :EURO_6"
4344
@@ -60,13 +61,16 @@ def create_buttons():
6061
6162addresses_mandatory_columns = {'shipmentId' :'str' ,'shipmentDate' :'str' ,'fromCountry' :'str' ,'toCountry' :'str' ,'weight' :'float' }
6263addresses_optional_columns = {'fromState' :'str' ,'fromPostalCode' :'str' ,'fromCity' :'str' ,'fromStreet' :'str' ,'toState' :'str' ,'toPostalCode' :'str' ,'toCity' :'str' ,'toStreet' :'str' ,'isRefrigirated' :'str' }
64+ addresses_optional_floats = ['distance' ]
6365
6466# Validate and convert data types
6567addresses = validate_and_convert_data_types (addresses ,addresses_mandatory_columns ,'mandatory' ,'addresses' )
6668if not addresses is None :
6769addresses = validate_and_convert_data_types (addresses ,addresses_optional_columns ,'optional' ,'addresses' )
68- if not addresses is None :
69- addresses = convert_dates (addresses , ['shipmentDate' ])
70+ if not addresses is None :
71+ addresses = convert_dates (addresses , ['shipmentDate' ])
72+ addresses = convert_to_float (addresses ,addresses_optional_floats ,'optional' )
73+ addresses = convert_df_to_dict_excluding_nan (addresses ,addresses_optional_floats )
7074if addresses is None :
7175return None
7276
@@ -75,7 +79,7 @@ def create_buttons():
7579headers = create_headers (api_key )
7680
7781payload = {
78- 'freightShipmentEmissionsByRoad' :addresses . to_dict ( orient = 'records' ) ,
82+ 'freightShipmentEmissionsByRoad' :addresses ,
7983'parameters' :parameters
8084 }
8185payload = save_scenario_check (save_scenario ,payload )
@@ -95,7 +99,7 @@ def create_buttons():
9599def forward_freight_shipment_emissions_road_sample_data ():
96100warnings .simplefilter ("ignore" ,category = UserWarning )
97101data_path = os .path .join (os .path .dirname (__file__ ),'sample_data' ,'CO2RoadAddresses.xlsx' )
98- addresses_df = pd .read_excel (data_path ,sheet_name = 'addresses' ,usecols = 'A:O ' ).fillna ("" )
102+ addresses_df = pd .read_excel (data_path ,sheet_name = 'addresses' ,usecols = 'A:P ' ).fillna ("" )
99103parameters = {
100104"vehicleType" :"truck" ,
101105"fuelType" :"diesel" ,
@@ -128,11 +132,12 @@ def reverse_freight_shipment_emissions_road(coordinates: pd.DataFrame, parameter
128132 - toLatitude (number): The latitude of the coordinate to which a shipment should arrive at.
129133 - toLongitude (numebr): The longitude of the coordinate to which a shipment should arrive at.
130134 - isRefrigirated (str): A YES/NO option that specifies whether the content being transferred through the shipments is refrigirated or not. If not specified will be taken as NO.
135+ - distance: Distance beteen sender and recipient. It will be calculated if not provided.
131136 - weight (number): The weight of the shipment.
132137
133138 parameters (dict): A dictionary containing parameters:
134139 - vehicleType: "van(0-3.5)", "truck", "truckUrbanTruck", "truckMGV", "truckHGV", "truckRigid(3.5-7.5)", "truckRigid(7.5-12)", "truckRigid(12-20)" , "truckRigid(20-26)", "truckRigid(26-32)", "truckArticulated(3.5-34)", "truckArticulated(34-40)", "truckArticulated(40-44)", "truckArticulated(44-60)", "truckArticulated(60-72)", "truckGeneral", "truckAutoCarrier", "truckDray", "truckExpedited", "truckFlatbed", "truckHeavybulk", "truckLTL", "truckMixed", "truckMoving", "truckPackage", "truckSpecialized", "truckTanker" or "truckTL"
135- - fuelType: enum "diesel", "petrol", "hybrid", " CNG", "LPG", "pluginHybrid ", "electricity" or "other"
140+ - fuelType: enum "diesel", "petrol", "CNG", "LPG", "electricity" or "other"
136141 - weightUnit: enum "kilograms" or "lbs"
137142 - emissionStandard: enum "EURO_5" or :EURO_6"
138143
@@ -155,21 +160,24 @@ def create_buttons():
155160
156161coordinates_mandatory_columns = {'shipmentId' :'str' ,'shipmentDate' :'str' ,'fromLatitude' :'float' ,'fromLongitude' :'float' ,'toLatitude' :'float' ,'toLongitude' :'float' ,'weight' :'float' }
157162coordinates_optional_columns = {'isRefrigirated' :'str' }
163+ coordinates_optional_floats = ['distance' ]
158164
159165# Validate and convert data types
160166coordinates = validate_and_convert_data_types (coordinates ,coordinates_mandatory_columns ,'mandatory' ,'coordinates' )
161167if not coordinates is None :
162168coordinates = validate_and_convert_data_types (coordinates ,coordinates_optional_columns ,'optional' ,'coordinates' )
163- if not coordinates is None :
164- coordinates = convert_dates (coordinates , ['shipmentDate' ])
169+ if not coordinates is None :
170+ coordinates = convert_dates (coordinates , ['shipmentDate' ])
171+ coordinates = convert_to_float (coordinates ,coordinates_optional_floats ,'optional' )
172+ coordinates = convert_df_to_dict_excluding_nan (coordinates ,coordinates_optional_floats )
165173if coordinates is None :
166174return None
167175url = create_url ("reverseco2emissionsroad" )
168176
169177headers = create_headers (api_key )
170178
171179payload = {
172- 'freightShipmentEmissionsByRoad' :coordinates . to_dict ( orient = 'records' ) ,
180+ 'freightShipmentEmissionsByRoad' :coordinates ,
173181'parameters' :parameters
174182 }
175183payload = save_scenario_check (save_scenario ,payload )
@@ -189,7 +197,7 @@ def create_buttons():
189197def reverse_freight_shipment_emissions_road_sample_data ():
190198warnings .simplefilter ("ignore" ,category = UserWarning )
191199data_path = os .path .join (os .path .dirname (__file__ ),'sample_data' ,'CO2RoadReverse.xlsx' )
192- coordinates_df = pd .read_excel (data_path ,sheet_name = 'coordinates' ,usecols = 'A:I ' ).fillna ("" )
200+ coordinates_df = pd .read_excel (data_path ,sheet_name = 'coordinates' ,usecols = 'A:J ' ).fillna ("" )
193201parameters = {
194202"vehicleType" :"truck" ,
195203"fuelType" :"diesel" ,