Fwiw the engine behind geom_raster needs explicit observation-per-row form for input (with no structural normalization), so conversion to points is perfectly proper here, albeit confusing in context. (It's closer to what graphics devices actually use ultimately, but the expansion is laid out very early in ggplot2 because there's no standard for intermediate forms.) Cheers, Mike On Wed, 22 Nov 2017, 07:12 Sarah Goslee, <sarah.goslee at gmail.com> wrote:> Hi, > > You might get more help from the R-sig-geo list, which is devoted to > spatial topics. > > However. > > The *.asc file is an ArcGIS raster export format. You should use > whatever the appropriate import commands are for your own gridded > rainfall data. If you have a different format, you might or might not > be able to import it directly with raster. > > ?raster will tell you more about the kinds of formats that function > can handle importing. > > I'm not sure what the intent of converting a raster to point data > actually is; if you have point data, then import it as point data. If > you have gridded data, then map it as gridded data. But if it makes > sense to you, then go for it. > > The comments in your code sample explain what the CSV file should be: > coordinates of the points to be mapped. > > I'm not even certain from your question what your objective is. > > What kind of rainfall data are you starting with? > What kind of maps do you want to produce? > > Sarah > > > > On Fri, Nov 17, 2017 at 3:40 AM, Stefano Sofia > <stefano.sofia at regione.marche.it> wrote: > > Dear R users, > > I need to produce rainfall maps using R. > > I know that this is possible, I looked though the web, I found the > example below reported (the author is Andrew Tredennick). > > I would ask you if this is the most performing way to make rainfall > maps; if yes would someone be able to give me an example of how file.asc > and pointfile.csv should be? If no would somebody please show me another > way providing a small example? > > > > Thank you for your help > > Stefano > > > > > > library(raster) > > library(ggplot2) > > > > #open ASCII file using ?raster? command, which converts the ASCII to a > raster object > > map <- raster(?/your/path/to/file.asc?) > > > > #convert the raster to points for plotting > > map.p <- rasterToPoints(map) > > > > #Make the points a dataframe for ggplot > > df <- data.frame(map.p) > > #Make appropriate column headings > > colnames(df) <- c(?Longitude?, ?Latitude?, ?MAP?) > > > > #Call in point data, in this case a fake transect (csv file with lat and > lon coordinates) > > sites <- data.frame(read.csv(?/your/path/to/pointfile.csv?)) > > > > #Now make the map > > ggplot(data=df, aes(y=Latitude, x=Longitude)) + > > geom_raster(aes(fill=MAP)) + > > geom_point(data=sites, aes(x=x, y=y), color=?white?, size=3, shape=4) + > > theme_bw() + > > coord_equal() + > > scale_fill_gradient(?MAP (mm/yr)?, limits=c(0,2500)) + > > theme(axis.title.x = element_text(size=16), > > axis.title.y = element_text(size=16, angle=90), > > axis.text.x = element_text(size=14), > > axis.text.y = element_text(size=14), > > panel.grid.major = element_blank(), > > panel.grid.minor = element_blank(), > > legend.position = ?right?, > > legend.key = element_blank() > > ) > > > > > > > > (oo) > > --oOO--( )--OOo---------------- > > Stefano Sofia PhD > > Area Meteorologica e Area nivologica - Centro Funzionale > > Servizio Protezione Civile - Regione Marche > > Via del Colle Ameno 5 > > 60126 Torrette di Ancona, Ancona > > Uff: 071 806 7743 > > E-mail: stefano.sofia at regione.marche.it > > ---Oo---------oO---------------- > > > > > -- > Sarah Goslee > http://www.functionaldiversity.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Dr. Michael Sumner Software and Database Engineer Australian Antarctic Division 203 Channel Highway Kingston Tasmania 7050 Australia [[alternative HTML version deleted]]
Thank you Sarah and Mike for your explanations. My final objective is to produce maps (png image or any kind of extension I can import in LaTeX) where rainfall data are interpolated, using the Inverse Distance method or Kriging. My input file (pointfile.csv in the reported example) reports the station code, lat and long of the meteorological station and the rainfall value (which might be the cumulate of a week or ten days or the period I need to investigate). Here a small example: Station_Code, Init_Year, Init_Month, Init_Day, Init_Hour, Init_Minute, Fin_Year, Fin_Month, Fin_Day, Fin_Hour, Fin_Minute, Rainfall_Cumulate, Long, Lat 1056, 2017 , 11 , 1 , 0 , 0 , 2017 , 11 , 11 , 0 , 0 , 28.40, 12.786904, 43.851849 1064, 2017 , 11 , 1 , 0 , 0 , 2017 , 11 , 11 , 0 , 0 , 27.20, 12.967556, 43.762669 1072, 2017 , 11 , 1 , 0 , 0 , 2017 , 11 , 11 , 0 , 0 , 21.80, 12.897710, 43.907555 As far as I can understand (as you can see, I am not an expert on GIS or any spatial topic) - my input file pointfile.csv is in "observation-per-row form"; - I need a grid (file.asc) where I can interpolate my rainfall data (I can get it, a resolution of 1km will be enough for me) - ggplot should produce the map I need; but where are the options for the interpolation method? Again, any help will be appreciated. Stefano (oo) --oOO--( )--OOo---------------- Stefano Sofia PhD Area Meteorologica e Area Nivologica - Centro Funzionale Servizio Protezione Civile - Regione Marche Via del Colle Ameno 5 60126 Torrette di Ancona, Ancona Uff: 071 806 7743 E-mail: stefano.sofia at regione.marche.it ---Oo---------oO---------------- ________________________________ Da: Michael Sumner [mdsumner at gmail.com] Inviato: mercoled? 22 novembre 2017 10.48 A: Sarah Goslee Cc: Stefano Sofia; r-help at r-project.org Oggetto: Re: [R] How to produce rainfall maps Fwiw the engine behind geom_raster needs explicit observation-per-row form for input (with no structural normalization), so conversion to points is perfectly proper here, albeit confusing in context. (It's closer to what graphics devices actually use ultimately, but the expansion is laid out very early in ggplot2 because there's no standard for intermediate forms.) Cheers, Mike On Wed, 22 Nov 2017, 07:12 Sarah Goslee, <sarah.goslee at gmail.com<mailto:sarah.goslee at gmail.com>> wrote: Hi, You might get more help from the R-sig-geo list, which is devoted to spatial topics. However. The *.asc file is an ArcGIS raster export format. You should use whatever the appropriate import commands are for your own gridded rainfall data. If you have a different format, you might or might not be able to import it directly with raster. ?raster will tell you more about the kinds of formats that function can handle importing. I'm not sure what the intent of converting a raster to point data actually is; if you have point data, then import it as point data. If you have gridded data, then map it as gridded data. But if it makes sense to you, then go for it. The comments in your code sample explain what the CSV file should be: coordinates of the points to be mapped. I'm not even certain from your question what your objective is. What kind of rainfall data are you starting with? What kind of maps do you want to produce? Sarah On Fri, Nov 17, 2017 at 3:40 AM, Stefano Sofia <stefano.sofia at regione.marche.it<mailto:stefano.sofia at regione.marche.it>> wrote:> Dear R users, > I need to produce rainfall maps using R. > I know that this is possible, I looked though the web, I found the example below reported (the author is Andrew Tredennick). > I would ask you if this is the most performing way to make rainfall maps; if yes would someone be able to give me an example of how file.asc and pointfile.csv should be? If no would somebody please show me another way providing a small example? > > Thank you for your help > Stefano > > > library(raster) > library(ggplot2) > > #open ASCII file using ?raster? command, which converts the ASCII to a raster object > map <- raster(?/your/path/to/file.asc?) > > #convert the raster to points for plotting > map.p <- rasterToPoints(map) > > #Make the points a dataframe for ggplot > df <- data.frame(map.p) > #Make appropriate column headings > colnames(df) <- c(?Longitude?, ?Latitude?, ?MAP?) > > #Call in point data, in this case a fake transect (csv file with lat and lon coordinates) > sites <- data.frame(read.csv(?/your/path/to/pointfile.csv?)) > > #Now make the map > ggplot(data=df, aes(y=Latitude, x=Longitude)) + > geom_raster(aes(fill=MAP)) + > geom_point(data=sites, aes(x=x, y=y), color=?white?, size=3, shape=4) + > theme_bw() + > coord_equal() + > scale_fill_gradient(?MAP (mm/yr)?, limits=c(0,2500)) + > theme(axis.title.x = element_text(size=16), > axis.title.y = element_text(size=16, angle=90), > axis.text.x = element_text(size=14), > axis.text.y = element_text(size=14), > panel.grid.major = element_blank(), > panel.grid.minor = element_blank(), > legend.position = ?right?, > legend.key = element_blank() > ) > > > > (oo) > --oOO--( )--OOo---------------- > Stefano Sofia PhD > Area Meteorologica e Area nivologica - Centro Funzionale > Servizio Protezione Civile - Regione Marche > Via del Colle Ameno 5 > 60126 Torrette di Ancona, Ancona > Uff: 071 806 7743 > E-mail: stefano.sofia at regione.marche.it<mailto:stefano.sofia at regione.marche.it> > ---Oo---------oO---------------- >-- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dr. Michael Sumner Software and Database Engineer Australian Antarctic Division 203 Channel Highway Kingston Tasmania 7050 Australia ________________________________ AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere informazioni confidenziali, pertanto ? destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit? ed urgenza, la risposta al presente messaggio di posta elettronica pu? essere visionata da persone estranee al destinatario. IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system. [[alternative HTML version deleted]]
Hi On 11/23/2017 10:04 AM, Stefano Sofia wrote:> Thank you Sarah and Mike for your explanations. My final objective is > to produce maps (png image or any kind of extension I can import in > LaTeX) where rainfall data are interpolated, using the Inverse > Distance method or Kriging. My input file (pointfile.csv in the > reported example) reports the station code, lat and long of the > meteorological station and the rainfall value (which might be the > cumulate of a week or ten days or the period I need to investigate). > Here a small example: Station_Code, Init_Year, Init_Month, Init_Day, > Init_Hour, Init_Minute, Fin_Year, Fin_Month, Fin_Day, Fin_Hour, > Fin_Minute, Rainfall_Cumulate, Long, Lat 1056, 2017 , 11 , 1 , 0 , 0 , > 2017 , 11 , 11 , 0 , 0 , 28.40, 12.786904, 43.851849 1064, 2017 , 11 , > 1 , 0 , 0 , 2017 , 11 , 11 , 0 , 0 , 27.20, 12.967556, 43.762669 1072, > 2017 , 11 , 1 , 0 , 0 , 2017 , 11 , 11 , 0 , 0 , 21.80, 12.897710, > 43.907555 As far as I can understand (as you can see, I am not an > expert on GIS or any spatial topic) - my input file pointfile.csv is > in "observation-per-row form"; - I need a grid (file.asc) where I can > interpolate my rainfall data (I can get it, a resolution of 1km will > be enough for me)If I understand, in order to interpolate point data to a grid, the steps you need to do in R are: 1. import the CSV of rain data and convert to a SpatialPointsDataFrame 2. Convert that SPDF to a projected coordinate system, such as UTM, for kriging 3. create an empty grid as the target for kriging, probably based on the extent of the rain data 4. Run kriging using the point rain data and target grid Here's a basic workflow for the above #----------------------------------- # Required libraries library(gstat) library(automap) library(rgdal) # Read in CSV file and convert to SPDF rain_data <- read.csv("pointfile.csv") str(rain_data) # Check what you have so far point_coords <- rain_data[c("Long","Lat")] coordinates(rain_data) <- point_coords p4str <- CRS("+init=epsg:4326")? # Since the coordinates are in Long/Lat, first declare this CRS proj4string(rain_data) <- p4str # Now Convert to UTM p4str_UTM <- CRS("+init=epsg:32633") rain_data_UTM <- spTransform(rain_data, p4str_UTM) str(rain_data_UTM)??? # Check that this is a SPDF in the UTM coordinate system # Create Grid for kriging output, using the extent of the rain data SPDF minx <-? rain_data_UTM at bbox[1,1] maxx <- rain_data_UTM at bbox[1,2] miny <- rain_data_UTM at bbox[2,1] maxy <- rain_data_UTM at bbox[2,2] pixel <- 1000??? ??? # Each pixel will be 1000 meters grd <- expand.grid(x=seq(minx, maxx, by=pixel), y=seq(miny, maxy, by=pixel)) coordinates(grd) <- ~x+y gridded(grd) <- TRUE proj4string(grd) <- p4str_UTM # Kriging, using autoKrige which creates a best guess variogram # The formula for ordinary kriging is "<data_column> ~ 1" OK_rain <- autoKrige(Rainfall_Cumulate ~ 1, rain_data_UTM, grd) #----------------------------------- The kriging result contains a component "prediction" which you can either plot directly, convert to an R raster object for plotting with ggplot2, or export to a Geotiff. HTH, Micha> Dear R users, >> I need to produce rainfall maps using R. I know that this is >> possible, I looked though the web, I found the example below reported >> (the author is Andrew Tredennick). I would ask you if this is the >> most performing way to make rainfall maps; if yes would someone be >> able to give me an example of how file.asc and pointfile.csv should >> be? If no would somebody please show me another way providing a small >> example? Thank you for your help Stefano-- Micha Silver Ben Gurion Univ. Sde Boker, Remote Sensing Lab cell: +972-523-665918