def create_db(lnk, newdb, overwrite=True, api='psql'): """ Create Relational Database APIS Available: * psql; * sqlite; """ if api == 'psql': def drop(cursor, database): cursor.execute("DROP DATABASE {};".format(database)) if "DATABASE" in lnk: raise ValueError("For this method, the dict used to connected to " "PostgreSQL could not have a DATABASE key") dbs = list_db(lnk) con = psqlcon(lnk) cs = con.cursor() if newdb in dbs and overwrite: drop(cs, newdb) cs.execute( "CREATE DATABASE {}{};".format( newdb, " TEMPLATE={}".format(lnk["TEMPLATE"]) \ if "TEMPLATE" in lnk else "" ) ) cs.close() con.close() elif api == 'sqlite': import os import sqlite3 try: DB = os.path.join(lnk, newdb) if os.path.exists(DB) and overwrite: from gasp.oss.ops import del_file del_file(os.path.join(DB)) conn = sqlite3.connect(DB) except Error as e: print e finally: conn.close() else: raise ValueError('API {} is not available'.format(api)) return newdb
def add_raster_store(raster, store_name, workspace, conf={ 'USER': '******', 'PASSWORD': '******', 'HOST': 'localhost', 'PORT': '8888' }, protocol='http'): """ Create a new store with a raster layer """ import os import requests from gasp.oss.ops import del_file from gasp.to.Xml import write_xml_tree url = ('{pro}://{host}:{port}/geoserver/rest/workspaces/{work}/' 'coveragestores?configure=all').format(host=conf['HOST'], port=conf['PORT'], work=workspace, pro=protocol) # Create obj with data to be written in the xml xmlTree = { "coverageStore": { "name": store_name, "workspace": workspace, "enabled": "true", "type": "GeoTIFF", "url": raster } } treeOrder = { "coverageStore": ["name", "workspace", "enabled", "type", "url"] } # Write XML xml_file = write_xml_tree(xmlTree, os.path.join(os.path.dirname(raster), 'new_store.xml'), nodes_order=treeOrder) # Send request with open(xml_file, 'rb') as f: r = requests.post(url, data=f, headers={'content-type': 'text/xml'}, auth=(conf['USER'], conf['PASSWORD'])) del_file(xml_file) return r
def get_rst_report_data(rst, UNITS=None): """ Execute r.report and get reported data """ import os from gasp import random_str from gasp.oss.ops 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
1 | R. xxx | 145 | x | ... | x 2 | R. xxx | 146 | x | ... | x 3 | R. xxx | 147 | x | ... | x 4 | R. xxx | 148 | x | ... | x 5 | R. xxx | 149 | x | ... | x FID should be the first column """ import xlrd from gasp.oss.ops import del_file from gasp.fm import tbl_to_obj from gasp.to import dict_to_xls if overwrite: del_file(out_path) # XLS data to dict data = tbl_to_obj( xls_path, sheet=sheet, useFirstColAsIndex=True, output='dict' ) # Split interest_column (attribute) for fid in data: for col in data[fid]: if str(col) == str(interest_column): str_lst = data[fid][col].split(rule) data[fid][interest_column+'_1'] = str_lst[0] data[fid][interest_column+'_2'] = str_lst[1] if len(str_lst) > 1 else ''
def shp_to_psql(con_param, shpData, srsEpsgCode, pgTable=None, api="pandas"): """ Send Shapefile to PostgreSQL if api is equal to "pandas" - GeoPandas API will be used; if api is equal to "shp2pgsql" - shp2pgsql tool will be used. """ import os from gasp.oss import get_filename if api == "pandas": from gasp.fm import tbl_to_obj from gasp.prop.feat import get_geom_type elif api == "shp2pgsql": from gasp import exec_cmd from gasp.sql import run_sql_file from gasp.oss.ops import del_file else: raise ValueError( 'api value is not valid. options are: pandas and shp2pgsql') # Check if shp is folder if os.path.isdir(shpData): from gasp.oss import list_files shapes = list_files(shpData, file_format='.shp') else: from gasp import goToList shapes = goToList(shpData) tables = [] for _i in range(len(shapes)): # Get Table name tname = get_filename(shapes[_i], forceLower=True) if not pgTable else \ pgTable[_i] if type(pgTable) == list else pgTable # Import data if api == "pandas": # SHP to DataFrame df = tbl_to_obj(shapes[_i]) df.rename(columns={x: x.lower() for x in df.columns.values}, inplace=True) if "geometry" in df.columns.values: geomCol = "geometry" elif "geom" in df.columns.values: geomCol = "geom" else: print df.columns.values raise ValuError("No Geometry found in shp") # GeoDataFrame to PSQL geodf_to_pgsql(con_param, df, tname, srsEpsgCode, get_geom_type(shapes[_i], name=True, py_cls=False, gisApi='ogr'), colGeom=geomCol) else: sql_script = os.path.join(os.path.dirname(shapes[_i]), tname + '.sql') cmd = ('shp2pgsql -I -s {epsg} -W UTF-8 ' '{shp} public.{name} > {out}').format(epsg=srsEpsgCode, shp=shapes[_i], name=tname, out=sql_script) outcmd = exec_cmd(cmd) run_sql_file(con_param, con_param["DATABASE"], sql_script) del_file(sql_script) tables.append(tname) return tables[0] if len(tables) == 1 else tables