【測繪程式設計試題集】 試題01 計程車軌跡資料計算

staHuri發表於2018-10-18

資料

資料

問題

問題1

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : Taxi.py
# @Author  : huifer
# @Time    : 2018/10/17 20:29
import math
import datetime


class Taxi(object):
    def __init__(self, path):
        self.path = path
        self.old_data = self.reads()
        self.v = self.sudu()
        self.se = self.start_end()

    def start_end(self):

        a = self.old_data
        start = a[2]
        end = a[-1]

        zhixianjuli = self.point_length(float(end[3]), float(end[4]), float(start[3]), float(start[4]))
        shijiancha = self.shijiancha_se(end[2], start[2])

        return [zhixianjuli, shijiancha]

    def sudu(self):
        """
        計算速度 (總長/ 總時間 單位(米/ 秒))
        :return:
        """
        a = self.old_data[1:]
        sum_time = 0
        sum_length = 0
        print("==============")
        print("開始時間\t", "結束時間\t", "方位角\t", "速度")
        for i in range(a.__len__()):
            if i + 1 < a.__len__():
                now = a[i]
                next_data = a[i + 1]

                length = self.point_length(float(now[3]), float(now[4]), float(next_data[3]), float(next_data[4]))
                sub_time = self.shijiancha_se(now[2], next_data[2])
                fangwei = self.fangweijiao(float(now[3]), float(now[4]), float(next_data[3]), float(next_data[4]))
                # print(sub_time , "=" , length ,"=",fangwei)
                print(format(self.get_confucianism_and_japan(now[2]), "0.5f"), "\t",
                      format(self.get_confucianism_and_japan(next_data[2]),"0.5f"), '\t',
                      format(math.degrees(fangwei) ,'0.3f'),'\t',
                      format((length / sub_time) /3600,'.3f')
                      )

                sum_time += sub_time
                sum_length += length

        # print(sum_time)
        # print(sum_length)
        print("==============")
        return sum_length / sum_time

    def fangweijiao(self, x1, y1, x2, y2):
        """
        方位角
        :param x1:
        :param y1:
        :param x2:
        :param y2:
        :return:
        """
        return math.atan((y2 - y1) - (x2 - x1))

    def point_length(self, x1, y1, x2, y2):
        """
        兩點間距離
        :param x1:
        :param y1:
        :param x2:
        :param y2:
        :return:
        """
        return abs(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))


    def reads(self):
        """
        讀取檔案
        :param path:
        :return:
        """
        taxi_data = list()
        with open(self.path, 'r', encoding='utf-8') as f:
            for i in f.readlines():
                now_data = i.replace('\n', '').split(",")
                taxi_data.append(now_data)
        return taxi_data


    def get_confucianism_and_japan(self, date_str):
        """
        計算儒日時間
        :param date_str:
        :return:
        """
        nian = int(date_str[:4])
        yue = int(date_str[4:6])
        ri = int(date_str[6:8])
        shi = int(date_str[8:10])
        fen = int(date_str[10:12])
        miao = int(date_str[12:14])
        xxishu = 7 / 4
        new_data = -678987 + 367 * nian - int(xxishu * (nian + int((yue + 9) / 12))) - int(
            (275 * yue) / 9) + ri + shi / 24 + fen / 1440 + miao / 86400
        return new_data

    def nyrsfm(self, date_str):
        """
        獲取文字日期的年月日時分秒
        :param date_str:
        :return:
        """
        Y = int(date_str[:4])
        M = int(date_str[4:6])
        D = int(date_str[6:8])
        H = int(date_str[8:10])
        N = int(date_str[10:12])
        S = int(date_str[12:14])
        return {
            'Y': Y,
            'M': M,
            'D': D,
            'H': H,
            'N': N,
            'S': S,
        }

    def shijiancha_se(self, data1, data2):
        """
        計算兩個時間差 單位秒
        :param data1:
        :param data2:
        :return:
        """
        a = self.nyrsfm(data1)
        b = self.nyrsfm(data2)
        asa = datetime.datetime(
            a.get('Y'),
            a.get('M'),
            a.get('D'),
            a.get('H'),
            a.get('N'),
            a.get('S'),
        )

        asb = datetime.datetime(
            b.get('Y'),
            b.get('M'),
            b.get('D'),
            b.get('H'),
            b.get('N'),
            b.get('S'),
        )

        return (asb - asa).seconds


if __name__ == '__main__':
    t = Taxi(r"計程車資料.txt")
    print("讀取到的資料")
    print(t.old_data)
    print("速度")
    print(t.v)
    print("起點終點")
    print(t.se)

結果

==============
開始時間	 結束時間	 方位角	 速度
57514.52277 	 57514.52338 	 89.966 	 0.007
57514.52338 	 57514.52400 	 89.964 	 0.006
57514.52400 	 57514.52459 	 89.858 	 0.007
57514.52459 	 57514.52523 	 89.946 	 0.006
57514.52523 	 57514.52587 	 89.957 	 0.006
57514.52587 	 57514.52709 	 89.980 	 0.007
57514.52709 	 57514.52767 	 89.960 	 0.006
57514.52767 	 57514.52825 	 89.963 	 0.007
57514.52825 	 57514.52888 	 89.959 	 0.005
57514.52888 	 57514.52950 	 89.936 	 0.007
57514.52950 	 57514.53012 	 89.932 	 0.007
57514.53012 	 57514.53075 	 -89.450 	 0.006

相關文章