4

I'm trying to convert my map (a SpatialPolygonsDataFrame) into a raster, using rasterize function, here is a bug that I don't understand...

> class(parcel) # this is my map
[1] "SpatialPolygonsDataFrame"

> attr(,"package")
[1] "sp"

> head(parcel)
AREAHA RECNO      AREA AREASQKM        OS_06 GRD_CAT_06
0 0.0519     0  519.1941        0 chemin prive        way
1 0.0127     1  127.0963        0 chemin prive        way
2 0.0596     2  596.2204        0 chemin prive        way
3 0.0248     3  248.0452        0    route D90        way
4 0.0375     4  375.0141        0 route privee        way
5 0.1260     5 1260.3941        0 chemin prive        way

> dim(parcel)
[1] 20069     6

> object.size(parcel)
88090144 bytes

> r <- raster(ncol=4001, nrow=4001)
> extent(r) <- extent(parcel)
> parcel_ras<-rasterize(parcel, r, "GRD_CAT_06", CRS("+init=epsg:27572"))
Erreur dans data.frame(ID = as.integer(putvals), value = lvs) :    arguments imply differing number of rows: 20069, 13

I just don't understand this comment : what is "putvals" ? There are 20069 polygons in my map, so what is "13" ?

I can't reproduce a smaller example of my map, so here are my data : http://dl.free.fr/fxdT2oczB

1 Answer 1

5

Your error says as.integer(putvals). The R rasterize function can't work on Strings. You have to transform your data first.

Something like this may work, but i would assign different ranks (ala 1,2,3,4,..) to your data. However i still get a (different) error for which i don't have an explanation. Maybe the size of your raster is incorrect...

library(sp);library(rgdal);library(raster)

r <- raster(ncol=4001, nrow=4001)

extent(r) <- extent(parcel)

parcel$Grd_ranks <- rank(parcel$GRD_CAT_06)

parcel_ras<- rasterize(parcel, r, "Grd_ranks", CRS("+init=epsg:27572"),fun='first')

Maybe you should use the QGIS or GRASS rasterize function instead. Much less complicated ;-)

4
  • Well thanks for your respons, I was about to use QGis to do this, I Dec 17, 2012 at 12:43
  • No problems with QGis, I just wanted to know how to do that with R, thanks again, Sophie Dec 17, 2012 at 12:44
  • Please consider voting up my post / accepting. This is how the system works here. Furthmore you should search for similar questions to yours prior to asking. If you look to the right you'll find many similiar questions.
    – Curlew
    Dec 17, 2012 at 13:20
  • I've find similar questions, but nothing efficient with my script, Thanks again, Dec 17, 2012 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.