GithubHelp home page GithubHelp logo

Comments (2)

rCarto avatar rCarto commented on May 20, 2024

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.

mgageo avatar mgageo commented on May 20, 2024

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)

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.