Exemple #1
0
def pub_pglyr(workspace, store, pg_table, title=None):
    """
    Publish PostGIS table in geoserver
    """
    
    import os;         import requests
    from gasp.pyt.char import random_str
    from gasp.pyt.Xml  import write_xml_tree
    from gasp.pyt.oss  import mkdir, del_folder
    from gasp.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
Exemple #2
0
def pub_rst_lyr(layername, datastore, workspace, epsg_code):
    """
    Publish a Raster layer
    """
    
    import os;            import requests
    from gasp.pyt.char    import random_str
    from gasp.pyt.Xml     import write_xml_tree
    from gasp.pyt.oss     import mkdir, del_folder
    from gasp.gt.prop.prj import epsg_to_wkt
    from gasp.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
Exemple #3
0
def tbl_fromdb_todb(from_db, to_db, tables, qForTbl=None, api='pandas'):
    """
    Send PGSQL Tables from one database to other
    """
    
    from gasp.pyt import obj_to_lst
    
    api = 'pandas' if api != 'pandas' and api != 'psql' else api
    
    tables = obj_to_lst(tables)
    
    if api == 'pandas':
        from gasp.sql.fm import q_to_obj
    
        for table in tables:
            if not qForTbl:
                tblDf = q_to_obj(from_db, "SELECT * FROM {}".format(
                    table), db_api='psql')
        
            else:
                if table not in qForTbl:
                    tblDf = q_to_obj(from_db, "SELECT * FROM {}".format(
                        table), db_api='psql')
            
                else:
                    tblDf = q_to_obj(from_db, qForTbl[table], db_api='psql')
        
            df_to_db(to_db, tblDf, table, api='psql')
    
    else:
        import os
        from gasp.pyt.oss import mkdir, del_folder
        from gasp.sql.fm  import dump_tbls
        from gasp.sql.to  import restore_tbls
        
        tmpFolder = mkdir(
            os.path.dirname(os.path.abspath(__file__)), randName=True
        )
        
        # Dump 
        sqlScript = dump_tbls(from_db, tables, os.path.join(
            tmpFolder, "tables_data.sql"
        ))
            
        # Restore
        restore_tbls(to_db, sqlScript, tables)
        
        del_folder(tmpFolder)
Exemple #4
0
def start_grass_linux_newLocation(gisdb,
                                  location,
                                  srs=None,
                                  grassBin=None,
                                  overwrite=True):
    """
    Method to open GRASS GIS on Linux Systems
    Creates a new location
    
    Parameters:
    * gisdb - abs path to grass workspace
    * location - name for the grass location
    * srs - epsg or file to define spatial reference system of the location that 
    will be created
    """

    location_path = os.path.join(gisdb, location)

    # Delete location if exists
    if os.path.exists(location_path):
        if overwrite:
            del_folder(location_path)

        else:
            raise ValueError('GRASS GIS 7 Location already exists')

    grassbin = grassBin if grassBin else 'grass76'
    startcmd = grassbin + ' --config path'

    outcmd = exec_cmd(startcmd)

    gisbase = outcmd.strip('\n')
    # Set GISBASE environment variable
    os.environ['GISBASE'] = gisbase
    # the following not needed with trunk
    os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'extrabin')
    # add path to GRASS addons
    home = os.path.expanduser("~")
    os.environ['PATH'] += os.pathsep + os.path.join(home, '.grass7', 'addons',
                                                    'scripts')
    # define GRASS-Python environment
    gpydir = os.path.join(gisbase, "etc", "python")
    sys.path.append(gpydir)

    if type(srs) == int:
        srs = str(srs)
        startcmd = '{} -c epsg:{} -e {}'

    elif type(srs) == str:
        startcmd = '{} -c {} -e {}'

    else:
        srs = ""
        startcmd = '{}{} -e {}'

    outcmd = exec_cmd(startcmd.format(grassbin, srs, location_path))

    # Set GISDBASE environment variable
    os.environ['GISDBASE'] = gisdb

    # See if there is location
    if not os.path.exists(location_path):
        raise ValueError('NoError, but location is not created')

    return gisbase
Exemple #5
0
def start_grass_win_newLocation(gisdb,
                                location,
                                srs=None,
                                grassBin=None,
                                overwrite=True):
    """
    Method to open GRASS GIS on MS Windows Systems
    Creates a new location
    
    Parameters:
    * gisdb - abs path to grass workspace
    * location - name for the grass location
    * srs - epsg or file to define spatial reference system of the location that 
    will be created
    
    
    To work, Path to GRASS GIS must be in the PATH Environment 
    Variable
    """

    # define database location
    location_path = os.path.join(gisdb, location)

    # Delete location if exists
    if os.path.exists(location_path):
        if overwrite:
            del_folder(location_path)

        else:
            raise ValueError('GRASS GIS 7 Location already exists')

    # the path to grass can't have white spaces
    grassbin = grassBin if grassBin else GRASS_BIN
    startcmd = grassbin + ' --config path'
    outcmd = exec_cmd(startcmd)

    # Set GISBASE environment variable
    gisbase = outcmd.strip().split('\r\n')[0]
    os.environ['GRASS_SH'] = os.path.join(gisbase, 'msys', 'bin', 'sh.exe')
    os.environ['GISBASE'] = gisbase
    # define GRASS-Python environment
    gpydir = os.path.join(gisbase, "etc", "python")
    sys.path.append(gpydir)

    # Define Command
    if type(srs) == int:
        srs = str(srs)
        startcmd = '{} -c epsg:{} -e {}'

    elif type(srs) == str:
        startcmd = '{} -c {} -e {}'

    else:
        startcmd = '{}{} -e {}'

    # open grass
    outcmd = exec_cmd(startcmd.format(grassbin, srs, location_path))

    # Set GISDBASE environment variable
    os.environ['GISDBASE'] = gisdb

    # See if there is location
    if not os.path.exists(location_path):
        raise ValueError('NoError, but location is not created')

    return gisbase
Exemple #6
0
def create_pgstore(store, workspace, pg_con):
    """
    Create a store for PostGIS data
    """

    import os
    import requests
    from gasp.pyt.char import random_str
    from gasp.pyt.Xml import write_xml_tree
    from gasp.pyt.oss import mkdir, del_folder
    from gasp.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
    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"): pg_con["DATABASE"],
                ("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