def __init__(self, url: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, pg_user: Optional[str] = None, pg_password: Optional[str] = None, pg_host: Optional[str] = None, pg_db: Optional[str] = None, **kwargs): super().__init__() try: from geo.Geoserver import Geoserver except ImportError: raise api.ApiError( 500, "Error: Cannot import Geoserver. Please install first.") self._url = url or os.getenv('XCUBE_HUB_GEOSERVER_URL') self._username = username or os.getenv('XCUBE_HUB_GEOSERVER_USERNAME') self._password = password or os.getenv('XCUBE_HUB_GEOSERVER_PASSWORD') self._pg_host = pg_host or os.getenv("XCUBE_HUB_POSTGIS_HOST") self._pg_user = pg_user or os.getenv("XCUBE_HUB_POSTGIS_USER") self._pg_password = pg_password or os.getenv( "XCUBE_HUB_POSTGIS_PASSWORD") self._pg_db = pg_db or os.getenv("XCUBE_HUB_POSTGIS_DB") self._geo = Geoserver(self._url, username=self._username, password=self._password) for prop, value in vars(self).items(): _raise_for_none(prop, value)
def checkWorkspace(geoserverCredentials, workspaceName): ''' Verificação de ausência/presença de workspace dentro do Geoserver. Caso o workspace especificado não exista, a função cria-o. ''' # Initialize the library geo = Geoserver('http://localhost:8080/geoserver', username=geoserverCredentials.get('username'),\ password=geoserverCredentials.get('password')) # Checking for workspace if geo.get_workspace(workspace=workspaceName) == None: getMessageString( '{} workspace not found. Let\'s create it.'.format(workspaceName)) geo.create_workspace(workspace=workspaceName) else: getMessageString('Found {} workspace.'.format(workspaceName))
def createFeatureStore(geoserverCredentials, postgresCredentials, workspaceName, storeName): ''' Verificação de ausência/presença de featurestore dentro do Geoserver. Caso a featurestore especificado não exista, a função cria-a. ''' geo = Geoserver('http://localhost:8080/geoserver', username=geoserverCredentials.get('username'),\ password=geoserverCredentials.get('password')) if geo.get_featurestore(workspace=workspaceName, store_name=storeName)\ == 'Error: Expecting value: line 1 column 1 (char 0)': getMessageString( '{} featurestore not found. Let\'s create it.'.format(storeName)) geo.create_featurestore( workspace=workspaceName, store_name=storeName, db=postgresCredentials.get('dbname'),\ host=postgresCredentials.get('host'), pg_user=postgresCredentials.get('user'), pg_password=postgresCredentials.get('password')) else: getMessageString('Found {} featurestore.'.format(storeName))
class Geoserver: def __init__(self, url, user, password): GS_URL = url GS_USER = user GS_PASS = password self.geo = Gs(GS_URL, username=GS_USER, password=GS_PASS) def create_ws(self, name): try: self.geo.create_workspace(workspace=name) except Exception as e: if e.args[0] != 'The workspace already exist': raise def load_layer(self, layer_name, file_path, workspace_name): self.geo.create_coveragestore(layer_name=layer_name, path=file_path, workspace=workspace_name)
def publishFeatureStore(geoserverCredentials, workspaceName, storeName, pgTableName): ''' Função para carregamento da view em base de dados para o featurestore. Necessita que exista a referida view dentro da base de dados Postgres, e do workspace e featurestore definida dentro do Geoserver. ''' geo = Geoserver('http://localhost:8080/geoserver', username=geoserverCredentials.get('username'),\ password=geoserverCredentials.get('password')) if geo.get_layer(workspace=workspaceName, layer_name=pgTableName)\ == "get_layer error: Expecting value: line 1 column 1 (char 0)".format(pgTableName, storeName): getMessageString('Forecast WMS successfully uploaded.') geo.publish_featurestore(workspace=workspaceName, store_name=storeName, pg_table=pgTableName) else: getMessageString( "There was an old forecast WMS stored. Let's overwrite it.") geo.delete_layer(layer_name=pgTableName, workspace=workspaceName) geo.publish_featurestore(workspace=workspaceName, store_name=storeName, pg_table=pgTableName)
def connect_geoserver(geoserver_url, user, password): return Geoserver(geoserver_url, user, password)
from django.db import models import datetime from django.db.models.signals import post_save, post_delete from django.dispatch import receiver import geopandas as gpd import os import glob import zipfile from sqlalchemy import * from geoalchemy2 import Geometry, WKTElement from geo.Geoserver import Geoserver from geo.Postgres import Db # initializing the library geo = Geoserver('http://127.0.0.1:8080/geoserver', username='******', password='******') # The shapefile model class Tiff(models.Model): name = models.CharField(max_length=50) description = models.CharField(max_length=1000, blank=True) file = models.FileField(upload_to='%Y/%m/%d') uploaded_date = models.DateField(default=datetime.date.today, blank=True) def __str__(self): return self.name @receiver(post_save, sender=Tiff)
def __init__(self, url, user, password): GS_URL = url GS_USER = user GS_PASS = password self.geo = Gs(GS_URL, username=GS_USER, password=GS_PASS)
class _GeoServer(GeoServiceBase): f""" Redis key-value pair database implementation of KeyValueStore Defines methods for getting, deleting and putting key value pairs :param host, port, db (see also https://github.com/andymccurdy/redis-py) Example: ``` db = KeyValueDatabase.instance(provider='redis', host='localhost', port=6379, db=0) ``` """ def __init__(self, url: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, pg_user: Optional[str] = None, pg_password: Optional[str] = None, pg_host: Optional[str] = None, pg_db: Optional[str] = None, **kwargs): super().__init__() try: from geo.Geoserver import Geoserver except ImportError: raise api.ApiError( 500, "Error: Cannot import Geoserver. Please install first.") self._url = url or os.getenv('XCUBE_HUB_GEOSERVER_URL') self._username = username or os.getenv('XCUBE_HUB_GEOSERVER_USERNAME') self._password = password or os.getenv('XCUBE_HUB_GEOSERVER_PASSWORD') self._pg_host = pg_host or os.getenv("XCUBE_HUB_POSTGIS_HOST") self._pg_user = pg_user or os.getenv("XCUBE_HUB_POSTGIS_USER") self._pg_password = pg_password or os.getenv( "XCUBE_HUB_POSTGIS_PASSWORD") self._pg_db = pg_db or os.getenv("XCUBE_HUB_POSTGIS_DB") self._geo = Geoserver(self._url, username=self._username, password=self._password) for prop, value in vars(self).items(): _raise_for_none(prop, value) def get_layers(self, database_id: str, fmt: str = 'geopandas') -> Sequence[Dict]: """ Get a key value :param fmt: return format [geopandas, json] :param database_id: :return: Collection """ try: res = self._geo.get_layers(workspace=database_id) if res['layers'] == '': raise api.ApiError( 404, f'No collections found in database {database_id}') layers = res['layers']['layer'] if fmt == 'geopandas': layers_res = { "collection_id": [], "database": [], "default_style": [], "geojson_url": [], "href": [], "name": [], "preview_url": [], "wfs_url": [] } for layer in layers: name = layer['name'] collection_id = name.replace(database_id + '_', '') collection = self.get_layer(collection_id=collection_id, database_id=database_id) for k, v in collection.to_dict().items(): layers_res[k].append(v) else: layers_res = [] for layer in layers: name = layer['name'] collection_id = name.replace(database_id + '_', '') collection = self.get_layer(collection_id=collection_id, database_id=database_id) layers_res.append(collection.to_dict()) if len(layers_res) == 0: raise api.ApiError( 404, f'No collections found in database {database_id}') return layers_res except Exception as e: raise api.ApiError(400, str(e)) def get_layer(self, collection_id: str, database_id: str) -> Optional[Collection]: """ Get a key value :param collection_id: :param database_id: :return: Optional[Collection] """ try: layer_name = database_id + '_' + collection_id try: layer = self._geo.get_layer(layer_name=layer_name, workspace=database_id) if 'get_layer error' in layer: raise api.ApiError(400, layer) except Exception as e: raise api.ApiError(400, str(e)) url = layer['layer']['resource']['href'] r = requests.get(url, auth=(self._username, self._password)) layer_wms = r.json() bbox = layer_wms['featureType']['nativeBoundingBox'] srs = layer_wms['featureType']['srs'] preview_url = f"{self._url}/{database_id}/wms?service=WMS&version=1.1.0&request=GetMap&" \ f"layers={database_id}:{layer_name}&" \ f"bbox={bbox['minx']},{bbox['miny']},{bbox['maxx']},{bbox['maxy']}&" \ f"width=690&height=768&srs={srs}&styles=&format=application/openlayers" geojson_url = f"{self._url}/{database_id}/ows?service=WFS&version=1.0.0&request=GetFeature&" \ f"typeName={database_id}:{layer_name}&maxFeatures=10&outputFormat=application/json" wfs_url = f"{self._url}/{database_id}/ows?service=WFS&" \ f"version=1.0.0&" \ f"request=GetFeature&" \ f"typeName={database_id}%3A{layer_name}&maxFeatures=10&" \ f"outputFormat=application%2Fvnd.google-earth.kml%2Bxml" collection = Collection(preview_url=preview_url, collection_id=collection_id, database=database_id, name=collection_id.replace( database_id, ''), geojson_url=geojson_url, wfs_url=wfs_url) return collection except Exception as e: raise api.ApiError(400, str(e)) def publish(self, collection_id: str, database_id: str) -> Optional[Collection]: """ Set a key value :param collection_id: :param database_id: :return: Optional[Collection] """ try: workspace = self._geo.get_workspace(workspace=database_id) if workspace is None: self._geo.create_workspace(workspace=database_id) self._geo.create_featurestore(store_name=database_id, workspace=database_id, host=self._pg_host, port=5432, db=self._pg_db, pg_user=self._pg_user, pg_password=self._pg_password) pg_table = database_id + '_' + collection_id self._geo.publish_featurestore(workspace=database_id, store_name=database_id, pg_table=pg_table) return self.get_layer(collection_id=collection_id, database_id=database_id) except Exception as e: raise api.ApiError(400, str(e)) def unpublish(self, collection_id: str, database_id: str) -> Optional[Collection]: """ Delete a key :param collection_id: :param database_id: :return: """ try: collection = self.get_layer(collection_id=collection_id, database_id=database_id) layer_name = database_id + '_' + collection_id self._geo.delete_layer(layer_name=layer_name, workspace=database_id) return collection except Exception as e: raise api.ApiError(400, str(e))
def __init__(self): global geo, catalog geo = Geoserver(Config.GEOSERVER_URL, Config.GEOSERVER_USER, Config.GEOSERVER_PASSWD) catalog = Catalog(Config.GEOSERVER_URL + '/rest', Config.GEOSERVER_USER, Config.GEOSERVER_PASSWD)
from geo.Geoserver import Geoserver from geo.Postgres import Db from geo.Style import catagorize_xml, classified_xml """ Connection """ geo = Geoserver("http://localhost:8080/geoserver", username="******", password="******") # pg = Db(dbname='postgres', user='******', password='******', host='localhost') # geo.create_workspace('demo2') """ New get requests """ # geo.get_version() # geo.get_manifest() # geo.get_status() # geo.get_system_status() # a = geo.create_shp_datastore( # r"C:\Program Files (x86)\GeoServer 2.15.1\data_dir\data\demo\C_Jamoat\C_Jamoat.zip", store_name='111') # print(a) # geo.get_layer('jamoat-db', workspace='demo') # geo.get_datastore('111', 'demo') # geo.get_style('hazard_exp', workspace='geoinformatics_center', ) # a = geo.get_styles() # a = geo.create_datastore( # 'datastore4', r"http://localhost:8080/geoserver/wfs?request=GetCapabilities", workspace='demo', overwrite=True) a = geo.create_shp_datastore( r"C:\Users\tek\Desktop\try\geoserver-rest\data\A_Admin_boundaries\A_Country\A_Country.zip", "aaa", "default",
from geo.Geoserver import Geoserver from geo.Postgres import Db from geo.Style import catagorize_xml, classified_xml ''' Connection ''' geo = Geoserver('http://localhost:8080/geoserver', username='******', password='******') # pg = Db(dbname='postgres', user='******', password='******', host='localhost') # geo.create_workspace('demo') ''' Coverage (raster) ''' # geo.create_coveragestore(r'C:\Users\tek\Desktop\geoserver-rest\data\C_EAR\a_Agriculture\agri_final_proj.tif', workspace='demo', overwrite=False) # geo.upload_style(r'C:\Users\tek\Desktop\geoserver-rest\data\style\dem.sld', workspace='demo', overwrite=True) # geo.publish_style('agri_final_proj', 'dem', 'demo') color_ramp = {'value1': '#ffff55', 'value2': '#505050', 'value3': '#404040'} geo.create_coveragestyle( style_name='demo', raster_path=r'C:\Users\GIC\Downloads\NDVI_20201104.tif', workspace='demo', color_ramp='RdYlGn') ''' Features (vector) ''' # geo.create_featurestore(store_name='fdemo', workspace='demo') # geo.publish_featurestore('fdemo','zones','demo') # a = geo.get_featuretypes(workspace='demo', store_name='fdemo') # a= geo.get_feature_attribute(feature_type_name='jamoat-db', workspace='demo', store_name='fdemo') # a= geo.get_featurestore('fdemo', 'demo')
import os from geo.Geoserver import Geoserver GEO_URL = os.getenv("GEO_URL", "http://localhost:8080/geoserver") geo = Geoserver(GEO_URL, username="******", password="******")
# -*- coding: utf-8 -*- """ Created on Fri Oct 15 11:59:01 2021 @author: younggis """ from geo.Geoserver import Geoserver geo = Geoserver('http://127.0.0.1:8090/geoserver', username='******', password='******') #geo.create_workspace(workspace='demo') #region 数据库 #geo.create_featurestore(store_name='geo_data', workspace='demo', db='postgis', host='localhost', pg_user='******', pg_password='******') #发布表为图层 #geo.create_coveragestore(layer_name='county', path=r'F:\temp\t_gis_county.shp', workspace='demo') #geo.publish_featurestore(workspace='demo', store_name='geo_data', pg_table='guanduan_new') #发布sql视图为图层 #sql = 'SELECT pipename, code, geom FROM famen_new' #geo.publish_featurestore_sqlview(store_name='geo_data', name='famen_new',key_column='code', sql=sql,workspace='demo') #endregion #region 矢量文件 #上传并解压压缩包 #geo.create_shp_datastore(path=r'F:\temp\t_gis_county.zip', store_name='geo_file', workspace='demo') #发布矢量文件(不上传,删除即服务报错) #geo.create_datastore(name="ds", path=r'F:\temp\t_gis_county.shp', workspace='demo') #geo.publish_featurestore(workspace='demo', store_name='ds', pg_table='t_gis_county')
from geo.Geoserver import Geoserver from geo.Postgres import Db from geo.Style import catagorize_xml, classified_xml ''' Connection ''' geo = Geoserver('http://localhost:8080/geoserver', username='******', password='******') # pg = Db(dbname='postgres', user='******', password='******', host='localhost') # geo.create_workspace('demo2') ''' New get requests ''' # geo.get_version() # geo.get_manifest() # geo.get_status() # geo.get_system_status() # a = geo.create_shp_datastore( # r"C:\Program Files (x86)\GeoServer 2.15.1\data_dir\data\demo\C_Jamoat\C_Jamoat.zip", store_name='111') # print(a) # geo.get_layer('jamoat-db', workspace='demo') # geo.get_datastore('111', 'demo') # geo.get_style('hazard_exp', workspace='geoinformatics_center', ) # a = geo.get_styles() a = geo.create_datastore( 'datastore4', r"http://localhost:8080/geoserver/wfs?request=GetCapabilities", workspace='demo', overwrite=True) # a = geo.create_shp_datastore('aaa', 'default')
from geo.Geoserver import Geoserver geo = Geoserver('http://127.0.0.1:8080/geoserver', username='******', password='******') # geo.create_workspace('demo') # geo.create_coveragestore( # lyr_name='raster1', path=r'C:\Users\gic\Desktop\geoserver-rest\data\raster\raster1.tif', workspace='demo') # geo.create_featurestore('postgis', workspace='demo', db='postgres', # pg_user='******', pg_password='******', host='127.0.0.1') # geo.publish_featurestore(store_name='postgis', # pg_table='jamoat-db', workspace='demo') # geo.upload_style( # path=r'C:\Users\gic\Desktop\geoserver-rest\data\style\raster1.sld', workspace='demo') # geo.publish_style(layer_name='raster1', # style_name='raster-new', workspace='demo') # geo.create_coveragestyle(raster_path=r'C:\Users\gic\Desktop\geoserver-rest\data\raster\raster1.tif', # style_name='raster-new', workspace='demo', color_ramp='hsv') # geo.create_outline_featurestyle('polygon-style', workspace='demo') geo.publish_style(layer_name='jamoat-db', style_name='polygon-style', workspace='demo')