Demo entry 6761929

3S2

   

Submitted by anonymous on Oct 08, 2018 at 11:04
Language: Python. Code size: 3.2 kB.

from arcpy import *
from dateutil.parser import parse
env.workspace='F:\\3S\\practice2\\GK\\tax_trace.gdb'
env.overwriteOutput=True
sr1=SpatialReference("WGS 1984")        #设置轨迹点坐标系统和应该转换为的投影坐标系统
sr2=SpatialReference("Xian 1980 GK CM 123E")


f=open('F:\\3S\\practice2\\GK\\26565.txt','r')

up,down=[],[]    #存放上下车点的轨迹记录
n='1'          #存储最开始的空车状态 1
ID=0
s=0

#'''将上车点、下车点转换为单独的Point几何对象'''
array_all = arcpy.Array()
list_up=[]
list_down=[]
up_time=[]
down_time=[]
timeUSE=[]

for itrace in f.readlines():
    ID+=1
    j=itrace.split(',')
    pnt1 = Point(float(j[8]),float(j[9]))
    array_all.add(pnt1)
    
    m=j[2]                              #与之前一条记录是否载客作对比,如果状态不同,则将之后的一条轨迹记录下来
    if m!=n:
        if m=='0':                      #将每一个上车点作为单独的Point对象进行存储
            up.append([ID,j[2],j[6],j[8],j[9]])
            pnt2 = Point(float(j[8]),float(j[9]))
            PG2=PointGeometry(pnt2,sr1)
            list_up.append(PG2)
        else:                           #将每一个下车点作为单独的Point对象进行存储
            down.append([ID,j[2],j[6],j[8],j[9]])
            pnt3 = Point(float(j[8]),float(j[9]))
            PG3=PointGeometry(pnt3,sr1)
            list_down.append(PG3)
            
        n=m
        s+=1
        #'''计算每次运载的时间'''
        if s%2==0:
            a=parse(up[int(s/2-1)][2])
            b=parse(down[int(s/2-1)][2])
            sec=(b-a).total_seconds()
            hh=sec//3600
            mm=(sec-hh*3600)//60
            ss=sec-hh*3600-mm*60            #将sec转换成---时:分:秒的形式
            up_time.append(up[int(s/2-1)][2][11:19])
            down_time.append(down[int(s/2-1)][2][11:19])
            timeUSE.append('%d:%d:%d'%(hh,mm,ss))

#'''对所有轨迹点、上车点、下车点进行输出为feature,投影转换的操作'''
point_all= Multipoint(array_all,sr1)

CopyFeatures_management(point_all,'tax_trace')
Project_management('tax_trace','tax_trace_prj',sr2)

CopyFeatures_management(list_up,'tax_up')
Project_management('tax_up','tax_up_prj',sr2)

CopyFeatures_management(list_down,'tax_down')
Project_management('tax_down','tax_down_prj',sr2)


arcpy.Delete_management('tax_trace')            #删除多余文件
arcpy.Delete_management('tax_up')
arcpy.Delete_management('tax_down')

#'''返回shp文件的字段名和几何要素'''
fields=['NAME','SHAPE@']
cur_shp=da.SearchCursor('GK',fields)
cur_up=da.SearchCursor('tax_up_prj','SHAPE@')
cur_down=da.SearchCursor('tax_down_prj','SHAPE@')

#将行政区划图中,每一个单独的几何对象和其所对应的NAME字段进行存储
NAME_shp,GEOM_shp=[],[]
for irow_shp in cur_shp:
    NAME_shp.append(irow_shp[0])
    GEOM_shp.append(irow_shp[1])
    
NAME_up=[]          #存放上车点、下车点所在区名的列表
NAME_down=[]

#利用within对'多边形几何对象'和'点几何对象'进行空间位置的判断
for irow_up in cur_up:
    for i in range(0,16):
        if irow_up[0].within(GEOM_shp[i])==True:
            NAME_up.append(NAME_shp[i])

        
for irow_down in cur_down:
    for i in range(0,16):
        if irow_down[0].within(GEOM_shp[i])==True:
            NAME_down.append(NAME_shp[i])
            
#格式化输出结果            
print('载客次数:',len(up))

result_list=[]
for i in range(0,len(up)):
    print NAME_up[i],NAME_down[i],up_time[i],down_time[i],timeUSE[i]


f.close()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).