Exemplo n.º 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)
Exemplo n.º 3
0
    def BFSOneTreeMap(self,
                      parentNode,
                      recordNum=0,
                      treeQueue=[],
                      currentDis=0):
        cateName = self.currentCateName

        self.treeNodesID += 1

        for each in treeQueue:
            id = "%d-%s" % (each[-4], each[-1])
            if id not in self.keepTreeStructList[cateName]:
                self.keepTreeStructList[cateName].append(id)

        queue = []
        # parentNRN = parentNode[4]
        # nothing = True

        # 六个交点计算,得出三个 gid,然后匹配方向加入 queue
        point = [parentNode[0], parentNode[1], parentNode[-4]]
        direction = [parentNode[5], parentNode[6]]
        tmpStepRes = self.getNextGIDs(point, direction)
        gids = tmpStepRes['res']
        intersectionPoint = tmpStepRes['endPoints']
        # originGid = tmpStepRes['originGid']
        queue += self.getNextDirections(gids, parentNode)
        queueCopy = queue[:]

        res = []

        # BFS Looping Condition
        while queue:
            vertex = queue.pop(0)

            gidStr = str(vertex[-4])
            nodeID = vertex[-1]

            treeStructID = "%s-%s" % (gidStr, nodeID)
            if treeStructID in self.keepTreeStructList[cateName]:
                continue

            # 距离判断 START
            dis = currentDis + getRealDistance(parentNode[0], parentNode[1],
                                               vertex[0], vertex[1])
            if dis > self.custom_params['max_distance'] * 1000:
                continue
            # 距离判断 END

            self.treeNodesID += 1
            self.currentData[cateName]['count'] += 1

            subres = {
                "root": {
                    "id": self.treeNodesID,
                    "lng": vertex[0],
                    "lat": vertex[1],
                    "num": parentNode[4],
                    "speed": parentNode[3],
                    "dis": dis
                },
                "children": []
            }

            node = self.deleteNode(gidStr, nodeID)
            childs = self.BFSOneTreeMap(vertex, vertex[4], queueCopy, dis)

            # nothing = False
            subres['children'] = childs
            res.append(subres)

        if len(res) == 0:
            self.treeNodesID += 1
            self.currentData[cateName]['count'] += 1
            res.append({
                "root": {
                    "id":
                    self.treeNodesID,
                    "lng":
                    intersectionPoint[0],
                    "lat":
                    intersectionPoint[1],
                    "num":
                    parentNode[4],
                    "speed":
                    parentNode[3],
                    "dis":
                    currentDis +
                    getRealDistance(parentNode[0], parentNode[1],
                                    intersectionPoint[0], intersectionPoint[1])
                }
            })
        # result
        # if nothing:
        # 	del res['children']

        return res