Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
    
Well thanks for your respons, I was about to use QGis to do this, I –  Sophie Padié Dec 17 '12 at 12:43
    
No problems with QGis, I just wanted to know how to do that with R, thanks again, Sophie –  Sophie Padié Dec 17 '12 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 '12 at 13:20
    
I've find similar questions, but nothing efficient with my script, Thanks again, –  Sophie Padié Dec 17 '12 at 13:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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