Comments (2)
Sans exemple reproductible il m'est difficile de vous aider, mais votre modification semble marcher.
Si vous utilisez la fonction suivante (j'ai juste remplacé les deux ligne en question par les votre) vous devriez obtenir ce que vous souhaitez :
getGridLayer2 <- function (spdf, cellsize, spdfid = NULL)
{
if (!requireNamespace("rgeos", quietly = TRUE)) {
stop("'rgeos' package needed for this function to work. Please install it.",
call. = FALSE)
}
if (is.null(spdfid)) {
spdfid <- names(spdf@data)[1]
}
spdf@data <- spdf@data[spdfid]
row.names(spdf@data) <- spdf@data[, spdfid]
spdf <- spChFIDs(spdf, spdf@data[, spdfid])
spdf@data$area <- rgeos::gArea(spdf, byid = TRUE)
boundingBox <- bbox(spdf)
rounder <- boundingBox%%cellsize
boundingBox[, 1] <- boundingBox[, 1] - rounder[, 1]
roundermax <- cellsize - rounder[, 2]
boundingBox[, 2] <- boundingBox[, 2] + cellsize - rounder[,
2]
## modification
boxCoordX <- seq(from = boundingBox[1,1]-500,
to = boundingBox[1,2]+500, by = cellsize)
boxCoordY <- seq(from = boundingBox[2,1]-500,
to = boundingBox[2,2]+500, by = cellsize)
##
spatGrid <- expand.grid(boxCoordX, boxCoordY)
spatGrid$id <- seq(1, nrow(spatGrid), 1)
coordinates(spatGrid) <- 1:2
gridded(spatGrid) <- TRUE
spgrid <- methods::as(spatGrid, "SpatialPolygonsDataFrame")
proj4string(spgrid) <- proj4string(spdf)
row.names(spgrid) <- as.character(spgrid$id)
over <- rgeos::gIntersects(spgrid, spdf, byid = TRUE)
x <- colSums(over)
spgrid <- spgrid[spgrid$id %in% names(x[x > 0]), ]
mask <- rgeos::gBuffer(spdf, byid = FALSE, id = NULL, width = 1,
quadsegs = 5, capStyle = "ROUND", joinStyle = "ROUND",
mitreLimit = 1)
spgrid <- rgeos::gIntersection(spgrid, mask, byid = TRUE,
id = as.character(spgrid@data$id), drop_lower_td = FALSE)
data <- data.frame(id = sapply(methods::slot(spgrid, "polygons"),
methods::slot, "ID"))
row.names(data) <- data$id
spgrid <- SpatialPolygonsDataFrame(spgrid, data)
spgrid@data$cell_area <- rgeos::gArea(spgrid, byid = TRUE)
proj4string(spgrid) <- proj4string(spdf)
parts <- rgeos::gIntersection(spgrid, spdf, byid = TRUE,
drop_lower_td = TRUE)
data <- data.frame(id = sapply(methods::slot(parts, "polygons"),
methods::slot, "ID"))
tmp <- data.frame(do.call("rbind", (strsplit(as.character(data$id),
" "))))
data$id1 <- as.vector(tmp$X1)
data$id2 <- as.vector(tmp$X2)
row.names(data) <- data$id
parts <- SpatialPolygonsDataFrame(parts, data)
proj4string(parts) <- proj4string(spdf)
parts@data$area_part <- rgeos::gArea(parts, byid = TRUE)
parts@data <- data.frame(parts@data, area_full = spdf@data[match(parts@data$id2,
spdf@data[, spdfid]), "area"])
parts@data$area_pct <- (parts@data$area_part/parts@data$area_full) *
100
areas <- parts@data[, c("id1", "id2", "area_pct")]
colnames(areas) <- c("id_cell", "id_geo", "area_pct")
return(list(spdf = spgrid, df = areas))
}
from cartography.
Merci pour votre réponse
En faisant un exemple complet, je n'ai plus eu l'erreur. J'ai donc cherché dans les scripts inclus de mon programme initial, et j'ai trouvé une surcharge de la fonction "names".
L'erreur provenait donc de mon code.
Encore merci pour votre bibliothèque
from cartography.
Related Issues (20)
- histogram legend HOT 5
- Legend not reflecting breaks when breaks have decimals? HOT 5
- using 'custom' colors? HOT 1
- CRS issues HOT 5
- orphaned hole, cannot find containing polygon HOT 1
- Error in labelLayer when nrow(x) == 1 & overlap = FALSE HOT 1
- New features HOT 6
- Custom Tiles HOT 4
- missing corner case in cartography::getTiles HOT 1
- add more methods to getBreaks HOT 6
- Format legends on layers
- Bug on waffleLayer
- colored pencil maps with r by using HOT 3
- spikeLayer() ? HOT 2
- Facets HOT 2
- Symbols sizes in the legend.
- Add alpha channel to pngs HOT 1
- Can cartography create curved road labels, and loop base maps of different extents? HOT 3
- Please remove dependencies on **rgdal**, **rgeos**, and/or **maptools** HOT 2
- Problème création carte Guadeloupe HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cartography.