项目需求
项目中需要对影像作差值处理之后自动发布,自动发布就要生成xml或者json数据存入geoserver文件中,我刚开始以为放到文件夹中就可以,但是需要重新启动geoserver才可以,这样的话功能会就会显得很鸡肋,后来查看了网上的大神的资料才知道geoserver是有restapi,但是在使用过程中也遇到一些问题,最终功夫不负有心人,解决了相关问题,所以记录一下。
项目实现
geoserver的RESTAPI写的其实很清楚,就是发送请求接收请求即可。
代码是用python实现的,以生成发布栅格影像为例(其中包括增删查改内容)。
涉及到发布栅格影像那就包括对其中几个文件夹进行操作
workspace、coveragestore、coverage、layer
其中增加删除等先后顺序弄错的话,作者就在这里绕了半天,特别需要注意,其他倒没有什么难点。
代码实现
- 1、增加工作区(找到工作区相关API介绍)
# 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>
- 2、增加coveragestore(找到coveragestore相关API介绍)
# 查看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>
- 3、新增coverage(coverages的API)
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["WGS 84 / UTM zone 48N", 
GEOGCS["WGS 84", 
DATUM["World Geodetic System 1984", 
SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], 
AUTHORITY["EPSG","6326"]], 
PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
UNIT["degree", 0.017453292519943295], 
AXIS["Geodetic longitude", EAST], 
AXIS["Geodetic latitude", NORTH], 
AUTHORITY["EPSG","4326"]], 
PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], 
PARAMETER["central_meridian", 105.0], 
PARAMETER["latitude_of_origin", 0.0], 
PARAMETER["scale_factor", 0.9996], 
PARAMETER["false_easting", 500000.0], 
PARAMETER["false_northing", 0.0], 
UNIT["m", 1.0], 
AXIS["Easting", EAST], 
AXIS["Northing", NORTH], 
AUTHORITY["EPSG","32648"]]</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)
解压并复制到$GEOSERVER_HOME/webapps/geoserver/WEB-INF/lib目录 重启GeoServer
具体可以查看网站学习css样式配置
同时也可以在Udig中配置好样式复制粘贴到geoserver样式设置中