You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python和MapServer通过WCS协议发布NetCDF数据的问题

基于Python和MapServer通过WCS协议发布NetCDF数据的问题

我想用MapServer的Python绑定通过WCS发布NetCDF资源,从MapServer测试套件里找了个示例地图文件,还把里面的wcs_onlineresource改成了我的API端点https://mydomain/get_wcs/test/wcs。

我的API是基于FastAPI写的,get_wcs方法代码如下:

@router.get('/get_wcs/{data_id}/wcs', response_class=Response)
async def get_wcs(data_id: str, full_request: Request):
    print('hoi!')
    map = mapscript.mapObj('/app/data/nc.map')
    print(map)
    ows_req = mapscript.OWSRequest()
    ows_req.setParameter('service', 'wcs')
    ows_req.setParameter('version', '2.0.0')
    ows_req.setParameter('request', 'getcoverage')
    ows_req.setParameter('coverageid', 'precipitation')
    ows_req.setParameter('format', 'netCDF')

    ows_req.type = mapscript.MS_GET_REQUEST
    print(ows_req)
    format = map.getOutputFormatByName("netCDF")
    print(format)
    mapscript.msIO_installStdoutToBuffer()
    dispatch_status = map.OWSDispatch(ows_req)
    result = mapscript.msIO_getStdoutBufferBytes()
    print(result)
    content_type = mapscript.msIO_stripStdoutBufferContentType()
    print(content_type)
    #print('so far no errors')
    #pass
    ows_req.setParameter("REQUEST", "GetCapabilities")
    mapscript.msIO_installStdoutToBuffer()
    dispatch_status = map.OWSDispatch(ows_req)
    if dispatch_status != mapscript.MS_SUCCESS:
        if os.getenv('DEBUG') == '1':
            logging.debug(f"DISPATCH status: {dispatch_status}")
    mapscript.msIO_stripStdoutBufferContentHeaders()
    _result = mapscript.msIO_getStdoutBufferBytes()
    content_type = 'text/xml'
    dom = xml.dom.minidom.parseString(_result)
    result_xml = dom.toprettyxml(indent="", newl="")
    return Response(result_xml, media_type=content_type)

现在我遇到了两个棘手的问题:

  • 调用GetCoverage接口时,result看起来是正确的二进制流,但我不知道该怎么正确返回它,也不清楚该如何处理不同类型的WCS请求;
  • 尝试返回默认的GetCapabilities结果时,生成的XML内容看起来没问题(能正常读取地图文件里的信息),但QGIS就是加载不了,XML大致内容如下:
<?xml version="1.0" ?>
<wcs:Capabilities xmlns:wcs="http://www.opengis.net/wcs/2.0" xmlns:ows="http://www.opengis.net/ows/2.0" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0" xmlns:swe="http://www.opengis.net/swe/2.0" xmlns:crs="http://www.opengis.net/wcs/crs/1.0" xmlns:int="http://www.opengis.net/wcs/interpolation/1.0" xsi:schemaLocation="http://www.opengis.net/wcs/2.0 http://schemas.opengis.net/wcs/2.0/wcsAll.xsd " version="2.0.0" updateSequence="2007-10-30T14:23:38Z">
  <ows:ServiceIdentification>
    <ows:Title>First Test Service</ows:Title>
    <ows:Abstract>Test Abstract</ows:Abstract>
    <ows:Keywords>
      <ows:Keyword>keyword</ows:Keyword>
      <ows:Keyword>list</ows:Keyword>
    </ows:Keywords>
    <ows:ServiceType codeSpace="OGC">OGC WCS</ows:ServiceType>
    <ows:ServiceTypeVersion>2.0.1</ows:ServiceTypeVersion>
    <ows:ServiceTypeVersion>1.1.1</ows:ServiceTypeVersion>
    <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
    <ows:Profile>http://www.opengis.net/spec/WCS/2.0/conf/core</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/WCS_protocol-binding_get-kvp/1.0/conf/get-kvp</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/WCS_protocol-binding_post-xml/1.0/conf/post-xml</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/GMLCOV/1.0/conf/gml-coverage</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/GMLCOV/1.0/conf/multipart</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/GMLCOV/1.0/conf/special-format</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/GMLCOV_geotiff-coverages/1.0/conf/geotiff-coverage</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/WCS_service-extension_crs/1.0/conf/crs</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/WCS_service-extension_scaling/1.0/conf/scaling</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/WCS_service-extension_range-subsetting/1.0/conf/record-subsetting</ows:Profile>
    <ows:Profile>http://www.opengis.net/spec/WCS_service-extension_interpolation/1.0/conf/interpolation</ows:Profile>
    <ows:Fees>NONE</ows:Fees>
    <ows:AccessConstraints>NONE</ows:AccessConstraints>
  </ows:ServiceIdentification>
  <ows:ServiceProvider>
    <ows:ProviderName>OSGeo</ows:ProviderName>
    <ows:ProviderSite xlink:type="simple" xlink:href="https://wms.wps.met.no/get_wcs/test/wcs"/>
    <ows:ServiceContact>
      <ows:IndividualName>Name FamilyName</ows:IndividualName>
      <ows:PositionName>Software Developer</ows:PositionName>
      <ows:ContactInfo>
        <ows:Phone>
          <ows:Voice>(000) 000-0000</ows:Voice>
          <ows:Facsimile>(000) 000-00000000</ows:Facsimile>
        </ows:Phone>
        <ows:Address>
          <ows:DeliveryPoint>000 Xoxox Rd</ows:DeliveryPoint>
          <ows:City>City</ows:City>
          <ows:AdministrativeArea>Province</ows:AdministrativeArea>
          <ows:PostalCode>xxx xxx</ows:PostalCode>
          <ows:Country>World</ows:Country>
          <ows:ElectronicMailAddress>epi@epi.com</ows:ElectronicMailAddress>
        </ows:Address>
        <ows:OnlineResource xlink:type="simple" xlink:href="https://wms.wps.met.no/get_wcs/test/wcs"/>
        <ows:HoursOfService>0800h - 1600h EST</ows:HoursOfService>
        <ows:ContactInstructions>during hours of service</ows:ContactInstructions>
      </ows:ContactInfo>
      <ows:Role>staff</ows:Role>
    </ows:ServiceContact>
  </ows:ServiceProvider>
  <ows:OperationsMetadata>
    <ows:Operation name="GetCapabilities">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:type="simple" xlink:href="https://endpoint/get_wcs/test/wcs?"/>
          <ows:Post xlink:type="simple" xlink:href="https://endpoint/get_wcs/test/wcs?">
            <ows:Constraint name="PostEncoding">
              <ows:AllowedValues>
                <ows:Value>XML</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Post>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
    <ows:Operation name="DescribeCoverage">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:type="simple" xlink:href="https://wms.wps.met.no/get_wcs/test/wcs?"/>
          <ows:Post xlink:type="simple" xlink:href="https://wms.wps.met.no/get_wcs/test/wcs?">
            <ows:Constraint name="PostEncoding">
              <ows:AllowedValues>
                <ows:Value>XML</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Post>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
    <ows:Operation name="GetCoverage">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:type="simple" xlink:href="https://wms.wps.met.no/get_wcs/test/wcs?"/>
          <ows:Post xlink:type="simple" xlink:href="https://wms.wps.met.no/get_wcs/test/wcs?">
            <ows:Constraint name="PostEncoding">
              <ows:AllowedValues>
                <ows:Value>XML</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Post>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
  </ows:OperationsMetadata>
  <wcs:ServiceMetadata>
    <wcs:formatSupported>application/x-netCDF</wcs:formatSupported>
    <wcs:Extension>
      <int:InterpolationMetadata>
        <int:InterpolationSupported>NEAREST</int:InterpolationSupported>
        <int:InterpolationSupported>AVERAGE</int:InterpolationSupported>
        <int:InterpolationSupported>BILINEAR</int:InterpolationSupported>
      </int:InterpolationMetadata>
      <crs:CrsMetadata>
        <crs:crsSupported>http://www.opengis.net/def/crs/EPSG/0/4326</crs:crsSupported>
      </crs:CrsMetadata>
    </wcs:Extension>
  </wcs:ServiceMetadata>
  <wcs:Contents>
    <wcs:CoverageSummary>
      <wcs:CoverageId>precipitation</wcs:CoverageId>
      <wcs:CoverageSubtype>gridCoverage</wcs:CoverageSubtype>
      <!-- 后续内容省略 -->
    </wcs:CoverageSummary>
  </wcs:Contents>
</wcs:Capabilities>

备注:内容来源于stack exchange,提问作者epifanio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:59:32