TransWikia.com

Accessing Rest Api of Geoserver using python

Geographic Information Systems Asked on May 17, 2021

is there anyway to access Rest Api of Geoserver using python other than gsconfig.
since it doesn’t support for me.i’m using python3.4
Like creating workspace,adding layers programmatically to Geoserver

4 Answers

I think it is answered at here but i will add a python module named gisconfig, details are below. There are libraries support HTTP capabilities e.g. Request. Further you can have look at here-though it is in developing stage.Github repo is at here.

e.g. to create layer at localhost-details is at previous github repo.

    from geoserver.catalog import Catalog
    cat = Catalog("http://localhost:8080/geoserver/")
    topp = self.cat.get_workspace("topp")
    shapefile_plus_sidecars = shapefile_and_friends("states")
    # shapefile_and_friends should look on the filesystem to find a shapefile
    # and related files based on the base path passed in
    #
    # shapefile_plus_sidecars == {
    #    'shp': 'states.shp',
    #    'shx': 'states.shx',
    #    'prj': 'states.prj',
    #    'dbf': 'states.dbf'
    # }

    # 'data' is required (there may be a 'schema' alternative later, for creating empty featuretypes)
    # 'workspace' is optional (GeoServer's default workspace is used by... default)
    # 'name' is required
    ft = self.cat.create_featuretype(name, workspace=topp, data=shapefile_plus_sidecars)

Answered by SIslam on May 17, 2021

As already mentioned slightly by Slslam, you can use Python's requests module to send the same POST requests to GeoServer's REST API as you could do from the command line with cUrl.

This is an example that I recently used to activate a layer not yet known to GeoServer but already available in the connected DataStore of the given workspace (a PostGIS database in my case), including activation of the TIME dimension.

In fact, the only thing that is different to the examples given on the cUrl page above is how to fire the POST request. Apart from that, the XML you send to the REST API can contain whatever you want as long as it is valid (otherwise, you'll get an error message back from the server in your requests variable).

To use the module, either install it via pip install requests or, preferably on Windows systems, by downloading this binary and installing it via pip install pathtothefile.whl.

import requests

# define your parameters
url = 'http://localhost:8080/geoserver/rest/workspaces/my_workspace/datastores/my_datastore/featuretypes'
headers = {'Content-Type': 'text/xml'}
auth = ('admin', 'geoserver')

# define your XML string that you want to send to the server
data = """
    <featureType>
    <name>my_layer_name</name>
    <srs>EPSG:4326</srs>
    <enabled>true</enabled>
    <metadata>
    <entry key="time">
    <dimensionInfo>
    <enabled>true</enabled>
    <attribute>datetime</attribute>
    <presentation>CONTINUOUS_INTERVAL</presentation>
    <units>ISO8601</units>
    <defaultValue><strategy>MAXIMUM</strategy></defaultValue>
    </dimensionInfo>
    </entry>
    </metadata>
    <store class="dataStore">
        <name>my_datastore</name>
    </store>
</featureType>
"""

# fire the request
r = requests.post(url, headers=headers, auth=auth, data=data)

# inspect the response
print(r.text)

Answered by Dirk on May 17, 2021

The new version is called - geoserver-config in GitHub repository.

I see it provides ease of use accessing GeoServer REST-Config APIs, for instance:

from geoserver.catalog import Catalog
#
cat = Catalog("http://localhost:8080/geoserver/rest/", "admin", "geoserver")
cat.get_workspaces(names="geosolutions,topp")
# or: 
cat.get_workspaces(names=["geosolutions", "topp"])
# or:
cat.get_stores(names=["sf", "mosaic"], workspaces=["nurc", "topp", "sf"])

...

Answered by Zlatomir Dimitrov on May 17, 2021

You can try the newer version of another library named geoserver-rest(pip install geoserver-rest). This library is very easy to use and supports dynamic styling for the GeoServer maps. You can check the documentation of the library here. Some of the cool examples of this library is explained below,


#import and initialize library
from geo.Geoserver import Geoserver
geo = Geoserver('http://localhost:8080/geoserver', username='admin', password='geoserver')

# upload raster file
geo.create_coveragestore(lyr_name='layer1' path=r'pathtorasterfile.tif', workspace='demo')


#create style file dynamically
geo.create_coveragestyle(raster_path=r'pathtorasterfile.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')



Answered by gis on May 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP