Comments (3)
Thanks. There was a problem with numeric (and factor) ids. Update cartography on your computer:
require(devtools)
devtools::install_github("Groupe-ElementR/cartography")
and it should be ok.
from cartography.
Merci, cela corrige bien le problème que j'avais.
J'ai modifié getGridLayer pour avoir des mailles en projection plane alignées sur le carroyage. Par exemple, pour un pas de 1000 mètres, je voulais avoir les coordonnées des angles en multiple de 1000.
Je mets le code si cela peut servir à d'autres
`getGridLayer2 <- function(spdf, cellsize, spdfid = NULL) {
print(sprintf("getGridLayer2() début"))
if (!requireNamespace("rgeos", quietly = TRUE)) {
stop("'rgeos' package needed for this function to work. Please install it.",
call. = FALSE)
}
print(str(spdf@data))
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]
print(str(boundingBox))
boxCoordX <- seq(from = boundingBox[1,1]-cellsize/2, to = boundingBox[1,2]+cellsize/2, by = cellsize)
boxCoordY <- seq(from = boundingBox[2,1]-cellsize/2, to = boundingBox[2,2]+cellsize/2, by = cellsize)
spatGrid <- expand.grid(boxCoordX, boxCoordY)
spatGrid$id <- seq(1, nrow(spatGrid), 1)
coordinates(spatGrid) <- 1:2 # promote to SpatialPointsDataFrame
gridded(spatGrid) <- TRUE # promote to SpatialPixelsDataFrame
spgrid <- methods::as(spatGrid, "SpatialPolygonsDataFrame") # promote to SpatialPolygonDataFrame
proj4string(spgrid) <-proj4string(spdf)
row.names(spgrid) <- as.character(spgrid$id)
On ne garde que ce qui touche le fond de carte initial
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.0, quadsegs=5, capStyle="ROUND",joinStyle="ROUND", mitreLimit=1.0)
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)
On calcule la table de passage
intersection
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)
Part de surface intersectée
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
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.