GeoServer rest API包
geoserver-rest的Python项目详细描述
安装
geoserver-rest
包对于管理GeoServer中的地理空间数据非常有用。该包对于创建、更新和删除geoserver工作空间、存储、图层和样式文件非常有用。要安装此软件包,应首先安装以下软件包:
依赖性
如果已经安装了所有依赖项,geoserver-rest
包可以与pip一起安装。在
pip install geoserver-rest
Windows安装
在windows中,可以使用pipwin
安装gdal和pycurl依赖项
现在可以使用pip install command
安装库
pip install geoserver-rest
Ubuntu安装
在ubuntu中,可以使用以下方法安装gdal和pycurl依赖项
sudo add-apt-repository ppa:ubuntugis/ppa sudo apt update -y; sudo apt upgrade -y; sudo apt install gdal-bin libgdal-dev python3-pycurl pip3 install pygdal=="`gdal-config --version`.*"
现在可以使用pip install命令安装geoserver-rest
库
pip3 install geoserver-rest
如何使用
此库用于创建工作区、coveragestore、featurestore、styles。下面显示了一些示例。在
初始化库
此步骤用于初始化库。它接受geoserver url、用户名、密码等参数。在
fromgeo.GeoserverimportGeoservergeo=Geoserver('http://127.0.0.1:8080/geoserver',username='admin',password='geoserver')
创建工作区
geo.create_workspace(workspace='demo')
创建覆盖率存储
它有助于将光栅数据发布到geoserver。在这里,如果不传递lyr_name
参数,它将以光栅文件名作为图层名。在
geo.create_coveragestore(lyr_name='layer1',path=r'path\to\raster\file.tif',workspace='demo')
**注意:**如果光栅加载不正确,请确保为光栅文件指定坐标系。在
如果layername已经存在于geoserver中,则可以传递另一个参数overwrite=True
geo.create_coveragestore(lyr_name='layer1',path=r'path\to\raster\file.tif',workspace='demo',overwrite=True)
创建功能存储和发布层
它用于将PostGIS与geoserver连接,并将其作为一个层发布。它只对vector data有用。postgres连接参数应作为参数传递。对于发布PostGIS表,pg\u table参数表示table name in postgres
geo.create_featurestore(store_name='geo_data',workspace='demo',db='postgres',host='localhost',pg_user='postgres',pg_password='admin')geo.publish_featurestore(workspace='demo',store_name='geo_data',pg_table='geodata_table_name')
上传并发布样式
它用于上载SLD文件和发布样式。如果样式名已经存在,可以传递参数overwrite=True
来覆盖它。样式的名称将是上载的文件名。在
geo.upload_style(path=r'path\to\sld\file.sld',workspace='demo')geo.publish_style(layer_name='geoserver_layer_name',style_name='sld_file_name',workspace='demo')
基于光栅创建覆盖样式(动态)并应用样式
它用于为光栅数据创建样式文件。您可以从matplotlib colormaps获得color_ramp
名称。默认情况下color_ramp='RdYlGn'
(红色到绿色渐变)。在
#Style name will be the same as the raster_file_namegeo.create_coveragestyle(raster_path=r'path\to\raster\file.tiff',style_name='style_1',workspace='demo',color_ramp='RdYiGn')geo.publish_style(layer_name='geoserver_layer_name',style_name='raster_file_name',workspace='demo')
注意:如果您有自己的自定义颜色和自定义标签,则可以按如下方式传递values:color
对来生成带有动态图例的地图
c_ramp={'label 1 value':'#ffff55','label 2 value':'#505050','label 3 value':'#404040','label 4 value':'#333333'}geo.create_coveragestyle(raster_path=r'path\to\raster\file.tiff',style_name='style_2',workspace='demo',color_ramp=c_ramp,cmap_type='values')# you can also pass this list of color if you have your custom colors for the color_ramp'''geo.create_coveragestyle(raster_path=r'path\to\raster\file.tiff', style_name='style_3', workspace='demo', color_ramp=[#ffffff, #453422, #f0f0f0, #aaaaaa], cmap_type='values')'''geo.publish_style(layer_name='geoserver_layer_name',style_name='raster_file_name',workspace='demo')
要生成分类光栅的样式,可以将另一个名为cmap_type='values'
的参数传递为
geo.create_coveragestyle(raster_path=r'path\to\raster\file.tiff',style_name='style_1',workspace='demo',color_ramp='RdYiGn',cmap_type='values')
Option | Type | Default | Description |
---|---|---|---|
style_name | string | file_name | This is optional field. If you don't pass the style_name parameter, then it will take the raster file name as the default name of style in geoserver |
raster_path | path | None | path to the raster file |
workspace | string | None | The name of the workspace |
color_ramp | string, list, dict | RdYlGn | The color ramp name. The name of the color ramp can be found here in matplotlib colormaps |
cmap_type | string | ramp | By default the continuous style will be generated, If you want to generate the style for classified raster then pass the parameter ^{ |
overwrite | boolean | False | For overwriting the previous style file in geoserver |
创建特色风格
用于动态创建点、线和多边形的样式。目前,它支持三种不同类型的特征样式
- 轮廓特征样式:用于创建只有边界颜色而没有填充样式的样式
- 分类功能样式:用于创建分类数据集
- 分类特征样式:对输入数据进行分类并设置样式:(目前只支持多边形几何体)
geo.create_outline_featurestyle(style_name='new_style'color="#3579b1"geom_type='polygon',workspace='demo')geo.create_catagorized_featurestyle(style_name='name_of_style',column_name='name_of_column',column_distinct_values=[1,2,3,4,5,6,7],workspace='demo')geo.create_classified_featurestyle(style_name='name_of_style'column_name='name_of_column',column_distinct_values=[1,2,3,4,5,6,7],workspace='demo')
注意:
- geom类型必须是
point
、line
或{}。在 color_ramp
名称可以从matplotlib colormaps获得。在
创建分类/分类功能样式的选项如下,
^{tb2}$一些删除请求示例
# delete workspacegeo.delete_workspace(workspace='demo')# delete layergeo.delete_layer(layer_name='agri_final_proj',workspace='demo')# delete feature store, i.e. remove postgresql connectiongeo.delete_featurestore(featurestore_name='ftry',workspace='demo')# delete coveragestore, i.e. delete raster storegeo.delete_coveragestore(coveragestore_name='agri_final_proj',workspace='demo')# delete style filegeo.delete_style(style_name='kamal2',workspace='demo')
自动化过程
下面的代码将首先将C:\Users\gic\Desktop\etlIa\
文件夹中的所有.rst
数据格式转换为tiff
格式,然后将所有tiff
文件上载到GeoServer。在
fromgeo.GeoserverimportGeoserverfromosgeoimportgdalimportglobimportosgeo=Geoserver('http://localhost:8080/geoserver',username='admin',password='geoserver')rst_files=glob.glob(r'C:\Users\gic\Desktop\etlIa\*.rst')# geo.create_workspace('geonode')forrstinrst_files:file_name=os.path.basename(file_name)src=gdal.Open(rst)tiff=r'C:\Users\tek\Desktop\try\{}'.format(file_name)gdal.Translate(tiff,src)geo.create_coveragestore(lyr_name=file_name,path=tiff,workspace='geonode')#, overwrite=True
贡献者离子
geoserverrest是用python编写的开源库,需要贡献者来保持这个库的向前发展。任何形式的贡献都是受欢迎的。在
致谢
由Tek Bahadur Kshetri为泰国亚洲理工学院地理信息中心活动创建和管理。在
- 项目
标签: