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
6162 addresses_mandatory_columns = {'shipmentId' : 'str' , 'shipmentDate' : 'str' , 'fromCountry' : 'str' , 'toCountry' : 'str' , 'weight' : 'float' }
6263 addresses_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
6567 addresses = validate_and_convert_data_types (addresses , addresses_mandatory_columns , 'mandatory' , 'addresses' )
6668 if not addresses is None :
6769 addresses = 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 )
7074 if addresses is None :
7175 return None
7276
@@ -75,7 +79,7 @@ def create_buttons():
7579 headers = create_headers (api_key )
7680
7781 payload = {
78- 'freightShipmentEmissionsByRoad' : addresses . to_dict ( orient = 'records' ) ,
82+ 'freightShipmentEmissionsByRoad' : addresses ,
7983 'parameters' : parameters
8084 }
8185 payload = save_scenario_check (save_scenario , payload )
@@ -95,7 +99,7 @@ def create_buttons():
9599def forward_freight_shipment_emissions_road_sample_data ():
96100 warnings .simplefilter ("ignore" , category = UserWarning )
97101 data_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 ("" )
99103 parameters = {
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
156161 coordinates_mandatory_columns = {'shipmentId' : 'str' , 'shipmentDate' : 'str' , 'fromLatitude' : 'float' , 'fromLongitude' : 'float' , 'toLatitude' : 'float' , 'toLongitude' : 'float' , 'weight' : 'float' }
157162 coordinates_optional_columns = {'isRefrigirated' : 'str' }
163+ coordinates_optional_floats = ['distance' ]
158164
159165 # Validate and convert data types
160166 coordinates = validate_and_convert_data_types (coordinates , coordinates_mandatory_columns , 'mandatory' , 'coordinates' )
161167 if not coordinates is None :
162168 coordinates = 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 )
165173 if coordinates is None :
166174 return None
167175 url = create_url ("reverseco2emissionsroad" )
168176
169177 headers = create_headers (api_key )
170178
171179 payload = {
172- 'freightShipmentEmissionsByRoad' : coordinates . to_dict ( orient = 'records' ) ,
180+ 'freightShipmentEmissionsByRoad' : coordinates ,
173181 'parameters' : parameters
174182 }
175183 payload = save_scenario_check (save_scenario , payload )
@@ -189,7 +197,7 @@ def create_buttons():
189197def reverse_freight_shipment_emissions_road_sample_data ():
190198 warnings .simplefilter ("ignore" , category = UserWarning )
191199 data_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 ("" )
193201 parameters = {
194202 "vehicleType" : "truck" ,
195203 "fuelType" : "diesel" ,
0 commit comments