-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathquick_slpk_server.py
221 lines (187 loc) · 7.35 KB
/
quick_slpk_server.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QUICK SLPK SERVER
======================
Minimalist web server engine to publish OGC SceneLayerPackage (.slpk) to Indexed 3d Scene Layer (I3S) web service.
How to use:
- Place .SLPK into a folder (default: "./slpk")
- Configure this script:
- webserver host
- webserver port
- slpk folder
- Launch this script
- Open browser to "host:port"
- Index page let you access your SLPK as I3S services
- Also provide an intern viewer for test
How to:
- Configure Index page: modify-> views/services_list.tpl
- Configure Viewer page: modify-> views/carte.tpl
Sources:
- python 2.x
- I3S Specifications: https://github.com/Esri/i3s-spec
- BottlePy 0.13+
- Arcgis Javascript API >=4.6
Autor: RIVIERE Romain
Date: 12/02/2018
Licence: GNU GPLv3
"""
# Import python modules
import bottlepy.bottle as bottle
from bottlepy.bottle import app, route, run, template, abort, response
from io import BytesIO
import os, sys, json, gzip, zipfile
#User parameter
host='localhost'
port=8080
home=os.path.join(os.path.dirname(os.path.realpath(__file__)),"slpk") #SLPK Folder
#*********#
#Functions#
#*********#
#List available SLPK
slpks=[f for f in os.listdir(home) if os.path.splitext(f)[1].lower()==u".slpk"]
def read(f,slpk):
"""read gz compressed file from slpk (=zip archive) and output result"""
if f.startswith("\\"): #remove first \
f=f[1:]
with open(os.path.join(home,slpk), 'rb') as file:
with zipfile.ZipFile(file) as zip:
if os.path.splitext(f)[1] == ".gz": #unzip GZ
gz= BytesIO(zip.read(f.replace("\\","/"))) #GZ file -> convert path sep to zip path sep
with gzip.GzipFile(fileobj=gz) as gzfile:
return gzfile.read()
else:
return zip.read(f.replace("\\","/")) #Direct read
# the decorator
def enable_cors(fn):
def _enable_cors(*args, **kwargs):
# set CORS headers
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
if bottle.request.method != 'OPTIONS':
# actual request; reply with the actual response
return fn(*args, **kwargs)
return _enable_cors
#*********#
#SRIPT****#
#*********#
app = app()
@app.route('/')
def list_services():
""" List all available SLPK, with LINK to I3S service and Viewer page"""
return template('services_list', slpks=slpks)
@app.route('/<slpk>/SceneServer')
@app.route('/<slpk>/SceneServer/')
@enable_cors
def service_info(slpk):
""" Service information JSON """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
SceneServiceInfo=dict()
SceneServiceInfo["serviceName"]=slpk
SceneServiceInfo["name"]=slpk
SceneServiceInfo["currentVersion"]=10.6
SceneServiceInfo["serviceVersion"]="1.6"
SceneServiceInfo["supportedBindings"]=["REST"]
SceneServiceInfo["layers"] = [json.loads(read("3dSceneLayer.json.gz",slpk))]
response.content_type = 'application/json'
return json.dumps(SceneServiceInfo)
@app.route('/<slpk>/SceneServer/layers/0')
@app.route('/<slpk>/SceneServer/layers/0/')
@enable_cors
def layer_info(slpk):
""" Layer information JSON """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
SceneLayerInfo=json.loads(read("3dSceneLayer.json.gz",slpk))
response.content_type = 'application/json'
return json.dumps(SceneLayerInfo)
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/')
@enable_cors
def node_info(slpk,layer,node):
""" Node information JSON """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
NodeIndexDocument=json.loads(read("nodes/%s/3dNodeIndexDocument.json.gz"%node,slpk))
response.content_type = 'application/json'
return json.dumps(NodeIndexDocument)
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/geometries/0')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/geometries/0/')
@enable_cors
def geometry_info(slpk,layer,node):
""" Geometry information bin """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
response.content_type = 'application/octet-stream; charset=binary'
response.content_encoding = 'gzip'
return read("nodes/%s/geometries/0.bin.gz"%node,slpk)
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/textures/0_0')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/textures/0_0/')
@enable_cors
def textures_info(slpk,layer,node):
""" Texture information JPG """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
response.headers['Content-Disposition'] = 'attachment; filename="0_0.jpg"'
response.content_type = 'image/jpeg'
try:
return read("nodes/%s/textures/0_0.jpg"%node,slpk)
except:
try:
return read("nodes/%s/textures/0_0.bin"%node,slpk)
except:
return ""
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/textures/0_0_1')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/textures/0_0_1/')
@enable_cors
def Ctextures_info(slpk,layer,node):
""" Compressed texture information """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
try:
return read("nodes/%s/textures/0_0_1.bin.dds.gz"%node,slpk)
except:
return ""
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/features/0')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/features/0/')
@enable_cors
def feature_info(slpk,layer,node):
""" Feature information JSON """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
print("%s")
FeatureData=json.loads(read("nodes/%s/features/0.json.gz"%node,slpk))
response.content_type = 'application/json'
return json.dumps(FeatureData)
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/shared')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/shared/')
@enable_cors
def shared_info(slpk,layer,node):
""" Shared node information JSON """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
try:
Sharedressource=json.loads(read("nodes/%s/shared/sharedResource.json.gz"%node,slpk))
response.content_type = 'application/json'
return json.dumps(Sharedressource)
except:
return ""
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/attributes/<attribute>/0')
@app.route('/<slpk>/SceneServer/layers/<layer>/nodes/<node>/attributes/<attribute>/0/')
@enable_cors
def attribute_info(slpk,layer,node,attribute):
""" Attribute information JSON """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
return read("nodes/%s/attributes/%s/0.bin.gz"%(node,attribute),slpk)
@app.route('/carte/<slpk>')
@enable_cors
def carte(slpk):
""" Preview data on a 3d globe """
if slpk not in slpks: #Get 404 if slpk doesn't exists
abort(404, "Can't found SLPK: %s"%slpk)
return template('carte', slpk=slpk, url="http://%s:%s/%s/SceneServer/layers/0"%(host,port,slpk))
#Run server
app.run(host=host, port=port)