@@ -406,32 +406,74 @@ get_conmat <- function (ways) {
406406sps_through_cycle <- function (ways , cyc ) {
407407
408408 cyc <- rbind (cyc , cyc [1 , ])
409- thepath <- NULL
409+ xy0 <- apply (do.call (rbind , do.call (c , ways )), 2 , mean )
410+
411+ # First reduce lists of ways doen to only those which connect with ways in
412+ # other groups:
413+ for (i in seq_len (nrow (cyc )) [- 1 ]) {
414+
415+ c0 <- cyc [i - 1 , 2 ] # the current way
416+ cf <- cyc [i - 1 , 1 ] # the 'from' way
417+ ct <- cyc [i , 2 ] # the 'to' way
418+
419+ nf <- rownames (do.call (rbind , ways [[cf ]]))
420+ nt <- rownames (do.call (rbind , ways [[ct ]]))
421+ w0 <- ways [[c0 ]]
422+ index_f <- which (vapply (
423+ w0 ,
424+ function (i ) any (rownames (i ) %in% nf ),
425+ logical (1L )
426+ ))
427+ index_t <- which (vapply (
428+ w0 ,
429+ function (i ) any (rownames (i ) %in% nt ),
430+ logical (1L )
431+ ))
432+ combs <- expand.grid (index_f , index_t )
433+ cm <- osmplotr ::: get_conmat (ways [[c0 ]])
434+ asp <- e1071 :: allShortestPaths (cm )
435+ paths <- apply (combs , 1 , function (i ) {
436+ unique (e1071 :: extractPath (asp , i [1 ], i [2 ]))
437+ }, simplify = FALSE )
438+ path_lens <- vapply (paths , length , integer (1L ))
439+ paths <- paths [which (path_lens == min (path_lens ))]
440+ if (length (paths ) > 1L ) {
441+ # Choose paths with outermost elements:
442+ dmax <- vapply (paths , function (p ) {
443+ these_ways <- do.call (rbind , w0 [p ])
444+ max (geodist :: geodist (xy0 , these_ways ))
445+ }, numeric (1L ))
446+ paths <- paths [[which.max (dmax )]]
447+ }
448+ ways [[c0 ]] <- ways [[c0 ]] [unlist (paths )]
449+ }
410450
451+ thepath <- NULL
411452 for (i in seq_len (nrow (cyc )) [- 1 ]) {
412453
413- w0 <- cyc [i - 1 , 2 ] # the current way
414- wf <- cyc [i - 1 , 1 ] # the 'from' way
415- wt <- cyc [i , 2 ] # the 'to' way
416- w0f <- do.call (rbind , ways [[w0 ]])
454+ c0 <- cyc [i - 1 , 2 ] # the current way
455+ cf <- cyc [i - 1 , 1 ] # the 'from' way
456+ ct <- cyc [i , 2 ] # the 'to' way
457+
458+ w0f <- do.call (rbind , ways [[c0 ]])
417459 if (is.null (thepath )) {
418460
419- wff <- do.call (rbind , ways [[wf ]])
461+ wff <- do.call (rbind , ways [[cf ]])
420462 } else {
421463
422464 wff <- thepath
423465 }
424- wtf <- do.call (rbind , ways [[wt ]])
466+ wtf <- do.call (rbind , ways [[ct ]])
425467 # start and end nodes that join to wf and wt:
426468 nst <- rownames (wff ) [which (rownames (wff ) %in% rownames (w0f ))]
427469 nend <- rownames (wtf ) [which (rownames (wtf ) %in% rownames (w0f ))]
428470
429471 w0f <- w0f [! duplicated (w0f ), ]
430472 adjmat <- array (NA , dim = rep (nrow (w0f ), 2 ))
431473 nms <- rownames (w0f )
432- for (j in seq (ways [[w0 ]])) {
474+ for (j in seq (ways [[c0 ]])) {
433475
434- wj <- ways [[w0 ]] [[j ]]
476+ wj <- ways [[c0 ]] [[j ]]
435477 indx <- match (rownames (wj ), nms )
436478 ifr <- indx [1 : (length (indx ) - 1 )]
437479 ito <- indx [- 1 ]
@@ -449,7 +491,10 @@ sps_through_cycle <- function (ways, cyc) {
449491 for (k in ito ) {
450492
451493 pathj <- e1071 :: extractPath (asp , j , k )
452- if (length (pathj ) < length (pathi )) {
494+ this_path <- (length (pathj ) < length (pathi ) && length (pathj ) > 2L ) ||
495+ (k == ito [length (ito )] && length (pathi ) == 1e6 )
496+ # if (length (pathj) < length (pathi)) {
497+ if (this_path ) {
453498 pathi <- pathj
454499 }
455500 }
0 commit comments