Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

WIP: handling viterbi breaks as multiple sequences #87

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2c1a010
initial work for handling viterbi breaks as multiple sequences
kodonnell Dec 11, 2016
d979ffd
handle case where viterbi breaks immediately after initialization
kodonnell Dec 12, 2016
6b856f1
merge U-turn work
kodonnell Dec 30, 2016
ade4037
refactoring sequences
kodonnell Jan 13, 2017
4b24cbc
use MatchEntry internally instead of GPXEntry
kodonnell Jan 14, 2017
5832623
debug and fix tests
kodonnell Jan 15, 2017
b6c3af4
rename timestep -> viterbimatchentry
kodonnell Jan 15, 2017
c0e5574
fix other tests
kodonnell Jan 15, 2017
2d184a2
tidying calcpath and gpxfile/main
kodonnell Jan 15, 2017
791e53c
web stuff ...
kodonnell Jan 15, 2017
eed78bf
giving up on that test ...
kodonnell Jan 15, 2017
ac105e7
woops, don't need that anymore ...
kodonnell Jan 15, 2017
d6bf213
refactor + tidy + all tests passing
kodonnell Jan 31, 2017
4e217d0
contiguous sequences
kodonnell Jan 31, 2017
f629883
undo test change to fix test change
kodonnell Feb 1, 2017
9e6cc60
add logging in again as per @stefanholder's request
kodonnell Feb 6, 2017
9d6f84b
Merge branch 'master' into sequences
kodonnell Feb 26, 2017
7f45557
note funny bug ...
kodonnell Feb 26, 2017
4a7420a
some changes as per @stefanholder
kodonnell Mar 20, 2017
13707e1
bringing back the missing readme
kodonnell Mar 20, 2017
efb7b57
a few more tidyups
kodonnell Mar 20, 2017
956e7d0
more tidy-ups
kodonnell Mar 20, 2017
1dd88e8
utilise LocationIndexTree.findWithinRadius
kodonnell Mar 20, 2017
56df0ab
ugly hacky gui ...
kodonnell Mar 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public GPXFile(MatchResult mr, InstructionList il) {
// TODO fetch time from GPX or from calculated route?
long time = 0;
for (int emIndex = 0; emIndex < mr.getEdgeMatches().size(); emIndex++) {
EdgeMatch em = mr.getEdgeMatches().get(emIndex);
MatchEdge em = mr.getEdgeMatches().get(emIndex);
PointList pl = em.getEdgeState().fetchWayGeometry(emIndex == 0 ? 3 : 2);
if (pl.is3D()) {
includeElevation = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.graphhopper.matching;

import com.graphhopper.util.GPXEntry;

public class GPXMapping {
public final GPXEntry originalGPXEntry;
public final int matchEdgeIdx;
public final boolean isNeighbor;
public final int neighborIdx;
public final MatchEntry matchEntry;
public GPXMapping(GPXEntry originalGPXEntry, MatchEntry matchEntry, int matchEdgeIdx, boolean isNeighbor, int neighborIdx) {
this.originalGPXEntry = originalGPXEntry;
this.matchEdgeIdx = matchEdgeIdx;
this.isNeighbor = isNeighbor;
this.neighborIdx = neighborIdx;
this.matchEntry = matchEntry;
}
}
499 changes: 159 additions & 340 deletions matching-core/src/main/java/com/graphhopper/matching/MapMatching.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.matching;

import com.graphhopper.util.EdgeIteratorState;

/**
*
* @author kodonnell
*/
public class MatchEdge {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about MatchedEdge instead of MatchEdge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was trying to be consistent with (the original) MatchResult as opposed to MatchedResult - happy to change all if you wish?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that consistency is needed here, so I would be happy with MatchedEdge (the matched edge) and MatchResult (the result is not matched, so I think this is fine).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


public final EdgeIteratorState edge;
public final long fromTime;
public final long toTime;

public MatchEdge(EdgeIteratorState edge, long fromTime, long toTime) {
assert edge != null : "edge should not be null";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a precondition of a public method is violated, an exception should rather be thrown (NullPointerException in this case).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - may be a few more of those. Will look now.

this.edge = edge;
this.fromTime = fromTime;
this.toTime = toTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.graphhopper.matching;

import java.util.List;

import com.bmw.hmm.SequenceState;
import com.graphhopper.matching.util.TimeStep;
import com.graphhopper.routing.Path;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.GPXEntry;
import com.graphhopper.util.shapes.GHPoint3D;

public class MatchEntry {
public final int sequenceIdx;
public final GPXEntry gpxEntry;
public final List<GPXEntry> neighboringGpxEntries;
public final GHPoint3D point;
public final EdgeIteratorState directedRealEdge;
public final int sequenceMatchEdgeIdx;
public final double distanceAlongRealEdge;
public MatchEntry(int sequenceIdx, SequenceState<GPXExtension, GPXEntry, Path> matchStep, TimeStep timeStep, EdgeIteratorState directedRealEdge, int sequenceMatchEdgeIdx, double distanceAlongRealEdge) {
this.sequenceIdx = sequenceIdx;
this.gpxEntry = matchStep.observation;
this.point = matchStep.state.getQueryResult().getSnappedPoint();
this.sequenceMatchEdgeIdx = sequenceMatchEdgeIdx;
this.directedRealEdge = directedRealEdge;
this.distanceAlongRealEdge = distanceAlongRealEdge;
this.neighboringGpxEntries = timeStep.getNeighboringEntries();
}

}
152 changes: 97 additions & 55 deletions matching-core/src/main/java/com/graphhopper/matching/MatchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,85 +17,127 @@
*/
package com.graphhopper.matching;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.graphhopper.matching.util.TimeStep;
import com.graphhopper.util.DistanceCalc;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.GPXEntry;

/**
*
* @author Peter Karich
* @author kodonnell
*/
public class MatchResult {

private List<EdgeMatch> edgeMatches;
private double matchLength;
private long matchMillis;
private double gpxEntriesLength;
private long gpxEntriesMillis;

public MatchResult(List<EdgeMatch> edgeMatches) {
setEdgeMatches(edgeMatches);
private final List<MatchSequence> sequences;
/**
* The length (meters) of the total *matched* path, excluding sequence breaks.
*/
private double matchDistance;
/**
* The time (milliseconds) to travel the *matched* path that makes up this sequence, assuming
* one travels at the speed limit and there are no turn costs between sequence connections.
*/
private long matchDuration;
/**
* The cumulative sequential great-line distance between all of the GPX entries, in meters,
* optionally skipping the distances between sequence breaks.
*/
private double gpxEntriesDistance;
/**
* The time (milliseconds) between the last and first GPX entry, optionally skipping the
* time between sequence breaks.
*/
private long gpxEntriesDuration;
/**
* A mapping of all the original GPX entries to their final matched position (i.e. which
* sequence, etc.)
*/
private List<GPXMapping> originalGPXMapping = null;
/**
* A list of all of the match edges (just a union of those for each sequence).
*/
private List<MatchEdge> matchEdges = null;

public MatchResult(List<MatchSequence> sequences) {
this.sequences = sequences;
}

public void setEdgeMatches(List<EdgeMatch> edgeMatches) {
if (edgeMatches == null) {
throw new IllegalStateException("edgeMatches cannot be null");

public void computeEdgeMatches(Map<String, EdgeIteratorState> virtualEdgesMap, int nodeCount, List<GPXEntry> originalGPXEntries) {
originalGPXMapping = new ArrayList<GPXMapping>();
matchEdges = new ArrayList<MatchEdge>();
matchDistance = 0;
matchDuration = 0;
for (MatchSequence sequence: sequences) {
sequence.computeMatchEdges(virtualEdgesMap, nodeCount);
matchDistance += sequence.getMatchDistance();
matchDuration += sequence.getMatchDuration();
int nMatchEdgesThusFar = matchEdges.size();
for (MatchEntry me: sequence.matchEntries) {
int matchEdgesIdx = nMatchEdgesThusFar + me.sequenceMatchEdgeIdx;
originalGPXMapping.add(new GPXMapping(me.gpxEntry, me, matchEdgesIdx, false, -1));
int neighborIdx = 0;
for (GPXEntry gpx: me.neighboringGpxEntries) {
originalGPXMapping.add(new GPXMapping(gpx, me, matchEdgesIdx, true, neighborIdx++));
}
}
matchEdges.addAll(sequence.matchEdges);
}

// check 'em
int n = originalGPXEntries.size();
assert originalGPXEntries.size() == n;
for (int gpxIdx = 0; gpxIdx < n; gpxIdx++) {
assert originalGPXEntries.get(gpxIdx).equals(originalGPXMapping.get(gpxIdx).originalGPXEntry);
}

this.edgeMatches = edgeMatches;
}

public void setGPXEntriesLength(double gpxEntriesLength) {
this.gpxEntriesLength = gpxEntriesLength;
}

public void setGPXEntriesMillis(long gpxEntriesMillis) {
this.gpxEntriesMillis = gpxEntriesMillis;
}

public void setMatchLength(double matchLength) {
this.matchLength = matchLength;

public void computeGPXStats(DistanceCalc distCalc, boolean skipSequenceBreaks) {
gpxEntriesDistance = 0;
gpxEntriesDuration = 0;
GPXEntry lastGPXEntry = null;
for (MatchSequence sequence: sequences) {
sequence.computeGPXStats(distCalc);
gpxEntriesDistance += sequence.getMatchDistance();
gpxEntriesDuration += sequence.getMatchDuration();
if (!skipSequenceBreaks) {
if(lastGPXEntry != null) {
GPXEntry firstGPXEntry = sequence.timeSteps.get(0).observation;
gpxEntriesDistance += distCalc.calcDist(lastGPXEntry.lat, lastGPXEntry.lon, firstGPXEntry.lat, firstGPXEntry.lon);
gpxEntriesDuration += (firstGPXEntry.getTime() - lastGPXEntry.getTime());
}
TimeStep lastTimeStep = sequence.timeSteps.get(sequence.timeSteps.size() - 1);
List<GPXEntry> neighbors = lastTimeStep.getNeighboringEntries();
lastGPXEntry = neighbors.isEmpty() ? lastTimeStep.observation : neighbors.get(neighbors.size() - 1);
}
}
}

public void setMatchMillis(long matchMillis) {
this.matchMillis = matchMillis;
public List<MatchEdge> getEdgeMatches() {
return matchEdges;
}

/**
* All possible assigned edges.
*/
public List<EdgeMatch> getEdgeMatches() {
return edgeMatches;

public List<GPXMapping> getOriginalGPXMapping() {
return originalGPXMapping;
}

/**
* Length of the original GPX track in meters
*/
public double getGpxEntriesLength() {
return gpxEntriesLength;
return gpxEntriesDistance;
}

/**
* Length of the original GPX track in milliseconds
*/

public long getGpxEntriesMillis() {
return gpxEntriesMillis;
return gpxEntriesDuration;
}

/**
* Length of the map-matched road in meters
*/
public double getMatchLength() {
return matchLength;
return matchDistance;
}

/**
* Length of the map-matched road in milliseconds
*/
public long getMatchMillis() {
return matchMillis;
}

@Override
public String toString() {
return "length:" + matchLength + ", seconds:" + matchMillis / 1000f + ", matches:" + edgeMatches.toString();
return matchDuration;
}
}
Loading