# -*- coding: utf-8 -*-
from Geoserver import Geoserver
import config

geoserver_config = config.CONFIG['geoserver']

# 连接GeoServer
geo = Geoserver(geoserver_config['url'], username=geoserver_config['username'], password=geoserver_config['password'])
geo.create_datastore(name="wuchuan0705", path=r'D:\temp\wuchuan0705.shp', workspace='spot')
geo.publish_featurestore(workspace='spot', store_name='wuchuan0705', pg_table='wuchuan0705')
geo.publish_style(layer_name='wuchuan0705', style_name='spot', workspace='spot')
# geo.create_coveragestyle(raster_path=r'D:\gisdata\tif\xyq.tif', style_name='style_raster', workspace='demo', color_ramp='RdBu_r')
# geo.publish_style(layer_name='demo_tiff', style_name='style_raster', workspace='demo')

# caching
# geo.caching_layer(layer_name='webgistest:capital', zoom_start=1, zoom_stop=15, auto_seed=False)
# geo.seed_caching_layer(layer_name='webgistest:capital', zoom_start=1, zoom_stop=15, seedtype='reseed')
# geo.seed_caching_layer(layer_name='webgistest:capital', zoom_start=1, zoom_stop=15, seedtype='truncate')
# geo.seed_caching_layer(layer_name='webgistest:capital', zoom_start=1, zoom_stop=15, seedtype='seed')
# 创建工作空间
# geo.create_workspace(workspace='demo')
# 上传影像数据到工作空间
# geo.create_coveragestore(layer_name='demo_tiff', path=r'D:/gisdata/HEBEI/HebeiDEM.tif', workspace='demo')
# 创建PostGIS数据库连接到工作空间
# geo.create_featurestore(store_name='pg_test', workspace='demo', db='webgistest', host='localhost', pg_user='postgres',pg_password='postgres', schema='public')
# 发布PostGIS数据表
# geo.publish_featurestore(workspace='demo', store_name='pg_test', pg_table='安国市-保定_shape'.encode('utf-8').decode('utf-8'))
# geo.publish_featurestore(workspace='demo', store_name='pg_test', pg_table='capital')
# 基于数据表做sql查询并发布
# sql = 'SELECT * FROM baoding_shape'
# geo.publish_featurestore_sqlview(store_name='pg_test', name='view_test', sql=sql, key_column='id', workspace='demo')
# 发布shp数据
# geo.create_shp_datastore(path=r'D:/gisdata/HEBEI/WYDC.zip', store_name='store', workspace='demo')
# geo.create_datastore(name="WYDC", path=r'D:/gisdata/HEBEI/WYDC.shp', workspace='demo')
# geo.publish_featurestore(workspace='demo', store_name='pg_test', pg_table='baoding_shape')
# WFS注册
# geo.create_datastore(name="wfs", path='http://localhost:28085/geoserver/wfs?request=GetCapabilities', workspace='demo')
# geo.publish_featurestore(workspace='demo', store_name='wfs', pg_table='wfs_layer_name')
# layergroup
# create a new layergroup from 2 existing layers
# geo.create_layergroup(
#     name="my_fancy_layergroup",
#     mode="single",
#     title="My Fancy Layergroup Title",
#     abstract_text="This is a very fancy Layergroup",
#     layers=["fancy_layer_1", "fancy_layer_2"],
#     workspace="my_space",  # None if you want to create a layergroup outside the workspace
#     keywords=["list", "of", "keywords"]
# )
# add another layer
# geo.add_layer_to_layergroup(
#     layergroup_name="my_fancy_layergroup",
#     layergroup_workspace="my_space",
#     layer_name="superfancy_layer",
#     layer_workspace="my_space"
# )
# remove a layer
# geo.remove_layer_from_layergroup(
#     layergroup_name="my_fancy_layergroup",
#     layergroup_workspace="my_space",
#     layer_name="superfancy_layer",
#     layer_workspace="my_space"
# )
# Style Vector
# geo.upload_style(path=r'E:/projects/QGIS/styles/point.sld', workspace='demo', name='point')
# geo.publish_style(layer_name='capital', style_name='point', workspace='demo')
# Style Tiff
# geo.create_coveragestyle(raster_path=r'D:/gisdata/HEBEI/HebeiDEM.tif', style_name='style_raster', workspace='demo', color_ramp='RdBu_r')
# geo.publish_style(layer_name='demo_tiff', style_name='style_raster', workspace='demo')
# Style Setting
# c_ramp = {
#     'label 1 value': '#ffff55',
#     'label 2 value': '#505050',
#     'label 3 value': '#404040',
#     'label 4 value': '#333333'
# }
# geo.create_coveragestyle(raster_path=r'D:/gisdata/HEBEI/HebeiDEM.tif',
#                             style_name='style_raster2',
#                             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'D:/gisdata/HEBEI/HebeiDEM.tif',
#                             style_name='style_raster3',
#                             workspace='demo',
#                             color_ramp=['#ffffff', '#453422',  '#f0f0f0', '#aaaaaa'],
#                             cmap_type='values')

# geo.publish_style(layer_name='demo_tiff', style_name='style_raster', workspace='demo')


# delete workspace
# geo.delete_workspace(workspace='demo')

# delete layer
# geo.delete_layer(layer_name='agri_final_proj', workspace='demo')

# delete feature store, i.e. remove postgresql connection
# geo.delete_featurestore(featurestore_name='ftry', workspace='demo')

# delete coveragestore, i.e. delete raster store
# geo.delete_coveragestore(coveragestore_name='agri_final_proj', workspace='demo')

# delete style file
# geo.delete_style(style_name='kamal2', workspace='demo')

# get geoserver version
# version = geo.get_version()
# print(version)
#
# # get ststem info
# status = geo.get_status()
# system_status = geo.get_system_status()
#
# # get workspace
# workspace = geo.get_workspaces()
#
# # get default workspace
# dw = geo.get_default_workspace()
#
# # get all the workspaces
# workspaces = geo.get_workspaces()
#
# # get datastore
# datastore = geo.get_datastores(workspace='webgistest')
#
# # get all the datastores
# datastores = geo.get_datastores()
#
# # get coveragestore
# # cs = geo.get_coveragestore(coveragestore_name='cs')
#
# # get all the coveragestores
# css = geo.get_coveragestores()
#
# # get layer
# layer = geo.get_layer(layer_name='capital')
#
# # get all the layers
# layers = geo.get_layers()
#
# # get layergroup
# # layergroup = geo.get_layergroup('layergroup_name')
#
# # get all the layers
# # layergroups = geo.get_layergroups()
#
# # get style
# # style = geo.get_style(style_name='style_name')
#
# # get all the styles
# styles = geo.get_styles()
#
# # get featuretypes
# featuretypes = geo.get_featuretypes(workspace='webgistest', store_name='webgistest')
#
# # get feature attribute
# # fa = geo.get_feature_attribute(feature_type_name='ftn', workspace='ws', store_name='sn')
#
# # get feature store
# # fs = geo.get_featurestore(store_name='sn', workspace='ws')
#
# # Reloads the GeoServer catalog and configuration from disk. This operation is used in cases where an external tool has modified the on-disk configuration. This operation will also force GeoServer to drop any internal caches and reconnect to all data stores.
# geo.reload()
#
# # Resets all store, raster, and schema caches. This operation is used to force GeoServer to drop all caches and store connections and reconnect to each of them the next time they are needed by a request. This is useful in case the stores themselves cache some information about the data structures they manage that may have changed in the meantime.
# geo.reset()
#
# # set default workspace
# geo.set_default_workspace(workspace='webgistest')
