[R-sig-Geo] How to clip and interpolate data using shapefile as a boundary

Robert J. Hijmans r.hijmans at gmail.com
Tue Oct 19 22:19:59 CEST 2010


Thiago,

You could get the bbox(polygon) and then subset the points for the
area of the polygon (keeping points just outside of it) and use these
points in e.g. gstat if you want to interpolate.

You can also turn this into a SpatialPointsDataFrame object and use
gridded(obj) = TRUE

In raster, to avoid the strips, you could try:
rprecip<-raster(xmn=-82.90,xmx=-34.30,ymn=-50.30,ymx=12.10)
res(rprecip) = 0.2
(i.e. assure that the resolution matches your data, and that points
fall in the center, not the intersections of, grid cell).

Hope this helps,

Robert


On Tue, Oct 19, 2010 at 1:06 PM, Thiago Veloso <thi_veloso at yahoo.com.br> wrote:
>   Thank you, Roman.
>   I was following that topic, but my case is slightly different. I have a xyz ascii file which needs to be converted to raster. With your tip I have read the reference manual of "raster" package and found the rasterfromXYZ function.
>   However, I am facing some troubles during this conversion. Below I am reproducing (with my data) the example demonstrated in the reference manual:
>> prec_ETA<-read.csv("AS_2010010112_obs20km.txt",sep=";",header=F)> prec_ETA<-na.omit(prec_ETA)> head(prec_ETA)     V1    V2   V31 -82.8 -50.2 0.002 -82.8 -50.0 1.363 -82.8 -49.8 2.864 -82.8 -49.6 3.635 -82.8 -49.4 3.266 -82.8 -49.2 3.40> rprecip<-raster(nrow=244,ncol=312,xmn=-82.80,xmx=-34.20,ymn=-50.20,ymx=12.00)> cells <- cellFromXY(rprecip, prec_ETA[,1:2])> rprecip[cells] <- prec_ETA[,3]> plot(rprecip)
>   When plotting the resulting raster, something seems to be wrong. The map contains unwanted strips, as seen at this link: http://img140.imageshack.us/img140/4994/plotrprecip.png
>   So, I don't know where I'm going wrong... Any ideas?
>   Cheers,
>   Thiago.
> --- On Tue, 19/10/10, Roman Luštrik <roman.lustrik at gmail.com> wrote:
>
> From: Roman Luštrik <roman.lustrik at gmail.com>
> Subject: Re: [R-sig-Geo] How to clip and interpolate data using shapefile as a boundary
> To: "Thiago Veloso" <thi_veloso at yahoo.com.br>
> Cc: "R SIG list" <r-sig-geo at stat.math.ethz.ch>
> Date: Tuesday, 19 October, 2010, 9:58
>
> You can easily clip rasters with polygons (or other rasters) via raster package.
>
> See this recent thread for one example using crop() and polygonsToRaster() functions. Other than that, the packages has comprehensive vignettes and maybe that's the place to start.
>
>
>
> Cheers,
> Roman
>
>
>
>
> On Tue, Oct 19, 2010 at 1:41 PM, Thiago Veloso <thi_veloso at yahoo.com.br> wrote:
>
>
>   Dear R-SIGgers,
>
>   My current rainfall dataset (ascii, at 0.2 degree resolution) spans the entire South America and has been loaded into R as a xyz variable:
>
>> summary(prec_data)       V1               V2               V3          Min.   :-82.80   Min.   :-50.20   Min.   :  0.000   1st Qu.:-70.65   1st Qu.:-34.65   1st Qu.:  0.000   Median :-58.50   Median :-19.10   Median :  0.000   Mean   :-58.50   Mean   :-19.10   Mean   :  4.002   3rd Qu.:-46.35   3rd Qu.: -3.55   3rd Qu.:  2.430   Max.   :-34.20   Max.   : 12.00   Max.   :250.150  > head(prec_data)     V1    V2   V31 -82.8 -50.2 0.002 -82.8 -50.0 1.363 -82.8 -49.8 2.864 -82.8 -49.6 3.635 -82.8 -49.4 3.266 -82.8 -49.2 3.40>
>
>
>
>   My objectives are the following:
>
>   1) Clip the data using a shapefile;
>
>   2) In order to completely fill the area with rainfall information, perform a interpolation technique (IDW or kriging) using as boundary the same shapefile above. This will allow me to generate risk maps for the occurence of a certain plant disease.
>
>
>
>   Could anybody please point me the directions on how to do this?
>
>   Thanks in advance,
>
>   Thiago.
>
>
>
>
>
>
>
>         [[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
>
>
>
>
>
> --
> In God we trust, all others bring data.
>
>
>
>
>
>        [[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