Beispiel #1
0
def get_capabilities():
    """
    Get GetCapabilities XML Data
    """

    import os
    import xmltodict
    from glass.pys.web import get_file
    from glass.cons.gsrv import con_gsrv
    from glass.pys.char import random_str
    from glass.pys.oss import del_file

    conparam = con_gsrv()

    url = ("{}://{}:{}/geoserver/wms?request=GetCapabilities"
           "&service=WMS&version=1.1.1").format(conparam["PROTOCOL"],
                                                conparam["HOST"],
                                                conparam["PORT"])

    xml = get_file(
        url,
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     random_str(10) + '.xml'))

    with open(xml) as xmlf:
        xmld = xmltodict.parse(xmlf.read())

    del_file(xml)

    return xmld
Beispiel #2
0
def pub_rst_lyr(layername, datastore, workspace, epsg_code):
    """
    Publish a Raster layer
    """

    import os
    import requests
    from glass.pys.char import random_str
    from glass.pys.Xml import write_xml_tree
    from glass.pys.oss import mkdir, del_folder
    from glass.g.prop.prj import epsg_to_wkt
    from glass.cons.gsrv import con_gsrv

    conf = con_gsrv()

    url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{work}/'
           'coveragestores/{storename}/coverages').format(host=conf['HOST'],
                                                          port=conf['PORT'],
                                                          work=workspace,
                                                          storename=datastore,
                                                          pro=conf['PROTOCOL'])

    # Create obj with data to be written in the xml
    xmlTree = {
        "coverage": {
            "name": layername,
            "title": layername,
            "nativeCRS": str(epsg_to_wkt(epsg_code)),
            "srs": 'EPSG:{}'.format(str(epsg_code)),
        }
    }

    # Write XML
    wTmp = mkdir(
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     random_str(7)))

    xml_file = write_xml_tree(xmlTree, os.path.join(wTmp, 'rst_lyr.xml'))

    # Create layer
    with open(xml_file, 'rb') as f:
        r = requests.post(url,
                          data=f,
                          headers={'content-type': 'text/xml'},
                          auth=(conf['USER'], conf['PASSWORD']))

    del_folder(wTmp)

    return r
Beispiel #3
0
def pub_pglyr(workspace, store, pg_table, title=None):
    """
    Publish PostGIS table in geoserver
    """

    import os
    import requests
    from glass.pys.char import random_str
    from glass.pys.Xml import write_xml_tree
    from glass.pys.oss import mkdir, del_folder
    from glass.cons.gsrv import con_gsrv

    gs_con = con_gsrv()

    # Create folder to write xml
    wTmp = mkdir(
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     random_str(7)))

    # Create obj with data to be written in the xml
    lyr_title = "Title {}".format(pg_table) if not title else title
    elements = {"featureType": {"name": pg_table, "title": lyr_title}}

    # Write the xml
    xml_file = write_xml_tree(elements,
                              os.path.join(wTmp, '{}.xml'.format(pg_table)))

    # Create Geoserver Layer
    url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{wname}/'
           'datastores/{store_name}/featuretypes').format(
               host=gs_con['HOST'],
               port=gs_con['PORT'],
               wname=workspace,
               store_name=store,
               pro=gs_con['PROTOCOL'])

    with open(xml_file, 'rb') as __xml:
        r = requests.post(url,
                          data=__xml,
                          headers={'content-type': 'text/xml'},
                          auth=(gs_con['USER'], gs_con['PASSWORD']))

        __xml.close()

    del_folder(wTmp)

    return r
Beispiel #4
0
def get_rst_report_data(rst, UNITS=None):
    """
    Execute r.report and get reported data
    """

    import os
    from glass.pys.char import random_str
    from glass.pys.oss import del_file

    REPORT_PATH = raster_report(rst,
                                os.path.join(
                                    os.path.dirname(os.path.abspath(__file__)),
                                    "{}.txt".format(random_str(6))),
                                _units=UNITS)

    report_data = sanitize_report(REPORT_PATH)

    del_file(REPORT_PATH)

    return report_data
Beispiel #5
0
def create_pgstore(store, workspace, db, dbset='default'):
    """
    Create a store for PostGIS data
    """

    import os
    import requests
    from glass.pys.char import random_str
    from glass.pys.Xml import write_xml_tree
    from glass.pys.oss import mkdir, del_folder
    from glass.cons.gsrv import con_gsrv
    from glass.cons.psql import con_psql

    gs_con = con_gsrv()
    pg_con = con_psql(db_set=dbset)

    # Create folder to write xml
    wTmp = mkdir(
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     random_str(7)))

    # Create obj with data to be written in the xml
    tree_order = {
        "dataStore": [
            "name", "type", "enabled", "workspace", "connectionParameters",
            "__default"
        ],
        "connection:Parameters": [("entry", "key", "port"),
                                  ("entry", "key", "user"),
                                  ("entry", "key", "passwd"),
                                  ("entry", "key", "dbtype"),
                                  ("entry", "key", "host"),
                                  ("entry", "key", "database"),
                                  ("entry", "key", "schema")]
    }

    xml_tree = {
        "dataStore": {
            "name": store,
            "type": "PostGIS",
            "enabled": "true",
            "workspace": {
                "name": workspace
            },
            "connectionParameters": {
                ("entry", "key", "port"): pg_con["PORT"],
                ("entry", "key", "user"): pg_con["USER"],
                ("entry", "key", "passwd"): pg_con["PASSWORD"],
                ("entry", "key", "dbtype"): "postgis",
                ("entry", "key", "host"): pg_con["HOST"],
                ("entry", "key", "database"): db,
                ("entry", "key", "schema"): "public"
            },
            "__default": "false"
        }
    }

    # Write xml
    xml_file = write_xml_tree(xml_tree,
                              os.path.join(wTmp, 'pgrest.xml'),
                              nodes_order=tree_order)

    # Create Geoserver Store
    url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{wname}/'
           'datastores.xml').format(host=gs_con['HOST'],
                                    port=gs_con['PORT'],
                                    wname=workspace,
                                    pro=gs_con['PROTOCOL'])

    with open(xml_file, 'rb') as f:
        r = requests.post(url,
                          data=f,
                          headers={'content-type': 'text/xml'},
                          auth=(gs_con['USER'], gs_con['PASSWORD']))
        f.close()

    del_folder(wTmp)

    return r