本文整理匯總了Python中db_manager.db_plugins.postgis.connector.PostGisDBConnector類的典型用法代碼示例。如果您正苦於以下問題:Python PostGisDBConnector類的具體用法?Python PostGisDBConnector怎麽用?Python PostGisDBConnector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了PostGisDBConnector類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: postgis_path_to_uri
def postgis_path_to_uri(path):
"""Convert layer path from QgsBrowserModel to full QgsDataSourceURI.
:param path: The layer path from QgsBrowserModel
:type path: string
:returns: layer uri.
:rtype: QgsDataSourceURI
"""
connection_name = path.split('/')[1]
schema = path.split('/')[2]
table_name = path.split('/')[3]
settings = QSettings()
key = "/PostgreSQL/connections/" + connection_name
service = settings.value(key + "/service")
host = settings.value(key + "/host")
port = settings.value(key + "/port")
if not port:
port = "5432"
db = settings.value(key + "/database")
use_estimated_metadata = settings.value(
key + "/estimatedMetadata", False, type=bool)
sslmode = settings.value(
key + "/sslmode", QgsDataSourceURI.SSLprefer, type=int)
username = ""
password = ""
if settings.value(key + "/saveUsername") == "true":
username = settings.value(key + "/username")
if settings.value(key + "/savePassword") == "true":
password = settings.value(key + "/password")
# Old save setting
if settings.contains(key + "/save"):
username = settings.value(key + "/username")
if settings.value(key + "/save") == "true":
password = settings.value(key + "/password")
uri = QgsDataSourceURI()
if service:
uri.setConnection(service, db, username, password, sslmode)
else:
uri.setConnection(host, port, db, username, password, sslmode)
uri.setUseEstimatedMetadata(use_estimated_metadata)
# Obtain the geometry column name
connector = PostGisDBConnector(uri)
tables = connector.getVectorTables(schema)
tables = [table for table in tables if table[1] == table_name]
if not tables:
return None
table = tables[0]
geom_col = table[8]
uri.setDataSource(schema, table_name, geom_col)
return uri
示例2: test_dbnameLessURI
def test_dbnameLessURI(self):
c = PostGisDBConnector(QgsDataSourceUri())
self.assertIsInstance(c, PostGisDBConnector)
uri = c.uri()
# No username was passed, so we expect it to be taken
# from PGUSER or USER environment variables
expected_user = os.environ.get('PGUSER') or os.environ.get('USER')
actual_user = self._getUser(c)
self.assertEqual(actual_user, expected_user)
# No database was passed, so we expect it to be taken
# from PGDATABASE or expected user
expected_db = os.environ.get('PGDATABASE') or expected_user
actual_db = self._getDatabase(c)
self.assertEqual(actual_db, expected_db)
示例3: importPostgis
def importPostgis(appdef, progress):
progress.setText("Importing into PostGIS")
progress.setProgress(0)
host = appdef["Deploy"]["PostGIS host"]
port = appdef["Deploy"]["PostGIS port"]
username = appdef["Deploy"]["PostGIS username"]
password = appdef["Deploy"]["PostGIS password"]
dbname = appdef["Deploy"]["PostGIS database"]
schema = appdef["Deploy"]["PostGIS schema"]
uri = QgsDataSourceURI()
uri.setConnection(host, port, dbname, username, password)
connector = PostGisDBConnector(uri)
schemas = connector.getSchemas()
schemaExists = schema in [s[1] for s in schemas]
for i, layer in enumerate(appdef["Layers"]):
if layer.method in [METHOD_WFS_POSTGIS, METHOD_WMS_POSTGIS]:
if not schemaExists:
connector.createSchema(schema)
schemaExists = True
tables = connector.getTables(schema=schema)
tablename = safeName(layer.layer.name())
tableExists = tablename in [t[1] for t in tables]
if tableExists:
connector.deleteTable([schema, tablename])
importLayerIntoPostgis(layer.layer, host, port, username, password,
dbname, schema, tablename, appdef["Settings"]["App view CRS"])
progress.setProgress(int(i*100.0/len(appdef["Layers"])))
示例4: sqlExec
def sqlExec(self, sql):
# Execute a SQL query and, return [header, data, rowCount]
connector = PostGisDBConnector(self.uri)
#print "DEBUG dbrequest : sql = " + sql
try:
c = connector._execute(None, unicode(sql))
data = []
header = connector._get_cursor_columns(c)
except:
print "Erreur SQL : " + str(sql) # debug purpose
raise
if header is None:
header = []
if len(header) > 0:
data = connector._fetchall(c)
row_count = c.rowcount
if row_count == -1:
row_count = len(data)
if c:
c.close()
del c
connector.__del__()
return [header, data, row_count]