1
2..gaps <- function(x) {
3	p <- as.polygons(floor(ext(x)+1), crs=crs(x))
4	e <- disagg(erase(p, x))
5	if (nrow(e) > 1) {
6		xmin = ext(p)[1]
7		i <- sapply(1:nrow(e), function(i) ext(e[i])[1] > xmin)
8		e <- e[i]
9		x <- rbind(x, e)
10	}
11	x
12}
13
14
15clean_further <- function(x, tolerance=0.0001) {
16	out <- as.lines(x)
17	out <- snap(out, tolerance)
18	out <- makeNodes(out)
19	out <- mergeLines(out)
20	as.polygons(out)
21}
22
23clean <- function(x) {
24	g <- gaps(x)
25	out <- erase(x)
26	out <- rbind(out, g)
27	g <- gaps(out)
28	rbind(out, g)
29}
30
31
32mergebyborder <- function(x, field) {
33	i <- is.na(x[[field, drop=TRUE]])
34	if (!any(i)) return(x)
35	s <- sharedPaths(x)
36	s$length <- perim(s)
37	from <- x[i]
38	x <- x[!i]
39
40	for (i in 1:nrow(from)) {
41
42
43	}
44}
45
46
47
48centerline <- function(p) {
49	v <- as.points(voronoi(p, tolerance=0))
50	v <- intersect(v, p)
51	v
52}
53
54
55
56#library(terra); messages = terra:::messages
57#p <- vect(system.file("ex/lux.shp", package="terra"))
58#h <- convHull(p[-12], "NAME_1")
59#x <- clean(h)
60#y <- clean_further(x)
61
62#hh <- rbind(h, h)
63#e <- erase(hh)
64#g <- gaps(e)
65
66#v1 = as.polygons(ext(0,1,0,1))
67#v2 = as.polygons(ext(1.01,2,0,1))
68#v <- rbind(v1, v2)
69#s = snap(v)
70
71