aaps_publish_layers_spatialite2.py

Publication script based on GeoServer REST API only - Emmanuel Blondel, Mar 17, 2017 07:26 PM

Download (4.39 KB)

 
1
#imports
2
#--------------------------------------------------------------------------------
3
#for geoserver imports please see installation of gsconfig py module at https://github.com/boundlessgeo/gsconfig
4
import urllib2, base64
5
from xml.dom import minidom
6
from geoserver.catalog import Catalog
7
from geoserver.resource import FeatureType
8
from geoserver.support import url
9

    
10
#Get geoserver catalog
11
#--------------------------------------------------------------------------------
12
#Geoserver applications for AAPS
13
#- Test: https://geoserver1-spatial-dev.d4science.org/geoserver
14
#- Prod: https://geoserver1-p-spatial.d4science.org/geoserver
15
gs_url = "https://geoserver1-spatial-dev.d4science.org/geoserver/rest"
16
gs_usr = "admin"
17
gs_pwd = "geoserver"
18
cat = Catalog(gs_url, gs_usr, gs_pwd, disable_ssl_certificate_validation=True)
19

    
20
#get geoserver workspace
21
#--------------------------------------------------------------------------------
22
ws_name = "aaps"
23
ws = cat.get_workspace("aaps")
24

    
25
#STEP 1/ create spatialite datastore (if not existing, for the Spatialite DB file considered)
26
#--------------------------------------------------------------------------------
27
data_name = "fishcages_greece.db"
28
ds_name = "aaps_spatialite2"
29
try:
30
    ds = cat.get_store(ds_name)
31
    print "Spatial datastore '" + ds.name + "' found in GeoServer"
32
except:
33
    print "No Spatialite datastore named '" + ds_name +"'"
34
    print "Creating Spatialite datastore..."
35
    #here path is ignored when uploading spatialite data (at step 2)
36
    #The reason of this is that currently no custom Geoserver_data_dir location is configured for this relative path
37
    #When uploading the data, Geoserver will update accordingly the data Spatialite datastore path
38
    ds_db = "/srv/geoserver_spatialite/data/" + data_name 
39
    ds = cat.create_datastore(ds_name, ws)
40
    ds.connection_parameters.update(database= ds_db,dbtype= "spatialite")
41
    cat.save(ds)
42
    print "Spatialite datastore '" + ds_name + "' created!"
43

    
44
#STEP 2/ upload spatialite data
45
#=====================================================================================
46
#1st method: GeoServer REST API (currently not possible because of bug in Geoserver)
47
#Once uploaded, the uploaded data will be renamed with the datastore name (ds_name)
48
#Please notice the parameters used for the upload:
49
#- configure=none : means that we upload only here, with no featuretype/layer publication (we do that later to customize the output)
50
#- update=overwrite : means that if you re-upload the file, it will be overwritten (alternative value for this param is 'append'
51
#-----------
52
#data & url for putting data
53
message = open(data_name, 'rb')
54
data = message.read()
55
upload_url = gs_url + '/workspaces/' + ws_name + '/datastores/' + ds_name + '/file.spatialite?configure=none&update=overwrite'
56
headers = {"Content-type": "application/x-sqlite3"}
57
headers, response = cat.http.request(upload_url, "PUT", data, headers)
58
uploaded = headers.status == 201
59

    
60
#STEP 3/ publish the layer    
61
#=====================================================================================
62
if uploaded:
63
    #get Spatialite datastore previosuly created
64
    #--------------------------------------------------------------------------------
65
    ds = cat.get_store(ds_name)
66
    #publish feature type with direct FeatureType class
67
    #--------------------------------------------------------------------------------
68
    #(gsconfig catalog.publish_featuretype() method support limited
69
    target_layer_name = "myspatialitelayer2"
70
    native_name = "farm"
71
    feature_type = FeatureType(cat, ds.workspace, ds, native_name)
72
    feature_type.dirty['nativeName'] = native_name
73
    feature_type.dirty['name'] = target_layer_name
74
    feature_type.dirty['srs'] = 'EPSG:4326'
75
    feature_type.dirty['nativeCRS'] = 'EPSG:4326'
76
    feature_type.enabled = True
77
    feature_type.title = "My AAPS Spatialite layer 2 (published with GeoServer REST API only)"
78
    #create featureType GS resource
79
    resource_url= gs_url + "/workspaces/" + ds.workspace.name + "/datastores/" + ds.name + "/featuretypes.json"
80
    headers = {"Content-type": "application/xml", "Accept": "application/xml"}
81
    headers, response = cat.http.request(resource_url, "POST", feature_type.message(), headers)
82
    #need further coding to publish specific layer styles, etc
83
    #reload catalogue
84
    cat.reload()
85
else:
86
   print "Error while uploading Spatialite DB file"    
87