Skip to content

Fix/edge dir bug with geo#136

Merged
robitalec merged 8 commits intomainfrom
fix/edge-dir-bug-with-geo
Feb 3, 2026
Merged

Fix/edge dir bug with geo#136
robitalec merged 8 commits intomainfrom
fix/edge-dir-bug-with-geo

Conversation

@robitalec
Copy link
Copy Markdown
Member

library(spatsoc)
library(data.table)

DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))
id <- 'ID'
datetime <- 'datetime'
timethreshold <- '20 minutes'
threshold <- 50
coords <- c('X', 'Y')
timegroup <- 'timegroup'
utm <- 32736

DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]

get_geometry(DT, coords = coords, crs = utm)
group_times(DT, datetime = datetime, threshold = timethreshold)
#>            ID        X       Y            datetime population
#>        <char>    <num>   <num>              <POSc>      <int>
#>     1:      A 715851.4 5505340 2016-11-01 00:00:54          1
#>     2:      A 715822.8 5505289 2016-11-01 02:01:22          1
#>     3:      A 715872.9 5505252 2016-11-01 04:01:24          1
#>     4:      A 715820.5 5505231 2016-11-01 06:01:05          1
#>     5:      A 715830.6 5505227 2016-11-01 08:01:11          1
#>    ---                                                       
#> 14293:      J 700616.5 5509069 2017-02-28 14:00:54          1
#> 14294:      J 700622.6 5509065 2017-02-28 16:00:11          1
#> 14295:      J 700657.5 5509277 2017-02-28 18:00:55          1
#> 14296:      J 700610.3 5509269 2017-02-28 20:00:48          1
#> 14297:      J 700744.0 5508782 2017-02-28 22:00:39          1
#>                        geometry minutes timegroup
#>                     <sfc_POINT>   <int>     <int>
#>     1: POINT (715851.4 5505340)       0         1
#>     2: POINT (715822.8 5505289)       0         2
#>     3: POINT (715872.9 5505252)       0         3
#>     4: POINT (715820.5 5505231)       0         4
#>     5: POINT (715830.6 5505227)       0         5
#>    ---                                           
#> 14293: POINT (700616.5 5509069)       0      1393
#> 14294: POINT (700622.6 5509065)       0      1394
#> 14295: POINT (700657.5 5509277)       0      1440
#> 14296: POINT (700610.3 5509269)       0      1395
#> 14297:   POINT (700744 5508782)       0      1396
edges <- edge_dist(DT, threshold = threshold, id = id, coords = coords,
                   timegroup = timegroup, returnDist = TRUE, fillNA = FALSE)
dyad_id(edges, id1 = 'ID1', id2 = 'ID2')
#>        timegroup    ID1    ID2  distance dyadID
#>            <int> <char> <char>     <num> <char>
#>     1:         1      G      B  5.782904    B-G
#>     2:         1      B      G  5.782904    B-G
#>     3:         4      G      B 21.129001    B-G
#>     4:         4      H      E 11.961466    E-H
#>     5:         4      B      G 21.129001    B-G
#>    ---                                         
#> 12652:      1440      I      C  2.831071    C-I
#> 12653:      1440      C      F  9.372972    C-F
#> 12654:      1440      I      F  7.512922    F-I
#> 12655:      1440      C      I  2.831071    C-I
#> 12656:      1440      F      I  7.512922    F-I
fusion_id(edges, threshold = threshold)

cent <- centroid_fusion(edges = edges, DT = DT, id = id)

edge_direction(edges = cent, DT = DT, id = id)
#> Error in data.table::setnames(m, geometry, geometry_id1): Items of 'old' not found in column names: [geometry]. Consider skip_absent=TRUE.

Created on 2025-12-23 with reprex v2.1.1

not from typical workflow but eg. if user provides fusion_centroid
output
@robitalec
Copy link
Copy Markdown
Member Author

Fixed by dropping geometry from edges if found before internal merge

library(spatsoc)
library(data.table)

DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))
id <- 'ID'
datetime <- 'datetime'
timethreshold <- '20 minutes'
threshold <- 50
coords <- c('X', 'Y')
timegroup <- 'timegroup'
utm <- 32736

DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]

get_geometry(DT, coords = coords, crs = utm)
group_times(DT, datetime = datetime, threshold = timethreshold)
#>            ID        X       Y            datetime population
#>        <char>    <num>   <num>              <POSc>      <int>
#>     1:      A 715851.4 5505340 2016-11-01 00:00:54          1
#>     2:      A 715822.8 5505289 2016-11-01 02:01:22          1
#>     3:      A 715872.9 5505252 2016-11-01 04:01:24          1
#>     4:      A 715820.5 5505231 2016-11-01 06:01:05          1
#>     5:      A 715830.6 5505227 2016-11-01 08:01:11          1
#>    ---                                                       
#> 14293:      J 700616.5 5509069 2017-02-28 14:00:54          1
#> 14294:      J 700622.6 5509065 2017-02-28 16:00:11          1
#> 14295:      J 700657.5 5509277 2017-02-28 18:00:55          1
#> 14296:      J 700610.3 5509269 2017-02-28 20:00:48          1
#> 14297:      J 700744.0 5508782 2017-02-28 22:00:39          1
#>                        geometry minutes timegroup
#>                     <sfc_POINT>   <int>     <int>
#>     1: POINT (715851.4 5505340)       0         1
#>     2: POINT (715822.8 5505289)       0         2
#>     3: POINT (715872.9 5505252)       0         3
#>     4: POINT (715820.5 5505231)       0         4
#>     5: POINT (715830.6 5505227)       0         5
#>    ---                                           
#> 14293: POINT (700616.5 5509069)       0      1393
#> 14294: POINT (700622.6 5509065)       0      1394
#> 14295: POINT (700657.5 5509277)       0      1440
#> 14296: POINT (700610.3 5509269)       0      1395
#> 14297:   POINT (700744 5508782)       0      1396
edges <- edge_dist(DT, threshold = threshold, id = id, coords = coords,
                   timegroup = timegroup, returnDist = TRUE, fillNA = FALSE)
dyad_id(edges, id1 = 'ID1', id2 = 'ID2')
#>        timegroup    ID1    ID2  distance dyadID
#>            <int> <char> <char>     <num> <char>
#>     1:         1      G      B  5.782904    B-G
#>     2:         1      B      G  5.782904    B-G
#>     3:         4      G      B 21.129001    B-G
#>     4:         4      H      E 11.961466    E-H
#>     5:         4      B      G 21.129001    B-G
#>    ---                                         
#> 12652:      1440      I      C  2.831071    C-I
#> 12653:      1440      C      F  9.372972    C-F
#> 12654:      1440      I      F  7.512922    F-I
#> 12655:      1440      C      I  2.831071    C-I
#> 12656:      1440      F      I  7.512922    F-I
fusion_id(edges, threshold = threshold)

cent <- centroid_fusion(edges = edges, DT = DT, id = id)

edge_direction(edges = cent, DT = DT, id = id)
#> Linking to GEOS 3.14.1, GDAL 3.12.0, PROJ 9.7.0; sf_use_s2() is TRUE
#>        timegroup    ID1    ID2 dyadID  distance fusionID
#>            <int> <char> <char> <char>     <num>    <int>
#>     1:         1      G      B    B-G  5.782904        1
#>     2:         1      B      G    B-G  5.782904        1
#>     3:         4      G      B    B-G 21.129001        2
#>     4:         4      H      E    E-H 11.961466        3
#>     5:         4      B      G    B-G 21.129001        2
#>    ---                                                  
#> 12652:      1440      I      C    C-I  2.831071     3349
#> 12653:      1440      C      F    C-F  9.372972     3348
#> 12654:      1440      I      F    F-I  7.512922     3350
#> 12655:      1440      C      I    C-I  2.831071     3349
#> 12656:      1440      F      I    F-I  7.512922     3350
#>                        centroid     direction_dyad
#>                     <sfc_POINT>            <units>
#>     1: POINT (699637.9 5509637)  0.932704118 [rad]
#>     2: POINT (699637.9 5509637) -2.208889158 [rad]
#>     3: POINT (699375.7 5509717) -0.508093298 [rad]
#>     4: POINT (701675.6 5504244) -2.064856591 [rad]
#>     5: POINT (699375.7 5509717)  2.633500732 [rad]
#>    ---                                            
#> 12652: POINT (702960.6 5509447)  2.145953261 [rad]
#> 12653: POINT (702960.7 5509451) -0.262152781 [rad]
#> 12654: POINT (702959.5 5509452) -0.007124986 [rad]
#> 12655: POINT (702960.6 5509447) -0.995639711 [rad]
#> 12656: POINT (702959.5 5509452)  3.134467675 [rad]

Created on 2025-12-23 with reprex v2.1.1

@robitalec robitalec merged commit e3cf401 into main Feb 3, 2026
0 of 6 checks passed
@robitalec robitalec added status: completed and removed status: confirmed bug is reproducible labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant