Hello Netra, how are you?

As it was pointed to me by Jon Olav Skoein in another post, it is possible
to do this by using the over funtion and the interpolationLocations
parameter of interpolate.

Basically the idea is to get the locations inside the polygons in your
shapefile and pass those as interpolationLocations.
Here's a code sample (which you may have to edit a bit for your particular
case) that works for me.

# coordsAInterpolar are the coordinates you want to interpolate, in your
case I think it would be rst
shp <- readShapeSpatial(pathSHP,
proj4string=sp::CRS(projectionForTheShapefile))
sel <- !is.na(over(coordsAInterpolar, shp))[,1]
interpolation <- interpolate(observations=xyz, # I think you have to rename
your "z" field to "value"

predictionLocations=coordsAInterpolar[sel,],  # don't know if it works
directly with rst[sel,], you'll have to try this
                                      methodName="automap", # interpolation
method
                                      optList=list(nclus=numCoresToUse), #
for multicore interpolation, may help you with calculation times
                                      outputWhat=list(mean=T, variance=T))
# output variables

Notice that you'll get predicted values only for the points inside your
shape, the values outside it will be left untouched so you'll likely have
to fill them manually by doing something like

rst[sel]$z <- interpolacion$predictions@data[sel,1]
rst[-sel]$z <- NA

Best
Pablo



Ing. Pablo Alfaro Piñeyro
MotionSoft Consulting S.R.L.
Tel: 095 938 487



2013/10/31 netra <npt_eng@hotmail.com>

> Hello,
> I am doing a raster interpolation using the gstat and raster packages. My
> raster area is very big but within raster area I have a river and this is
> the smallest extent of raster that covers the river. I want to interpolate
> only inside river not in the whole raster area. Is there any ways that I
> could limit the analysis by using mask shape file or something?
>
> Thanks for your help
>
>
> code snippet
>
>                 #read raster
> rst <- raster("contor_to_dem_5m.asc")
>
> #define model
>                 mg <- gstat(id = "z", formula = z~1, locations = ~x+y,
> data=xyz,
>                                     nmax=7, set=list(idp = .2)) #
>
> #interpolate raster
>                 #this is time consuming :(
>                 z <- interpolate(rst, mg)
>
>
> Netra
>
>
>
>
> --
> View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/raster-interpolation-within-shape-file-tp7585003.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

