Geoserver的restAPI接口及样式(style)设置

Geoserver的restAPI接口及样式style设置

项目需求

项目中需要对影像作差值处理之后自动发布,自动发布就要生成xml或者json数据存入geoserver文件中,我刚开始以为放到文件夹中就可以,但是需要重新启动geoserver才可以,这样的话功能会就会显得很鸡肋,后来查看了网上的大神的资料才知道geoserver是有restapi,但是在使用过程中也遇到一些问题,最终功夫不负有心人,解决了相关问题,所以记录一下。

项目实现

geoserver的RESTAPI写的其实很清楚,就是发送请求接收请求即可。
代码是用python实现的,以生成发布栅格影像为例(其中包括增删查改内容)。
涉及到发布栅格影像那就包括对其中几个文件夹进行操作
workspace、coveragestore、coverage、layer
其中增加删除等先后顺序弄错的话,作者就在这里绕了半天,特别需要注意,其他倒没有什么难点。

代码实现

 # TODO 查询指定工作区
    def getworkspaces(self):
        headers = {'Accept': 'application/xml'}
        response = requests.get('http://localhost:8080/geoserver/rest/workspaces/'+self.workspace,
                                auth=('admin', 'geoserver'), headers=headers)
        return response.status_code  # todo  查看工作区下的数据存储 
 # todo 新增工作区
    def addworkspace(self):  
        headers = {'content-type': 'application/xml'}
        response = requests.post(self.basicworkspaceurl,
                                 data=open('workspaces.xml').read(), auth=('admin', 'geoserver'), headers=headers)
        print(response.status_code)

其中workspaces.xml内容如下(包含的是你要增加的工作区名字)

<?xml version="1.0" encoding="UTF-8"?>
<workspace>
	<name>TheWaterQualityComparision</name>
</workspace>
# 查看coveragestore,其中coveragename是其名字,还有指定其对应的工作区
  def getcoveragestore(self,coveragename):
        headers = {'Accept': 'application/xml'}
        response = requests.get('http://localhost:8080/geoserver/rest/workspaces/' + self.workspace+"/coveragestores/"+coveragename,
                                auth=('admin', 'geoserver'), headers=headers)
        return response.status_code  
 # 新增coveragestore,其中coveragename是其名字,还有指定其对应的工作区
    def addcoveragestroe(self,coveragestorepath): # TODO 新增coveragestore
        headers = {'content-type': 'application/xml'}
        response = requests.post(self.basicworkspaceurl+"/"+self.workspace+"/coveragestores",
                                 data=open(coveragestorepath+'/coveragestore.xml').read(), auth=('admin', 'geoserver'), headers=headers)
        print(response.status_code)

其中coveragestore.xml内容如下(name,描述,存储路径、工作区名字)

<?xml version="1.0" encoding="UTF-8"?>
<coverageStore>
  <name>20192929cha</name>
  <description>testapi2</description>
	<type>GeoTIFF</type>
	<enabled>true</enabled>
  <url>file://G:\Water_enviroment_Data\2016\2016_calss.tif\C\CHA.tif</url>
  <workspace>
		<name>TheWaterQualityComparision</name>  
  </workspace>
</coverageStore>
 def addcoverage(self,coveragename,coveragepath):    #TODO 新增coverage
        headers = {'Accept': 'application/xml', 'content-type': 'application/xml'}
        url=self.basicworkspaceurl+"/"+self.workspace+"/coveragestores/"+coveragename+"/coverages"
        ps = requests.post(url=url, auth=('admin', 'geoserver'), data=open(coveragepath+'/coverage.xml').read(), headers=headers)
        return ps.status_code

其中coverages.xml内容如下(详情请查看官网)

<?xml version="1.0" encoding="UTF-8"?>
<coverage>
  <name>20192929cha</name>
  <nativeName>20192929cha</nativeName>
  <namespace>
      <name>TheWaterQualityComparision</name>
  </namespace>
  <title>201929cha</title>
  <description>Generated from GeoTIFF</description>
  <abstract>20192929cha</abstract>
  <keywords>
    <string>20192929cha</string>
    <string>WCS</string>
    <string>GeoTIFF</string>
  </keywords>
  <nativeCRS class="projected">PROJCS[&quot;WGS 84 / UTM zone 48N&quot;, &#xd;
  GEOGCS[&quot;WGS 84&quot;, &#xd;
    DATUM[&quot;World Geodetic System 1984&quot;, &#xd;
      SPHEROID[&quot;WGS 84&quot;, 6378137.0, 298.257223563, AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]], &#xd;
      AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]], &#xd;
    PRIMEM[&quot;Greenwich&quot;, 0.0, AUTHORITY[&quot;EPSG&quot;,&quot;8901&quot;]], &#xd;
    UNIT[&quot;degree&quot;, 0.017453292519943295], &#xd;
    AXIS[&quot;Geodetic longitude&quot;, EAST], &#xd;
    AXIS[&quot;Geodetic latitude&quot;, NORTH], &#xd;
    AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]], &#xd;
  PROJECTION[&quot;Transverse_Mercator&quot;, AUTHORITY[&quot;EPSG&quot;,&quot;9807&quot;]], &#xd;
  PARAMETER[&quot;central_meridian&quot;, 105.0], &#xd;
  PARAMETER[&quot;latitude_of_origin&quot;, 0.0], &#xd;
  PARAMETER[&quot;scale_factor&quot;, 0.9996], &#xd;
  PARAMETER[&quot;false_easting&quot;, 500000.0], &#xd;
  PARAMETER[&quot;false_northing&quot;, 0.0], &#xd;
  UNIT[&quot;m&quot;, 1.0], &#xd;
  AXIS[&quot;Easting&quot;, EAST], &#xd;
  AXIS[&quot;Northing&quot;, NORTH], &#xd;
  AUTHORITY[&quot;EPSG&quot;,&quot;32648&quot;]]</nativeCRS>
  <srs>EPSG:32648</srs>
  <nativeBoundingBox>
    <minx>375599.354</minx>
    <maxx>386214.354</maxx>
    <miny>3386915.462</miny>
    <maxy>3412574.462</maxy>
    <crs class="projected">EPSG:32648</crs>
  </nativeBoundingBox>
  <latLonBoundingBox>
    <minx>103.69913517033088</minx>
    <maxx>103.81295582363128</maxx>
    <miny>30.608345080421742</miny>
    <maxy>30.840889367453464</maxy>
    <crs>EPSG:4326</crs>
  </latLonBoundingBox>
  <projectionPolicy>REPROJECT_TO_DECLARED</projectionPolicy>
  <enabled>true</enabled>
  <metadata>
    <entry key="dirName">CHATEST_geotiff_coverage</entry>
  </metadata>
  <store class="coverageStore">
      <name>20192929cha</name>
  </store>
  <nativeFormat>GeoTIFF</nativeFormat>
  <grid dimension="2">
    <range>
      <low>0 0</low>
      <high>10615 25659</high>
    </range>
    <transform>
      <scaleX>1.0</scaleX>
      <scaleY>-1.0</scaleY>
      <shearX>0.0</shearX>
      <shearY>0.0</shearY>
      <translateX>375599.854</translateX>
      <translateY>3412573.962</translateY>
    </transform>
    <crs>EPSG:32648</crs>
  </grid>
  <supportedFormats>
    <string>GEOTIFF</string>
    <string>GeoPackage (mosaic)</string>
    <string>VRT</string>
    <string>EHdr</string>
    <string>ERDASImg</string>
    <string>RST</string>
    <string>AIG</string>
    <string>DTED</string>
    <string>ArcGrid</string>
    <string>GIF</string>
    <string>PNG</string>
    <string>JPEG</string>
    <string>TIFF</string>
    <string>ImageMosaic</string>
    <string>ImagePyramid</string>
    <string>NITF</string>
    <string>ENVIHdr</string>
    <string>SRP</string>
    <string>RPFTOC</string>
  </supportedFormats>
  <interpolationMethods>
    <string>nearest neighbor</string>
    <string>bilinear</string>
    <string>bicubic</string>
  </interpolationMethods>
  <defaultInterpolationMethod>nearest neighbor</defaultInterpolationMethod>
  <dimensions>
    <coverageDimension>
      <name>RED_BAND</name>
      <description>GridSampleDimension[-Infinity,Infinity]</description>
      <range>
        <min>-inf</min>
        <max>inf</max>
      </range>
      <unit>W.m-2.Sr-1</unit>
      <dimensionType>
        <name>UNSIGNED_8BITS</name>
      </dimensionType>
    </coverageDimension>
    <coverageDimension>
      <name>GREEN_BAND</name>
      <description>GridSampleDimension[-Infinity,Infinity]</description>
      <range>
        <min>-inf</min>
        <max>inf</max>
      </range>
      <unit>W.m-2.Sr-1</unit>
      <dimensionType>
        <name>UNSIGNED_8BITS</name>
      </dimensionType>
    </coverageDimension>
    <coverageDimension>
      <name>BLUE_BAND</name>
      <description>GridSampleDimension[-Infinity,Infinity]</description>
      <range>
        <min>-inf</min>
        <max>inf</max>
      </range>
      <unit>W.m-2.Sr-1</unit>
      <dimensionType>
        <name>UNSIGNED_8BITS</name>
      </dimensionType>
    </coverageDimension>
  </dimensions>
  <requestSRS>
    <string>EPSG:32648</string>
  </requestSRS>
  <responseSRS>
    <string>EPSG:32648</string>
  </responseSRS>
  <parameters>
    <entry>
      <string>InputTransparentColor</string>
      <string>#000000</string>
    </entry>
    <entry>
      <string>SUGGESTED_TILE_SIZE</string>
      <string>512,512</string>
    </entry>
  </parameters>
</coverage>

按照上述顺序执行完成之后,我们访问网站即可查看我们发布的数据。

  • 4、关于删除发布的数据以及工作区
    需要按照顺序执行,否则将会报错
    删除工作区–>需要先删除工作区里面的数据存储内容。否则直接删除工作区删除不了(很重要)
    要删除数据存储的内容,必须先删除数据存储里面的layer和coverage.必须具有优先级
    workspace(coveragestore(layer\coverages))
def deletelayer():  # TODO 删除layer
    headers = {'content-type': 'application/xml'}
    response = requests.delete(
        'http://localhost:8080/geoserver/rest/workspaces/TheWaterQualityComparision/layers/2016_2018CHA'
        , auth=('admin', 'geoserver'), headers=headers)
    print(response.status_code)
def  deletecovrerage():  # todo 删除coverages
    headers = {'content-type': 'application/xml'}
    response = requests.delete(
        'http://localhost:8080/geoserver/rest/workspaces/TheWaterQualityComparision/coveragestores/2016_2018TN/coverages/2016_2018CHA'
        , auth=('admin', 'geoserver'), headers=headers)
    print(response.status_code)
def deletecoverageStore(): # todo 删除coveragestore
    headers = {'content-type': 'application/xml'}
    response = requests.delete('http://localhost:8080/geoserver/rest/workspaces/TheWaterQualityComparision/coveragestores/2016_2018CHA'
                               , auth=('admin', 'geoserver'), headers=headers)
    print(response.status_code)
def deleteworkspace():   #TODO 删除工作区
    headers = {'content-type': 'application/xml'}
    response = requests.delete('http://localhost:8080/geoserver/rest/workspaces/TheWaterQualityComparision'
                            , auth=('admin', 'geoserver'), headers=headers)
    print(response.status_code)
  • 5、关于geoserver样式设置
    geoserver可支持SLD、CSS、YSLD、MBStyle四种方式定义Style。可查看官网进行学习
    其中css需要下载相应插件进行安装

解压并复制到$GEOSERVER_HOME/webapps/geoserver/WEB-INF/lib目录 重启GeoServer

在这里插入图片描述
具体可以查看网站学习css样式配置
在这里插入图片描述
同时也可以在Udig中配置好样式复制粘贴到geoserver样式设置中

发布了31 篇原创文章 · 获赞 29 · 访问量 8743

猜你喜欢

转载自blog.csdn.net/m0_38051293/article/details/102484319
今日推荐