Giter Club home page Giter Club logo

Comments (3)

rCarto avatar rCarto commented on August 18, 2024

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.

mgageo avatar mgageo commented on August 18, 2024

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.

rCarto avatar rCarto commented on August 18, 2024

merci

from cartography.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.