[R-sig-Geo] Convert data.frame/SpatialPointsDataFrame to raster
Vijay Lulla
v|j@y|u||@ @end|ng |rom gm@||@com
Thu Aug 1 17:12:24 CEST 2019
I second Hugo's suggestion. IMO, since your points are not a regular grid
you will be unable to use `rasterXYZ`. The only thing I would add to
Hugo's answers is that you can set the number of rows/cols that you wish in
your final raster by setting various args in the raster function.
example(rasterize) has some good examples.
HTH,
Vijay.
On Thu, Aug 1, 2019 at 4:44 AM Hugo Costa <hugoagcosta using gmail.com> wrote:
> Dear Milu,
>
> the best I can suggest is something like this.
>
> library(raster)
> df<-read.csv("path/to/data/downloaded/from/GoogleDrive")
> try <- subset(df, year==2010)
>
> sp<-SpatialPointsDataFrame(try[,1:2],data=try, proj4string =
> CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
>
> r<-rasterize(sp, y=raster(), field=try$TWL_5, fun=mean, background=NA)
> plot(r)
>
> Hope this helps somehow
> Good luck
> Hugo
>
> Miluji Sb <milujisb using gmail.com> escreveu no dia quinta, 1/08/2019 à(s)
> 09:13:
>
>> Dear Vijay,
>>
>> Thank you again for your reply. I tried attaching my data for two years
>> and
>> two variables but the message was rejected. I have saved the data on a
>> Google Drive folder here
>> <
>> https://drive.google.com/drive/folders/15LvLysXqIIua8ukkp8UNEnlJMSK6mA1K?usp=sharing
>> >.
>> Hope this will work. Thanks again!
>>
>> Sincerely,
>>
>> Milu
>>
>> On Thu, Aug 1, 2019 at 9:52 AM Miluji Sb <milujisb using gmail.com> wrote:
>>
>> > Dear Vijay,
>> >
>> > Thank you again for your reply. I have attached my data for two years
>> and
>> > two variables. Hope they will go through, otherwise the files are also
>> > available here
>> > <
>> https://drive.google.com/drive/folders/15LvLysXqIIua8ukkp8UNEnlJMSK6mA1K?usp=sharing
>> >.
>> > Thanks again!
>> >
>> > Sincerely,
>> >
>> > Milu
>> >
>> > On Wed, Jul 31, 2019 at 10:46 PM Vijay Lulla <vijaylulla using gmail.com>
>> wrote:
>> >
>> >> Hmm...I had seen your data and thought that it was just some sample
>> that
>> >> you'd shared. If this is your whole data then I don't know how to
>> create a
>> >> raster from just one row that is returned from subsetting the
>> dataframe.
>> >>
>> >> Sorry for the noise.
>> >>
>> >> On Wed, Jul 31, 2019 at 4:16 PM Miluji Sb <milujisb using gmail.com> wrote:
>> >>
>> >>> Hello,
>> >>>
>> >>> Thank you for your kind reply. Here is a snapshot of the original
>> data.
>> >>> I had pasted it at the bottom of my first email but forgot to mention
>> it.
>> >>> Thanks again!
>> >>>
>> >>> df <- structure(list(lon = c(180, 179.762810919291, 179.523658017568,
>> >>> 179.311342656601, 179.067616041778, 178.851382109362,
>> 178.648816406322,
>> >>> 178.501097394651, 178.662722495847, 178.860599151485), lat =
>> >>> c(-16.1529296875,
>> >>> -16.21659020822, -16.266117894201, -16.393550535614,
>> -16.4457378034442,
>> >>> -16.561653799838, -16.6533087696649, -16.7741069281329,
>> >>> -16.914110607613,
>> >>> -16.9049389730284), nsdec = structure(c(1L, 3L, 4L, 5L, 6L, 7L,
>> >>> 8L, 9L, 10L, 2L), .Label = c("1 of 10", "10 of 10", "2 of 10",
>> >>> "3 of 10", "4 of 10", "5 of 10", "6 of 10", "7 of 10", "8 of 10",
>> >>> "9 of 10"), class = "factor"), TWL_5 = c(2.13810426616849,
>> >>> 2.16767864033646,
>> >>> 2.16881240361846, 2.20727073247015, 2.27771608519709, 2.3649601141941,
>> >>> 2.44210984856767, 2.52466349543977, 2.63982954290745, 2.71828906773926
>> >>> ), TWL_50 = c(2.38302354555823, 2.43142793944275, 2.45733044901087,
>> >>> 2.53057109758284, 2.61391337469939, 2.71040967066483,
>> 2.82546443373866,
>> >>> 2.9709907727849, 3.1785797371187, 3.33227647990861), TWL_95 =
>> >>> c(2.63753852023063,
>> >>> 2.7080249053612, 2.75483681166049, 2.86893038433795, 2.97758282474101,
>> >>> 3.14541928966618, 3.3986143008625, 3.68043269045659, 4.09571655859075,
>> >>> 4.57299670034984), year = c(2010, 2020, 2030, 2040, 2050, 2060,
>> >>> 2070, 2080, 2090, 2100)), row.names = c(NA, 10L), class =
>> "data.frame")
>> >>>
>> >>> Sincerely,
>> >>>
>> >>> Milu
>> >>>
>> >>> On Wed, Jul 31, 2019 at 9:20 PM Vijay Lulla <vijaylulla using gmail.com>
>> >>> wrote:
>> >>>
>> >>>> ?`rasterFromXYZ` states that "x and y represent spatial coordinates
>> and
>> >>>> must be on a regular grid." And, it appears to me that you might be
>> losing
>> >>>> values by rounding lon/lat values. The help file further suggests
>> that
>> >>>> `rasterize` might be the function you're looking for. List members
>> will
>> >>>> (certainly I will) find it more helpful to propose other solutions
>> if you
>> >>>> post a small reproducible example of your original georeferenced
>> dataset so
>> >>>> that we get an idea of what data you're using.
>> >>>>
>> >>>> Sorry, I cannot be of more help.
>> >>>>
>> >>>> On Wed, Jul 31, 2019 at 10:45 AM Miluji Sb <milujisb using gmail.com>
>> wrote:
>> >>>>
>> >>>>> Dear all,
>> >>>>>
>> >>>>> I have georeferenced dataset with multiple variables and years. The
>> >>>>> data is
>> >>>>> at ~100 km (1° × 1°) spatial resolution. I would like to convert
>> this
>> >>>>> into
>> >>>>> a raster.
>> >>>>>
>> >>>>> I have filtered the data for one year and one variable and did the
>> >>>>> following;
>> >>>>>
>> >>>>> try <- subset(df, year==2010)
>> >>>>> try <- try[,c(1,2,4)]
>> >>>>> try$lon <- round(try$lon)
>> >>>>> try$lat <- round(try$lat)
>> >>>>> r_imp <- rasterFromXYZ(try)
>> >>>>>
>> >>>>> Two issues; is it possible to convert the original dataset with the
>> >>>>> multiple variables and years to a raster? If not, how can I avoid
>> >>>>> rounding
>> >>>>> the coordinates? Currently, I get this error "Error in
>> >>>>> rasterFromXYZ(try) :
>> >>>>> x cell sizes are not regular" without rounding.
>> >>>>>
>> >>>>> Any help will be greatly appreciated. Thank you!
>> >>>>>
>> >>>>> Sincerely,
>> >>>>>
>> >>>>> Shouro
>> >>>>>
>> >>>>> ## Data
>> >>>>> df <- structure(list(lon = c(180, 179.762810919291,
>> 179.523658017568,
>> >>>>> 179.311342656601, 179.067616041778, 178.851382109362,
>> 178.648816406322,
>> >>>>> 178.501097394651, 178.662722495847, 178.860599151485), lat =
>> >>>>> c(-16.1529296875,
>> >>>>> -16.21659020822, -16.266117894201, -16.393550535614,
>> -16.4457378034442,
>> >>>>> -16.561653799838, -16.6533087696649, -16.7741069281329,
>> >>>>> -16.914110607613,
>> >>>>> -16.9049389730284), nsdec = structure(c(1L, 3L, 4L, 5L, 6L, 7L,
>> >>>>> 8L, 9L, 10L, 2L), .Label = c("1 of 10", "10 of 10", "2 of 10",
>> >>>>> "3 of 10", "4 of 10", "5 of 10", "6 of 10", "7 of 10", "8 of 10",
>> >>>>> "9 of 10"), class = "factor"), TWL_5 = c(2.13810426616849,
>> >>>>> 2.16767864033646,
>> >>>>> 2.16881240361846, 2.20727073247015, 2.27771608519709,
>> 2.3649601141941,
>> >>>>> 2.44210984856767, 2.52466349543977, 2.63982954290745,
>> 2.71828906773926
>> >>>>> ), TWL_50 = c(2.38302354555823, 2.43142793944275, 2.45733044901087,
>> >>>>> 2.53057109758284, 2.61391337469939, 2.71040967066483,
>> 2.82546443373866,
>> >>>>> 2.9709907727849, 3.1785797371187, 3.33227647990861), TWL_95 =
>> >>>>> c(2.63753852023063,
>> >>>>> 2.7080249053612, 2.75483681166049, 2.86893038433795,
>> 2.97758282474101,
>> >>>>> 3.14541928966618, 3.3986143008625, 3.68043269045659,
>> 4.09571655859075,
>> >>>>> 4.57299670034984), year = c(2010, 2020, 2030, 2040, 2050, 2060,
>> >>>>> 2070, 2080, 2090, 2100)), row.names = c(NA, 10L), class =
>> "data.frame")
>> >>>>>
>> >>>>> [[alternative HTML version deleted]]
>> >>>>>
>> >>>>> _______________________________________________
>> >>>>> R-sig-Geo mailing list
>> >>>>> R-sig-Geo using r-project.org
>> >>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >>>>>
>> >>>>
>> >>>>
>> >>>>
>> >>
>>
>> [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo using r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list