Пример #1
0
    def iterateFile(self, file):
        print "Delta for current running %f" % self.delta
        count = 0
        with open(file, 'rb') as f:
            firstLine = True
            currentNo = -1
            fromLat = -1
            fromLng = -1
            fromTime = -1

            for line in f:
                count += 1
                line = line.strip('\n')
                linelist = line.split(',')

                # 旅程标识
                no = "%s-%s-%s-%s" % (linelist[5], linelist[6], linelist[8],
                                      linelist[9])
                toLat = linelist[3]
                toLng = linelist[4]
                toTime = int(linelist[2])

                if firstLine:  # 第一行初始化
                    firstLine = False
                    currentNo = no
                    fromLat = toLat
                    fromLng = toLng
                    fromTime = toTime
                else:
                    if currentNo == no:  # 同一段旅程
                        # 如果当前点位置不变则继续遍历
                        if (fromLat == toLat
                                and fromLng == toLng) or fromTime == toTime:
                            continue

                        fPoint = [float(fromLng), float(fromLat)]
                        tPoint = [float(toLng), float(toLat)]

                        fromGid = getFormatGID(fPoint)['gid']
                        toGid = getFormatGID(tPoint)['gid']
                        distance = getRealDistance(fromLng, fromLat, toLng,
                                                   toLat)
                        speed = distance / (toTime - fromTime)
                        direction = getDirection(fPoint,
                                                 tPoint)  # w n s e 四个字符之一

                        self.updateResByLine(fPoint, tPoint, fromGid, toGid,
                                             direction, speed)

                        fromLat = toLat
                        fromLng = toLng
                        fromTime = toTime
                    else:  # 新旅程第一个点
                        currentNo = no
                        fromLat = toLat
                        fromLng = toLng
                        fromTime = toTime

        f.close()
        print "Total %d records in this file." % (count)
    def iterateFileNew(self, file):
        print "Delta for current running %f" % self.delta
        count = 0
        with open(file, 'rb') as f:
            firstLine = True

            deviceDirectionDict = {}
            # deviceDirectionToDict = {}

            for line in f:
                count += 1
                line = line.strip('\n')
                linelist = line.split(',')
                #print(getRealDistance(linelist[5], linelist[6], linelist[4], linelist[3])/(linelist[2] - linelist[7]))
                currentDevice = linelist[1]
                if firstLine:
                    firstLine = False

                if not deviceDirectionDict.has_key(currentDevice):
                    deviceDirectionDict[currentDevice] = {}

                # if not deviceDirectionToDict.has_key(currentDevice):
                # 	deviceDirectionToDict[currentDevice] = {}

                # left
                fromLat = linelist[6]
                fromLng = linelist[5]
                fromTime = int(linelist[7])

                toLat = linelist[3]
                toLng = linelist[4]
                toTime = int(linelist[2])

                if (fromLat == toLat
                        and fromLng == toLng) or fromTime == toTime:
                    continue

                fPoint = [float(fromLng), float(fromLat)]
                tPoint = [float(toLng), float(toLat)]

                fromGid = getFormatGID(fPoint, self.LngSPLIT, self.LatSPLIT,
                                       self.locs)['gid']
                toGid = getFormatGID(tPoint, self.LngSPLIT, self.LatSPLIT,
                                     self.locs)['gid']
                distance = getRealDistance(fromLng, fromLat, toLng, toLat)
                speed = distance / (toTime - fromTime)
                direction = getDirection(fPoint, tPoint)  # w n s e 四个字符之一

                position = fromLat + '-' + fromLng

                if not deviceDirectionDict[currentDevice].has_key(position):
                    # 当前位置没有被记录过, 那么要记录当前位置的方向。
                    deviceDirectionDict[currentDevice][position] = True
                    self.updateResByLine(fPoint, tPoint, fromGid, toGid,
                                         direction, speed)

                # right
                fromLat = toLat
                fromLng = toLng
                fromTime = toTime

                toLat = linelist[9]
                toLng = linelist[8]
                toTime = int(linelist[10])

                if (fromLat == toLat
                        and fromLng == toLng) or fromTime == toTime:
                    continue

                fPoint = [float(fromLng), float(fromLat)]
                tPoint = [float(toLng), float(toLat)]

                fromGid = getFormatGID(fPoint, self.LngSPLIT, self.LatSPLIT,
                                       self.locs)['gid']
                toGid = getFormatGID(tPoint, self.LngSPLIT, self.LatSPLIT,
                                     self.locs)['gid']
                distance = getRealDistance(fromLng, fromLat, toLng, toLat)
                speed = distance / (toTime - fromTime)
                direction = getDirection(fPoint, tPoint)  # w n s e 四个字符之一

                position = fromLat + '-' + fromLng

                if not deviceDirectionDict[currentDevice].has_key(position):
                    # 当前位置没有被记录过, 那么要记录当前位置的方向。

                    deviceDirectionDict[currentDevice][position] = True
                    self.updateResByLine(fPoint, tPoint, fromGid, toGid,
                                         direction, speed)

                # position = toLat + '-' + toLng
                # if not deviceDirectionToDict[currentDevice].has_key(position):
                # 	deviceDirectionDict[currentDevice][position] = [fPoint, tPoint, fromGid, toGid, direction, speed, 'to', distance]
                # else:
                # 	if deviceDirectionDict[currentDevice][position][7] < distance:
                # 		deviceDirectionDict[currentDevice][position] =  [fPoint, tPoint, fromGid, toGid, direction, speed, 'to', distance]

        f.close()
        print "Total %d records in this file." % (count)
Пример #3
0
# -*- coding: utf-8 -*-
__author__ = 'lenovo'

from util.tripFlow.base import getFormatGID
from util.tripFlow.base import getDirection
from util.tripFlow.extractGridEdges import ExtractGridEdges

tPoint = [161.12, 40.35]
fPoint = [161.57, 39.79]

PROP = {
    'index': 9,
    'delta': -1,
    'IDIRECTORY': '/datahouse',
    'ODIRECTORY': '/datahouse',
    'suffix': 1
}
task = ExtractGridEdges(PROP)
direction = getDirection(fPoint, tPoint)
fromGid = getFormatGID(fPoint)['gid']
toGid = getFormatGID(tPoint)['gid']
print(direction)
speed = 2.3
task.updateResByLine(fPoint, tPoint, fromGid, toGid, direction, speed)

print(task.resByCate['from'])
print(task.resByCate['to'])

#证明from和to的方向是一样的