ImageWMSServer.py
10.1 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# coding=utf-8
#author: 4N
#createtime: 2021/10/26
#email: nheweijun@sina.com
import numpy
from app.util.component.ParameterUtil import ParameterUtil
from .Cache import Cache
from app.modules.service.models import ImageService
from flask import Response
from .ImageData import ImageData
from .MyThread import MyThread
from .Opencv import Opencv
import json
import random
import configure
import threading
from .ImageServer import ImageServer
class ImageWMSServer(ImageServer):
_instance_lock = threading.Lock()
singleton = None
def __init__(self):
pass
def __new__(cls, *args, **kwargs):
if not cls.singleton:
with ImageWMSServer._instance_lock:
cls.singleton = super().__new__(cls)
return cls.singleton
def wms(self,service_name,parameter,type="name"):
# 获取缓存信息
image_service_info, zoo, servers = Cache.cache_data(service_name, type=type)
# 转换参数
parameter = ParameterUtil.to_lower(parameter)
re = parameter.get("request")
if re and re.__eq__("GetCapabilities"):
return self.get_wms_capabilities(image_service_info["service"])
bbox = parameter.get("bbox")
width = int(parameter.get("width")) if parameter.get("width") else 256
height = int(parameter.get("height")) if parameter.get("height") else 256
image_type = parameter.get("format") if parameter.get("format") else "image/png"
quality = int(parameter.get("quality")) if parameter.get("quality") else 30
extent = [float(x) for x in bbox.split(",")]
intersect_image = [im for im in image_service_info["images"] if
self.determin_intersect(json.loads(im.get("extent")), extent)]
if len(intersect_image) > 1:
# 结果矩阵
empty_list = [numpy.zeros((height, width), dtype=int) + 65536,
numpy.zeros((height, width), dtype=int) + 65536,
numpy.zeros((height, width), dtype=int) + 65536]
pixel_array = numpy.zeros((height, width, 3), dtype=int)
thread_list = []
for image in intersect_image:
# 该影像的服务器,随机选取一个
image_servers = image.get("server").split(",")
image_servers = [ser for ser in image_servers if ser in servers]
if len(image_servers) > 0:
indx = int(random.random() * len(image_servers))
image_server = image_servers[indx]
else:
image_server = "None"
bands = json.loads(image.get("band_view"))
image_data = ImageData(image_server, image)
thread: MyThread = MyThread(image_data.get_data, args=(extent, bands, height, width))
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
data = thread.get_result()
# 掩膜在中央接口生成,合图
mask = numpy.zeros((height, width), dtype=int)
mask2 = numpy.zeros((height, width), dtype=int)
jizhun = data[:, :, 0]
mask[jizhun == 65536] = 1
mask[jizhun != 65536] = 0
mask2[jizhun == 65536] = 0
mask2[jizhun != 65536] = 1
# 掩膜计算
for i, d in enumerate(empty_list):
empty_list[i] = empty_list[i] * mask + data[:, :, i] * mask2
for ii in [0, 1, 2]:
# opencv 颜色排序为GBR
pixel_array[:, :, 2 - ii] = empty_list[ii]
elif len(intersect_image) == 1:
# 该影像的服务器,随机选取一个
image = intersect_image[0]
image_servers = image.get("server").split(",")
image_servers = [ser for ser in image_servers if ser in servers]
if len(image_servers) > 0:
indx = int(random.random() * len(image_servers))
image_server = image_servers[indx]
else:
image_server = "None"
bands = json.loads(image.get("band_view"))
image_data = ImageData(image_server, image)
pixel_array_t = image_data.get_data(extent, bands, height, width)
pixel_array = numpy.zeros((height, width, 3), dtype=int)
for ii in [0, 1, 2]:
# opencv 颜色排序为GBR
pixel_array[:, :, 2 - ii] = pixel_array_t[:, :, ii]
else:
# 结果矩阵
pixel_array = numpy.zeros((height, width, 3), dtype=int) + 65536
# 将图片生成在内存中,然后直接返回response
im_data = Opencv.create_image(image_type, pixel_array, quality)
if parameter.get("overview"):
return pixel_array
return Response(im_data, mimetype=image_type.lower())
def get_wms_capabilities(self, image_service: ImageService):
xml = '''<?xml version="1.0" encoding="utf-8" ?>
<WMS_Capabilities version="1.2.0">
<Service>
<Name>WMS</Name>
<Title>{service_title}</Title>
<Abstract>{abstract}</Abstract>
<Keywords>GIMS</Keywords>
<OnlineResource/>
<Fees>none</Fees>
<AccessConstraints>none</AccessConstraints>
</Service>
<Capability>
<Request>
<GetCapabilities>
<Format>text/xml</Format>
<DCPType>
<HTTP>
<Get>
<OnlineResource xlink:href="{url}" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</Get>
</HTTP>
</DCPType>
</GetCapabilities>
<GetMap>
<Format>png</Format>
<Format>jpeg</Format>
<Format>gif</Format>
<Format>image/png</Format>
<Format>image/jpeg</Format>
<Format>image/gif</Format>
<DCPType>
<HTTP>
<Get>
<OnlineResource xlink:href="{url}" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</Get>
</HTTP>
</DCPType>
</GetMap>
<Map>
<Format>
<PNG/>
<GIF/>
<JPG/>
</Format>
<DCPType>
<HTTP>
<Get onlineResource="{url}"/>
</HTTP>
</DCPType>
</Map>
<Capabilities>
<Format>
<WMS_XML/>
</Format>
<DCPType>
<HTTP>
<Get onlineResource="{url}"/>
</HTTP>
</DCPType>
</Capabilities>
<FeatureInfo>
<Format>
<XML/>
<MIME/>
</Format>
<DCPType>
<HTTP>
<Get onlineResource="{url}"/>
</HTTP>
</DCPType>
</FeatureInfo>
</Request>
<Exception>
<Format>
<WMS_XML/>
<INIMAGE/>
<BLANK/>
</Format>
</Exception>
<Layer>
<Name>{service_name}</Name>
<Title>{service_title}</Title>
<CRS>{crs}</CRS>
<BoundingBox CRS="{crs}" maxx="{maxx}" maxy="{maxy}" minx="{minx}" miny="{miny}"/>
<Layer queryable="1">
<CRS>{crs}</CRS>
<Name>{layer_name}</Name>
<Title>{layer_title}</Title>
<BoundingBox CRS="{crs}" maxx="{maxx}" maxy="{maxy}" minx="{minx}" miny="{miny}"/>
</Layer>
</Layer>
</Capability>
</WMS_Capabilities>'''
extent = json.loads(image_service.get("extent"))
xml = xml.format(service_title=image_service.get("name"),
service_name=image_service.get("name"),
abstract="None",
crs="ESPG:4326",
layer_name=image_service.get("name"),
layer_title=image_service.get("name"),
maxx=extent[2],
maxy=extent[3],
minx=extent[0],
miny=extent[1],
url="http://{}/API/Service/Image/WMS?guid={}".format(configure.deploy_ip_host,
image_service.get("guid")))
r = Response(response=xml, status=200, mimetype="application/xml")
r.headers["Content-Type"] = "text/xml; charset=utf-8"
return r
def determin_intersect(self, extent1, extent2):
if extent2[2] < extent1[0] or extent2[0] > extent1[2] or extent2[1] > extent1[
3] or extent2[3] < extent1[1]:
return False
else:
return True