aaps_publish_layers_spatialite.py

Emmanuel Blondel, Feb 17, 2017 07:19 PM

Download (7.62 KB)

 
1
#imports
2
#--------------------------------------------------------------------------------
3
import urllib2, base64
4
from xml.dom import minidom
5
from geoserver.catalog import Catalog
6
from geoserver.resource import FeatureType
7
from geoserver.support import url
8

    
9
#Get geoserver catalog
10
#--------------------------------------------------------------------------------
11
gs_url = "https://geoserver1-spatial-dev.d4science.org/geoserver/rest"
12
gs_usr = "admin"
13
gs_pwd = "geoserver"
14
cat = Catalog(gs_url, gs_usr, gs_pwd, disable_ssl_certificate_validation=True)
15

    
16
#get geoserver workspace
17
#--------------------------------------------------------------------------------
18
ws_name = "aaps"
19
ws = cat.get_workspace("aaps")
20

    
21
#create spatialite datastore (if not existing)
22
#--------------------------------------------------------------------------------
23
data_name = "test.db"
24
ds_name = "aaps_spatialite"
25
try:
26
    ds = cat.get_store(ds_name)
27
    print "Spatial datastore '" + ds.name + "' found in GeoServer"
28
except:
29
    print "No Spatialite datastore named '" + ds_name +"'"
30
    print "Creating Spatialite datastore..."
31
    ds_db = "/srv/geoserver_spatialite/data/" + data_name
32
    ds = cat.create_datastore(ds_name, ws)
33
    ds.connection_parameters.update(database= ds_db,dbtype= "spatialite")
34
    cat.save(ds)
35
    print "Spatialite datastore '" + ds_name + "' created!"
36

    
37
#upload spatialite data
38
#=====================================================================================
39
#1st method: GeoServer REST API (currently not possible because of bug in Geoserver)
40
#-----------
41
#-----------
42

    
43
#2d method: Combination of GCube Home-Library & Data-Transfer
44
#-----------
45
gcubeToken = "<your_token_here>" #use your token here. Ask CNR in case of problem
46

    
47
#using gcube home-library to upload file to workspace
48
#-----------------------------------------------------
49
uploaded_in_workspace = False
50
data_file = "D:/Mes documents/Documents/DEV/www/bluebridge/aaps/" + data_name
51
message = open(data_file, 'rb')
52
data_description = data_name
53
hl_baseurl = "https://workspace-repository.d4science.org/home-library-webapp"
54
hl_path = "/Home/emmanuel.blondel/Workspace/AAPS/INCOMING"
55
hl_upload_url = hl_baseurl + "/rest/Upload?name="+data_name+"&description=" + data_description + "&parentPath=" + hl_path
56
headers = {"gcube-token": gcubeToken, "Content-type": 'application/x-sqlite3'}
57
headers, response = cat.http.request(hl_upload_url, "POST", message, headers) #here we use the http.request utility of gsconfig instead of urllib2
58
uploaded_in_workspace = headers.status == 200
59

    
60
#inherit data URL (longURL) from the file uploaded in the workspace
61
data_url = ""
62
if uploaded_in_workspace:
63
    print "Spatialite DB file successfully uploaded to i-Marine workspace"
64
    print "Getting Spatialite DB file public link (long URL)"
65
    hl_getlink_url = hl_baseurl + "/rest/GetPublicLink?absPath=" + hl_path +"/" + data_name
66
    headers = {"gcube-token": gcubeToken}
67
    headers, response = cat.http.request(hl_getlink_url, "GET", "", headers)
68
    publiclink_fetched = headers.status == 200
69
    if publiclink_fetched:
70
        xmldoc = minidom.parseString(response)
71
        data_url = xmldoc.documentElement.firstChild.nodeValue
72
        print "Successfully fetched public link -> " + data_url
73
    else:
74
        print "Error while fetching Spatialite file public link"
75
else:
76
    print "Error while trying to upload local Spatialite file to i-Marine workspace"
77

    
78
#using data-transfer to transfer workspace uploaded file to Geoserver data dir
79
uploadStatus = 'TRANSFERRING';
80
if data_url != "":
81
    dt_baseurl = "https://geoserver1-spatial-dev.d4science.org/data-transfer-service/gcube/service"
82
    dt_upload_url = dt_baseurl + "/Requests"
83
    data_policy = "REWRITE"
84
    data_xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><transferRequest><id></id><httpDownloadSettings><source>"+data_url+"</source><options><range><min>80</min><max>80</max></range></options></httpDownloadSettings><destinationSettings><persistenceId>/geoserver</persistenceId><subFolder></subFolder><destinationFileName>"+data_name+"</destinationFileName><createSubfolders>true</createSubfolders><onExistingFileName>"+data_policy+"</onExistingFileName><onExistingSubFolder>APPEND</onExistingSubFolder></destinationSettings><pluginInvocations/></transferRequest>"
85
    print(data_xml)
86
    handler=urllib2.HTTPHandler(debuglevel=1)
87
    opener = urllib2.build_opener(handler)
88
    urllib2.install_opener(opener)
89
    req = urllib2.Request(dt_upload_url, data_xml)  
90
    req.add_header('Accept', 'application/xml')
91
    req.add_header('Content-Type', 'application/xml')
92
    req.add_header('gcube-token', gcubeToken)
93
    req.get_method = lambda:"POST"
94
    response = urllib2.urlopen(req)
95
    print "Response status code = %s" % str(response.code)
96
    xmldoc = minidom.parse(response)
97
    print xmldoc.toxml()
98
    #check upload status
99
    uploadId = xmldoc.getElementsByTagName('id')[0].firstChild.nodeValue
100
    print "Checking upload status for id = '%s'" % uploadId
101
    while uploadStatus == 'TRANSFERRING':
102
        handler=urllib2.HTTPHandler(debuglevel=1)
103
        opener = urllib2.build_opener(handler)
104
        urllib2.install_opener(opener)
105
        dt_status_url = dt_baseurl + "/TransferStatus/" + uploadId
106
        req = urllib2.Request(dt_status_url)  
107
        req.add_header('gcube-token', gcubeToken)
108
        req.get_method = lambda:"GET"  
109
        response = urllib2.urlopen(req)
110
        xmldoc = minidom.parse(response)
111
        uploadStatus = xmldoc.getElementsByTagName('status')[0].firstChild.nodeValue
112
        uploadPercent = xmldoc.getElementsByTagName('percent')[0].firstChild.nodeValue
113
        print "Upload status = '%s' " % uploadStatus
114
        print "Upload percentage = %s " % str(float(uploadPercent) * 100)
115
else:
116
    print "Error fetching public URL of spatialite file in i-marine workspace"
117

    
118
#publish the layer    
119
if uploadStatus == 'SUCCESS':
120
   #get Spatialite datastore previosuly created
121
   #--------------------------------------------------------------------------------
122
   ds = cat.get_store(ds_name)
123
   #publish feature type with direct FeatureType class
124
   #--------------------------------------------------------------------------------
125
   #(gsconfig catalog.publish_featuretype() method support limited
126
   native_name = "farm"
127
   feature_type = FeatureType(cat, ds.workspace, ds, native_name)
128
   feature_type.dirty['nativeName'] = native_name
129
   feature_type.dirty['name'] = "myspatialitelayer"
130
   feature_type.dirty['srs'] = 'EPSG:4326'
131
   feature_type.dirty['nativeCRS'] = 'EPSG:4326'
132
   feature_type.enabled = True
133
   feature_type.title = "My AAPS Spatialite layer"
134
   #create featureType GS resource
135
   resource_url= gs_url + "/workspaces/" + ds.workspace.name + "/datastores/" + ds.name + "/featuretypes.json"
136
   headers = {"Content-type": "application/xml", "Accept": "application/xml"}
137
   headers, response = cat.http.request(resource_url, "POST", feature_type.message(), headers)
138
   #need further coding to publish specific layer styles, etc
139
   #reload catalogue
140
   cat.reload()
141
   #delete Spatialite file temporarily uploaded to workspace
142
   print "Deleting Spatialite data from i-Marine workspace"
143
   hl_delete_url = hl_baseurl + "/rest/Delete?absPath=" + hl_path +"/" + data_name
144
   headers = {"gcube-token": gcubeToken}
145
   headers, response = cat.http.request(hl_delete_url, "GET", "", headers)
146
   deleted = headers.status == 200
147
   if deleted:
148
       print "Successfully deleted Spatialite file from workspace"
149
   else:
150
       print "Error while trying to delete Spatialite file from workspace"
151
else:
152
   print "Error while uploading Spatialite file to GeoServer"
153

    
154

    
155