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= 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 2


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.

  • 1
    One remark: it seems like you connect as SYSTEM and have your data tables in that schema too. That is very wrong. SYSTEM is used for database administration only. Never use it for anything else and especially not fir any application data: use your own schema(s) for that. Aug 23, 2019 at 6:50
  • Thank you for advice :) Aug 23, 2019 at 8:51

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

    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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.