TransWikia.com

Load layer From Oracle using QgsDataSourceUri in QGIS3 always return Invalid Layer

Geographic Information Systems Asked by Miti Teerasuwan on April 25, 2021

I’m tring to load spatial layer from Oracle but when i called function QgsVectorLayer(uri.uri(), “LAYER” , ‘oracle’) it still inValid layer

This is my code

> def LoadFromOracle(tablename):
>     GeometryColumn = Get_GeometryColumn(tablename)
>     print("GeoMetry Column is")
>     print(GeometryColumn)
>     oracleSetting = settingdb.GetSettingOracle()
>     uri = QgsDataSourceUri()
>     uri.setConnection(oracleSetting[3], oracleSetting[4], oracleSetting[0], oracleSetting[1], oracleSetting[2])
>     uri.setWkbType(QgsWkbTypes.Polygon)
>     uri.setDataSource('', tablename, GeometryColumn)
>     uri.setKeyColumn('ID1')
>     vlayer = QgsVectorLayer(uri.uri(), tablename, 'oracle')
>     print(uri.uri())
>     print ("uri wkbtype == %s layer geometry type == %s wkbType == %s"%(uri.wkbType(),vlayer.geometryType(),vlayer.wkbType()))
>     crs = QgsCoordinateReferenceSystem("EPSG:24048")
>     vlayer.setCrs(crs)
> 
>     return vlayer

I’m tring to use uri.setWkbType function but it is not working.
When I try to check valid status of QgsVectorLayer with this code.it still “False”

layer = self.LoadFromOracle('PROVINCE')
print ('Layer is valid = %s'%self.layer.isValid())

result of the code :

> dbname='XE' host=127.0.0.1 port=1521 user='SYSTEM' password='SYSTEM' key='ID1' estimatedmetadata=true type=Polygon table="PROVINCE" (GEOMETRY) sql=
> uri wkbtype == 3 layer geometry type == 3 wkbType == 0
> Layer is valid = False 

2 Answers

Now I found the solution It's not a problem with my code It's problem in my PyCharm with pyEnv. To solve this problem you can check path of your Python File and Check your Python interpreter.

Answered by Miti Teerasuwan on April 25, 2021

Nice work! Here is another way of doing the above while applying an SQL query:

user = "me"
pWord = "password"

def loadFromOracle(tName, user, pWord):
    uri = QgsDataSourceUri() #Instantiate the uri object

    host = 'bcgw.bcgov'
    port = "1521"
    dbName = 'idwprod1.bcgov'
    uri.setConnection(host, port, dbName, user, pWord)
    
#    uri.setWkbType(QgsWkbTypes.Polygon) # not needed

    schema, table = 'WHSE_LEGAL_ADMIN_BOUNDARIES', 'ABMS_MUNICIPALITIES_SP'
    geom = "SHAPE"
    sql = "admin_area_abbreviation = 'Castlegar'"
    keyColumn = "OBJECTID"
    uri.setDataSource(schema, table, geom, sql, keyColumn)
 
    vLayer = QgsVectorLayer(uri.uri(), tName, 'oracle')
    return vLayer
    
outLayer = "Castlegar"
vLayer = loadFromOracle(outLayer, user, pWord) # FUNCTION CALL
QgsProject.instance().addMapLayer(vLayer)
print(f"Layer {outLayer} added OK!") if vLayer.isValid() else print("Layer failed to load!")

I'm curious whether anybody knows how to do this with more than one table (i.e. two tables from different schema) at the same time. What I want to do is to inner join two tables then add them to my project as a single QgsVectorLayer.

Answered by grego on April 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP