image_service_register.py
6.8 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
# coding=utf-8
#author: 4N
#createtime: 2021/7/19
#email: nheweijun@sina.com
from ..models import db,Service,ImageService,Image,ServiceFunction
from app.util.component.ApiTemplate import ApiTemplate
import uuid
import os
import json
import datetime
from .image_wms import Api as RealApi
import cv2
import configure
class Api(ApiTemplate):
api_name = "注册影像服务"
def para_check(self):
if not self.para.get("function_types"):
raise Exception("缺乏影像服务类型!")
if self.para.get("function_types").__contains__("WMTS") and (not self.para.get("scheme_guid")):
raise Exception("选择WMTS功能,但是缺乏切片方案!")
def process(self):
# 返回结果
res = {}
try:
guids = self.para.get("guids").split(",")
name = self.para.get("name")
function_types = self.para.get("function_types")
image_service_guid = uuid.uuid1().__str__()
service_guid = uuid.uuid1().__str__()
service_function_guid = uuid.uuid1().__str__()
this_time = datetime.datetime.now()
image_service = ImageService(guid=image_service_guid,
name=name,
create_time=this_time,
scheme_guid=self.para.get("scheme_guid"),
crs = self.para.get("crs"),
service_guid=service_guid)
image_service_exetent = []
for g in guids:
image = Image.query.filter_by(guid=g).one_or_none()
if image:
image_extent = json.loads(image.extent)
if not image_service_exetent:
image_service_exetent = image_extent
else:
image_service_exetent[0] = min(image_extent[0],image_service_exetent[0])
image_service_exetent[1] = min(image_extent[1], image_service_exetent[1])
image_service_exetent[2] = max(image_extent[2], image_service_exetent[2])
image_service_exetent[3] = max(image_extent[3], image_service_exetent[3])
image_service.images.append(image)
image_service.extent = json.dumps(image_service_exetent)
service = Service(
guid = service_guid,
name = name,
title = self.para.get("title"),
state = 1,
create_time = this_time,
update_time = this_time,
description = self.para.get("description"),
#node = Column(Integer),
type = self.para.get("type"),
catalog_guid = self.para.get("catalog_guid")
)
for type in function_types.split(","):
service_function = ServiceFunction(guid=service_function_guid,
service_guid=service_guid,
type=type)
db.session.add(service_function)
db.session.add(service)
db.session.add(image_service)
#获得overview
overview_file = self.get_overview(service,image_service)
service.overview="http://{}/API/Service/Overview/{}".format(
configure.deploy_ip_host,overview_file)
db.session.commit()
res["data"] = image_service_guid
res["result"] = True
except Exception as e:
db.session.rollback()
raise e
return res
def get_overview(self,service,image_service):
api = RealApi("")
query_extent = json.loads(image_service.extent)
if query_extent[2] - query_extent[0] > query_extent[3] - query_extent[1]:
offset = ((query_extent[2] - query_extent[0]) - (query_extent[3] - query_extent[1])) / 2.0
query_extent = [query_extent[0], query_extent[1] - offset, query_extent[2], query_extent[3] + offset]
else:
offset = ((query_extent[3] - query_extent[1]) - (query_extent[2] - query_extent[0])) / 2.0
query_extent = [query_extent[0] - offset, query_extent[1], query_extent[2] + offset, query_extent[3]]
bbox = ",".join([str(x) for x in query_extent])
api.para = {"service_name":service.name,"bbox":bbox,"overview":1,"width":512,"height":512}
res = api.process()
dir_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "overview")
gid = uuid.uuid1().__str__()
store_file = os.path.join(dir_path, "{}.jpg".format(gid))
cv2.imwrite(store_file, res, [cv2.IMWRITE_JPEG_QUALITY, 30])
return "{}.jpg".format(gid)
api_doc = {
"tags": ["影像接口"],
"parameters": [
# 通用参数
{"name": "name",
"in": "formData",
"type": "string",
"required": "true",
"description": "[地图服务,切片服务,影像服务]"},
{"name": "title",
"in": "formData",
"type": "string",
"description": "[地图服务,切片服务,影像服务]"},
{"name": "description",
"in": "formData",
"type": "string",
"description": "[地图服务,切片服务,影像服务]"},
{"name": "type",
"in": "formData",
"type": "string",
"enum": ["地图服务","切片服务","影像服务"],
"required": "true",
"description": "[地图服务,切片服务,影像服务]"},
{"name": "catalog_guid",
"in": "formData",
"type": "string",
"description": "[地图服务,切片服务,影像服务]"},
# 影像参数
{"name": "guids",
"in": "formData",
"type": "string",
"description": "[影像服务]影像guids,以英文逗号相隔"},
{"name": "scheme_guid",
"in": "formData",
"type": "string",
"description": "[影像服务]切片方案"},
{"name": "function_types",
"in": "formData",
"type": "string",
"description": "[影像服务]功能类型,以逗号相隔,可选WMTS,WMS"},
],
"responses": {
200: {
"schema": {
"properties": {
}
}
}
}
}