Exemplo n.º 1
0
 def __init__(
     self,
     track,
     res=1,
     buffer=10,
     prop=0.5,
     length=0,
     time=None,
     mode=MODE_PARALLEL,
     type=TYPE_SELECT,
 ):
     """TODO"""
     self.track = track
     self.time = time
     self.type = type
     self.prop = prop
     self.mode = mode
     self.length = length
     self.segments = []
     for i in range(1, len(track), 1):
         pt1 = track[i].position.copy()
         pt2 = pt1.copy()
         dx = track[i].position.getX() - track[i - 1].position.getX()
         dy = track[i].position.getY() - track[i - 1].position.getY()
         R = (dx * dx + dy * dy)**(0.5)
         if R == 0:
             continue
         pt1.translate(+buffer * dy / R, -buffer * dx / R)
         pt2.translate(-buffer * dy / R, +buffer * dx / R)
         self.segments.append(Track([Obs(pt1), Obs(pt2)]))
Exemplo n.º 2
0
 def test_create_index(self):
     
     GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
     
     track = Track()
     p1 = Obs(ENUCoords(550, 320), GPSTime.readTimestamp('2020-01-01 10:00:00'))
     track.addObs(p1)
     p2 = Obs(ENUCoords(610, 325), GPSTime.readTimestamp('2020-01-01 10:08:00'))
     track.addObs(p2)
     p3 = Obs(ENUCoords(610, 330), GPSTime.readTimestamp('2020-01-01 10:17:00'))
     track.addObs(p3)
     p4 = Obs(ENUCoords(650, 330), GPSTime.readTimestamp('2020-01-01 10:21:00'))
     track.addObs(p4)
     p5 = Obs(ENUCoords(675, 340), GPSTime.readTimestamp('2020-01-01 10:25:00'))
     track.addObs(p5)
     #track.plot()
     #track.plotAsMarkers()
     
     TRACES = []
     TRACES.append(track)
     collection = TrackCollection(TRACES)
     
     res = (25, 4)
     index = SpatialIndex(collection, res, 0.05, True)
     index.plot()
     
     
     # =====================================================================
     self.assertEqual(index.request(0, 0), [0])
     self.assertEqual(index.request(1, 0), [0])
     self.assertEqual(index.request(0, 1), [])
     self.assertEqual(index.request(1, 1), [0])
     self.assertEqual(index.request(2, 0), [])
     self.assertEqual(index.request(2, 1), [0])
Exemplo n.º 3
0
 def test_write_csv_path(self):
     
     GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
     track = Track()
     p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
     track.addObs(p1)
     p2 = Obs(ENUCoords(0, 1), GPSTime.readTimestamp('2020-01-01 10:00:01'))
     track.addObs(p2)
     p3 = Obs(ENUCoords(1, 1), GPSTime.readTimestamp('2020-01-01 10:00:02'))
     track.addObs(p3)
     p4 = Obs(ENUCoords(1, 2), GPSTime.readTimestamp('2020-01-01 10:00:03'))
     track.addObs(p4)
     p5 = Obs(ENUCoords(2, 2), GPSTime.readTimestamp('2020-01-01 10:00:04'))
     track.addObs(p5)
     
     csvpath = os.path.join(self.resource_path, 'data/test/test_write_csv_path.wkt')
     FileWriter.writeToFile(track, csvpath)
     contents = open(csvpath).read()
     
     txt  = "0.000,0.000,0.000,01/01/2020 10:00:00\n"
     txt += "0.000,1.000,0.000,01/01/2020 10:00:01\n"
     txt += "1.000,1.000,0.000,01/01/2020 10:00:02\n"
     txt += "1.000,2.000,0.000,01/01/2020 10:00:03\n"
     txt += "2.000,2.000,0.000,01/01/2020 10:00:04\n"
     self.assertEqual(contents.strip(), txt.strip())
Exemplo n.º 4
0
 def test_write_csv_2AF_desordre(self):
     
     GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
     track = Track()
     p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
     track.addObs(p1)
     p2 = Obs(ENUCoords(0, 1), GPSTime.readTimestamp('2020-01-01 10:00:01'))
     track.addObs(p2)
     p3 = Obs(ENUCoords(1, 1), GPSTime.readTimestamp('2020-01-01 10:00:02'))
     track.addObs(p3)
     p4 = Obs(ENUCoords(1, 2), GPSTime.readTimestamp('2020-01-01 10:00:03'))
     track.addObs(p4)
     p5 = Obs(ENUCoords(2, 2), GPSTime.readTimestamp('2020-01-01 10:00:04'))
     track.addObs(p5)
     
     track.addAnalyticalFeature(Analytics.speed)
     track.compute_abscurv()
     
     csvpath = os.path.join(self.resource_path, 'data/test/test_write_csv_2AF_desordre.wkt')
     af_names = ['speed', 'abs_curv']
     FileWriter.writeToFile(track, csvpath, id_E=3, id_N=2, id_U=0, id_T=1, h=1, 
                            separator=";", af_names=af_names)
     contents = open(csvpath).read()
     
     txt  = "#srid: ENU\n"
     txt += "#U;time;N;E;speed;abs_curv\n"
     txt += "0.000;01/01/2020 10:00:00;0.000;0.000;1.0;0\n"
     txt += "0.000;01/01/2020 10:00:01;1.000;0.000;0.7071067811865476;1.0\n"
     txt += "0.000;01/01/2020 10:00:02;1.000;1.000;0.7071067811865476;2.0\n"
     txt += "0.000;01/01/2020 10:00:03;2.000;1.000;0.7071067811865476;3.0\n"
     txt += "0.000;01/01/2020 10:00:04;2.000;2.000;1.0;4.0\n"
     self.assertEqual(contents.strip(), txt.strip())
Exemplo n.º 5
0
	def setUp(self):
		self.track = Track()
		p1 = Obs(ENUCoords(0, 0), "2020-01-01 10:00:00")
		self.track.addObs(p1)
		p2 = Obs(ENUCoords(0, 1), "2020-01-01 10:00:01")
		self.track.addObs(p2)
		p3 = Obs(ENUCoords(1, 2), "2020-01-01 10:00:02")
		self.track.addObs(p3)
Exemplo n.º 6
0
def __mapOnNetwork(
    track, network, obs_noise=50, transition_cost=10, search_radius=50, debug=False
):
    """TODO"""

    if debug:
        f1 = open("observation.dat", "a")
        f2 = open("transition.dat", "a")

    track.createAnalyticalFeature("obs_noise", obs_noise)
    verbose = True
    global STATES
    global net
    STATES = []
    net = network
    to_run = range(len(track))
    if verbose:
        print("Map-matching preparation...")
        to_run = progressbar.progressbar(to_run)
    for i in to_run:
        STATES.append([])
        E = network.spatial_index.neighborhood(track[i].position, unit=1)
        for elem in E:
            eg = network.EDGES[network.getEdgeId(elem)].geom
            p, d, v = __projOnTrack(track[i].position, eg)
            if d < search_radius:
                STATES[-1].append(
                    (p, elem, __distToNode(eg, p, v, 0), __distToNode(eg, p, v, 1))
                )
                if debug:
                    wkt = Track([Obs(track[i].position), Obs(p)]).toWKT()
                    f1.write(str(i) + ' "' + wkt + '" ' + str(d) + "\n")

    model = Dynamics.HMM()
    model.setStates(__states)
    model.setTransitionModel(__tst_log)
    model.setObservationModel(__obs_log)
    model.estimate(
        track,
        obs=["x", "y"],
        mode=Dynamics.MODE_OBS_AS_2D_POSITIONS,
        verbose=verbose * Dynamics.MODE_VERBOSE_PROGRESS,
    )

    for k in progressbar.progressbar(range(len(track))):
        X = [track[k].position.getX(), track["hmm_inference", k][0].getX()]
        Y = [track[k].position.getY(), track["hmm_inference", k][0].getY()]
        plt.plot(X, Y, "g-")
        track[k].position.setX(track["hmm_inference", k][0].getX())
        track[k].position.setY(track["hmm_inference", k][0].getY())
Exemplo n.º 7
0
    def run_routing_backward(self, target: Union[int, Node]) -> Union[Track, None]:   
        """Computes shortest path between the source node used in forward step
        :func:`run_routing_forward` and any target node.

        If target node has not been reached during forward search, a None object is
        returned by the function.

        :param target: A target node
        :return: A track between the source node specified in
            :func:`run_routing_forward` and a target node. The track contains topologic
            and non-topologic vertices. If the node target has not been reached during
            forward step, None object is output
        """
        target = self.__correctInputNode(target)
        node = self.NODES[target]
        track = Track()
        track.addObs(Obs(node.coord))
        if node.antecedent == "":
            return None
        while (node.poids != 0) and (node.antecedent != ""):
            e = self.EDGES[node.antecedent_edge]
            edge_geom = e.geom.copy()
            if e.source != node:
                edge_geom = edge_geom.reverse()
            track = track + (edge_geom > 1)
            node = node.antecedent
        return track
Exemplo n.º 8
0
 def test_write_csv_minim(self):
     
     GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
     track = Track()
     p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
     track.addObs(p1)
     p2 = Obs(ENUCoords(0, 1), GPSTime.readTimestamp('2020-01-01 10:00:01'))
     track.addObs(p2)
     
     csvpath = os.path.join(self.resource_path, 'data/test/test_write_csv_minim.wkt')
     FileWriter.writeToFile(track, csvpath, id_E=0,id_N=1,id_U=2,id_T=3,h=1, separator=";")
     contents = open(csvpath).read()
     
     txt  = "#srid: ENU\n"
     txt += "#E;N;U;time\n"
     txt += "0.000;0.000;0.000;01/01/2020 10:00:00\n"
     txt += "0.000;1.000;0.000;01/01/2020 10:00:01\n"
     self.assertEqual(contents.strip(), txt.strip())
Exemplo n.º 9
0
def findStopsLocal(track, speed=1, duration=10):

    track = track.copy()
    stops = Track()

    # track.segmentation("speed", "#mark", speed)
    track.operate(Operator.Operator.DIFFERENTIATOR, "#mark")
    track.operate(Operator.Operator.RECTIFIER, "#mark")

    TRACES = split(track, "#mark")

    TMP_MEAN_X = []
    TMP_MEAN_Y = []
    TMP_MEAN_Z = []
    TMP_STD_X = []
    TMP_STD_Y = []
    TMP_STD_Z = []
    TMP_DURATION = []
    TMP_NBPOINTS = []

    TMP_SIGMA_X = []
    TMP_SIGMA_Y = []
    TMP_SIGMA_Z = []

    for i in range(0, len(TRACES), 2):
        if TRACES[i].duration() < duration:
            continue
        stops.addObs(
            Obs(TRACES[i].getCentroid().copy(),
                TRACES[i].getFirstObs().timestamp.copy()))
        TMP_SIGMA_X.append(TRACES[i].operate(Operator.Operator.AVERAGER, "x"))
        TMP_SIGMA_Y.append(TRACES[i].operate(Operator.Operator.AVERAGER, "y"))
        TMP_SIGMA_Z.append(TRACES[i].operate(Operator.Operator.AVERAGER, "z"))
        TMP_SIGMA_X.append(TRACES[i].operate(Operator.Operator.STDDEV, "x"))
        TMP_SIGMA_Y.append(TRACES[i].operate(Operator.Operator.STDDEV, "y"))
        TMP_SIGMA_Z.append(TRACES[i].operate(Operator.Operator.STDDEV, "z"))
        TMP_NBPOINTS.append(TRACES[i].size())
        TMP_DURATION.append(TRACES[i].duration())

    if stops.size() == 0:
        return stops

    # stops.createAnalyticalFeature("radius", TMP_RADIUS)
    stops.createAnalyticalFeature("mean_x", TMP_MEAN_X)
    stops.createAnalyticalFeature("mean_y", TMP_MEAN_Y)
    stops.createAnalyticalFeature("mean_z", TMP_MEAN_Z)
    stops.createAnalyticalFeature("sigma_x", TMP_STD_X)
    stops.createAnalyticalFeature("sigma_y", TMP_STD_Y)
    stops.createAnalyticalFeature("sigma_z", TMP_STD_Z)
    stops.createAnalyticalFeature("duration", TMP_DURATION)
    stops.createAnalyticalFeature("nb_points", TMP_NBPOINTS)

    stops.operate(Operator.Operator.QUAD_ADDER, "sigma_x", "sigma_y", "rmse")

    return stops
Exemplo n.º 10
0
 def test_create_index_collection2(self):
     
     GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
             
     track = Track()
     p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
     track.addObs(p1)
     p2 = Obs(ENUCoords(3.1, 3), GPSTime.readTimestamp('2020-01-01 10:08:00'))
     track.addObs(p2)
     p3 = Obs(ENUCoords(3.1, 4.5), GPSTime.readTimestamp('2020-01-01 10:17:00'))
     track.addObs(p3)
     
     p4 = Obs(ENUCoords(4.5, 4.5), GPSTime.readTimestamp('2020-01-01 10:21:00'))
     track.addObs(p4)
     p5 = Obs(ENUCoords(6, 5.5), GPSTime.readTimestamp('2020-01-01 10:21:00'))
     track.addObs(p5)
     
     p6 = Obs(ENUCoords(7, 4.5), GPSTime.readTimestamp('2020-01-01 10:21:00'))
     track.addObs(p6)
     p7 = Obs(ENUCoords(11, 5.5), GPSTime.readTimestamp('2020-01-01 10:21:00'))
     track.addObs(p7)
     p8 = Obs(ENUCoords(13, 10), GPSTime.readTimestamp('2020-01-01 10:25:00'))
     track.addObs(p8)
             #track.plot()
             #track.plotAsMarkers()
             
     TRACES = []
     TRACES.append(track)
     collection = TrackCollection(TRACES)
             
     index = SpatialIndex(collection, (2, 2))
     index.plot()
     
     # =====================================================================
     # =====================================================================
     self.assertEqual(index.request(0, 0), [0])
     self.assertEqual(index.request(1, 0), [0])
     self.assertEqual(index.request(0, 1), [])
     self.assertEqual(index.request(1, 1), [0])
     self.assertEqual(index.request(2, 0), [])
     self.assertEqual(index.request(2, 1), [])
     self.assertEqual(index.request(1, 2), [0])
     self.assertEqual(index.request(2, 2), [0])
     self.assertEqual(index.request(3, 2), [0])
     self.assertEqual(index.request(3, 3), [])
     self.assertEqual(index.request(4, 2), [0])
     self.assertEqual(index.request(4, 3), [])
     self.assertEqual(index.request(4, 4), [])
     self.assertEqual(index.request(5, 2), [0])
     self.assertEqual(index.request(5, 3), [0])
     self.assertEqual(index.request(5, 4), [])
Exemplo n.º 11
0
def wktLineStringToObs(wkt, srid):
    """
    Une polyligne de n points est modélisée par une Track (timestamp = 1970/01/01 00 :00 :00)
        Cas LINESTRING()
    """

    # Creation d'une liste vide
    TAB_OBS = list()

    # Separation de la chaine
    coords_string = wkt.split("(")
    coords_string = coords_string[1]
    coords_string = coords_string.split(")")[0]

    coords = coords_string.split(",")

    for i in range(0, len(coords)):
        sl = coords[i].strip().split(" ")
        x = float(sl[0])
        y = float(sl[1])
        if len(sl) == 3:
            z = float(sl[2])
        else:
            z = 0.0

        if not srid.upper() in [
                "ENUCOORDS",
                "ENU",
                "GEOCOORDS",
                "GEO",
                "ECEFCOORDS",
                "ECEF",
        ]:
            print("Error: unknown coordinate type [" + str(srid) + "]")
            exit()

        if srid.upper() in ["ENUCOORDS", "ENU"]:
            point = ENUCoords(x, y, z)
        if srid.upper() in ["GEOCOORDS", "GEO"]:
            point = GeoCoords(x, y, z)
        if srid.upper() in ["ECEFCOORDS", "ECEF"]:
            point = ECEFCoords(x, y, z)

        TAB_OBS.append(Obs(point, GPSTime()))

    return TAB_OBS
Exemplo n.º 12
0
    def setUp(self):

        GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")

        self.track = Track()
        p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.track.addObs(p1)
        p2 = Obs(ENUCoords(0, 1), GPSTime.readTimestamp('2020-01-01 10:00:01'))
        self.track.addObs(p2)
        p3 = Obs(ENUCoords(1, 1), GPSTime.readTimestamp('2020-01-01 10:00:02'))
        self.track.addObs(p3)
        p4 = Obs(ENUCoords(1, 2), GPSTime.readTimestamp('2020-01-01 10:00:03'))
        self.track.addObs(p4)
        p5 = Obs(ENUCoords(2, 2), GPSTime.readTimestamp('2020-01-01 10:00:04'))
        self.track.addObs(p5)
        p6 = Obs(ENUCoords(2, 3), GPSTime.readTimestamp('2020-01-01 10:00:06'))
        self.track.addObs(p6)
        p7 = Obs(ENUCoords(3, 3), GPSTime.readTimestamp('2020-01-01 10:00:08'))
        self.track.addObs(p7)
        p8 = Obs(ENUCoords(3, 4), GPSTime.readTimestamp('2020-01-01 10:00:10'))
        self.track.addObs(p8)
        p9 = Obs(ENUCoords(4, 4), GPSTime.readTimestamp('2020-01-01 10:00:12'))
        self.track.addObs(p9)
Exemplo n.º 13
0
    def readFromGpx(path, srid="GEO"):
        """
        Reads (multiple) tracks in .gpx file
        """

        tracks = TrackCollection()

        format_old = GPSTime.getReadFormat()
        GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")

        doc = minidom.parse(path)

        trks = doc.getElementsByTagName("trk")

        for trk in trks:
            trace = t.Track()
            trkpts = trk.getElementsByTagName("trkpt")
            for trkpt in trkpts:
                lon = float(trkpt.attributes["lon"].value)
                lat = float(trkpt.attributes["lat"].value)

                hgt = utils.NAN
                eles = trkpt.getElementsByTagName("ele")
                if eles.length > 0:
                    hgt = float(eles[0].firstChild.data)

                time = ""
                times = trkpt.getElementsByTagName("time")
                if times.length > 0:
                    time = GPSTime(times[0].firstChild.data)
                else:
                    time = GPSTime()

                point = Obs(utils.makeCoords(lon, lat, hgt, srid), time)
                trace.addObs(point)

            tracks.addTrack(trace)

        # pourquoi ?
        # --> pour remettre le format comme il etait avant la lectre :)   
        GPSTime.setReadFormat(format_old)

        collection = TrackCollection(tracks)
        return collection
Exemplo n.º 14
0
def tabCoordsLineStringToObs(coords, srid):
    """TODO"""

    # Creation d'une liste vide
    TAB_OBS = list()

    for i in range(0, len(coords)):
        sl = coords[i]
        x = float(sl[0])
        y = float(sl[1])
        if len(sl) == 3:
            z = float(sl[2])
        else:
            z = 0.0

        if not srid.upper() in [
                "ENUCOORDS",
                "ENU",
                "GEOCOORDS",
                "GEO",
                "ECEFCOORDS",
                "ECEF",
        ]:
            print("Error: unknown coordinate type [" + str(srid) + "]")
            exit()

        if srid.upper() in ["ENUCOORDS", "ENU"]:
            point = ENUCoords(x, y, z)
        if srid.upper() in ["GEOCOORDS", "GEO"]:
            point = GeoCoords(x, y, z)
        if srid.upper() in ["ECEFCOORDS", "ECEF"]:
            point = ECEFCoords(x, y, z)

        TAB_OBS.append(Obs(point, GPSTime()))

    return TAB_OBS
Exemplo n.º 15
0
def findStopsGlobal(track,
                    diameter=20,
                    duration=60,
                    downsampling=1,
                    verbose=True):
    """Find stop points in a track based on two parameters:
    Maximal size of a stop (as the diameter of enclosing circle,
    in ground units) and minimal time duration (in seconds)
    Use downsampling parameter > 1 to speed up the process"""

    # If down-sampling is required
    if downsampling > 1:
        track = track.copy()
        track **= track.size() / downsampling

    # ---------------------------------------------------------------------------
    # Computes cost matrix as :
    #    Cij = 0 if size of enclosing circle of pi, pi+1, ... pj-1 is > diameter
    #    Cij = 0 if time duration between pi and p-1 is < duration
    #    Cij = (j-i)**2 = square of the number of points of segment otherwise
    # ---------------------------------------------------------------------------
    C = np.zeros((track.size(), track.size()))
    RANGE = range(track.size() - 2)
    if verbose:
        print("Minimal enclosing circles computation:")
        RANGE = progressbar.progressbar(RANGE)
    for i in RANGE:
        for j in range(i + 1, track.size() - 1):
            if track[i].distance2DTo(track[j - 1]) > diameter:
                C[i, j] = 0
                break
            if track[j - 1].timestamp - track[i].timestamp <= duration:
                C[i, j] = 0
                continue
            C[i, j] = 2 * minCircle(track.extract(i, j - 1))[1]
            C[i, j] = (C[i, j] < diameter) * (j - i)**2
    C = C + np.transpose(C)

    # ---------------------------------------------------------------------------
    # Computes optimal partition with dynamic programing
    # ---------------------------------------------------------------------------
    segmentation = optimalPartition(C, MODE_SEGMENTATION_MAXIMIZE, verbose)

    stops = Track()

    TMP_RADIUS = []
    TMP_MEAN_X = []
    TMP_MEAN_Y = []
    TMP_MEAN_Z = []
    TMP_IDSTART = []
    TMP_IDEND = []
    TMP_STD_X = []
    TMP_STD_Y = []
    TMP_STD_Z = []
    TMP_DURATION = []
    TMP_NBPOINTS = []

    for i in range(len(segmentation) - 1):
        portion = track.extract(segmentation[i], segmentation[i + 1] - 1)
        C = minCircle(portion)
        if (C[1] > diameter / 2) or (portion.duration() < duration):
            continue
        stops.addObs(Obs(C[0], portion.getFirstObs().timestamp))
        TMP_RADIUS.append(C[1])
        TMP_MEAN_X.append(portion.operate(Operator.Operator.AVERAGER, "x"))
        TMP_MEAN_Y.append(portion.operate(Operator.Operator.AVERAGER, "y"))
        TMP_MEAN_Z.append(portion.operate(Operator.Operator.AVERAGER, "z"))
        TMP_STD_X.append(portion.operate(Operator.Operator.STDDEV, "x"))
        TMP_STD_Y.append(portion.operate(Operator.Operator.STDDEV, "y"))
        TMP_STD_Z.append(portion.operate(Operator.Operator.STDDEV, "z"))
        TMP_IDSTART.append(segmentation[i] * downsampling)
        TMP_IDEND.append((segmentation[i + 1] - 1) * downsampling)
        TMP_NBPOINTS.append(segmentation[i + 1] - segmentation[i])
        TMP_DURATION.append(portion.duration())

    if stops.size() == 0:
        return stops

    stops.createAnalyticalFeature("radius", TMP_RADIUS)
    stops.createAnalyticalFeature("mean_x", TMP_MEAN_X)
    stops.createAnalyticalFeature("mean_y", TMP_MEAN_Y)
    stops.createAnalyticalFeature("mean_z", TMP_MEAN_Z)
    stops.createAnalyticalFeature("id_ini", TMP_IDSTART)
    stops.createAnalyticalFeature("id_end", TMP_IDEND)
    stops.createAnalyticalFeature("sigma_x", TMP_STD_X)
    stops.createAnalyticalFeature("sigma_y", TMP_STD_Y)
    stops.createAnalyticalFeature("sigma_z", TMP_STD_Z)
    stops.createAnalyticalFeature("duration", TMP_DURATION)
    stops.createAnalyticalFeature("nb_points", TMP_NBPOINTS)

    stops.operate(Operator.Operator.QUAD_ADDER, "sigma_x", "sigma_y", "rmse")
    stops.base = track.base

    return stops
Exemplo n.º 16
0
    def setUp(self):

        #----------------------------------------------------------------------
        #   4 sommets sur axes du cercle trigonométrique
        GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")

        self.trace1 = Track()
        c1 = ENUCoords(1, 0, 0)
        p1 = Obs(c1, GPSTime.readTimestamp("2018-01-01 10:00:00"))
        self.trace1.addObs(p1)
        c2 = ENUCoords(0, 1, 0)
        p2 = Obs(c2, GPSTime.readTimestamp("2018-01-01 10:00:12"))
        self.trace1.addObs(p2)
        c3 = ENUCoords(-1, 0, 0)
        p3 = Obs(c3, GPSTime.readTimestamp("2018-01-01 10:00:40"))
        self.trace1.addObs(p3)
        c4 = ENUCoords(0, -1, 0)
        p4 = Obs(c4, GPSTime.readTimestamp("2018-01-01 10:01:50"))
        self.trace1.addObs(p4)
        self.trace1.addObs(p1)

        # ---------------------------------------------------------------------
        # Un escalier
        self.trace2 = Track()
        pm3 = Obs(ENUCoords(-2, -1),
                  GPSTime.readTimestamp('2020-01-01 09:59:44'))
        self.trace2.addObs(pm3)
        pm2 = Obs(ENUCoords(-1, -1),
                  GPSTime.readTimestamp('2020-01-01 09:59:48'))
        self.trace2.addObs(pm2)
        pm1 = Obs(ENUCoords(-1, 0),
                  GPSTime.readTimestamp('2020-01-01 09:59:55'))
        self.trace2.addObs(pm1)
        p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace2.addObs(p1)
        p2 = Obs(ENUCoords(0, 2), GPSTime.readTimestamp('2020-01-01 10:00:01'))
        self.trace2.addObs(p2)
        p3 = Obs(ENUCoords(1, 2), GPSTime.readTimestamp('2020-01-01 10:00:02'))
        self.trace2.addObs(p3)
        p4 = Obs(ENUCoords(1, 5), GPSTime.readTimestamp('2020-01-01 10:00:03'))
        self.trace2.addObs(p4)
        p5 = Obs(ENUCoords(2, 5), GPSTime.readTimestamp('2020-01-01 10:00:04'))
        self.trace2.addObs(p5)
        p6 = Obs(ENUCoords(2, 9), GPSTime.readTimestamp('2020-01-01 10:00:06'))
        self.trace2.addObs(p6)
        p7 = Obs(ENUCoords(3, 9), GPSTime.readTimestamp('2020-01-01 10:00:08'))
        self.trace2.addObs(p7)
        p8 = Obs(ENUCoords(3, 14),
                 GPSTime.readTimestamp('2020-01-01 10:00:10'))
        self.trace2.addObs(p8)
        p9 = Obs(ENUCoords(4, 14),
                 GPSTime.readTimestamp('2020-01-01 10:00:12'))
        self.trace2.addObs(p9)
        p10 = Obs(ENUCoords(4, 20),
                  GPSTime.readTimestamp('2020-01-01 10:00:15'))
        self.trace2.addObs(p10)

        # ---------------------------------------------------------------------
        #
        self.trace3 = Track()
        p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p1)
        p2 = Obs(ENUCoords(1.5, 0.5),
                 GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p2)
        p3 = Obs(ENUCoords(2, 2), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p3)
        p4 = Obs(ENUCoords(3.75, 0.6),
                 GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p4)
        p5 = Obs(ENUCoords(5, 0.5),
                 GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p5)
        p6 = Obs(ENUCoords(3.55, -0.5),
                 GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p6)
        p7 = Obs(ENUCoords(1.8, -1.2),
                 GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p7)
        p8 = Obs(ENUCoords(1, -3),
                 GPSTime.readTimestamp('2020-01-01 10:00:00'))
        self.trace3.addObs(p8)
Exemplo n.º 17
0
def noise(track,
          sigma=[1],
          kernel=[Kernel.DiracKernel()],
          distribution=DISTRIBUTION_NORMAL,
          mode='linear',
          force=False):
    """Track noising with Cholesky factorization of gaussian process covariance matrix:

    .. math::

        h(x2-x1)=\\exp-\\left(\\frac{x2-x1}{scope}\\right)^2

    If :math:`X` is a gaussian white noise, :math:`Cov(LX) = L^t*L` => if :math:`L` is a
    Cholesky factorization of a semi-postive-definite matrix :math:`S`,
    :math:`then Cov(LX) = L^T*L = S` and :math:`Y=LX`` has :math:`S` as covariance matrix.

    :param track: the track to be smoothed (input track is not modified)
    :param sigma: noise amplitude(s) (in observation coordinate units)
    :param kernel: noise autocovariance function(s)
	:param mode: 'linear' (default), 'circular' or 'euclidian'
	:param force: force definite-positive matrix with removal of negative eigen values"""

    sigma = utils.listify(sigma)
    kernel = utils.listify(kernel)

    if len(sigma) != len(kernel):
        sys.exit(
            "Error: amplitude and kernel arrays must have same size in 'noise' function"
        )

    N = track.size()
    track.compute_abscurv()

    noised_track = track.copy()

    for n in range(len(sigma)):

        SIGMA_S = utils.makeCovarianceMatrixFromKernel(kernel[n],
                                                       track,
                                                       force=force,
                                                       mode=mode)
        SIGMA_S += np.identity(N) * 1e-12
        SIGMA_S *= sigma[n]**2 / SIGMA_S[0, 0]

        # Cholesky decomposition
        L = np.linalg.cholesky(SIGMA_S)

        # Noise simulation
        if distribution == DISTRIBUTION_NORMAL:
            Xx = np.random.normal(0.0, 1.0, N)
            Xy = np.random.normal(0.0, 1.0, N)
            Xz = np.random.normal(0.0, 1.0, N)
        if distribution == DISTRIBUTION_UNIFORM:
            Xx = np.random.uniform(-1.73205, 1.73205, N)
            Xy = np.random.uniform(-1.73205, 1.73205, N)
            Xz = np.random.uniform(-1.73205, 1.73205, N)
        if distribution == DISTRIBUTION_LAPLACE:
            Xx = np.random.laplace(0.0, 0.5, N)
            Xy = np.random.laplace(0.0, 0.5, N)
            Xz = np.random.laplace(0.0, 0.5, N)
        Yx = np.matmul(L, Xx)
        Yy = np.matmul(L, Xy)
        Yz = np.matmul(L, Xz)

        # Building noised track
        for i in range(N):
            pt = noised_track.getObs(i).position
            pt.setX(pt.getX() + Yx[i])
            pt.setY(pt.getY() + Yy[i])
            pt.setZ(pt.getZ() + Yz[i])
            obs = Obs(pt, track.getObs(i).timestamp)

        if mode == 'circular':
            noised_track.loop()

    return noised_track
Exemplo n.º 18
0
    def test_create_index_collection1(self):
        
        GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
                
        track = Track()
        p1 = Obs(ENUCoords(0, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        track.addObs(p1)
        p2 = Obs(ENUCoords(2.5, 3), GPSTime.readTimestamp('2020-01-01 10:08:00'))
        track.addObs(p2)
        p3 = Obs(ENUCoords(2.5, 5), GPSTime.readTimestamp('2020-01-01 10:17:00'))
        track.addObs(p3)
        p4 = Obs(ENUCoords(7, 5), GPSTime.readTimestamp('2020-01-01 10:21:00'))
        track.addObs(p4)
        p5 = Obs(ENUCoords(10, 10), GPSTime.readTimestamp('2020-01-01 10:25:00'))
        track.addObs(p5)
                #track.plot()
                #track.plotAsMarkers()
                
        TRACES = []
        TRACES.append(track)
        collection = TrackCollection(TRACES)
                
        index = SpatialIndex(collection, (2, 2))
        index.plot()
        
        # =====================================================================
        # =====================================================================
        self.assertEqual(index.request(0, 0), [0])
        self.assertEqual(index.request(1, 0), [])
        self.assertEqual(index.request(0, 1), [0])
        self.assertEqual(index.request(1, 1), [0])
        self.assertEqual(index.request(2, 0), [])
        self.assertEqual(index.request(2, 1), [])
        self.assertEqual(index.request(1, 2), [0])
        self.assertEqual(index.request(2, 2), [0])
        self.assertEqual(index.request(3, 2), [0])
        self.assertEqual(index.request(3, 3), [0])
        self.assertEqual(index.request(4, 3), [0])
        self.assertEqual(index.request(4, 4), [0])
        
        
        # # =====================================================================
        self.assertEqual(index.request(ENUCoords(0, 0)), [0])
        self.assertEqual(index.request(ENUCoords(2.5, 3)), [0])
        self.assertEqual(index.request(ENUCoords(2.5, 5)), [0])
        self.assertEqual(index.request(ENUCoords(7, 5)), [0])
        self.assertEqual(index.request(ENUCoords(10, 10)), [0])
        self.assertEqual(index.request(ENUCoords(0.5, 2.5)), [0])
        self.assertEqual(index.request(ENUCoords(4.2, 5.8)), [0])

        
        # # =====================================================================
        self.assertEqual(index.request([ENUCoords(2.1, 0.5), ENUCoords(1.1, 1.1)]), [0])
        self.assertEqual(index.request([ENUCoords(2.1, 0.5), ENUCoords(7.1, 3.5)]), [])
        self.assertEqual(index.request([ENUCoords(5.8, 5.8), ENUCoords(2.1, 1.1)]), [0])
        
        
        # # =====================================================================
        self.assertEqual(index.request(track), [0])
 
        track2 = Track()
        p6 = Obs(ENUCoords(2.2, 0), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        track2.addObs(p6)
        p7 = Obs(ENUCoords(2.2, 3.8), GPSTime.readTimestamp('2020-01-01 10:08:00'))
        track2.addObs(p7)
        p8 = Obs(ENUCoords(6.5, 3.8), GPSTime.readTimestamp('2020-01-01 10:08:00'))
        track2.addObs(p8)
        self.assertEqual(index.request(track2), [0])
        
        
        track3 = Track()
        p9 = Obs(ENUCoords(6.5, 3.8), GPSTime.readTimestamp('2020-01-01 10:00:00'))
        track3.addObs(p9)
        p10 = Obs(ENUCoords(6.5, 7), GPSTime.readTimestamp('2020-01-01 10:08:00'))
        track3.addObs(p10)
        p11 = Obs(ENUCoords(10, 7), GPSTime.readTimestamp('2020-01-01 10:08:00'))
        track3.addObs(p11)
        self.assertEqual(index.request(track3), [0])


        # # =====================================================================
        # # =====================================================================
        self.assertCountEqual(index.neighborhood(0, 4, 0), [])
        self.assertCountEqual(index.neighborhood(0, 4, 1), [])
        self.assertCountEqual(index.neighborhood(0, 4, 2), [0])
        self.assertCountEqual(index.neighborhood(0, 4, 3), [0])
    
        self.assertCountEqual(index.neighborhood(3, 0, 0), [])
        self.assertCountEqual(index.neighborhood(3, 0, 1), [])
        self.assertCountEqual(index.neighborhood(3, 0, 2), [0])
        self.assertCountEqual(index.neighborhood(3, 0, 3), [0])
    
        self.assertCountEqual(index.neighborhood(2, 2, 0), [0])
        self.assertCountEqual(index.neighborhood(2, 2, 1), [0])
        self.assertCountEqual(index.neighborhood(2, 2, 2), [0])
        #self.assertCountEqual(index.neighborhood(2, 2, 3), [0])
    
        # # UNIT = -1
        self.assertCountEqual(index.neighborhood(2, 1, -1), [0])
        self.assertCountEqual(index.neighborhood(2, 0, -1), [0])
        self.assertCountEqual(index.neighborhood(0, 1, -1), [0])
        self.assertCountEqual(index.neighborhood(1, 1, -1), [0])
        self.assertCountEqual(index.neighborhood(0, 4, -1), [0])
        self.assertCountEqual(index.neighborhood(3, 4, -1), [0])
        self.assertCountEqual(index.neighborhood(4, 4, -1), [0])
        self.assertCountEqual(index.neighborhood(2, 4, -1), [0])
        
        
        # # =====================================================================
        self.assertCountEqual(index.neighborhood(ENUCoords(0, 0.1)), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.5, 3)), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.5, 5)), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(7, 5)), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(10, 10)), [0])
        
        self.assertCountEqual(index.neighborhood(ENUCoords(6.5, 3.8), None, 0), [])
        self.assertCountEqual(index.neighborhood(ENUCoords(6.5, 3.8), None, 1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(6.5, 3.8), None, 2), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(6.5, 3.8), None, 3), [0])

        self.assertCountEqual(index.neighborhood(ENUCoords(2.2, 3.8), None, 0), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.2, 3.8), None, 1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.2, 3.8), None, 2), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.2, 3.8), None, 3), [0])
        
        self.assertCountEqual(index.neighborhood(ENUCoords(9.9, 7), None, 0), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(9.9, 7), None, 1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(9.9, 7), None, 2), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(9.9, 7), None, 3), [0])
       
        #  # UNIT = -1
        self.assertCountEqual(index.neighborhood(ENUCoords(0, 0), None, -1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.5, 3), None, -1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.5, 5), None, -1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(7, 5), None, -1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(10, 10), None, -1), [0])
        
        self.assertCountEqual(index.neighborhood(ENUCoords(6.5, 3.8), None, -1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(2.2, 3.8), None, -1), [0])
        self.assertCountEqual(index.neighborhood(ENUCoords(9.9, 7), None, -1), [0])
  
    
        # # =====================================================================
        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(0.1, 2.1)], None, 0), [0])
        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(0.1, 2.1)], None, 1), [0])
        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(0.1, 2.1)], None, 2), [0])
        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(0.1, 2.1)], None, -1), [0])

        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(7.1, 3.5)]), [])
        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(7.1, 3.5)], None, 2), [0])
        self.assertEqual(index.neighborhood([ENUCoords(2.1, 0.5), ENUCoords(7.1, 3.5)], None, -1), [0])
        
        self.assertEqual(index.neighborhood([ENUCoords(5.8, 5.8), ENUCoords(2.1, 1.1)]), [0])
        self.assertEqual(index.neighborhood([ENUCoords(5.8, 5.8), ENUCoords(2.1, 1.1)], None, 1), [0])
        self.assertEqual(index.neighborhood([ENUCoords(5.8, 5.8), ENUCoords(2.1, 1.1)], None, 2), [0])
        self.assertEqual(index.neighborhood([ENUCoords(5.8, 5.8), ENUCoords(2.1, 1.1)], None, -1), [0])
        
        
        # # =====================================================================
        self.assertEqual(index.neighborhood(track), [0])
        self.assertEqual(index.neighborhood(track, None, 1), [0])
        self.assertEqual(index.neighborhood(track, None, 3), [0])
        self.assertEqual(index.neighborhood(track, None, -1), [0])
 
        self.assertEqual(index.neighborhood(track2), [0])
        self.assertEqual(index.neighborhood(track2, None, 0), [0])
        self.assertEqual(index.neighborhood(track2, None, 1), [0])
        self.assertEqual(index.neighborhood(track2, None, 3), [0])
        self.assertEqual(index.neighborhood(track2, None, -1), [0])
        
        self.assertEqual(index.neighborhood(track3), [0])
        self.assertEqual(index.neighborhood(track3, None, 0), [0])
        self.assertEqual(index.neighborhood(track3, None, 1), [0])
        self.assertEqual(index.neighborhood(track3, None, 2), [0])
        self.assertEqual(index.neighborhood(track3, None, 3), [0])
        self.assertEqual(index.neighborhood(track3, None, -1), [0])
Exemplo n.º 19
0
    def test_selection_track_constraint(self):

        GPSTime.setReadFormat("4Y-2M-2D 2h:2m:2s")
        chemin = os.path.join(self.resource_path, './data/trace1.dat')
        trace = FileReader.readFromFile(chemin, 2, 3, -1, 4, separator=",")
        trace.plot()

        # =====================================================================
        trace1 = Track()
        c1 = trace.getObs(1350).position
        c0 = ENUCoords(c1.getX() + 5000, c1.getY())
        c2 = ENUCoords(c1.getX() - 5000, c1.getY())
        p1 = Obs(c0, GPSTime.readTimestamp("2018-07-31 14:00:00"))
        p2 = Obs(c1, GPSTime.readTimestamp("2018-07-31 14:01:00"))
        p3 = Obs(c2, GPSTime.readTimestamp("2018-07-31 14:02:00"))
        trace1.addObs(p1)
        trace1.addObs(p2)
        trace1.addObs(p3)
        plt.plot(trace1.getX(), trace1.getY(), 'r-')
        plt.show()

        c3 = TrackConstraint(trace1, mode=MODE_PARALLEL)
        s = Selector([c3])
        selector = GlobalSelector([s])
        isSelection = selector.contains(trace)
        self.assertFalse(isSelection)

        c4 = TrackConstraint(trace1, mode=MODE_CROSSES)
        s = Selector([c4])
        selector = GlobalSelector([s])
        isSelection = selector.contains(trace)
        self.assertTrue(isSelection)

        # =====================================================================
        trace1 = Track()
        c0 = ENUCoords(
            trace.getObs(1349).position.getX(),
            trace.getObs(1349).position.getY())
        c1 = ENUCoords(
            trace.getObs(1350).position.getX(),
            trace.getObs(1350).position.getY())
        c2 = ENUCoords(
            trace.getObs(1351).position.getX(),
            trace.getObs(1351).position.getY())
        p1 = Obs(c0, GPSTime.readTimestamp("2018-07-31 14:00:00"))
        p2 = Obs(c1, GPSTime.readTimestamp("2018-07-31 14:01:00"))
        p3 = Obs(c2, GPSTime.readTimestamp("2018-07-31 14:02:00"))
        trace1.addObs(p1)
        trace1.addObs(p2)
        trace1.addObs(p3)
        trace.plot()
        plt.plot(trace1.getX(), trace1.getY(), 'r-')
        plt.show()

        c3 = TrackConstraint(trace1, mode=MODE_PARALLEL)
        s = Selector([c3])
        selector = GlobalSelector([s])
        isSelection = selector.contains(trace)
        self.assertTrue(isSelection)

        c4 = TrackConstraint(trace1, mode=MODE_CROSSES)
        s = Selector([c4])
        selector = GlobalSelector([s])
        isSelection = selector.contains(trace)
        self.assertTrue(isSelection)
Exemplo n.º 20
0
 def __init__(self, pt1, pt2, time=None, type=TYPE_SELECT):
     """TODO"""
     self.gate = Track([Obs(pt1), Obs(pt2)])
     self.time = time
     self.type = type
Exemplo n.º 21
0
def example3():

    start = GeoCoords(2.4320023, 48.84298, 100).toECEFCoords()
    print(start)

    path = "data/psr.dat"

    track = Track()

    for i in range(47):
        track.addObs(Obs(ECEFCoords(0, 0, 0)))
    track.createAnalyticalFeature("m0", [0] * 47)
    track.createAnalyticalFeature("sx0", [0] * 47)
    track.createAnalyticalFeature("sy0", [0] * 47)
    track.createAnalyticalFeature("sz0", [0] * 47)
    track.createAnalyticalFeature("m1", [0] * 47)
    track.createAnalyticalFeature("sx1", [0] * 47)
    track.createAnalyticalFeature("sy1", [0] * 47)
    track.createAnalyticalFeature("sz1", [0] * 47)
    track.createAnalyticalFeature("m2", [0] * 47)
    track.createAnalyticalFeature("sx2", [0] * 47)
    track.createAnalyticalFeature("sy2", [0] * 47)
    track.createAnalyticalFeature("sz2", [0] * 47)
    track.createAnalyticalFeature("m3", [0] * 47)
    track.createAnalyticalFeature("sx3", [0] * 47)
    track.createAnalyticalFeature("sy3", [0] * 47)
    track.createAnalyticalFeature("sz3", [0] * 47)
    track.createAnalyticalFeature("m4", [0] * 47)
    track.createAnalyticalFeature("sx4", [0] * 47)
    track.createAnalyticalFeature("sy4", [0] * 47)
    track.createAnalyticalFeature("sz4", [0] * 47)

    with open(path) as fp:
        line = True
        for i in range(47):
            for j in range(5):
                line = fp.readline()
                vals = line[:-2].split(",")
                track.setObsAnalyticalFeature("sx" + str(j), i, float(vals[1]))
                track.setObsAnalyticalFeature("sy" + str(j), i, float(vals[2]))
                track.setObsAnalyticalFeature("sz" + str(j), i, float(vals[3]))
                track.setObsAnalyticalFeature("m" + str(j), i, float(vals[4]))
            line = fp.readline()

    def F(x):
        plan = ECEFCoords(x[0, 0], x[1, 0], x[2, 0]).toENUCoords(start)
        plan.E += x[5, 0] * math.sin(x[4, 0])
        plan.N += x[5, 0] * math.cos(x[4, 0])
        xyz = plan.toECEFCoords(start)
        return np.array([[xyz.X], [xyz.Y], [xyz.Z], [x[3, 0] + x[6, 0]],
                         [x[4, 0]], [x[5, 0]], [x[6, 0]]])

    def H(x, k, track):
        return np.array([
            [((x[0, 0] - track["sx0", k])**2 + (x[1, 0] - track["sy0", k])**2 +
              (x[2, 0] - track["sz0", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx1", k])**2 + (x[1, 0] - track["sy1", k])**2 +
              (x[2, 0] - track["sz1", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx2", k])**2 + (x[1, 0] - track["sy2", k])**2 +
              (x[2, 0] - track["sz2", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx3", k])**2 + (x[1, 0] - track["sy3", k])**2 +
              (x[2, 0] - track["sz3", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx4", k])**2 + (x[1, 0] - track["sy4", k])**2 +
              (x[2, 0] - track["sz4", k])**2)**0.5 + x[3, 0]]
        ])

    Q = 1e0 * np.eye(7, 7)
    Q[3, 3] = 0
    Q[4, 4] = 1e-10
    Q[5, 5] = 1e-1
    Q[6, 6] = 1e-1
    R = 1e1 * np.eye(5, 5)

    X0 = np.array([[start.getX()], [start.getY()], [start.getZ()], [0], [0],
                   [0], [0]])
    P0 = 1e5 * np.eye(7, 7)
    P0[3, 3] = 1e6
    P0[4, 4] = 1e1
    P0[5, 5] = 1e1
    P0[6, 6] = 1e3

    UKF = Kalman(spreading=1)
    UKF.setTransition(F, Q)
    UKF.setObservation(H, R)
    UKF.setInitState(X0, P0)
    UKF.summary()

    UKF.estimate(track, ["m0", "m1", "m2", "m3", "m4"],
                 mode=Dynamics.MODE_STATES_AS_3D_POSITIONS)

    track.toGeoCoords()

    track.plot('r-')

    plt.show()

    KmlWriter.writeToKml(track, path="couplage.kml", type="LINE")
Exemplo n.º 22
0
def example4():

    start = GeoCoords(2.4320023, 48.84298, 100).toECEFCoords()

    path = "data/psr_all.dat"

    track = Track()

    Nepochs = 534
    for i in range(Nepochs):
        track.addObs(Obs(ECEFCoords(0, 0, 0)))
    track.createAnalyticalFeature("m0", [0] * Nepochs)
    track.createAnalyticalFeature("sx0", [0] * Nepochs)
    track.createAnalyticalFeature("sy0", [0] * Nepochs)
    track.createAnalyticalFeature("sz0", [0] * Nepochs)
    track.createAnalyticalFeature("m1", [0] * Nepochs)
    track.createAnalyticalFeature("sx1", [0] * Nepochs)
    track.createAnalyticalFeature("sy1", [0] * Nepochs)
    track.createAnalyticalFeature("sz1", [0] * Nepochs)
    track.createAnalyticalFeature("m2", [0] * Nepochs)
    track.createAnalyticalFeature("sx2", [0] * Nepochs)
    track.createAnalyticalFeature("sy2", [0] * Nepochs)
    track.createAnalyticalFeature("sz2", [0] * Nepochs)
    track.createAnalyticalFeature("m3", [0] * Nepochs)
    track.createAnalyticalFeature("sx3", [0] * Nepochs)
    track.createAnalyticalFeature("sy3", [0] * Nepochs)
    track.createAnalyticalFeature("sz3", [0] * Nepochs)
    track.createAnalyticalFeature("m4", [0] * Nepochs)
    track.createAnalyticalFeature("sx4", [0] * Nepochs)
    track.createAnalyticalFeature("sy4", [0] * Nepochs)
    track.createAnalyticalFeature("sz4", [0] * Nepochs)
    track.createAnalyticalFeature("m5", [0] * Nepochs)
    track.createAnalyticalFeature("sx5", [0] * Nepochs)
    track.createAnalyticalFeature("sy5", [0] * Nepochs)
    track.createAnalyticalFeature("sz5", [0] * Nepochs)

    with open(path) as fp:
        line = True
        for i in range(Nepochs):
            for j in range(6):
                line = fp.readline()
                vals = line[:-1].split(",")
                track.setObsAnalyticalFeature("sx" + str(j), i, float(vals[1]))
                track.setObsAnalyticalFeature("sy" + str(j), i, float(vals[2]))
                track.setObsAnalyticalFeature("sz" + str(j), i, float(vals[3]))
                track.setObsAnalyticalFeature("m" + str(j), i, float(vals[4]))
            line = fp.readline()

    track = track % [False, True]

    def F(x):
        return np.array([[x[0, 0]], [x[1, 0]], [x[2, 0]], [x[3, 0] + x[4, 0]],
                         [x[4, 0]]])

    def H(x, k, track):
        return np.array([
            [((x[0, 0] - track["sx0", k])**2 + (x[1, 0] - track["sy0", k])**2 +
              (x[2, 0] - track["sz0", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx1", k])**2 + (x[1, 0] - track["sy1", k])**2 +
              (x[2, 0] - track["sz1", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx2", k])**2 + (x[1, 0] - track["sy2", k])**2 +
              (x[2, 0] - track["sz2", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx3", k])**2 + (x[1, 0] - track["sy3", k])**2 +
              (x[2, 0] - track["sz3", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx4", k])**2 + (x[1, 0] - track["sy4", k])**2 +
              (x[2, 0] - track["sz4", k])**2)**0.5 + x[3, 0]],
            [((x[0, 0] - track["sx5", k])**2 + (x[1, 0] - track["sy5", k])**2 +
              (x[2, 0] - track["sz5", k])**2)**0.5 + x[3, 0]]
        ])

    Q = 1e0 * np.eye(5, 5)
    Q[3, 3] = 0
    Q[4, 4] = 1e0

    def R(k):
        P = 1e1 * np.eye(6, 6)
        if (k >= 70) and (k < 267):
            for i in range(3, 6):
                P[i, i] = 1e16
        return P

    for k in range(70, 267):
        for i in range(3, 6):
            track.setObsAnalyticalFeature("m" + str(i), k, 20000000)

    X0 = np.array([[start.X], [start.Y], [start.Z], [0], [0]])
    P0 = 1e5 * np.eye(5, 5)
    P0[3, 3] = 1e8
    P0[4, 4] = 1e6

    UKF = Kalman(spreading=1)
    UKF.setTransition(F, Q)
    UKF.setObservation(H, R)
    UKF.setInitState(X0, P0)
    UKF.summary()

    UKF.estimate(track, ["m0", "m1", "m2", "m3", "m4", "m5"],
                 mode=Dynamics.MODE_STATES_AS_3D_POSITIONS)
    track.toGeoCoords()

    KmlWriter.writeToKml(track,
                         path="couplage.kml",
                         type="POINT",
                         c1=[0, 1, 0, 1])

    track.plot('r+')
    plt.show()
Exemplo n.º 23
0
def intersection(track1, track2, withTime=-1):

    if not (track1.getSRID() == track2.getSRID()):
        print("Error: tracks must have same SRID to compute intersections")
        exit()

    I = Track()
    TMP_I = []
    TMP_J = []
    TMP_TPS2 = []

    for i in range(len(track1) - 1):

        x11 = track1[i].position.getX()
        y11 = track1[i].position.getY()
        x12 = track1[i + 1].position.getX()
        y12 = track1[i + 1].position.getY()
        seg1 = [x11, y11, x12, y12]

        for j in range(len(track2) - 1):

            x21 = track2[j].position.getX()
            y21 = track2[j].position.getY()
            x22 = track2[j + 1].position.getX()
            y22 = track2[j + 1].position.getY()
            seg2 = [x21, y21, x22, y22]

            if isSegmentIntersects(seg1, seg2):
                P1 = cartesienne(seg1)
                P2 = cartesienne(seg2)
                A = np.zeros((2, 2))
                B = np.zeros((2, 1))
                A[0, 0] = P1[0]
                A[0, 1] = P1[1]
                B[0, 0] = -P1[2]
                A[1, 0] = P2[0]
                A[1, 1] = P2[1]
                B[1, 0] = -P2[2]

                X = np.linalg.solve(A, B)

                x = X[0, 0]
                y = X[1, 0]
                p = Utils.makeCoords(x, y, 0, track1.getSRID())

                # Linear interpolation on track 1
                w1 = p.distance2DTo(track1[i].position)
                w2 = p.distance2DTo(track1[i + 1].position)
                p.setZ((w1 * track1[i + 1].position.getZ() +
                        w2 * track1[i].position.getZ()) / (w1 + w2))
                t1 = track1[i].timestamp.toAbsTime()
                t2 = track1[i].timestamp.toAbsTime()
                ta = (w1 * t2 + w2 * t1) / (w1 + w2)

                # Linear interpolation on track 2
                w1 = p.distance2DTo(track2[j].position)
                w2 = p.distance2DTo(track2[j + 1].position)
                t1 = track2[i].timestamp.toAbsTime()
                t2 = track2[i].timestamp.toAbsTime()
                tb = (w1 * t2 + w2 * t1) / (w1 + w2)

                # Add intersection
                if (withTime == -1) or (abs(tb - ta) < withTime):
                    I.addObs(Obs(p, GPSTime.readUnixTime(ta)))
                    TMP_TPS2.append(GPSTime.readUnixTime(tb))
                    TMP_I.append(i)
                    TMP_J.append(j)

    if I.size() > 0:
        I.createAnalyticalFeature("timestamp2", TMP_TPS2)
        I.createAnalyticalFeature("id1", TMP_I)
        I.createAnalyticalFeature("id2", TMP_J)

    return I
Exemplo n.º 24
0
def findStopsGlobalForRTK(track,
                          std_max=2e-2,
                          duration=5,
                          downsampling=1,
                          verbose=True):
    """Find stop points in a track based on maximal size of a stop and minimal
    time duration

    Two parameters:

        - Maximal size of a stop (as the standard deviation per axis, in ground units)
        - Minimal time duration (in seconds)

    Use downsampling parameter > 1 to speed up the process.

    Default is set for precise RTK GNSS survey (2 cm for 5 sec)
    """

    # If down-sampling is required
    if downsampling > 1:
        track = track.copy()
        track **= track.size() / downsampling

    # ---------------------------------------------------------------------------
    # Computes cost matrix as :
    #    Cij = 0 if sqrt(0.33*(std_x^2 + std_y^2 + std_Z^2)) > std_max
    #    Cij = 0 if time duration between pi and p-1 is < duration
    #    Cij = (j-i)**2 = square of the number of points of segment otherwise
    # ---------------------------------------------------------------------------
    C = np.zeros((track.size(), track.size()))
    RANGE = range(track.size() - 2)
    if verbose:
        print("Minimal enclosing circles computation:")
        RANGE = progressbar.progressbar(RANGE)
    for i in RANGE:
        for j in range(i + 1, track.size() - 1):
            if track[i].distanceTo(track[j - 1]) > 3 * std_max:
                C[i, j] = 0
                break
            if track[j - 1].timestamp - track[i].timestamp <= duration:
                C[i, j] = 0
                continue
            portion = track.extract(i, j - 1)
            varx = portion.operate(Operator.Operator.VARIANCE, "x")
            vary = portion.operate(Operator.Operator.VARIANCE, "y")
            varz = portion.operate(Operator.Operator.VARIANCE, "z")
            C[i, j] = math.sqrt(varx + vary + varz)
            C[i, j] = (C[i, j] < std_max) * (j - i)**2
    C = C + np.transpose(C)

    # ---------------------------------------------------------------------------
    # Computes optimal partition with dynamic programing
    # ---------------------------------------------------------------------------
    segmentation = optimalPartition(C, MODE_SEGMENTATION_MAXIMIZE, verbose)

    stops = Track()

    TMP_RADIUS = []
    TMP_MEAN_X = []
    TMP_MEAN_Y = []
    TMP_MEAN_Z = []
    TMP_IDSTART = []
    TMP_IDEND = []
    TMP_STD_X = []
    TMP_STD_Y = []
    TMP_STD_Z = []
    TMP_DURATION = []
    TMP_NBPOINTS = []

    for i in range(len(segmentation) - 1):
        portion = track.extract(segmentation[i], segmentation[i + 1] - 1)

        radius = C[segmentation[i], segmentation[i + 1]]
        if radius == 0:
            continue

        xm = portion.operate(Operator.Operator.AVERAGER, "x")
        ym = portion.operate(Operator.Operator.AVERAGER, "y")
        zm = portion.operate(Operator.Operator.AVERAGER, "z")

        xv = portion.operate(Operator.Operator.VARIANCE, "x")
        yv = portion.operate(Operator.Operator.VARIANCE, "y")
        zv = portion.operate(Operator.Operator.VARIANCE, "z")

        pt = portion[0].position.copy()
        pt.setX(xm)
        pt.setY(ym)
        pt.setZ(zm)
        stops.addObs(Obs(pt, portion[0].timestamp))

        TMP_RADIUS.append(math.sqrt(xv + yv + zv))
        TMP_MEAN_X.append(xm)
        TMP_MEAN_Y.append(ym)
        TMP_MEAN_Z.append(zm)
        TMP_STD_X.append(xv**0.5)
        TMP_STD_Y.append(yv**0.5)
        TMP_STD_Z.append(zv**0.5)
        TMP_IDSTART.append(segmentation[i] * downsampling)
        TMP_IDEND.append((segmentation[i + 1] - 1) * downsampling)
        TMP_NBPOINTS.append(segmentation[i + 1] - segmentation[i])
        TMP_DURATION.append(portion.duration())

    if stops.size() == 0:
        return stops

    stops.createAnalyticalFeature("radius", TMP_RADIUS)
    stops.createAnalyticalFeature("mean_x", TMP_MEAN_X)
    stops.createAnalyticalFeature("mean_y", TMP_MEAN_Y)
    stops.createAnalyticalFeature("mean_z", TMP_MEAN_Z)
    stops.createAnalyticalFeature("id_ini", TMP_IDSTART)
    stops.createAnalyticalFeature("id_end", TMP_IDEND)
    stops.createAnalyticalFeature("sigma_x", TMP_STD_X)
    stops.createAnalyticalFeature("sigma_y", TMP_STD_Y)
    stops.createAnalyticalFeature("sigma_z", TMP_STD_Z)
    stops.createAnalyticalFeature("duration", TMP_DURATION)
    stops.createAnalyticalFeature("nb_points", TMP_NBPOINTS)

    stops.operate(Operator.Operator.QUAD_ADDER, "sigma_x", "sigma_y", "rmse")
    stops.base = track.base

    return stops