Skip to content

Commit

Permalink
Remove all dependencies to maptools,rgeos,rgdal + indexation vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementCalenge committed Apr 7, 2023
1 parent 76caba9 commit 5c5c4ad
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 2,187 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.gitignore
.Rhistory
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
*.rds
archives/

/doc/
/Meta/
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Package: adehabitatHR
Version: 0.4.19
Date: 2021-01-09
Depends: R (>= 3.0.1), sp, methods, deldir, ade4, adehabitatMA, adehabitatLT
Suggests: maptools, tkrplot, MASS, rgeos
Version: 0.4.21
Date: 2023-04-06
Depends: R (>= 3.0.1), sp, methods, ade4, adehabitatMA, adehabitatLT
Suggests: tkrplot, MASS, sf, deldir
Imports: graphics, grDevices, stats
Title: Home Range Estimation
Author: Clement Calenge, contributions from Scott Fortmann-Roe
Maintainer: Clement Calenge <clement.calenge@ofb.gouv.fr>
Description: A collection of tools for the estimation of animals home range.
VignetteBuilder: utils
License: GPL (>= 2)
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
useDynLib(adehabitatHR, .registration=TRUE)

import(sp,adehabitatLT,methods,deldir,ade4,adehabitatMA)
import(sp,adehabitatLT,methods,ade4,adehabitatMA)
importFrom("graphics", "box", "grid", "image", "lines", "par", "title")
importFrom("grDevices", "chull", "contourLines", "grey", "n2mfrow")
importFrom("stats", "na.omit", "quantile", "var")
Expand Down
29 changes: 12 additions & 17 deletions R/CharHull.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
xy <- as.data.frame(xy)
names(xy) <- c("x","y")
tri <- deldir(xy[,1], xy[,2])
tri <- triang.list(tri)
tri <- deldir::deldir(xy[,1], xy[,2])
tri <- deldir::triang.list(tri)
tri <- lapply(1:length(tri), function(i) {
rbind(tri[[i]], tri[[i]][1,])
})
Expand Down Expand Up @@ -77,21 +77,16 @@ CharHull <- function(xy, unin = c("m", "km"), unout = c("ha", "m2", "km2"),

resa <- .charhull(x)

pol <- as(resa, "SpatialPolygons")
lip <- list(pol[1])
for (j in 2:nrow(resa)) {
poo <- rbind(pol[j], lip[[j-1]])
pls <- slot(poo, "polygons")
pls1 <- lapply(pls, maptools::checkPolygonsHoles)
slot(poo, "polygons") <- pls1
lip[[j]] <- rgeos::gUnionCascaded(poo, id = rep(j,
length(row.names(poo))))
resasf <- sf::st_as_sf(resa)
lipsf <- list(sf::st_sf(data.frame(id=1, sf::st_geometry(resasf[1,]))))
for (j in 2:nrow(resasf)) {
lipsf[[j]] <- sf::st_sf(data.frame(id=j,sf::st_union(resasf[1:j,])))
}
are <- .arcpspdf(lip[[1]])
for (j in 2:length(lip)) {
are[j] <- .arcpspdf(lip[[j]])
lipsfg <- as(do.call(rbind, lipsf), "Spatial")
are <- .arcpspdf(lipsfg[1,])
for (j in 2:length(lipsfg)) {
are[j] <- .arcpspdf(lipsfg[j,])
}
spP <- do.call("rbind", lip)
if (unin == "m") {
if (unout == "ha")
are <- are/10000
Expand All @@ -104,8 +99,8 @@ CharHull <- function(xy, unin = c("m", "km"), unout = c("ha", "m2", "km2"),
if (unout == "m2")
are <- are * 1e+06
}
df <- data.frame(area = are, percent = resa[[2]])
resa <- SpatialPolygonsDataFrame(spP, df)
df <- data.frame(area = are, percent = resasf[[2]])
resa <- SpatialPolygonsDataFrame(as(lipsfg,"SpatialPolygons"), df)
if (!is.na(pfs))
proj4string(resa) <- CRS(pfs)
res[[i]] <- resa
Expand Down
92 changes: 46 additions & 46 deletions R/LoCoH.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

## k-Locoh

LoCoH.k <- function(xy, k=5, unin = c("m", "km"),
unout = c("ha", "m2", "km2"),
duplicates=c("random","remove"), amount = NULL)
Expand Down Expand Up @@ -83,31 +81,26 @@ LoCoH.k <- function(xy, k=5, unin = c("m", "km"),
oo <- oo[order(ar),]
ar <- ar[order(ar)]


## then, "incremental" union:
lip <- list(SpatialPolygons(list(Polygons(list(Polygon(rbind(pol[[1]], pol[[1]][1,]))), 1))))
n <- k
lipsf <- lapply(1:length(pol), function(i)
sf::st_sf(data.frame(id=i,sf::st_sfc(sf::st_polygon(list(rbind(pol[[i]], pol[[i]][1,])))))))
dej <- unlist(oo[1,])

n <- k
lipsf2 <- list(lipsf[[1]])
for (i in 2:nrow(xy)) {
poo <- rbind(SpatialPolygons(list(Polygons(list(Polygon(rbind(pol[[i]],pol[[i]][1,]))), i))), lip[[i-1]])
pls <- slot(poo, "polygons")
pls1 <- lapply(pls, maptools::checkPolygonsHoles)
slot(poo, "polygons") <- pls1
lip[[i]] <- rgeos::gUnionCascaded(poo, id=rep(i, length(row.names(poo))))
## Union des polygones progressive
lipsf2[[i]] <- sf::st_sf(data.frame(id=i,sf::st_union(rbind(lipsf[[i]],lipsf2[[i-1]]))))
dej <- c(dej, unlist(oo[i,!(unlist(oo[i,])%in%dej)]))
n[i] <- length(dej)
}
lipsfg <- as(do.call(rbind, lipsf2), "Spatial")

## Compute the area
are <- .arcpspdf(lip[[1]])
for (i in 2:nrow(xy)) {
are[i] <- .arcpspdf(lip[[i]])
are <- .arcpspdf(lipsfg[1,])
for (j in 2:length(lipsfg)) {
are[j] <- .arcpspdf(lipsfg[j,])
}

## And the results, as a SpatialPolygonDataFrame object
spP <- do.call("rbind", lip)

## The data frame:
if (unin == "m") {
if (unout == "ha")
are <- are/10000
Expand All @@ -122,7 +115,8 @@ LoCoH.k <- function(xy, k=5, unin = c("m", "km"),
}

df <- data.frame(area=are, percent=100*n/nrow(xy))
res <- SpatialPolygonsDataFrame(spP, df)
res <- SpatialPolygonsDataFrame(lipsfg, df)

if (!is.na(pfs))
proj4string(res) <- CRS(pfs)
return(res)
Expand Down Expand Up @@ -261,7 +255,6 @@ MCHu.rast <- function(x, spdf, percent=100)


### R LoCoH

LoCoH.r <- function(xy, r, unin = c("m", "km"),
unout = c("ha", "m2", "km2"),
duplicates=c("random","remove"), amount = NULL)
Expand Down Expand Up @@ -358,33 +351,38 @@ LoCoH.r <- function(xy, r, unin = c("m", "km"),
if (max==0)
stop("the distance is too small: there were only isolated points\nPlease increase the value of r")



## then, "incremental" union:
lip <- list(SpatialPolygons(list(Polygons(list(Polygon(rbind(pol[[1]], pol[[1]][1,]))), 1))))
lipsf <- lapply(1:length(pol), function(i) {
if (nrow(pol[[i]])>2) {
return(sf::st_sf(data.frame(id=i,sf::st_sfc(sf::st_polygon(list(rbind(pol[[i]], pol[[i]][1,])))))))
} else {
return(NULL)
}
})
dej <- oo[[1]]
n <- num[1]

for (i in 2:nrow(xy)) {
if (nrow(pol[[i]])>2) {
poo <- rbind(SpatialPolygons(list(Polygons(list(Polygon(rbind(pol[[i]],pol[[i]][1,]))), i))), lip[[i-1]])
if (!is.null(lipsf[[i]])) {
poo <- rbind(lipsf[[i]], lipsf[[i-1]])
} else {
poo <- lip[[i-1]]
poo <- lipsf[[i-1]]
}
pls <- slot(poo, "polygons")
pls1 <- lapply(pls, maptools::checkPolygonsHoles)
slot(poo, "polygons") <- pls1

lip[[i]] <- rgeos::gUnionCascaded(poo, id=rep(i, length(row.names(poo))))
lipsf[[i]] <- sf::st_sf(data.frame(id=i,sf::st_geometry(sf::st_union(poo))))
dej <- c(dej, oo[[i]][!(oo[[i]]%in%dej)])
n[i] <- length(dej)
}

## Compute the area
are <- .arcpspdf(lip[[1]])
are <- .arcpspdf(as(lipsf[[1]], "Spatial"))
for (i in 2:nrow(xy)) {
are[i] <- .arcpspdf(lip[[i]])
are[i] <- .arcpspdf(as(lipsf[[i]], "Spatial"))
}

## And the results, as a SpatialPolygonDataFrame object
spP <- do.call("rbind", lip)
spP <- as(do.call("rbind", lipsf),"Spatial")

## The data frame:
n[n>max] <- max
Expand Down Expand Up @@ -491,7 +489,6 @@ LoCoH.r.area <- function(xy, rrange, percent=100, unin = c("m", "km"),


### A LoCoH

LoCoH.a <- function(xy, a, unin = c("m", "km"),
unout = c("ha", "m2", "km2"),
duplicates=c("random","remove"), amount = NULL)
Expand Down Expand Up @@ -586,35 +583,37 @@ LoCoH.a <- function(xy, a, unin = c("m", "km"),
num <- num[ind]

## then, "incremental" union:
lip <- list(SpatialPolygons(list(Polygons(list(Polygon(rbind(pol[[1]], pol[[1]][1,]))), 1))))
lipsf <- lapply(1:length(pol), function(i) {
if (nrow(pol[[i]])>2) {
return(sf::st_sf(data.frame(id=i,sf::st_sfc(sf::st_polygon(list(rbind(pol[[i]], pol[[i]][1,])))))))
} else {
return(NULL)
}
})
dej <- oo[[1]]
n <- num[1]

for (i in 2:nrow(xy)) {
if (nrow(pol[[i]])>3) {
poo <- rbind(SpatialPolygons(list(Polygons(list(Polygon(rbind(pol[[i]],pol[[i]][1,]))), i))), lip[[i-1]])
if (!is.null(lipsf[[i]])) {
poo <- rbind(lipsf[[i]], lipsf[[i-1]])
} else {
poo <- lip[[i-1]]
poo <- lipsf[[i-1]]
}
pls <- slot(poo, "polygons")
pls1 <- lapply(pls, maptools::checkPolygonsHoles)
slot(poo, "polygons") <- pls1

lip[[i]] <- rgeos::gUnionCascaded(poo, id=rep(i, length(row.names(poo))))
lipsf[[i]] <- sf::st_sf(data.frame(id=i,sf::st_geometry(sf::st_union(poo))))
dej <- c(dej, oo[[i]][!(oo[[i]]%in%dej)])
n[i] <- length(dej)
}

## Compute the area
are <- .arcpspdf(lip[[1]])
are <- .arcpspdf(as(lipsf[[1]], "Spatial"))
for (i in 2:nrow(xy)) {
are[i] <- .arcpspdf(lip[[i]])
are[i] <- .arcpspdf(as(lipsf[[i]], "Spatial"))
}

## And the results, as a SpatialPolygonDataFrame object
spP <- do.call("rbind", lip)
spP <- as(do.call("rbind", lipsf),"Spatial")

## The data frame:
df <- data.frame(area=are, percent=100*n/nrow(xy))
if (unin == "m") {
if (unout == "ha")
are <- are/10000
Expand All @@ -629,6 +628,7 @@ LoCoH.a <- function(xy, a, unin = c("m", "km"),
}


df <- data.frame(area=are, percent=100*n/nrow(xy))
res <- SpatialPolygonsDataFrame(spP, df)
if (!is.na(pfs))
proj4string(res) <- CRS(pfs)
Expand Down
15 changes: 0 additions & 15 deletions inst/CITATION

This file was deleted.

2 changes: 1 addition & 1 deletion man/LoCoH.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ LoCoH.a.area(xy, arange, percent=100, unin = c("m", "km"),
}
\note{

These functions rely on the packages rgeos, gpclib, and maptools.
These functions rely on the package sf.

The LoCoH family of methods for locating Utilization Distributions
consists of three algorithms: Fixed k LoCoH, Fixed r LoCoH, and
Expand Down
2 changes: 1 addition & 1 deletion man/kernelUD.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ estUDm2spixdf(x)

Silverman, B.W. (1986)
\emph{Density estimation for statistics and data
analysis}. London: Chapman \& Hall.
analysis}. London: Chapman and Hall.

Worton, B.J. (1989) Kernel methods for estimating the utilization
distribution in home-range studies. \emph{Ecology}, \bold{70}, 164--168.
Expand Down
24 changes: 12 additions & 12 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ void prodmatAAtB (double **a, double **b)
void prodmatAtBrandomC (double **a, double **b, double **c, int*permut)
/*--------------------------------------------------
* Produit matriciel AtB
* les lignes de B sont permutÚes par la permutation permut
* les lignes de B sont permutes par la permutation permut
--------------------------------------------------*/
{
int j, k, i, i0, lig, col, col2;
Expand Down Expand Up @@ -1353,7 +1353,7 @@ void CVmise(int *nloc, double *xlo, double *ylo,
int i, nlo, nh, ndist;
double *xl, *yl, h, *dists;

/* Allocation de mémoire */
/* Allocation de memoire */
nlo = *nloc;
nh = *nhteste;

Expand Down Expand Up @@ -2691,7 +2691,7 @@ SEXP filtreLmin(SEXP df, double Lmin, SEXP PA2, SEXP fll)
int nrow, i, k, nfl, fl;
double sx, sy, ggg;

/* récup des vecteurs */
/* recup des vecteurs */
PROTECT(x = coerceVector(VECTOR_ELT(df,0), REALSXP));
PROTECT(y = coerceVector(VECTOR_ELT(df,1), REALSXP));
PROTECT(date = coerceVector(VECTOR_ELT(df,2), REALSXP));
Expand All @@ -2716,13 +2716,13 @@ SEXP filtreLmin(SEXP df, double Lmin, SEXP PA2, SEXP fll)
}
}

/* allocation mémoire */
/* allocation memoire */
PROTECT(xn = allocVector(REALSXP, k));
PROTECT(yn = allocVector(REALSXP, k));
PROTECT(daten = allocVector(REALSXP, k));
PROTECT(PAn = allocVector(REALSXP, k));

/* préparation de la boucle */
/* preparation de la boucle */
REAL(xn)[0] = REAL(x)[0];
REAL(yn)[0] = REAL(y)[0];
REAL(daten)[0] = REAL(date)[0];
Expand All @@ -2739,8 +2739,8 @@ SEXP filtreLmin(SEXP df, double Lmin, SEXP PA2, SEXP fll)

for (i=1; i< nrow; i++) {

/* si la distance entre localisations successives est inférieure à Lmin, on
se contente d'en calculer la moyenne (que l'on stockera à la première date)
/* si la distance entre localisations successives est inferieure a Lmin, on
se contente d'en calculer la moyenne (que l'on stockera a la premiere date)
*/
ggg = 1;
if (length(PA) > 1) {
Expand All @@ -2753,8 +2753,8 @@ SEXP filtreLmin(SEXP df, double Lmin, SEXP PA2, SEXP fll)
sy = sy + REAL(y)[i];
} else {

/* Si la nouvelle localisation est la première éloignée après un clusters de Lmin,
On commence par stocker la précédente (moyenne des positions)
/* Si la nouvelle localisation est la premiere eloignee apres un clusters de Lmin,
On commence par stocker la precedente (moyenne des positions)
*/
if (nfl > 1) {
REAL(xn)[k] = sx/((double) nfl);
Expand All @@ -2774,13 +2774,13 @@ SEXP filtreLmin(SEXP df, double Lmin, SEXP PA2, SEXP fll)
REAL(PAn)[k] = 1;
}

/* et au cas , on garde cette loc en mémoire */
/* et au cas ou, on garde cette loc en memoire */
sx = REAL(x)[i];
sy = REAL(y)[i];
}
}

/* On renvoie alors les résultats dans un data.frame */
/* On renvoie alors les resultats dans un data.frame */
PROTECT(df2 = allocVector(VECSXP, 4));
SET_VECTOR_ELT(df2, 0, xn);
SET_VECTOR_ELT(df2, 1, yn);
Expand Down Expand Up @@ -2962,7 +2962,7 @@ SEXP fillsegments(SEXP df2, SEXP Tmaxr, SEXP taur, SEXP hminr, SEXP D, SEXP Lmin
}


/* On calcule maintenant, sur la base d'une grille passée, l'estimation kernel */
/* On calcule maintenant, sur la base d'une grille passee, l'estimation kernel */
SEXP mkde(SEXP xyh, SEXP grid)
{

Expand Down
Loading

0 comments on commit 5c5c4ad

Please sign in to comment.