capabilities.py 12.2 KB
# coding=utf-8
#author:        4N
#createtime:    2021/4/6
#email:         nheweijun@sina.com


from flask import Response
from app.util.component.ApiTemplate import ApiTemplate
from ..models import ImageService
from ..models import Service,TileScheme
import json
import configure


class Api(ApiTemplate):

    api_name = "影像能力文档"

    def process(self):

        guid = self.para.get("guid")
        image_service:ImageService = ImageService.query.filter_by(guid=guid).one_or_none()
        if not image_service:
            raise Exception("服务不存在!")
        service = Service.query.filter_by(guid=image_service.service_guid).one_or_none()

        if service.type.__eq__("ImageWMS"):
            xml =  self.get_wms_capabilities(image_service)

        elif service.type.__eq__("ImageWMTS"):
            #arcgis能加载
            xml = self.get_wmts_capabilities(image_service,service)
        else:
            xml = ""

        r = Response(response=xml, status=200, mimetype="application/xml")
        r.headers["Content-Type"] = "text/xml; charset=utf-8"
        return r

    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.extent)
        xml = xml.format(service_title=image_service.name,
                         service_name=image_service.name,
                         abstract= "None" ,
                         crs="ESPG:4326",
                         layer_name=image_service.name,
                         layer_title=image_service.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.guid))
        return xml

    def get_wmts_capabilities(self, image_service: ImageService,service:Service):
        tile_scheme:TileScheme = TileScheme.query.filter_by(guid = image_service.scheme_guid).one_or_none()
        if not tile_scheme:
            raise Exception("切片方案不存在!")

        xml = '''<Capabilities xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd" version="1.0.0">
             <!--  Service Identification  -->
             <ows:ServiceIdentification>
             <ows:Title>{title}</ows:Title>
             <ows:ServiceType>OGC WMTS</ows:ServiceType>
             <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
             </ows:ServiceIdentification>
             
             <!--  Operations Metadata  -->
             <ows:OperationsMetadata>
             <ows:Operation name="GetCapabilities">
             <ows:DCP>
             <ows:HTTP>
             <ows:Get xlink:href="{capabilities_url}">
             <ows:Constraint name="GetEncoding">
             <ows:AllowedValues>
             <ows:Value>RESTful</ows:Value>
             </ows:AllowedValues>
             </ows:Constraint>
             </ows:Get>
             
             <!--  add KVP binding in 10.1  -->
             <ows:Get xlink:href="{tile_url}?">
             <ows:Constraint name="GetEncoding">
             <ows:AllowedValues>
             <ows:Value>KVP</ows:Value>
             </ows:AllowedValues>
             </ows:Constraint>
             </ows:Get>
             </ows:HTTP>
             </ows:DCP>
             </ows:Operation>
             <ows:Operation name="GetTile">
             <ows:DCP>
             <ows:HTTP>
             <ows:Get xlink:href="{tile_url}">
             <ows:Constraint name="GetEncoding">
             <ows:AllowedValues>
             <ows:Value>RESTful</ows:Value>
             </ows:AllowedValues>
             </ows:Constraint>
             </ows:Get>
             <ows:Get xlink:href="{tile_url}?">
             <ows:Constraint name="GetEncoding">
             <ows:AllowedValues>
             <ows:Value>KVP</ows:Value>
             </ows:AllowedValues>
             </ows:Constraint>
             </ows:Get>
             </ows:HTTP>
             </ows:DCP>
             </ows:Operation>
             </ows:OperationsMetadata>
             
             <Contents>
             
             <!-- Layer -->
             
             
             <Layer>
             <ows:Title>{title}</ows:Title>
             <ows:Identifier>{title}</ows:Identifier>
             <ows:BoundingBox crs="{crs}">
             <ows:LowerCorner>{xmin} {ymin}</ows:LowerCorner>
             <ows:UpperCorner>{xmax} {ymax}</ows:UpperCorner>
             </ows:BoundingBox>
             
             <Style isDefault="true">
             <ows:Title>Default Style</ows:Title>
             <ows:Identifier>default</ows:Identifier>
             </Style>
             <Format>image/png</Format>
             <TileMatrixSetLink>
             <TileMatrixSet>{tile_name}</TileMatrixSet>
             </TileMatrixSetLink>
             
             <ResourceURL format="image/png" resourceType="tile" template="{tile_url}"/>
             
             </Layer>
             
             <!-- TileMatrixSet -->
             
             
             <TileMatrixSet>
             
             <TileMatrix>
             <ows:Title>{tile_title}</ows:Title>
             <ows:Abstract>{tile_description}</ows:Abstract>
             <ows:Identifier>{tile_name}</ows:Identifier>
             <ows:SupportedCRS>{crs}</ows:SupportedCRS>
             
             {tile_matrix}
             
             </TileMatrix>
             
             </TileMatrixSet>
             
             
             </Contents>
             <ServiceMetadataURL xlink:href="{capabilities_url}"/>
             </Capabilities>'''




        tile_matrix_each = '''
             <TileMatrix>
             <ows:Identifier>{lev}</ows:Identifier>
             <ScaleDenominator>{scale}</ScaleDenominator>
             <TopLeftCorner>{top_left}</TopLeftCorner>
             <TileWidth>{cols}</TileWidth>
             <TileHeight>{rows}</TileHeight>
             </TileMatrix>
        '''

        tile_matrix = ""
        top_left = tile_scheme.top_left
        for level in json.loads(tile_scheme.levels):
            tile_matrix = "{}{}".format(tile_matrix,tile_matrix_each.format(lev=level["level"],
                                                                            scale=level["scale"],
                                                                            top_left=top_left,
                                                                            cols=tile_scheme.cols,
                                                                            rows=tile_scheme.rows))

        extent = json.loads(image_service.extent)

        xml = xml.format(capabilities_url = "http://{}/API/Service/Image/Capabilities?guid={}".format(configure.deploy_ip_host,image_service.guid),
                         tile_url = "http://{}/API/Service/Image/Tile?guid={}".format(configure.deploy_ip_host,image_service.guid),
                         crs = tile_scheme.crs,
                         xmin = extent[0],
                         ymin = extent[1],
                         xmax = extent[2],
                         ymax = extent[3],
                         # TileMatrix = "{TileMatrix}",
                         # TileRow = "{TileRow}",
                         # TileCol = "{TileCol}",
                         guid = image_service.guid,
                         title = service.title,
                         tile_title = tile_scheme.name,
                         tile_name = tile_scheme.name,
                         tile_description = tile_scheme.description,
                         tile_matrix=tile_matrix
                         )

        return xml


    api_doc = {
    "tags": ["影像接口"],
    "parameters": [
        {"name": "guid",
         "in": "formData",
         "type": "string",
         "description": "服务guid"},
    ],
    "responses": {
        200: {
            "schema": {
                "properties": {
                }
            }
        }
    }
}