I hava another geodata question.
I am trying to access the elevation data for specific points with the get_elev_point ()
I have about 160 points that I would like to get the elevation data for, so I would like to not do it one by one but I do not know how to retrieve them all at once.
I have made a data.frame of all my points where the first column x is longitude and the second column y is latitude and these are the only data in the data.frame.
> elevationpoints <-get_elev_point(locations = pontok_df, units="meters", prj = ll_prj, src = "aws")
Error in seq.default(from = ceiling(min_tile[2]), to =
floor(max_tile[2])) : 'to' must be a finite number In addition:
Warning message: In log(tan(lat_rad) + (1/cos(lat_rad))) : NaNs
produced
But it does not seem to yield any results. Could you please help me? Much appreciated!
Kamilla
Related
I am trying to perform the simple task of estimating a kernel density utilisation distribution across the foraging tracks of all females in my data set (just a visualisation exercise), and have opted for the kernelUD function within the adehabitatHR package in R.
I can set up a simple example of the SpatialPoints object I have been working with, which is formatted in long-lat format.
female <- filter(tracks, Sex == "Female")
# check the range of the longitude and latitude
range(female[,c("Latitude")])
[1] 20.71389 84.20619
range(female[,c("Longitude")])
[1] -23.85262 105.20330
# make the SpatialPoints object
sp.female <- SpatialPoints(coords = female[,c("Longitude", "Latitude")],
proj4string = CRS("+proj=longlat +ellps=WGS84"))
So no points are outside of the expected range for either longitude or latitude, but when I then try and perform the kernelUD:
kd.female <- kernelUD(sp.female, h = "href")
Error in `proj4string<-`(`*tmp*`, value = CRS(pfs1)) :
Geographical CRS given to non-conformant data: -105.076705907
This data point does not appear in the object I am working with, so I am at a loss as to how to troubleshoot the error.
I'm running the following package versions on R v3.6.3
> packageVersion('adehabitatHR')
[1] ‘0.4.19’
> packageVersion('rgdal')
[1] ‘1.5.23’
> packageVersion('sp')
[1] ‘1.4.5’
Thanks in advance for any help.
I have found that transforming to UTM appears to fix the problem, but I would be keen to know why the kernelUD function does not work on data where the coordinates are formatted in classic longitude and latitude.
I hava another geodata question. I am trying to access the elevation data for specific points with the get_elev_point ()
I have about 160 points that I would like to get the elevation data for, so I would like to not do it one by one but I do not know how to retrieve them all at once.
I have made a data.frame of all my points where the first column x is longitude and the second column y is latitude and these are the only data in the data.frame.
I am using the elevatr package
Reproducible example:
location_1 <- data.frame (x=-7.37,y=5.775)
location_1_elev <-get_elev_point(locations = location_1, units="meters", prj = ll_prj, src = "aws")
When I do this, everything is fine and I get an elevation point back, but when I try to access several points at the same time I run into errors.
I took the earthquake data from R and transformed it into a data.frame that has only the longitude and latitiude. Then tried to access the elevation points via get_elev_points and got the error message:
data(quakes)
head(quakes)
locations <- data.frame(x = c(quakes$long, 1000), y = c(quakes$lat, 1000))
quakes_elev <-get_elev_point(locations = locations, units="meters", prj = ll_prj, src = "aws")
Error: API did not return tif
Do you have any tips how to make this happen, to be able to access multiple elevation points?
Thank you!
ps.: Sorry for my clumsy asking, I am only learning now.
It's likely the error is caused because the points are in the sea so there is no land elevation tif file to get an elevation from. For example, here is an example that tries to find the elevation of a point in the sea and then a point on land.
library(elevatr)
ll_prj <- "EPSG:4326"
sea <- data.frame(x=181.62, y=-20.42)
# This errors
sea_elev <- get_elev_point(locations = sea, units='meters', prj=ll_prj, src='aws')
# Error: This url: https://s3.amazonaws.com/elevation-tiles-prod/geotiff/5/32/18.tif did not return a tif
land <- data.frame(x=-71.3036, y=44.2700)
# This works
land_elev <- get_elev_point(locations = land, units='meters', prj=ll_prj, src='aws')
land_elev$elevation
# [1] 1478
Using the epqs option returns NA without error so I suppose you could replace that with 0 if you want to use sea level as the elevation.
I finally had time to look into this.
The root causes of the problem are 1) data near 180 degrees, and 2) longitude using 0 to 360.
The first issue was a problem as I was grabbing the next higher tile for a given longitude. The end result is an x/y/z tile that didn't exist and thus would return an error. I have a fix that only grabs the tile that corresponds with a given longitude and not the next higher. This is fixed and pushed to https://github/jhollist/elevatr.
The second issue is a problem as elevatr assumes longitude between -180 and 180. So if you convert the quakes dataset to this and use the newer version of elevatr on GitHub, #kamilla-choni-pléh code will work.
install.packages("remotes")
remotes::install_github("jhollist/elevatr")
library(elevatr)
ll_prj <- "EPSG:4326"
data(quakes)
locations <- data.frame(x = c(quakes$long), y = c(quakes$lat))
locations$x <- ifelse(locations$x >= 180, locations$x - 360, locations$x)
quakes_elev <-get_elev_point(locations = locations, units="meters", prj = ll_prj, src = "aws")
quakes_elev$elevation
If using a higher zoom level, though it may complain as it is currently creating a raster that spans -180 to 180 (i.e. the globe). I am still mulling over how I should deal with that in elevatr.
I have this, and I am trying to use gDistance to calculate the distance between each centroid and the city of Baghdad. I am trying to do it like this:
gDistance(districts_centroids, districts#data$ADM3NAME %in% c("Baghdad"), byid=TRUE)
Where district_centroids are Formal Class SpatialPoints, and the districts#data... is basically the city of Baghdad in the shp file.
I get an error saying the following:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘is.projected’ for signature ‘"logical"’
In addition: Warning message:
In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") :
Spatial object 1 is not projected; GEOS expects planar coordinates
I am completely new to R and I don't really know what's going on.
Any help would be appreciated
Thank you!
This is a great question. After taking a look at things, it was decided the best way to answer this question was using simple feature objects rather than spatial objects.
The map in question looks familiar. I used the IRQ_2_sf.rds map which is probably the same map used and shown above. There are other alternative ways to achieve the solution to this question. Jupyter Lab is the IDE used.
Using the Google API and the geocode function, the coordinates for Baghdad were retrieved.
baghdad <- geocode("Baghdad, Iraq", source = c("google") )
A tibble: 1 × 2
lon lat
<dbl> <dbl>
44.36607 33.31524
Then sf functions were used to create the sf column object.
baghdad.sfg <- st_point(c(lon, lat), dim = "XY")
baghdad.sfc <- st_sfc(baghdad.sfg, crs = 4326)
Then, using the sf map named iraq, the centroids were created.
Note: warning message - st_centroid does not give correct centroids for longitude/latitude data. For this answer, the centroids will be close
enough.
iraq.c <- st_centroid(iraq)
The distance from each centroid to Baghdad gets determined in kilometers.
head(dist <- data.frame(c(st_distance(iraq.c$geom[], baghdad.sfc)/1000)))
Units: [m]
[1] 28.63250 59.61553 215.43354 323.06418 259.14509 113.55356
And then create a date frame that includes the names, the centroid geometry and the distance values. Requires some cleaning and binding.
distance <- c(dist$c.st_distance.iraq.c.geom....baghdad.sfc..1000.)
x <- distance[]
d_Bdad_Km <- as.numeric(str_extract(x, "[0-9]*.[0-9]."))
iraq2 <- iraq[-c(5, 8,9,10,11,12,13)]
df_dist <- cbind(iraq2, d_Bdad_Km) # df as desired
And then the outputs gets plotted.
plot(iraq$geom)
plot(iraq.c$geom, add = TRUE, col = "red")
plot(baghdad.sfc, add = TRUE, pch = 19, col = "blue")
Please ask if there are any follow-up questions. The plot can be viewed at this link:
I was trying to follow this kriging exercise and ended up going on a 6 hour adventure in an attempt to create a grid file like meuse.grid in the link. I'm very new to R and geostats in general so I'd really appreciate any help. I managed to create a SpatialPolygonsDataFrame using a shapefile to get: this
cishape <- st_read("data/files/cishape.shp")
map <- as_Spatial(cishape)
plot(map)
I then managed to get this which seems to be closer to correct although the shape is now more off:
grd <- makegrid(poly, n=10000); colnames(grd) <-c("x", "y");
grd_pts <- SpatialPoints(coords=grd, proj4string = CRS(proj4string(poly)));
grd_pts_in <- grd_pts[poly,];
gdf <- as.data.frame(coordinates(grd_pts_in));
ggplot(gdf) + geom_point(aes(x=x, y=y))
I can't seem to make this work following the kriging exercise though. When I make it to the bit where:
grd <- as(grd, "SpatialPixelsDataFrame
plot(grd), I get the error: Error in .subset2(x, i, exact = exact) : subscript out of bounds. I read that it is because my spdf is actually has no data and should use SpatialPixels instead, but then I get the error Error in matrix(FALSE, ncells[2], ncells[1]) : invalid 'nrow' value (too large or NA). So then I tried SpatialPoints instead and got this chunk.
Please help, I don't know what I'm doing wrong and I feel like I'm losing my sanity.
I am trying to plot 4 point shapefiles. They are close to identical: each is a SpatialPointsDataFrame, each with:
CR="+proj=longlat +datum=WGS84"
each with 4 variables. The first has 312 points, the second 337, the third 948, and the fourth 345 obs. I can plot each of them, individually or in one map, using R's simple plot() function.
However, when I go to plot them according to a variable value, using spplot, the first two plot perfectly, and the second two give me this error:
Error in matrix(0, rows.per.page, cols.per.page) :
invalid 'nrow' value (too large or NA)
I don't know what this means. I saw one forum response on the subject which pointed towards an error in the CRS. But I know it's not the CRS, as I made these shapefiles w/ identical CRS strings.
Thanks so much in advance for any insights!