def testFeatureTypeCreate(self):
        shapefile_plus_sidecars = shapefile_and_friends("test/data/states")
        expected = {
            'shp': 'test/data/states.shp',
            'shx': 'test/data/states.shx',
            'dbf': 'test/data/states.dbf',
            'prj': 'test/data/states.prj'
        }

        self.assertEqual(len(expected), len(shapefile_plus_sidecars))
        for k, v in expected.items():
            self.assertEqual(v, shapefile_plus_sidecars[k])

        sf = self.cat.get_workspaces("sf")[0]
        self.cat.create_featurestore("states_test", shapefile_plus_sidecars,
                                     sf.name)
        self.assert_(
            len(self.cat.get_resources("states_test", workspaces=sf.name)) > 0)

        self.assertRaises(
            ConflictingDataError, lambda: self.cat.create_featurestore(
                "states_test", shapefile_plus_sidecars, sf))

        lyr = self.cat.get_layer("states_test")
        self.cat.delete(lyr)
        self.assert_(self.cat.get_layer("states_test") is None)
Beispiel #2
0
def publishShpLayerFromFeatureStore(workspaceName,
                                    workspaceUri,
                                    featureStoreName,
                                    layerName,
                                    shpPath,
                                    native_crs='EPSG:4326',
                                    srs='EPSG:4326'):
    '''
    2018-6-12:
    该方法在测试后,出现无法发布featuretype的问题,暂时不知道原因,所以用下面的方法替换
    2018-6-19:
    经过测试,发现该方法中的 cat.publish_featuretype 只能用于发布postgis数据库中的数据,采用如下所示示例,具体还有待测试;
    要发布shp数据的wms服务,应该用publishShpLayerFromDataStore方法
    '''
    workspace = cat.get_workspace(workspaceName)
    if workspace is None:
        workspace = cat.create_workspace(workspaceName, workspaceUri)
    shapefile_plus_sidecars = geoUtil.shapefile_and_friends(shpPath)
    featureStore = cat.get_store(featureStoreName, workspace)
    if featureStore is None:
        featureStore = cat.create_datastore('newDatastoreName',
                                            'newWorkspaceName')
        featureStore.connection_parameters.update(host='localhost',
                                                  port='5432',
                                                  database='postgis',
                                                  user='******',
                                                  passwd='password',
                                                  dbtype='postgis',
                                                  schema='postgis')
        cat.save(featureStore)
    publishedLayer = cat.publish_featuretype(layerName, featureStore,
                                             native_crs, srs)
    print('success')
    return publishedLayer
Beispiel #3
0
    def testFeatureTypeCreate(self):
        shapefile_plus_sidecars = shapefile_and_friends("test/data/states")
        expected = {
            'shp': 'test/data/states.shp',
            'shx': 'test/data/states.shx',
            'dbf': 'test/data/states.dbf',
            'prj': 'test/data/states.prj'
        }

        self.assertEqual(len(expected), len(shapefile_plus_sidecars))
        for k, v in expected.items():
            self.assertEqual(v, shapefile_plus_sidecars[k])

        sf = self.cat.get_workspaces("sf")[0]
        self.cat.create_featurestore("states_test", shapefile_plus_sidecars, sf.name)
        self.assert_(len(self.cat.get_resources("states_test", workspaces=sf.name)) > 0)

        self.assertRaises(
            ConflictingDataError,
            lambda: self.cat.create_featurestore("states_test", shapefile_plus_sidecars, sf)
        )

        lyr = self.cat.get_layer("states_test")
        self.cat.delete(lyr)
        self.assert_(self.cat.get_layer("states_test") is None)
Beispiel #4
0
def publishShpLayerFromDataStore(workspaceName,
                                 workspaceUri,
                                 datastoreName,
                                 layerName,
                                 shpPath,
                                 native_crs='EPSG:4326',
                                 srs='EPSG:4326'):
    '''
    2018-6-19:
    经测试,调用 cat.add_data_to_store 方法后,不需要再调用cat.publish_featuretype方法,即可发布shp数据wms服务
    :param shpPath: shppath中的shape文件名不能包含扩展名.shp
    '''
    workspace = cat.get_workspace(workspaceName)
    if workspace is None:
        workspace = createWorkspace(workspaceName, workspaceUri)
    dataStore = cat.get_store(datastoreName, workspaceName)
    if dataStore is None:
        dataStore = createDataStore(datastoreName, workspaceName)
    shapefile_plus_sidecars = geoUtil.shapefile_and_friends(shpPath)
    # 将shp数据添加到datastore中,添加之后,即发布了wms服务
    cat.add_data_to_store(dataStore,
                          layerName,
                          shapefile_plus_sidecars,
                          workspaceName,
                          overwrite=True)
Beispiel #5
0
def populateCatalog(cat):
    cleanCatalog(cat)
    cat.create_workspace(WORKSPACE, "http://test.com")
    ws = cat.get_workspace(WORKSPACE)
    path = os.path.join(os.path.dirname(__file__), "data", PT2)
    data = shapefile_and_friends(path)
    cat.create_featurestore(PT2, data, ws)
    path = os.path.join(os.path.dirname(__file__), "data", PT3)
    data = shapefile_and_friends(path)
    cat.create_featurestore(PT3, data, ws)
    sldfile = os.path.join(os.path.dirname(__file__), "resources", "vector.sld")
    with open(sldfile, 'r') as f:
        sld = f.read()
    cat.create_style(STYLE, sld, True)
    group = cat.create_layergroup(GROUP, [PT2])
    cat.save(group)
    cat.create_workspace(WORKSPACEB, "http://testb.com")
    cat.set_default_workspace(WORKSPACE)
Beispiel #6
0
def populateCatalog(cat):
    cleanCatalog(cat)
    cat.create_workspace(WORKSPACE, "http://test.com")
    ws = cat.get_workspace(WORKSPACE)
    path = os.path.join(os.path.dirname(__file__), "data", PT2)
    data = shapefile_and_friends(path)
    cat.create_featurestore(PT2, data, ws)
    path = os.path.join(os.path.dirname(__file__), "data", PT3)
    data = shapefile_and_friends(path)
    cat.create_featurestore(PT3, data, ws)
    sldfile = os.path.join(os.path.dirname(__file__), "resources",
                           "vector.sld")
    with open(sldfile, 'r') as f:
        sld = f.read()
    cat.create_style(STYLE, sld, True)
    group = cat.create_layergroup(GROUP, [PT2])
    cat.save(group)
    cat.create_workspace(WORKSPACEB, "http://testb.com")
    cat.set_default_workspace(WORKSPACE)
Beispiel #7
0
    def geoserver_upload_shape(self, input_file):
        print "upload", input_file

        path, file_ext = os.path.split(input_file)

        cat = Catalog(self.GEOSERVER_HOST, self.GEOSERVER_USER, self.GEOSERVER_PASSWORD)
        shapeFile = shapefile_and_friends(input_file)
        print shapeFile
        cat.create_featurestore(name=file_ext, data=shapeFile, workspace=cat.get_workspace(self.GEOSERVER_WORKSPACE),
                                overwrite=True, charset=None)
Beispiel #8
0
    def createFeatureStore(self, filePathNoExt, gsWorkspace, dataStoreName):
        """ Create "Feature store" in the gsconfig.py stle
        """
        from geoserver.util import shapefile_and_friends
        shapefile_plus_sidecars = shapefile_and_friends(filePathNoExt)

        if not gsWorkspace:
            ws = self.cat.get_default_workspace()
        else:
            ws = self.cat.get_workspace(gsWorkspace)
        self.cat.create_featurestore(dataStoreName, shapefile_plus_sidecars, ws)
Beispiel #9
0
    def createFeatureStore(self, filePathNoExt, gsWorkspace, dataStoreName):
        """ Create "Feature store" in the gsconfig.py stle
        """
        from geoserver.util import shapefile_and_friends
        shapefile_plus_sidecars = shapefile_and_friends(filePathNoExt)

        if not gsWorkspace:
            ws = self.cat.get_default_workspace()
        else:
            ws = self.cat.get_workspace(gsWorkspace)
        self.cat.create_featurestore(dataStoreName, shapefile_plus_sidecars,
                                     ws)
Beispiel #10
0
    def testTimeDimension(self):
        sf = self.cat.get_workspace("sf")
        files = shapefile_and_friends(
            os.path.join(gisdata.GOOD_DATA, "time", "boxes_with_end_date"))
        self.cat.create_featurestore("boxes_with_end_date", files, sf)

        get_resource = lambda: self.cat._cache.clear() or self.cat.get_layer(
            'boxes_with_end_date').resource

        # configure time as LIST
        resource = get_resource()
        timeInfo = DimensionInfo("time",
                                 "true",
                                 "LIST",
                                 None,
                                 "ISO8601",
                                 None,
                                 attribute="date")
        resource.metadata = {'time': timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual("LIST", timeInfo.presentation)
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual("date", timeInfo.attribute)
        self.assertEqual("ISO8601", timeInfo.units)

        # disable time dimension
        timeInfo = resource.metadata['time']
        timeInfo.enabled = False
        # since this is an xml property, it won't get written unless we modify it
        resource.metadata = {'time': timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(False, timeInfo.enabled)

        # configure with interval, end_attribute and enable again
        timeInfo.enabled = True
        timeInfo.presentation = 'DISCRETE_INTERVAL'
        timeInfo.resolution = '3 days'
        timeInfo.end_attribute = 'enddate'
        resource.metadata = {'time': timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual('DISCRETE_INTERVAL', timeInfo.presentation)
        self.assertEqual('3 days', timeInfo.resolution_str())
        self.assertEqual('enddate', timeInfo.end_attribute)
Beispiel #11
0
    def testTimeDimension(self):
        sf = self.cat.get_workspace("sf")
        files = shapefile_and_friends(os.path.join(gisdata.GOOD_DATA, "time", "boxes_with_end_date"))
        self.cat.create_featurestore("boxes_with_end_date", files, sf)

        get_resource = lambda: self.cat._cache.clear() or self.cat.get_layer('boxes_with_end_date').resource

        # configure time as LIST
        resource = get_resource()
        timeInfo = DimensionInfo("time", "true", "LIST", None, "ISO8601", None, attribute="date")
        resource.metadata = {'time':timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual("LIST", timeInfo.presentation)
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual("date", timeInfo.attribute)
        self.assertEqual("ISO8601", timeInfo.units)

        # disable time dimension
        timeInfo = resource.metadata['time']
        timeInfo.enabled = False
        # since this is an xml property, it won't get written unless we modify it
        resource.metadata = {'time' : timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(False, timeInfo.enabled)

        # configure with interval, end_attribute and enable again
        timeInfo.enabled = True
        timeInfo.presentation = 'DISCRETE_INTERVAL'
        timeInfo.resolution = '3 days'
        timeInfo.end_attribute = 'enddate'
        resource.metadata = {'time' : timeInfo}
        self.cat.save(resource)
        # and verify
        resource = get_resource()
        timeInfo = resource.metadata['time']
        self.assertEqual(True, timeInfo.enabled)
        self.assertEqual('DISCRETE_INTERVAL', timeInfo.presentation)
        self.assertEqual('3 days', timeInfo.resolution_str())
        self.assertEqual('enddate', timeInfo.end_attribute)
Beispiel #12
0
    def testFeatureTypeCreate(self):
        shapefile_plus_sidecars = shapefile_and_friends("test/data/states")
        expected = {
            'shp': 'test/data/states.shp',
            'shx': 'test/data/states.shx',
            'dbf': 'test/data/states.dbf',
            'prj': 'test/data/states.prj'
        }

        self.assertEqual(len(expected), len(shapefile_plus_sidecars))
        for k, v in expected.iteritems():
            self.assertEqual(v, shapefile_plus_sidecars[k])

        sf = self.cat.get_workspace("sf")
        self.cat.create_featurestore("states_test", shapefile_plus_sidecars, sf)

        self.assert_(self.cat.get_resource("states_test", workspace=sf) is not None)

        self.assertRaises(
            ConflictingDataError,
            lambda: self.cat.create_featurestore("states_test", shapefile_plus_sidecars, sf)
        )

        self.assertRaises(
            UploadError,
            lambda: self.cat.create_coveragestore("states_raster_test", shapefile_plus_sidecars, sf)
        )

        bogus_shp = {
            'shp': 'test/data/Pk50095.tif',
            'shx': 'test/data/Pk50095.tif',
            'dbf': 'test/data/Pk50095.tfw',
            'prj': 'test/data/Pk50095.prj'
        }

        self.assertRaises(
            UploadError,
            lambda: self.cat.create_featurestore("bogus_shp", bogus_shp, sf)
        )

        lyr = self.cat.get_layer("states_test")
        self.cat.delete(lyr)
        self.assert_(self.cat.get_layer("states_test") is None)
Beispiel #13
0
    def testFeatureTypeCreate(self):
        shapefile_plus_sidecars = shapefile_and_friends("test/data/states")
        expected = {
            'shp': 'test/data/states.shp',
            'shx': 'test/data/states.shx',
            'dbf': 'test/data/states.dbf',
            'prj': 'test/data/states.prj'
        }

        self.assertEqual(len(expected), len(shapefile_plus_sidecars))
        for k, v in expected.iteritems():
            self.assertEqual(v, shapefile_plus_sidecars[k])
 
        sf = self.cat.get_workspace("sf")
        self.cat.create_featurestore("states_test", shapefile_plus_sidecars, sf)

        self.assert_(self.cat.get_resource("states_test", workspace=sf) is not None)

        self.assertRaises(
            ConflictingDataError, 
            lambda: self.cat.create_featurestore("states_test", shapefile_plus_sidecars, sf)
        )

        self.assertRaises(
            UploadError,
            lambda: self.cat.create_coveragestore("states_raster_test", shapefile_plus_sidecars, sf)
        )

        bogus_shp = {
            'shp': 'test/data/Pk50095.tif',
            'shx': 'test/data/Pk50095.tif',
            'dbf':    'test/data/Pk50095.tfw',
            'prj':    'test/data/Pk50095.prj'
        }

        self.assertRaises(
            UploadError,
            lambda: self.cat.create_featurestore("bogus_shp", bogus_shp, sf)
        )

        lyr = self.cat.get_layer("states_test")
        self.cat.delete(lyr)
        self.assert_(self.cat.get_layer("states_test") is None)
 def publishShpLayerFromDataStore(self,
                                  workspaceName,
                                  workspaceUri,
                                  datastoreName,
                                  layerName,
                                  shpPath,
                                  native_crs='EPSG:4326',
                                  srs='EPSG:4326'):
     '''
     :param shpPath: shppath中的shape文件名不能包含扩展名.shp
     '''
     workspace = self.getWorkspace(workspaceName)
     if workspace is None:
         workspace = self.createWorkspace(workspaceName, workspaceUri)
     dataStore = self.getDataStore(datastoreName, workspaceName)
     if dataStore is None:
         dataStore = self.createDataStore(datastoreName, workspaceName)
     shapefile_plus_sidecars = geoUtil.shapefile_and_friends(shpPath)
     self.cat.add_data_to_store(dataStore, layerName,
                                shapefile_plus_sidecars, workspaceName)
Beispiel #15
0
from logging.handlers import RotatingFileHandler
from os import path

# Python 3 backported
from collections import OrderedDict

# 3rd party libraries
from geoserver.catalog import Catalog
from geoserver.resource import Coverage, FeatureType
from geoserver.util import shapefile_and_friends

# ############################################################################
# ########## MAIN #############
# ##################################

# connection
cat = Catalog("http://localhost:8080/geoserver/rest", "admin", "geoserver")

# READ
all_layers = cat.get_layers()

print(len(all_layers))

# WRITE
wks_esipe = cat.get_workspace("ESIPE_IG3")
shapefile_plus_sidecars = shapefile_and_friends(
    r"data_samples\idf_RGP_emploi_exh")
feat = cat.create_featurestore("test_gsoncif_write",
                               workspace=wks_esipe,
                               data=shapefile_plus_sidecars)
Beispiel #16
0
def createDataStore(name, filename, format="shapefile"):
    cat = Catalog("{0}/rest/".format(geoserver_connection), geoserver_username,
                  geoserver_password)
    ws = cat.get_workspace(workspace)
    msg = ""
    if format == "shapefile":
        shapefile = shapefile_and_friends(filename)
        try:
            cat.create_featurestore(name, shapefile, workspace)
        except ConflictingDataError as inst:
            msg = str(inst)
        except:
            raise
        resource = cat.get_resource(name, workspace=ws)
        resource.projection = 'EPSG:4326'
        cat.save(resource)
        resource.projection_policy = 'REPROJECT_TO_DECLARED'
        cat.save(resource)
        resource.refresh()
        bbox = resource.latlon_bbox[:4]
        solr_geom = 'ENVELOPE({0},{1},{2},{3})'.format(bbox[0], bbox[1],
                                                       bbox[3], bbox[2])
        return {
            "solr_geom": solr_geom,
            "msg": msg,
            "resource_type": resource.resource_type
        }
    elif format == "image":
        if cat.get_store(name):
            newcs = cat.get_store(name, workspace=ws)
            msg = "Geoserver datastore already existed. Update existing datastore."
        else:
            newcs = cat.create_coveragestore2(name, ws)
        newcs.type = "GeoTIFF"
        newcs.url = filename
        cat.save(newcs)
        # add coverage
        url = "{0}/rest/workspaces/{1}/coveragestores/{2}/coverages.json"
        url = url.format(geoserver_connection, ws.name, name)
        headers = {"Content-Type": "application/json"}
        coverageName = os.path.splitext(os.path.basename(filename))[0]
        postdata = {
            "coverage": {
                "nativeCoverageName": coverageName,
                "name": coverageName,
                'projectionPolicy': 'REPROJECT_TO_DECLARED',
                'srs': 'EPSG:4326'
            }
        }
        requests.post(url,
                      json.dumps(postdata),
                      headers=headers,
                      auth=(geoserver_username, geoserver_password))
        # Reproject
        resource = cat.get_resource(name, workspace=ws)
        url = "{0}/rest/workspaces/{1}/coveragestores/{2}/coverages/{2}?{3}"
        parameters = "recalculate=nativebbox,latlonbbox"
        url = url.format(geoserver_connection, ws.name, name, parameters)
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        requests.post(url,
                      headers=headers,
                      auth=(geoserver_username, geoserver_password))
        # resource.refresh()
        bbox = resource.latlon_bbox[:4]
        solr_geom = 'ENVELOPE({0},{1},{2},{3})'.format(bbox[0], bbox[1],
                                                       bbox[3], bbox[2])
        return {
            "solr_geom": solr_geom,
            "msg": msg,
            "resource_type": resource.resource_type
        }
    return True
Beispiel #17
0
logging.captureWarnings(True)
logger.setLevel(logging.INFO)  # all errors will be get
log_form = logging.Formatter("%(asctime)s || %(levelname)s "
                             "|| %(module)s || %(message)s")
logfile = RotatingFileHandler("LOG_infos_geoserver.log", "a", 10000000, 2)
logfile.setLevel(logging.INFO)
logfile.setFormatter(log_form)
logger.addHandler(logfile)
logger.info('=================================================')

# ############################################################################
# ########## MAIN ##################
# ##################################

# connection
cat = Catalog("http://localhost:8080/geoserver/rest", "admin", "geoserver")

# READ --------------------------------------------------------------------
all_layers = cat.get_layers()

print("Total layers count: " + len(all_layers))

# WRITE -------------------------------------------------------------------
# publish a shapefiles
wks_esipe = cat.get_workspace("ESIPE_IG3")
input_shapefiles = shapefile_and_friends(
    path.realpath(r"data_samples\idf_RGP_emploi_exh"))
feat = cat.create_featurestore("test_gsoncif_write",
                               workspace=wks_esipe,
                               data=input_shapefiles)
Beispiel #18
0
 def upload_shapefile(self, layer_name, shapefile_path):
     path = shapefile_path.split(".shp")[0]
     shapefile = shapefile_and_friends(path)
     ft = self.catalog.create_featurestore(layer_name, shapefile,
                                           self.workspace)
     self.save(ft)