-
Notifications
You must be signed in to change notification settings - Fork 1
/
geoServerUploader.R
120 lines (105 loc) · 5.3 KB
/
geoServerUploader.R
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#***********************************************************************
# geoServerUploader.R
#***********************************************************************
# This script allows to upload a tiff file into a GeoServer deployment
# And performs some updates into a mongo database
# All variables related to users, passwords and hosts must be given
# externally at execution time, your are not going to found
# sensitive information in this package.
#
# Uriel Alejandro Moreno Ortiz
# umoreno@humboldt.org.co
# 21-08-2019
#***********************************************************************
require('bitops') #Must be loaded first
require('RCurl') #Once loaded bitops make sense to load this
require('stringr') #to use: str_pad
require('gdalUtils') #to use: gdal_translate
require("mongolite") #to connect to the database
geoServerUploader<-function(localConfObj,bioModelObj,sftpConfObj,GeoConfObj,mongoConfObj){
print(paste0("Starting geoServerUploader for ",bioModelObj$theTiff))
# gdal_translate runs some fixes on tif files in order to be a valid file for geoserver
#if a raster file does not upload at the first run you should try setting localConfObj$forceFix=TRUE
if(localConfObj$forceFix){
print("Using gdal_translate to fix the tif locally")
gdal_translate(paste0(localConfObj$pathToTiff,bioModelObj$theTiff),
paste0(localConfObj$pathToFixedTiff,bioModelObj$theTiff))
}else{
localConfObj$pathToFixedTiff=localConfObj$pathToTiff
}
# Path to the geoserver data files, for a new taxID
taxIdFolder<-paste0(sftpConfObj$remoteBase,str_pad(bioModelObj$taxID,5,"left",pad="0"))
# First connection to create the folder for given a taxID
# sftp://user:pass@host/<path>/<file>
tryCatch(
ftpUpload(I('Connecting to SFTP- GeoServer BioModelos'),paste0("sftp://",sftpConfObj$sftpUser,":",sftpConfObj$sftpPassword,"@",GeoConfObj$geoServerHost,"/tmp/deleteMe.txt"),postquote =c(paste0('MkDir ',taxIdFolder))),
error=function(err){return(NULL)}
)
# Second connection to upload the fixed tiff into new folder
# sftp://user:pass@host/<path>/<file>
print(paste0("Uploading to folder ",str_pad(bioModelObj$taxID,5,"left",pad="0")," in ",GeoConfObj$geoServerHost))
ftpUpload(paste0(localConfObj$pathToFixedTiff,bioModelObj$theTiff),paste0("sftp://",sftpConfObj$sftpUser,":",sftpConfObj$sftpPassword,"@",GeoConfObj$geoServerHost,taxIdFolder,'/',bioModelObj$theTiff))
print(paste0("Setting workspace and storages taxid-",str_pad(bioModelObj$taxID,5,"left",pad="0"),":",bioModelObj$modelID," into GeoServer"))
# Create workspaces for each species
query1<-paste0("curl -u ",GeoConfObj$gsUser,":",GeoConfObj$gsPassword," -s -S -XPOST -H 'Content-type: text/xml' -d '<workspace><name>taxid-",str_pad(bioModelObj$taxID,5,"left",pad="0"),"</name></workspace>' 'http://",GeoConfObj$geoServerHost,":",GeoConfObj$geoServerPort,"/geoserver/rest/workspaces.xml'")
res1=system(query1)
#Create storage for tiff
query2<-paste0("curl -u ",GeoConfObj$gsUser,":",GeoConfObj$gsPassword," -s -S -XPOST -H 'Content-type: text/xml' -d '<coverageStore><name>",bioModelObj$modelID,"</name><workspace>taxid-",str_pad(bioModelObj$taxID,5,"left",pad="0"),"</workspace><enabled>true</enabled><type>GeoTIFF</type><url>models/",str_pad(bioModelObj$taxID,5,"left",pad="0"),"/",bioModelObj$theTiff,"</url></coverageStore>' 'http://",GeoConfObj$geoServerHost,":",GeoConfObj$geoServerPort,"/geoserver/rest/workspaces/taxid-",str_pad(bioModelObj$taxID,5,"left",pad="0"),"/coveragestores'")
res2=system(query2)
# Setting tif as coverage with json
jsonFragment=paste0('{
"coverage": {
"abstract": "BioModelos Humboldt",
"defaultInterpolationMethod": "nearest neighbor",
"description": "Generated from R to BioModelos",
"enabled": true,
"interpolationMethods": {
"string": [
"nearest neighbor",
"bilinear",
"bicubic"
]
},
"keywords": {
"string": [
',bioModelObj$keywords,'
]
},
"name": "',bioModelObj$modelID,'",
"nativeFormat": "GeoTIFF",
"requestSRS": {
"string": [
"EPSG:4326"
]
},
"responseSRS": {
"string": [
"EPSG:4326"
]
},
"srs": "EPSG:4326",
"supportedFormats": {
"string": [
"ARCGRID",
"IMAGEMOSAIC",
"GTOPO30",
"GEOTIFF",
"GIF",
"PNG",
"JPEG",
"TIFF"
]
},
"title": "',substr(bioModelObj$theTiff,0,str_length(bioModelObj$theTiff)-4),'"
}
}')
query3<-paste0("curl -u ",GeoConfObj$gsUser,":",GeoConfObj$gsPassword," -s -S -X POST -H 'Content-type: application/json' -d '",jsonFragment,"' 'http://",GeoConfObj$geoServerHost,":",GeoConfObj$geoServerPort,"/geoserver/rest/workspaces/taxid-",str_pad(bioModelObj$taxID,5,"left",pad="0"),"/coveragestores/",bioModelObj$modelID,"/coverages'")
res3=system(query3)
print("Updating geoTIFF attribute in database")
mongoDB <- mongo(mongoConfObj$mongoCollection, url = mongoConfObj$mongoUrl)
where<-paste0('{"modelID":"',bioModelObj$modelID,'"}')
update<-paste0('{"$set":{"geoTIFF":"',bioModelObj$theTiff,'"}}')
mongoDB$update(where,update,multiple=FALSE)
print("Process completed successfully...")
print("Try more models")
}