Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

23 new function aggregate lineparts sfr #58

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4484c32
Create aggregate_lineparts_sf.r
SanderDevisscher Aug 7, 2024
472cc69
improve example
SanderDevisscher Aug 9, 2024
3eb47af
add logic to work with sf_id == "sf_id"
SanderDevisscher Aug 9, 2024
23383ed
Create aggregate_lineparts_sf.Rd
SanderDevisscher Aug 9, 2024
55d866e
Update NAMESPACE
SanderDevisscher Aug 9, 2024
94b36a4
Increment version number to 1.2.0
SanderDevisscher Aug 9, 2024
75075a2
Merge pull request #59 from inbo/main
SanderDevisscher Aug 9, 2024
8348223
Build pkgdown site [skip ci]
github-actions[bot] Aug 9, 2024
4248d5f
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 20, 2024
c87c8a4
Build pkgdown site [skip ci]
github-actions[bot] Aug 20, 2024
0855554
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 23, 2024
7b4c066
Increment version [skip ci]
github-actions[bot] Aug 23, 2024
624b471
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 23, 2024
22a45df
Increment version [skip ci]
github-actions[bot] Aug 23, 2024
a173d1e
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 23, 2024
42187b8
Increment version [skip ci]
github-actions[bot] Aug 23, 2024
f5d842a
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 26, 2024
646e3c7
Increment version [skip ci]
github-actions[bot] Aug 26, 2024
5adc1ab
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 26, 2024
5ad0ab3
Increment version [skip ci]
github-actions[bot] Aug 26, 2024
2f17d28
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Aug 26, 2024
689859a
Increment version [skip ci]
github-actions[bot] Aug 26, 2024
716b783
Build pkgdown site [skip ci]
github-actions[bot] Aug 26, 2024
3ab681d
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Oct 11, 2024
8744694
update rd's
SanderDevisscher Oct 11, 2024
3ef37d8
Increment version [skip ci]
github-actions[bot] Oct 11, 2024
1a86397
Build pkgdown site [skip ci]
github-actions[bot] Oct 11, 2024
22e9f73
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Oct 11, 2024
467a57b
Increment version [skip ci]
github-actions[bot] Oct 11, 2024
b3e6b0c
Build pkgdown site [skip ci]
github-actions[bot] Oct 11, 2024
0b71408
Merge branch 'main' into 23-new-function-aggregate_lineparts_sfr
SanderDevisscher Dec 9, 2024
d7fbebb
Increment version [skip ci]
github-actions[bot] Dec 9, 2024
c56c11f
Build pkgdown site [skip ci]
github-actions[bot] Dec 9, 2024
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fistools
Title: Tools & data used for wildlife management & invasive species in Flanders
Version: 1.2.14
Version: 1.2.15
Authors@R: c(
person(given = "Sander", middle = "", family = "Devisscher", "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2015-5731")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(CRS_extracter)
export(UUID_List)
export(aggregate_lineparts_sf)
export(apply_grtsdb)
export(calculate_polygon_centroid)
export(check)
Expand Down
188 changes: 188 additions & 0 deletions R/aggregate_lineparts_sf.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#' Connect seperate line parts into 1 line
#'
#' This function takes a sf object with separate line parts and connects them into 1 line.
#' The function is based on the st_union function from the sf package.
#' The function is designed to work with sf objects that have a column with unique
#' identifiers for the separate line parts.
#' The function will connect the line parts based on the unique identifier.
#'
#' @param sf_data A sf object with separate line parts
#' @param sf_id A character string with the name of the column with unique identifiers
#'
#' @return A sf object with connected line parts
#'
#' @family spatial
#' @export
#' @author Sander Devisscher
#'
#' @examples
#' \dontrun{
#' # create a sf object containing 2 seperate linstrings with wgs84 coordinates that lay within belgium
#' # add a column with the same id for both linestrings & a unique label for each line
#' sf_data <- sf::st_sfc(sf::st_linestring(matrix(c(5.5, 5.0, 50.0, 50.6), ncol = 2)),
#' sf::st_linestring(matrix(c(4.7, 4.8, 50.8, 50.8), ncol = 2))) %>%
#' sf::st_sf(id = c("a", "a")) %>%
#' dplyr::mutate(label = as.factor(dplyr::row_number()))
#'
#' # plot sf_data using leaflet
#' # create a palette for label
#' pal <- leaflet::colorFactor(palette = "RdBu", levels = sf_data$label)
#'
#' plot <- leaflet::leaflet() %>%
#' leaflet::addTiles() %>%
#' leaflet::addPolylines(data = sf_data, color = ~pal(label), weight = 5, opacity = 1)
#'
#' # connect the line parts
#' sf_data_connected <- aggregate_lineparts_sf(sf_data, "id")
#'
#' # add sf_data_connected to plot
#' plot <- plot %>%
#' leaflet::addPolylines(data = sf_data_connected, color = "black", weight = 2, opacity = 0.5)
#'
#' plot
#' }

aggregate_lineparts_sf <- function(sf_data,
sf_id){

# check if sf_data is a sf object
if(!inherits(sf_data, "sf")){
stop("sf_data is not a sf object")
}

# check if sf_id is a character string
if(!is.character(sf_id)){
warning("sf_id is not a character string >> converting to character string")
sf_id <- as.character(sf_id)
}

# check if sf_id is a column in sf_data
if(!(sf_id %in% names(sf_data))){
stop("sf_id is not a column in sf_data")
} else {
# check if sf_id == "sf_id"
if(sf_id == "sf_id"){
} else {
sf_data <- sf_data %>%
dplyr::mutate(sf_id = as.character(sf_data[[sf_id]]))
}
}

# check if geometry is present
if(!"geometry" %in% names(sf_data)){
sf_data <- sf_data %>%
dplyr::mutate(geometry = sf::st_geometry(.))
}

# get unique sf_ids
sf_ids <- unique(sf_data$sf_id)

output <- data.frame()

for(i in sf_ids){
# get line parts with the same sf_id
sf_unit <- sf_data %>%
dplyr::filter(sf_id == i) %>%
sf::st_union(by_feature = FALSE) %>%
sf::st_cast("LINESTRING")

# create empty data frame to store points
temp <- data.frame() %>%
dplyr::mutate(lon = NA_integer_,
lat = NA_integer_)

# loop over line parts to convert them to points
for(n in 1:length(sf_unit)){
temp <- temp %>%
dplyr::add_row(lon = sf_unit[[n]][,1],
lat = sf_unit[[n]][,2]) %>%
dplyr::distinct(lon, lat) %>%
dplyr::arrange(lat, lon)

sf_point <- temp %>%
sf::st_as_sf(coords = c("lon", "lat"))

# order points
sf_point_ordered <- data.frame()

a <- 1

# start progress bar
pb <- progress::progress_bar$new(format = " [:bar] :percent ETA: :eta",
total = nrow(sf_point),
clear = FALSE,
width = 60)

while(a <= nrow(sf_point)){
# tick progress bar
pb$tick()

if(a == 1){
# get first point
sf_point_ref <- sf_point[a,]
# select the other points of the linepart
outside <- sapply(sf::st_intersects(sf_point, sf_point_ref),function(x){length(x)==0})
sf_point_comp <- sf_point[outside, ]

# calculate distance between points
sf_point_comp$distance <- sf::st_distance(sf_point_comp, sf_point_ref)

# get the point with the smallest distance
sf_point_new_ref <- as.data.frame(sf_point_comp) %>%
sf::st_as_sf() %>%
dplyr::mutate(min = min(distance, na.rm = TRUE)) %>%
dplyr::filter(distance == min) %>%
dplyr::select(geometry)

# add the new point to the ordered points
sf_point_ordered <- rbind(sf_point_ref, sf_point_new_ref)

a <- a + 1

}else{
# get the other points of the linepart
sf_point_ref <- sf_point_new_ref
# select the other points of the linepart
outside <- sapply(sf::st_intersects(sf_point, sf_point_ordered),function(x){length(x)==0})
# calculate distance between points
sf_point_comp <- sf_point[outside, ]
# calculate distance between points
sf_point_comp$distance <- sf::st_distance(sf_point_comp, sf_point_ref)
# get the point with the smallest distance
sf_point_new_ref <- as.data.frame(sf_point_comp) %>%
sf::st_as_sf() %>%
dplyr::mutate(min = min(distance, na.rm = TRUE)) %>%
dplyr::filter(distance == min) %>%
dplyr::select(geometry)
# add the new point to the ordered points
sf_point_ordered <- rbind(sf_point_ordered, sf_point_new_ref)

a <- a + 1
}
}

# convert ordered points to linestring
test_sf <- sf_point_ordered %>%
sf::st_coordinates() %>%
sf::st_linestring() %>%
sf::st_geometry()
}

# add sf_id to the linestring
test_sf2 <- as.data.frame(test_sf) %>%
dplyr::mutate(sf_id = i)

# add linestring to output
if(nrow(output) == 0){
output <- test_sf2
}else{
output <- rbind(output, test_sf2)
}
}

# convert output to sf object
output <- output %>%
sf::st_as_sf()

return(output)
}
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CODE_OF_CONDUCT.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pandoc: 2.9.2.1
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
last_built: 2024-11-13T15:22Z
last_built: 2024-12-09T15:10Z
3 changes: 2 additions & 1 deletion docs/reference/CRS_extracter.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/UUID_List.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading