forked from ISRICWorldSoil/GSIF_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_WorldGrids.R
More file actions
66 lines (60 loc) · 2.21 KB
/
Copy pathtutorial_WorldGrids.R
File metadata and controls
66 lines (60 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# title : tutorial_WorldGrids.R
# purpose : Accessing and using WorldGrids;
# reference : [http://worldgrids.org/doku.php?id=wiki:functions]
# producer : Prepared by T. Hengl and J. Mendes de Jesus
# address : In Wageningen, NL.
# inputs : WorldGrids GeoTiffs [http://worldgrids.org]
# outputs : subsets of WorldGrids;
## load all required libraries
library(XML)
library(sp)
library(rgdal)
library(gdalUtils)
gdal_setInstallation() ## CAN TAKE 1-2 MINUTES!
## layers of interest:
wg.lst <- c("worldgrids:CNTGAD3a", "worldgrids:EVMMOD3a", "worldgrids:G12IGB3a", "worldgrids:TDMMOD3a", "worldgrids:TWISRE3a")
## Web Coverage Service
## location of service:
wcs = "http://wms3.isric.org/geoserver/worldgrids/wcs?"
## create an XML file:
l1 <- newXMLNode("WCS_GDAL")
l1.s <- newXMLNode("ServiceURL", wcs, parent=l1)
l1.l <- newXMLNode("CoverageName", wg.lst[1], parent=l1)
l1
xml.out = paste0(gsub(":", "_", wg.lst[1]), ".xml")
saveXML(l1, file=xml.out)
## test it:
gdalinfo(xml.out)
## works ok!
## subset to some bounding box:
te <- c(20,40,22,42)
bb <- matrix(nrow=2, c(-180,-90,180,90))
o.x = 43200 + round(43200*(te[1]-bb[1,2])/(bb[1,2]-bb[1,1]))
o.y = round(21600*(bb[2,2]-te[4])/(bb[2,2]-bb[2,1]))
d.y = round(21600*(te[4]-te[2])/(bb[2,2]-bb[2,1]))
d.x = round(43200*(te[3]-te[1])/(bb[1,2]-bb[1,1]))
## resample and read to R:
tif.out <- paste0(gsub(":", "_", wg.lst[1]), ".tif")
gdal_translate(src_dataset=xml.out, dst_dataset=tif.out, of="GTiff", srcwin=paste(c(o.x, o.y, d.x, d.y), collapse=" "))
wg1km <- readGDAL(tif.out)
library(raster)
plot(raster(wg1km))
## DO NOT RUN:
library(GSIF)
URI = "http://wps.worldgrids.org/pywps.cgi"
server <- list(URI=URI, request="execute",
version="version=1.0.0", service.name="service=wps",
identifier="identifier=sampler_local1pt_nogml")
glcesa3.wps <- new("WPS", server=server, inRastername="glcesa3a")
# show(biocl15.wps)
prl <- getProcess(glcesa3.wps)
prl[7]
describe(glcesa3.wps, identifier="overlay")
p1 <- data.frame(lon=15, lat=15)
coordinates(p1) <- ~lon+lat
proj4string(p1) <- CRS("+proj=longlat +datum=WGS84")
p1
over(glcesa3.wps, p1)
# fetch grids and load the to R:
glcesa3 <- subset(glcesa3.wps, bbox=matrix(c(20,40,22,42), nrow=2))
image(glcesa3)