wmts_register.py 6.5 KB
# coding=utf-8
#author:        4N
#createtime:    2021/7/19
#email:         nheweijun@sina.com


from app.util.component.ApiTemplate import ApiTemplate
import uuid
from .models import WMTS
from app.models import Service,db
import datetime
import configure
import requests
from requests import Response
import json
class Api(ApiTemplate):

    api_name = "注册WMTS服务"

    def process(self):
        # 返回结果
        res = {}

        try:

            this_time = datetime.datetime.now()
            service_guid = uuid.uuid1().__str__()
            wmts_service_guid = uuid.uuid1().__str__()

            service = Service(
                guid = service_guid,
                name = self.para.get("name"),
                alias = self.para.get("alias"),
                state = 1,
                create_time = this_time,
                update_time = this_time,
                description = self.para.get("description"),
                node = 1,
                overview = "http://{}/API/Service/Overview/{}".format(
                    configure.deploy_ip_host,
                    self.para.get("overview") if self.para.get("overview") else "wmts_tb.png"),
                type = self.para.get("type"),
                service_guid = wmts_service_guid,
                catalog_guid = self.para.get("catalog_guid")
            )

            #已存在就删除
            wmts_isexist = WMTS.query.filter_by(name=self.para.get("name")).one_or_none()
            if wmts_isexist:
                db.session.delete(wmts_isexist)

            wmts = WMTS(
                guid = wmts_service_guid,
                name = self.para.get("name"),
                wmts_type = self.para.get("wmts_type"),
                vendor = self.para.get("vendor"),
                create_time = this_time,
                crs = self.para.get("crs"),
                datasource = self.para.get("datasource"),
                description = self.para.get("description"),
                layer_name = self.para.get("layer_name"),
                layer_alias = self.para.get("layer_alias"),
                layer_tile = self.para.get("layer_title"),
                layer_style = self.para.get("layer_style"),
                layer_format = self.para.get("layer_format"),
                layer_extent = self.para.get("layer_extent"),
                layer_description = self.para.get("layer_description"),
                scheme_guid = self.para.get("scheme_guid"),
                service_guid = service_guid,
                metadata_url="{}/DMap/Services/{}/MapServer/WMTSServer".format(configure.wmts_url,self.para.get("name"))
            )

            db.session.add(service)
            db.session.add(wmts)
            db.session.commit()

            # 调用WMTS的注册服务接口
            try:
                url = "{}/dmap/api/manager/updatecache?servicename={}".format(configure.wmts_url,service.name)
                resp:Response = requests.get(url)
                if not resp.json()["status"].__eq__("true"):
                    raise Exception("调用WMTS的注册服务接口失败!")
            except Exception as e:
                db.session.delete(service)
                db.session.delete(wmts)
                db.session.commit()
                raise e
            res["data"] = service_guid
            res["result"] = True
        except Exception as e:
            db.session.rollback()
            raise e
        return res

    api_doc = {
        "tags": ["WMTS接口"],
        "parameters": [
            {"name": "name",
             "in": "formData",
             "type": "string",
             "description": "[WMS,WMTS,影像WMS,影像WMTS]"},
            {"name": "alias",
             "in": "formData",
             "type": "string",
             "description": "[WMS,WMTS,影像WMS,影像WMTS]"},
            {"name": "description",
             "in": "formData",
             "type": "string",
             "description": "[WMS,WMTS,影像WMS,影像WMTS]"},
            {"name": "type",
             "in": "formData",
             "type": "string",
             "description": "[WMS,WMTS,影像WMS,影像WMTS]"},
            {"name": "catalog_guid",
             "in": "formData",
             "type": "string",
             "description": "[WMS,WMTS,影像WMS,影像WMTS]"},
            {"name": "scheme_guid",
             "in": "formData",
             "type": "string",
             "description": "[WMTS,影像WMTS]切片方案"},
            {"name": "overview",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]缩略图"},
            {"name": "wmts_type",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]wmts_type"},
            {"name": "vendor",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]厂商"},
            {"name": "crs",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]坐标系"},
            {"name": "datasource",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]数据路径"},
            {"name": "layer_name",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层名"},
            {"name": "layer_alias",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层别名"},
            {"name": "layer_title",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层标题"},
            {"name": "layer_style",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层样式"},
            {"name": "layer_format",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层format"},
            {"name": "layer_extent",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层范围"},
            {"name": "layer_description",
             "in": "formData",
             "type": "string",
             "description": "[WMTS]图层描述"},
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }