[R-sig-Geo] apply() proj4string to list of rasters

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Nov 1 18:59:44 CET 2016


You can do it by rewriting `projection(r) <- Z` as a function, and
assigning it back to the same name. I think this captures the essence:

Two rasters with default projections (epsg:4326)

 > r1 = raster()
 > r2 = raster()

Loop over names, get, set projection, re-assign:

 > for(rname in c("r1","r2")){
     assign(rname, "projection<-"(get(rname),"+init=epsg:27700"))
   }

See:

 > projection(r1)
[1] "+init=epsg:27700 +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717
+x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy
+towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894"

and the same for r2.

There's usually more than one way to do this but this seems to work....

Of course you should have stored your rasters in a plain old R list,
then it would just be:

for(i in 1:length(rlist)){
 projection(rasters[[i]]) = whatever
}




On Tue, Nov 1, 2016 at 4:37 PM, john polo <jpolo at mail.usf.edu> wrote:
> R-folks,
>
> I imported >1000 rasters with
>
> rlist <- list.files(pattern=".ht.laz.bil")
>
> for (i in rlist) { assign(unlist(strsplit(i,"[.]"))[1], raster(i)) }
>
>
> These rasters do not have a coordinate reference system.
>
>> ras895500_3965500ht
> class       : RasterLayer
> dimensions  : 34, 15, 510  (nrow, ncol, ncell)
> resolution  : 5, 5  (x, y)
> extent      : 895500, 895575, 3965830, 3966000  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> data source : k:\outLiDAR\normalized\ras895500_3965500ht.laz.bil
> names       : ras895500_3965500ht.laz
>
>
> The rasters don't have the same extent, I can't use stack().
>
> I tried
>
> epsgm <- make_EPSG()
> adpro <- epsgm[3587,"prj4"]
> lapply(ls(pattern="ht"), function(x) {proj4string(x) <- adpro})
>
> Error in (function (classes, fdef, mtable)  :
>   unable to find an inherited method for function ‘proj4string<-’ for
> signature ‘"character", "character"’
>
> I searched and found a possible solution with mapply():
>
> rrlis <- ls(patter="ht")
>> pfix4
> function(x) proj4string(x) <- adpro
>> mapply(pfix4, rrlis)
> Error in (function (classes, fdef, mtable)  :
>   unable to find an inherited method for function ‘proj4string<-’ for
> signature ‘"character", "character"’
>
> My problem apparently is trying to assign to a character. If I make a list
> of the raster objects, the lists are of characters and the functions won't
> work on those. How can I iterate through the raster objects in memory to
> apply a function?
>
>
> John
>
>
> --
> Men occasionally stumble
> over the truth, but most of them
> pick themselves up and hurry off
> as if nothing had happened.
> -- Winston Churchill
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list