@@ -842,17 +842,23 @@ export class Graph {
842
842
try {
843
843
var matches = await new Promise ( ( resolve , reject ) => {
844
844
this . osrm . match ( hmmOptions , function ( err , response ) {
845
- if ( err )
845
+ if ( err ) {
846
+ console . log ( "got error" )
846
847
reject ( err ) ;
847
- else
848
+ }
849
+ else {
850
+ console . log ( "Succesful match" )
848
851
resolve ( response )
852
+ }
849
853
} ) ;
850
854
} ) ;
851
855
852
856
var visitedEdges :Set < string > = new Set ( ) ;
853
857
var visitedEdgeList :string [ ] = [ ] ;
854
858
855
859
if ( matches [ 'matchings' ] && matches [ 'matchings' ] . length > 0 ) {
860
+ //console.log(JSON.stringify(feature.geometry, null, 4))
861
+ //console.log(JSON.stringify(matches['matchings'][0]['geometry'], null, 4))
856
862
857
863
var match = matches [ 'matchings' ] [ 0 ] ;
858
864
if ( 0 < match . confidence ) {
@@ -902,8 +908,13 @@ export class Graph {
902
908
}
903
909
904
910
if ( visitedEdgeList . length > 0 ) {
905
- var startPoint = turfHelpers . point ( feature . geometry . coordinates [ 0 ] ) ;
906
- var endPoint = turfHelpers . point ( feature . geometry . coordinates [ feature . geometry . coordinates . length - 1 ] ) ;
911
+ // Use the first non-null point (in the matches), rather than just the first point from the raw trace
912
+ // This way, we are guaranteed to get a match if the OSRM matching was successful
913
+ var firstNonNull = 0 , lastNonNull = matches [ 'tracepoints' ] . length - 1
914
+ while ( ! matches [ 'tracepoints' ] [ firstNonNull ] && firstNonNull ++ ) ;
915
+ while ( ! matches [ 'tracepoints' ] [ lastNonNull ] && lastNonNull -- ) ;
916
+ var startPoint = turfHelpers . point ( matches [ 'tracepoints' ] [ firstNonNull ] [ 'location' ] ) ;
917
+ var endPoint = turfHelpers . point ( matches [ 'tracepoints' ] [ lastNonNull ] [ 'location' ] ) ;
907
918
908
919
909
920
var startCandidate :PointCandidate = await this . getPointCandidateFromRefId ( startPoint , visitedEdgeList [ 0 ] , null ) ;
0 commit comments