示例#1
0
文件: session.py 项目: zonakre/gasp
def start_grass_linux_newLocation(gisdb,
                                  location,
                                  srs,
                                  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:
        startcmd = '{} -c epsg:{} -e {}'

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

    outcmd = exec_cmd(startcmd.format(grassbin, str(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
示例#2
0
def copy_fromdb_todb2(conFrom, conTo, tables):
    """
    Send PGSQL Tables from one database to another using
    pg_dump and pg_restore
    """

    import os
    from gasp import goToList
    from gasp.oss.ops import create_folder, del_folder
    from gasp.sql.mng.tbl import dump_table
    from gasp.sql.mng.tbl import restore_table

    tmpFolder = create_folder(os.path.dirname(os.path.abspath(__file__)),
                              randName=True)

    tables = goToList(tables)

    for table in tables:
        # Dump
        sqlScript = dump_table(conFrom, table,
                               os.path.join(tmpFolder, table + ".sql"))

        # Restore
        tblname = restore_table(conTo, sqlScript, table)

    del_folder(tmpFolder)
示例#3
0
文件: lyrs.py 项目: zonakre/gasp
def publish_raster_layer(layername,
                         datastore,
                         workspace,
                         epsg_code,
                         conf={
                             'USER': '******',
                             'PASSWORD': '******',
                             'HOST': 'localhost',
                             'PORT': '8888'
                         },
                         protocol='http'):
    """
    Publish a Raster layer
    """

    import os
    import requests
    from gasp.to.Xml import write_xml_tree
    from gasp import random_str
    from gasp.oss.ops import create_folder, del_folder
    from gasp.prop.prj import epsg_to_wkt

    url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{work}/'
           'coveragestores/{storename}/coverages').format(host=conf['HOST'],
                                                          port=conf['PORT'],
                                                          work=workspace,
                                                          storename=datastore,
                                                          pro=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 = create_folder(
        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
示例#4
0
文件: lyrs.py 项目: zonakre/gasp
def publish_postgis_layer(workspace,
                          store,
                          pg_table,
                          title=None,
                          gs_con={
                              'USER': '******',
                              'PASSWORD': '******',
                              'HOST': 'localhost',
                              'PORT': '8888'
                          },
                          protocol='http'):
    """
    Publish PostGIS table in geoserver
    """

    import os
    import requests

    from gasp.oss.ops import create_folder, del_folder
    from gasp import random_str
    from gasp.to.Xml import write_xml_tree

    # Create folder to write xml
    wTmp = create_folder(
        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=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
示例#5
0
def copy_fromdb_todb(conFromDb, conToDb, tables, qForTbl=None, api='pandas'):
    """
    Send PGSQL Tables from one database to other
    """

    from gasp import goToList

    api = 'pandas' if api != 'pandas' and api != 'psql' else api

    tables = goToList(tables)

    if api == 'pandas':
        from gasp.fm.sql import query_to_df
        from gasp.to.sql import df_to_db

        for table in tables:
            if not qForTbl:
                tblDf = query_to_df(conFromDb,
                                    "SELECT * FROM {}".format(table),
                                    db_api='psql')

            else:
                if table not in qForTbl:
                    tblDf = query_to_df(conFromDb,
                                        "SELECT * FROM {}".format(table),
                                        db_api='psql')

                else:
                    tblDf = query_to_df(conFromDb,
                                        qForTbl[table],
                                        db_api='psql')

            df_to_db(conToDb, tblDf, table, api='psql')

    else:
        import os
        from gasp.oss.ops import create_folder, del_folder
        from gasp.sql.mng.tbl import dump_table
        from gasp.sql.mng.tbl import restore_table

        tmpFolder = create_folder(os.path.dirname(os.path.abspath(__file__)),
                                  randName=True)

        for table in tables:
            # Dump
            sqlScript = dump_table(conFromDb, table,
                                   os.path.join(tmpFolder, table + ".sql"))

            # Restore
            tblname = restore_table(conToDb, sqlScript, table)

        del_folder(tmpFolder)
示例#6
0
文件: session.py 项目: zonakre/gasp
def start_grass_win_newLocation(gisdb,
                                location,
                                srs,
                                grassBin,
                                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 'grass76'
    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:
        startcmd = '{} -c epsg:{} -e {}'

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

    # open grass
    outcmd = exec_cmd(startcmd.format(grassbin, str(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
示例#7
0
文件: stores.py 项目: zonakre/gasp
def create_psqlstore(store,
                     workspace,
                     pg_con,
                     gs_con={
                         'USER': '******',
                         'PASSWORD': '******',
                         'HOST': 'localhost',
                         'PORT': '8888'
                     },
                     protocol='http'):
    """
    Create a store for PostGIS data
    """

    import os
    import requests

    from gasp.oss.ops import create_folder, del_folder
    from gasp import random_str
    from gasp.to.Xml import write_xml_tree

    # Create folder to write xml
    wTmp = create_folder(
        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=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