예제 #1
0
 def _process_reittiopas_results_to_features(self,segments):
     #Array of routes, get route
     avg_km = ""
     avg_speed = ""
     avg_co2 = 0.00
     avg_time = ""
     features = []
     #route
     if segments != None and len(segments) > 0:
         if "length" in segments:
             length = str(segments["length"])
             avg_km = float(float(length) / 1000)
         if "duration" in segments:
             time = str(segments["duration"])
             avg_time = float(float(time) / 3600)
             avg_speed = float(avg_km / avg_time)
         for segment in segments["legs"]:
             feature = Feature()
             feature.set_type("LineString")
             #set up feature coords
             if "locs" in segment:
                 #coordinates nodes
                 coords = []
                 for node in segment["locs"]:
                     c = []
                     x=""
                     y=""
                     if "coord" in node:
                         coord = node["coord"]
                         if "x" in coord:
                             x = coord["x"]
                             c.append(x)
                         if "y" in coord:
                             y = coord["y"]
                             c.append(y)
                     coords.append(c)
                 feature.set_coords(coords)
             if "type" in segment:
                 act = segment["type"]
                 activity = ""
                 if act == "walk":
                     activity = "on-foot"
                 elif self._is_number(act) == True:
                     activity = "in-vehicle"
                 else:
                     activity = "unknown"
                 feature.set_activity(activity)
             if "length" in segment:
                 length = str(segment["length"])
                 kms = float(float(length) / 1000)
                 feature.set_km(kms)
             if "duration" in segment:
                 time = str(segment["duration"])
                 hours = float(float(time) / 3600)
                 speed = float(feature.get_km() / hours)
                 feature.set_speed(speed)
                 feature.set_time(hours)
             if "code" in segment:
                 code = str(segment["code"])
                 if len(code) > 1:
                     transport = code[1:]
                     type = self._interpret_type_of_transport(code[0], segment["type"])
                     if transport != "" and type != "":
                         feature.set_transport(transport)
                         feature.set_transport_type(type)
             features.append(feature)
             avg_co2 = avg_co2 + float(feature.get_co2())
         #recorde trip route
     self.set_features(features)
     self.set_km(avg_km)
     self.set_time(avg_time)
     self.set_speed(avg_speed)
     self.set_co2(avg_co2)
     
     return self._build_geojson_trip()
     
예제 #2
0
def _get_location_coordinates(feature_array, last, end_time, start_time, locations):
    features = []
    present_location = None
    previous_location = None
    distance = 0.00
    segment_distances = []
    activity_distances = dict()
    ed_tyyppi = ""
    tyyppi = ""
    first_location_index=0
    
    kms = float(6373)
    for i in range(0,last):
        feature = Feature()
        ed_tyyppi = tyyppi
        time, time1, time2 = "", "", ""
        speed = 0.00
        transport = ""
        co2 = 0.00
        seg_km = 0.00
        
        #Setup variables
        tyyppi = feature_array[i]['geometry']['type']
        coords = feature_array[i]["geometry"]["coordinates"]
        activity = feature_array[i]["properties"]["activity"]
        
        start_time = locations[first_location_index].time
        time = get_trip_segment_time(feature_array, last, end_time, i, start_time)

        # append first_location_index
    
        if tyyppi == 'LineString':
            first_location_index += len(coords) - 1
        else:
            # it is a point
            # and location has not changed
            pass
        
        #Setup feature
        feature.set_type(tyyppi)
        feature.set_coords(coords)
        feature.set_activity(activity)
        feature.set_time(time)
        
        if tyyppi == "LineString" or tyyppi == "Point" or tyyppi=="Feature":
            
                
            if tyyppi == "Point":
                previous_location = present_location
                present_location = coords
                if previous_location != None and present_location != None:
                    if len(present_location) > 0 and len(previous_location) > 0:
                        distance = distance + kms*_calculate_trip_distance(present_location[1], present_location[0], previous_location[1],previous_location[0])
                
            elif tyyppi == "LineString":
                seg_km = 0.00
                for coord in coords:
                    previous_location = present_location
                    present_location = coord
                    #logger.info(present_location, previous_location)
                    if previous_location != None and present_location != None:
                        if len(present_location) > 0 and len(previous_location) > 0:
                            km=kms*_calculate_trip_distance(present_location[1], present_location[0], previous_location[1],previous_location[0])
                            distance = distance + km
                            if ed_tyyppi == "LineString":
                                seg_km = seg_km + km
                segment_distances.append(seg_km)
                activity_distances[activity]=seg_km
        else:
            coords = feature_array[i]["geometry"]["coordinates"]
            activity = feature_array[i]["properties"]["activity"]
            feature.set_coords(coords)
        
        feature.set_km(seg_km)
        
        if (time != "" or time != None) and (seg_km > 0.00):
            speed = _calculate_avg_speed(time, km)
        
        feature.set_speed(speed)
        features.append(feature)
        
    return distance, features