Code of CarSharing

Data description

The research data of station-based car sharing in Beijing are from July 1st, 2017 to September 30th, 2017. The data includes three parts:

Table 1.Order data field and description.

Data field Data field
Order ID 17070210112314087756
Pick-up time stamp 2017/09/26 22 40 00
Drop-off time stamp 2017/09/27 01 20 00
Pick-up station The ground parking lot of Taishan Hotel
Drop-off station The ground parking lot of Taishan Hotel

Table 2. The data fields of a tracking record

Data field Data field
Order ID 17070210112314087756
Time stamp 2017/09/26 22 40 05
Longitude 116.400632°E
Latitude 39.990488°N
Instantaneous speed 12.2 km/h

Table 3. The information about car sharing stations

Name Longitude Latitude
The ground parking lot of Taishan Hotel 116.335795°E 40.055445°N
The parking lot of Wenjin International Apartment 116.333669°E 39.998997°N
The ground parking lot of LOFTEL 116.473679°E 40.003319°N

code

requirements:

import pandas as pd
import os
import datetime
import time
import requests

get longitude:

def get_lon(traj_line):
    return traj_line.split(',')[2]

get Latitude:

def get_lat(traj_line):
    return traj_line.split(',')[3]

get timestamp:

def get_time(traj_line):
    return traj_line.split(',')[1]

get speed:

def get_speed(traj_line):
    return float(traj_line.split(',')[4])*0.277#转化为米每秒

Calculates the small distance between two timestamps:

def get_traj_s(this_time, next_time, next_speed):
    traj_time=get_traj_time(this_time, next_time)
    return traj_time * next_speed

Calculates the time difference between two timestamps:

def get_traj_time(this_time, next_time):
    this_time=datetime.datetime.strptime(this_time, "%Y-%m-%d %H:%M:%S")
    next_time=datetime.datetime.strptime(next_time, "%Y-%m-%d %H:%M:%S")
    return (next_time - this_time).seconds

Stop for no more than one day, and judge whether the start and end time of the pause spans the dark and the daytime.:

def judge_one_day_beg_end_time(beg_time,end_time):
    '''
    首先确定开始时间,在确定结束时间,分情况计算
    :param beg_time:停住点开始时间
    :param end_time:停住点结束时间
    :return: [day_time,night_time]
    '''
    day_time_beg=datetime.datetime.strptime('07:00:00', "%H:%M:%S")#白天开始时间
    day_time_end=datetime.datetime.strptime('19:00:00', "%H:%M:%S")#白天结束时间
    day_time=0
    night_time=0
    #只在一天内,仅考虑小时分钟秒
    beg_time = datetime.datetime.strptime(beg_time.split(' ')[1], "%H:%M:%S")
    end_time = datetime.datetime.strptime(end_time.split(' ')[1], "%H:%M:%S")
    if beg_time.hour<7:#开始时长在七点以前
        if end_time.hour<7:#结束时间也为七点以前,全是晚上时间
            night_time = (end_time-beg_time).seconds
            print(night_time)
        if 7<=end_time.hour<19:#结束时间在7点以后但在19.00之前
            night_time = (day_time_beg - beg_time).seconds
            day_time = (end_time-day_time_beg).seconds
        if end_time.hour>=19:#结束时间在19点之后
            day_time=12*60*60
            night_time=(day_time_beg - beg_time).seconds+(end_time-day_time_end).seconds
    if 7<=beg_time.hour<19:#开始时间在7点到19点之前
        if 7<=end_time.hour<19:#结束时间在7点到19点之前
            day_time=(end_time-beg_time).seconds
        if end_time.hour>=19:#结束时间在19点以后
            day_time=(day_time_end-beg_time).seconds
            night_time=(end_time-day_time_end).seconds
    if beg_time.hour>=19:#开始时间在19点之后
        print(beg_time,end_time)
        night_time=(end_time-beg_time).seconds
        print(night_time)
    print(day_time,night_time)
    return [day_time,night_time]

Determine whether the start time and end time of the pause span between dark and daytime, considering that the pause duration is longer than one day:

def judge_beg_end_time(beg_time,end_time):
    '''
    首先判断停驻时间有没有跨天,没有跨天先直接运算,跨天进行拆分计算
    :param beg_time:停住点开始时间
    :param end_time:停住点结束时间
    :return:[day_time,night_time]
    '''
    day_time = 0
    night_time = 0
    beg_time_day = beg_time.split(' ')[0].split('-')[2]
    end_time_day = end_time.split(' ')[0].split('-')[2]
    if end_time_day==beg_time_day:#没有跨天
        print('------同一天--------')
        print(beg_time,end_time)
        day_time,night_time=judge_one_day_beg_end_time(beg_time,end_time)
        print('--------------------')
    else:#跨天了
        print('---------跨天了--------')
        print(beg_time.split(' ')[0],end_time.split(' ')[0])
        day_list=[i for i in range(0,(datetime.datetime.strptime(end_time.split(' ')[0], "%Y-%m-%d")-datetime.datetime.strptime(beg_time.split(' ')[0], "%Y-%m-%d")).days+1)]
        print(day_list)
        print(beg_time,beg_time.split(' ')[0] + ' 23:59:59')
        first_day_time = judge_one_day_beg_end_time(beg_time, beg_time.split(' ')[0] + ' 23:59:59')
        print(end_time.split(' ')[0] + ' 00:00:01',end_time)
        last_day_time = judge_one_day_beg_end_time(end_time.split(' ')[0] + ' 00:00:01', end_time)
        day_time = first_day_time[0] + last_day_time[0]+(len(day_list)-2)*12*60*60
        night_time = first_day_time[1] + last_day_time[1]+(len(day_list)-2)*12*60*60
        print(day_time,night_time)
        print('-----------------------')
    return (day_time,night_time)

Gets administrative zones according to latitude and longitude:

def get_region(lat,lon):
    address = lat+','+lon
    url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak=IroxpcdGFHGYIlbOhQQVMptRHmLqxBFa&output=json&coordtype=wgs84ll&location=' + str(address)
    try:
        response=requests.get(url)
        if response.content:
            answer = response.json()
            return answer['result']['addressComponent']['district']
        else:
            print('---空数据---')
            return '空地区'
    except Exception as e:
        print(e)
        print('未知异常')

main:

for traj_file in os.listdir(traj_file_path):
    # print(traj_file)
    with open(traj_file_path+traj_file) as traj_file_read:
        if traj_file!='17072712213417435564.txt':
            continue
        traj_file_id=traj_file.split('.')[0]
        beg_time='def_beg_time'
        end_time='def_end_time'
        stop_point_N=0
        all_stop_time=0
        stop_time=0
        S=0#里程
        traj_file_lines = traj_file_read.readlines()
        for index in range(0,len(traj_file_lines)-1,1):#构造索引并遍历
            if index<len(traj_file_lines)-2:#只计算到倒数第二行
                traj_file_this_line = traj_file_lines[index].strip('\n')#去除\n  当前行
                traj_file_next_line = traj_file_lines[index+1].strip('\n')#去除\n   下一行
                this_time=get_time(traj_file_this_line)#当前行时间
                next_time=get_time(traj_file_next_line)#下一行时间
                this_speed=get_speed(traj_file_this_line)#当前行速度
                next_speed=get_speed(traj_file_next_line)#下一行速度
                S=S+get_traj_s(this_time,next_time,next_speed)#累加距离
                #停住点
                if this_speed==0 and next_speed==0:#当前行下一行速度为0
                    if beg_time=='def_beg_time':
                        beg_time=this_time
                    stop_time+=get_traj_time(this_time,next_time)
                if this_speed==0 and next_speed!=0:#当前行为0 下一行不为0
                    end_time=this_time
                    if stop_time>=300:#记录
                        day_time,night_time=judge_beg_end_time(beg_time,end_time)
                        with open('./stop_data/'+traj_file_id+'stop_point.txt','a') as stop_write:
                            this_lon = get_lon(traj_file_this_line)
                            this_lat = get_lat(traj_file_this_line)
                            region='get_region(this_lat,this_lon)'
                            stop_write.write(str(stop_time)+','+str(this_lon)+','+str(this_lat)+','+beg_time+','+end_time+','+str(day_time/60)+','+str(night_time/60)+','+region+'\n')
                        all_stop_time+=stop_time
                        stop_point_N+=1
                    stop_time=0
                    beg_time = 'def_beg_time'
                    end_time = 'def_end_time'
        S=S/1000
        with open('./id_S.txt','a') as S_write:
            S_write.write(traj_file_id+','+str(S)+','+str(stop_point_N)+','+str(all_stop_time/3600)+'\n')