aaps_publish_layers_shapefile.py

Emmanuel Blondel, Feb 16, 2017 04:25 PM

Download (2.31 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 prepare_upload_bundle, 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

    
17
#upload shapefile data with GeoServer REST API
18
#--------------------------------------------------------------------------------
19
#data & url for putting data
20
shp_name = "fishcages_Greece"
21
archive = "D:/app/" + shp_name + ".zip" #specify your working directory here
22
ws_name = "aaps" #name of the workspace
23
ds_name = "aaps_datastore" #name of the datastore
24
message = open(archive, 'rb')
25
upload_url = gs_url + '/workspaces/' + ws_name + '/datastores/' + ds_name + '/file.shp?configure=none&update=overwrite'
26
headers = {"Content-type": "application/zip", "Accept": "application/xml"}
27
headers, response = cat.http.request(upload_url, "PUT", message, headers)
28
uploaded = headers.status == 201
29

    
30
#publish feature type with direct FeatureType class
31
#--------------------------------------------------------------------------------
32
#(gsconfig catalog.publish_featuretype() method support limited
33
if uploaded:
34
    ds = cat.get_store(ds_name)
35
    feature_type = FeatureType(cat, ds.workspace, ds, shp_name)
36
    feature_type.dirty['nativeName'] = shp_name
37
    feature_type.dirty['name'] = shp_name
38
    feature_type.dirty['srs'] = 'EPSG:4326'
39
    feature_type.dirty['nativeCRS'] = 'EPSG:4326'
40
    feature_type.enabled = True
41
    feature_type.title = "some title"
42
    #create featureType GS resource
43
    resource_url= gs_url + "/workspaces/" + ds.workspace.name + "/datastores/" + ds.name + "/featuretypes.json"
44
    headers = {"Content-type": "application/xml", "Accept": "application/xml"}
45
    headers, response = cat.http.request(resource_url, "POST", feature_type.message(), headers)
46
    #TODO need further coding to publish specific layer styles, etc
47
    #reload catalogue
48
    cat.reload()
49
else:
50
   print "Error while uploading ESRI Shapefile"    
51

    
52

    
53