Re: [R-sig-Geo] help: Problem getting centroids inside lists

2018-09-10 Thread MacQueen, Don via R-sig-Geo
If all of your data frames had enough points then this should work:

myfun1 <- function(le) {
  list(one=geosphere::centroid(coordinates(le$one)),
   two=geosphere::centroid(coordinates(le$two))
   )
}

lapply(ct, myfun1)

To handle the one point case, try the following, which I think works with your 
example data, but is untested if there just two points.
It also assumes the data frames are named 'one' and 'two', and will ignore any 
others. To handle other names, I think you
could replace  $one  with  [[1]]  and  $two  with  [[2]]  .

myfun <- function(le) {
  list(one=if (length(le$one)>1) geosphere::centroid(coordinates(le$one)) else 
coodinates(le$one),
   two=if (length(le$two)>1) geosphere::centroid(coordinates(le$two)) else 
coordinates(le$two)
   )
}

lapply(ct, myfun)


To see a little bit of what's going on, try

  myfun(ct[[1]])
  myfun1(ct[[1]])
  myfun1(ct[[2]])
  myfun(ct[[2]])



I would also verify that the length method for objects of class SpatialPoints 
does return the number of points, as it appears to:

> class(ct$a$two)
[1] "SpatialPoints"
attr(,"package")
[1] "sp"

> length(ct$a$one)
[1] 3
> length(ct$a$two)
[1] 3
> length(ct$a$two)
[1] 3
> length(ct$b$two)
[1] 1

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 9/10/18, 10:56 AM, "R-sig-Geo on behalf of Ariel Fuentesdi" 
 wrote:

Hi everyone,

I have a list of coordinates called "ct" and I want to extract the
centroids of each sublist, but it only works when it has only 3 or more
points. In ct$b$two only has one point; in that case, I would rescue that
point as If it were a centroid.

This is what I did:

library(dplyr)
library(geosphere)

ct <- list(a = list(one = data.frame(lon = c(-180, -160, -60), lat = c(-20,
5, 0)),
two = data.frame(lon = c(-18, -16, -6), lat = c(-2, 50,
10))),
   b = list(one = data.frame(lon = c(-9, -8, -3), lat = c(-1, 25,
5)),
two = data.frame(lon = c(-90), lat = c(-1

coordinates(ct$a$one) <- ~lon+lat
coordinates(ct$a$two) <- ~lon+lat
coordinates(ct$b$one) <- ~lon+lat
coordinates(ct$b$two) <- ~lon+lat

s <- 1:length(ct)
ctply <- list()
for (i in s){
 for (j in 1:length(ct[[i]])) {
  ctply[[i]][j] <- ifelse(test = lengths(ct[[i]][1]) > 2, yes = sapply(X =
ct[[i]][j],
FUN = function(y) geosphere::centroid(slot(y, "coords"))), no =
ct[[i]][j] )
 }
}

Thanks in advance

Regards,
Ariel

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[R-sig-Geo] Isohyet maps where the inverse distance method does not work correctly

2018-09-10 Thread Stefano Sofia
Dear list users,
I implemented a code for creation of snowfall isohyets through the inverse 
distance method applied to a Digital Elevation Model.
Within a big area (approximatively 2.000 km2) I use only six stations, I would 
expect the snowfall esteem getting smaller with higher distances from the 
stations.
In my case the snowfall values remain constant all over the area.

This is the relevant part of my code (df_prec is the data frame that contains 
the coordinates of the stations and the snowfall cumulates).
Could somebody be so patient to spot where is my (big) mistake? (Here attached 
- as exception - the file picture1.pdf is an example of the result.)

Thank you for your attention and your help
Stefano Sofia



## LOADING SHAPEFILE FOR AREA5
Shape_area <- readOGR(dsn="area.shp", layer="area")
Shape_area_projection <- "+init=epsg:32633 +proj=utm +zone=33 +datum=WGS84 
+units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
Shape_area <- spTransform(Shape_area, CRS(Shape_area_projection))

## LOADING DEM
area_DEM raster("dem.tif")
area_DEM <- projectRaster(area_DEM, crs = CRS("+init=epsg:32633"))

## EXTRACTING THE ELEVATION VALUES TO MY POINTS
df_prec$ExtractedElevationValues <- extract(x=area_DEM, y=df_prec)

## CREATING A NEW GRID
newgrid <- as(area_DEM, "SpatialGridDataFrame")
newgrid <- raster(newgrid)
names(newgrid) <- "ExtractedElevationValues"

## Inverse Distance Weighted
if (sum(df_prec$Cumulata) > 0)
{
  OK_rain <- gstat(formula=snowfall_cumulate~ExtractedElevationValues, 
data=df_prec, locations=newgrid)
  rain_rast <- interpolate(newgrid, OK_rain, xyOnly=FALSE, na.rm=FALSE)

} else
{
  rain_rast <- raster(df_prec)*0
}




 (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.so...@regione.marche.it
---Oo-oO



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.

--
Questo messaggio  stato analizzato da Libra ESVA ed  risultato non infetto.
This message was scanned by Libra ESVA and is believed to be clean.



picture1.pdf
Description: picture1.pdf
___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo