[R-sig-Geo] rasterize shape file and output as .img

Jim Burke j.burke at earthlink.net
Mon Apr 6 05:46:10 CEST 2009


Milton,

Consider, and implement from the example
below from from R tip a day. It outputs
as an png.
http://onertipaday.blogspot.com/

R PROGRAM. I called mine original_r.txt
original <- read.table("original.txt",row.names=1)
# outputs a nicely sized png to the file below.
png("immigration_barplot_me.png", width = 1419, height = 736)
library(RColorBrewer) # take a look at 
http://www.personal.psu.edu/cab38/ColorBrewer/ColorBrewer_intro.html
# display.brewer.all()
FD.palette <- c("#984EA3","#377EB8","#4DAF4A","#FF7F00","#E41A1C")
options(scipen=10)
par(mar=c(6, 6, 3, 3), las=2)
data4bp <- t(original[,c(5,4,2,3,1)])
barplot( data4bp, beside=F,col=FD.palette, border=FD.palette, space=1, 
legend=F, ylab="Number of People", main="Migration to the United States 
by Source Region (1820 - 2006)", mgp=c(4.5,1,0) )
legend( "topleft", legend=rev(rownames(data4bp)), fill=rev(FD.palette) )
box()
dev.off()

R DATA - save it as ORIGINAL.TXT
Europe Asia Americas Africa Oceania
1820-30 106487 36 11951 17 33333
1831-40 495681 53 33424 54 69911
1841-50 1597442 141 62469 55 53144
1851-60 2452577 41538 74720 210 29169
1861-70 2065141 64759 166607 312 18005
1871-80 2271925 124160 404044 358 11704
1881-90 4735484 69942 426967 857 13363
1891-00 3555352 74862 38972 350 18028
1901-10 8056040 323543 361888 7368 46547
1911-20 4321887 247236 1143671 8443 14574
1921-30 2463194 112059 1516716 6286 8954
1931-40 347566 16595 160037 1750 2483
1941-50 621147 37028 354804 7367 14693
1951-60 1325727 153249 996944 14092 25467
1961-70 1123492 427642 1716374 28954 25215
1971-80 800368 1588178 1982735 80779 41254
1981-90 761550 2738157 3615225 176893 46237
1991-00 1359737 2795672 4486806 354939 98263
2001-06 1073726 2265696 3037122 446792 185986

Hope this helps,
Jim Burke





milton ruser wrote:
> Dear Roger,
>
> Sorry for my delay.
>
> I still can't do that. Please, see below the steps and the error message.
> I shapefile have only one polygon, and only one collumn (CoverClass).
>
>   
>> reser<-readOGR(dsn=".", layer="ARN_026_214pt5m")
>>     
> OGR data source with driver: ESRI Shapefile
> Source: ".", layer: "ARN_026_214pt5m"
> with  1  rows and  3  columns
> Feature type: wkbPolygon with 2 dimensions
>   
>> reser.df<-reser at data
>> reser.df$ClassCover<-1
>> reser.df<-subset(reser.df, select=c(ClassCover))
>> reser at data<-reser.df <reser at data%3C-reser.df>
>> reser at data
>>     
>   ClassCover
> 0          1
>   
>> reser.SG <- Sobj_SpatialGrid(reser)$SG
>>
>> plot(reser.SG)
>> plot(reser, add=T, col="red")
>> str(reser.SG)
>>     
> Formal class 'SpatialGrid' [package "sp"] with 5 slots
>   ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
>   .. .. ..@ cellcentre.offset: Named num [1:2] 371506 8764564
>   .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
>   .. .. ..@ cellsize         : Named num [1:2] 941 941
>   .. .. .. ..- attr(*, "names")= chr [1:2] "max" "max"
>   .. .. ..@ cells.dim        : int [1:2] 100 75
>   ..@ grid.index : int(0)
>   ..@ coords     : num [1:2, 1:2] 371506 464669 8764564 8834201
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : NULL
>   .. .. ..$ : chr [1:2] "x" "y"
>   ..@ bbox       : num [1:2, 1:2] 371035 8764093 465139 8834672
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : chr [1:2] "x" "y"
>   .. .. ..$ : chr [1:2] "min" "max"
>   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>   .. .. ..@ projargs: chr NA
>   
>> o <- overlay(reser, reser.SG)
>> o
>>     
>      ClassCover
> NA           NA
> NA.1         NA
>   
>> reser.SGDF <- SpatialGridDataFrame(slot(reser.SG, "grid"),
>>     
> +   proj4string=CRS(proj4string(reser.SG)), data=o)
> Error in validityMethod(object) :
>   unequal number of objects in full grid and data slot
>   
>
>
>   
>> sessionInfo()
>>     
> R version 2.9.0 alpha (2009-03-26 r48224)
> i386-pc-mingw32
> locale:
> LC_COLLATE=English_Canada.1252;LC_CTYPE=English_Canada.1252;LC_MONETARY=English_Canada.1252;LC_NUMERIC=C;LC_TIME=English_Canada.1252
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> other attached packages:
> [1] raster_0.8.9-6  maptools_0.7-20 foreign_0.8-34  rgdal_0.6-7
> [5] sp_0.9-32
> loaded via a namespace (and not attached):
> [1] grid_2.9.0      lattice_0.17-20
>
> Best wishes,
>
> milton
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>



More information about the R-sig-Geo mailing list