Repository navigation
Expand file tree
/
Copy pathgrid.py
More file actions
81 lines (65 loc) · 2.72 KB
/
Copy pathgrid.py
File metadata and controls
81 lines (65 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import numpy as np
import sys
from base import *
from distanceUtils import *
class trajGrid:
""" A class representing a simple grid to index trajectory points.
Atrributes:
data { (int,int): {int: [int]} }: dictionary mapping a grid cell
to a dictionary containing traj points lying in the cell.
startLat (float): smallest y-value of indexed points.
startLon (float): smallest x-value of indexed points.
delta (float): size of grid cell.
numX (int): number of cells in the x-direction.
numY (int): number of cells in the y-direction.
"""
def __init__(self, data, startLat, startLon, delta, numX, numY):
""" Simple initialization of class. """
self.data = data
self.startLat = startLat
self.startLon = startLon
self.delta = delta
self.xCells = numX
self.yCells = numY
def gridData(trajs, simpTrajs, delta):
""" Create an index of trajectory points.
Args:
trajs ({int : traj}): dictionary of traj objects.
simpTajs ([simpleTraj]): list of simplified trajectories whose points
are to be indexed.
delta (float): size of grid cell.
Returns:
trajGrid object containing the points feom the simplified trajectories.
"""
# Compute the x and y extent of the points.
simpTraj = simpTrajs[0]
trID = simpTraj.trajID
p = trajs[trID].pts[simpTraj.indices[0]]
xMin, yMin = p.lon, p.lat
xMax, yMax = xMin, yMin
for simpTraj in simpTrajs:
trID = simpTraj.trajID
for index in simpTraj.indices:
p = trajs[trID].pts[index]
xMin, xMax = min(xMin, p.lon), max(xMax, p.lon)
yMin, yMax = min(yMin, p.lat), max(yMax, p.lat)
# Initialize grid which is a dictionary mapping from the cell (xIndex, yIndex)
# to the contents of the cell.
# grid is of the form {(xIndex, yIndex) : {trajID : [indices of points in simpTraj]}}.
xCells = int(np.ceil((xMax-xMin)/delta))
yCells = int(np.ceil((yMax-yMin)/delta))
grid = {}
for simpTraj in simpTrajs:
trID = simpTraj.trajID
for index in simpTraj.indices:
p = trajs[trID].pts[index]
xCell = int(np.floor((p.lon - xMin)/delta))
yCell = int(np.floor((p.lat - yMin)/delta))
# Make sure cell exists.
if ((xCell, yCell) in grid) is False:
grid[(xCell, yCell)] = {}
# Add trajID to cell if not there.
if (trID in grid[(xCell, yCell)]) is False:
grid[(xCell, yCell)][trID] = []
grid[(xCell, yCell)][trID].append(index)
return trajGrid(grid, yMin, xMin, delta, xCells, yCells)