Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getGridLayer : alignement des côtés des mailles #11

Closed
mgageo opened this issue Nov 26, 2015 · 2 comments
Closed

getGridLayer : alignement des côtés des mailles #11

mgageo opened this issue Nov 26, 2015 · 2 comments
Labels

Comments

@mgageo
Copy link

mgageo commented Nov 26, 2015

Je viens d'utiliser cette fonction pour générer une grille au pas de 1000 mètres.
Les coordonnées du centre des mailles sont en multiple de 1000, ce qui fait que les côtés sont décalés de 500 mètres.
Je voudrais obtenier un maillage standard Lambert93 avec les côtés en multple de 1000.
J'ai recopié la fonction getGridLayer, pour faire cette 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)
mais j'ai une erreur avant ces instructions
Error in names(spdf@data) : argument inutilisé spdf@data)

Je suis en Windows10/R 3.2.2/cartography 1.1

Et sinon un grand merci pour ce package qui facilite beaucoup de traitements

@rCarto
Copy link
Member

rCarto commented Nov 30, 2015

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))
}

@rCarto rCarto closed this as completed Nov 30, 2015
@mgageo
Copy link
Author

mgageo commented Nov 30, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants