Exemplo n.º 1
0
def plotTrajectoryProfiles(pathToSave,
                           folderName,
                           trajectories,
                           origin,
                           end,
                           savefig=True,
                           showfig=True):
    if (not os.path.isdir("./{path}/{folderToSave}".format(
            path=pathToSave, folderToSave=folderName))):
        os.makedirs("./{path}/{folderToSave}".format(path=pathToSave,
                                                     folderToSave=folderName))

    for i in range(0, len(trajectories)):
        last_point_in_trajectory = trajectories[i][len(trajectories[i]) - 1]
        # end[0] is end latitude, end[1] is end longitude
        if (
                utils.nearOrigin(
                    end[0], end[1],
                    last_point_in_trajectory[utils.dataDict["latitude"]],
                    last_point_in_trajectory[utils.dataDict["longitude"]])
        ):  # only plot profile for those trajectories between origin and end
            # 1. acceleration profile from start point to end point
            accelerations, timeAxis, distanceAxis = getSpeedAccelerations(
                trajectories[i], utils.dataDict)
            plt.scatter(
                distanceAxis,
                accelerations,
                label="trajectory_acceleration_profile_{i}".format(i=i))
            if (savefig):
                plt.savefig(
                    "./{path}/{folderToSave}/trajectory_acceleration_profile_{i}.png"
                    .format(path=pathToSave, folderToSave=folderName, i=i))
            if (showfig):
                plt.show()
            plt.clf()
            # 2. speed profile from start point to end point
            speeds, timeAxis, distanceAxis = getSpeeds(trajectories[i],
                                                       utils.dataDict)
            plt.scatter(distanceAxis,
                        speeds,
                        label="trajectory_speed_profile_{i}".format(i=i))
            if (savefig):
                plt.savefig(
                    "./{path}/{folderToSave}/trajectory_speed_profile_{i}.png".
                    format(path=pathToSave, folderToSave=folderName, i=i))
            if (showfig):
                plt.show()
            plt.clf()
Exemplo n.º 2
0
def plotTrajectoryProfiles(pathToSave, folderName, trajectories, origin, end, savefig = True,showfig = True):
	if(not os.path.isdir("./{path}/{folderToSave}".format(path = pathToSave, folderToSave = folderName))):
		os.makedirs("./{path}/{folderToSave}".format(path = pathToSave, folderToSave = folderName))

	for i in range(0, len(trajectories)):
		last_point_in_trajectory = trajectories[i][len(trajectories[i]) -1]
		# end[0] is end latitude, end[1] is end longitude
		if(utils.nearOrigin(end[0],end[1],last_point_in_trajectory[utils.dataDict["latitude"]], last_point_in_trajectory[utils.dataDict["longitude"]])): # only plot profile for those trajectories between origin and end		
			# 1. acceleration profile from start point to end point
			accelerations, timeAxis, distanceAxis = getSpeedAccelerations(trajectories[i], utils.dataDict)
			plt.scatter(distanceAxis, accelerations, label = "trajectory_acceleration_profile_{i}".format(i = i))
			if(savefig):
				plt.savefig("./{path}/{folderToSave}/trajectory_acceleration_profile_{i}.png".format(path = pathToSave, folderToSave = folderName, i = i))
			if(showfig):
				plt.show()
			plt.clf()
			# 2. speed profile from start point to end point 
			speeds, timeAxis , distanceAxis= getSpeeds(trajectories[i], utils.dataDict)
			plt.scatter(distanceAxis, speeds, label = "trajectory_speed_profile_{i}".format(i = i))
			if(savefig):
				plt.savefig("./{path}/{folderToSave}/trajectory_speed_profile_{i}.png".format(path = pathToSave, folderToSave = folderName, i = i))
			if(showfig):
				plt.show()
			plt.clf()
def extractTrajectoriesUntilOD(data, originTS, originLatitude, originLongtitude, endTS, endLatitude, endLongtitude, show = True, save = False, clean = False, fname = "", path = "plots"):
	"""
	returns: OD_trajectories: in x,y coordinate;
			 OD_trajectories_lat_lon: in lat, lon coordinate;
	"""
	
	maxSpeed = 0
	for i in range(0, data.shape[0]):
		speed_over_ground = data[i][utils.dataDict["speed_over_ground"]]
		if(speed_over_ground > maxSpeed and speed_over_ground != 102.3): #1023 indicates speed not available
			maxSpeed = speed_over_ground
	print "This tanker maxSpeed:", maxSpeed, " knot"
	
	OD_trajectories = [] # origin destination endpoints trajectory
	i = 0
	while(i< data.shape[0]):
		cur_pos = data[i]
		if(utils.nearOrigin( \
			originLatitude, \
			originLongtitude, \
			cur_pos[utils.dataDict["latitude"]], \
			cur_pos[utils.dataDict["longitude"]], \
			thresh = 0.0) and \
			cur_pos[utils.dataDict["ts"]] == originTS): # must be exact point

			this_OD_trajectory = []
			this_OD_trajectory.append(cur_pos)
			i += 1
			while(i < data.shape[0] and \
				(not utils.nearOrigin( \
					endLatitude, \
					endLongtitude, \
					data[i][utils.dataDict["latitude"]], \
					data[i][utils.dataDict["longitude"]], \
					thresh = 0.0))):
				this_OD_trajectory.append(data[i])
				i += 1
			if(i < data.shape[0]):
				this_OD_trajectory.append(data[i]) # append the destination endpoint
			this_OD_trajectory = np.asarray(this_OD_trajectory) # make it to be an np 2D array

			""" box/radius approach in cleaning of points around origin"""
			j = 1
			print "checking points around origin:", j
			while(j < this_OD_trajectory.shape[0] and \
				utils.nearOrigin( \
					originLatitude, \
					originLongtitude, \
					this_OD_trajectory[j][utils.dataDict["latitude"]], \
					this_OD_trajectory[j][utils.dataDict["longitude"]], \
					thresh = utils.NEIGHBOURHOOD_ORIGIN)):
				j += 1
			print "last point around origin:", j
			this_OD_trajectory_around_origin = this_OD_trajectory[0:j]

			"""Take the box mean, treat timestamp as averaged as well"""
			this_OD_trajectory_mean_origin = boxMeanTrajectoryPoints(this_OD_trajectory_around_origin, originLatitude, originLongtitude)
			print "mean start point x,y : ", utils.LatLonToXY( \
				originLatitude, \
				originLongtitude, \
				this_OD_trajectory_mean_origin[utils.dataDict["latitude"]], \
				this_OD_trajectory_mean_origin[utils.dataDict["longitude"]])
			OD_trajectories.append(np.insert(this_OD_trajectory[j:],0,this_OD_trajectory_mean_origin, axis = 0))
			break  # only one trajectory per pair OD, since OD might be duplicated
		i += 1

	OD_trajectories = np.array(OD_trajectories)
	OD_trajectories_lat_lon = copy.deepcopy(OD_trajectories)
	for i in range(0, len(OD_trajectories)):
		for j in range(0, len(OD_trajectories[i])):
			x, y = utils.LatLonToXY(originLatitude, originLongtitude, OD_trajectories[i][j][utils.dataDict["latitude"]], OD_trajectories[i][j][utils.dataDict["longitude"]])
			OD_trajectories[i][j][utils.data_dict_x_y_coordinate["y"]] = y
			OD_trajectories[i][j][utils.data_dict_x_y_coordinate["x"]] = x
		# plotting purpose
		plt.scatter(OD_trajectories[i][0:len(OD_trajectories[i]),utils.data_dict_x_y_coordinate["x"]], \
			OD_trajectories[i][0:len(OD_trajectories[i]),utils.data_dict_x_y_coordinate["y"]])
	if(not plt.gca().yaxis_inverted()):
		plt.gca().invert_yaxis()
	if(save):
		plt.savefig("./{path}/{fname}.png".format(path = path, fname = fname))
	if(show):
		plt.show()
	if(clean):
		plt.clf()

	return OD_trajectories, OD_trajectories_lat_lon