python qgis 3 adding features fails partly

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

python qgis 3 adding features fails partly

Reginald
Hello list,

Using the python console in QGIS 3.4.4 with the following code:
<code>
from qgis.PyQt.QtCore import *
from PyQt5.QtGui import *

perceellayer=None
premielayer=None
layers = [layer for layer in QgsProject.instance().mapLayers().values()]
for lyr in layers:
    if lyr.name() == "Adpf1836007":
        perceellayer = lyr
    if lyr.name() == "premies_zwaluwen":
        premielayer = lyr
features = perceellayer.selectedFeatures()
geom = None
for f in features:
    if geom == None:
        geom = f.geometry()        
    else:
        geom = geom.combine(f.geometry())

feat = QgsFeature(premielayer.fields())
feat.setGeometry(geom)
result = iface.openFeatureForm(premielayer, feat, showModal=False)

print(result)
premielayer.addFeature(feat)
premielayer.updateFields()
print("klaar")
</code>

- when premies_zwaluwen is a postgis layer trying to save the edit fails
with the message that id cannot be NULL altough in the featureform I put
another value
- when premies_zwaluwen is a shapefile the geometry gets added but all the
fields of the new feature are empty altough I put a value in every field
from the featureform.

Any idea what I am doing wrong?

Regards,




--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: python qgis 3 adding features fails partly

Reginald
Hi,

I'm still scratching my head over this one.
As a test I created a table 'test' in postgis:
id: pkey type oid
naam: type text
geom: type geometry

select updateGeometrySrid('test','geom',31370) # assigning projected CRS to
geometry

When I try to add this table to qgis using the database explorer I get an
error because there is no geometry in the table.
So I was wondering if it is impossible to add an empty postgis table to
qgis?

Regards,



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: python qgis 3 adding features fails partly

gisedegem
You can add an empty table to QGIS but it depends on the definition of your geometry column.

This table can be added:
CREATE TABLE public.test
(
    id oid NOT NULL,
    naam text COLLATE pg_catalog."default" NOT NULL,
    geom geometry(Point,31370),
    CONSTRAINT test_pkey PRIMARY KEY (id)
)


This table cannot be added:
CREATE TABLE public.test
(
    id oid NOT NULL,
    naam text COLLATE pg_catalog."default" NOT NULL,
    geom geometry(Geometry,31370),
    CONSTRAINT test_pkey PRIMARY KEY (id)
)

The easiest way to create the new table is to use the DB Manager.

Michel

-----Oorspronkelijk bericht-----
Van: Qgis-user <[hidden email]> Namens Reginald
Verzonden: donderdag 28 februari 2019 9:22
Aan: [hidden email]
Onderwerp: Re: [Qgis-user] python qgis 3 adding features fails partly

Hi,

I'm still scratching my head over this one.
As a test I created a table 'test' in postgis:
id: pkey type oid
naam: type text
geom: type geometry

select updateGeometrySrid('test','geom',31370) # assigning projected CRS to geometry

When I try to add this table to qgis using the database explorer I get an error because there is no geometry in the table.
So I was wondering if it is impossible to add an empty postgis table to qgis?

Regards,



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: python qgis 3 adding features fails partly

Reginald
Thanks Michel for this explanation.
I can now indeed add the table to QGIS.

I used MultiPolygon in stead of Point and created the test table.
I changed my code to:


from qgis.PyQt.QtCore import *
from PyQt5.QtGui import *

perceellayer=None
premielayer=None
layers = [layer for layer in QgsProject.instance().mapLayers().values()]
for lyr in layers:
    if lyr.name() == "Adpf1836007": #layer we use to copy existing geometry
        perceellayer = lyr
    if lyr.name() == "test":
        premielayer = lyr
features = perceellayer.selectedFeatures()
geom = None
for f in features:
    if geom == None:
        geom = f.geometry()        
    else:
        geom = geom.combine(f.geometry())

caps = premielayer.dataProvider().capabilities()
if caps & premielayer.dataProvider().AddFeatures:
    # de laag is editeerbaar
    feat = QgsFeature(premielayer.fields())
    feat.setGeometry(geom)
    result = iface.openFeatureForm(premielayer, feat, showModal=False)
    print(result)
    premielayer.addFeature(feat)
    premielayer.updateFields()
    print("klaar")
else:
    print("Gelieve de laag met betreffende premies editeerbaar te maken door
op het potloodje te klikken")

When I select a parcel and run the code with the test layer set as editable
I get this error when I click OK:

2019-02-28T12:43:18     CRITICAL    Laag test : PostGIS fout bij het
toevoegen van objecten: ERROR: null value in column "id" violates not-null
constraint
             DETAIL: Failing row contains (null, null,
01060000208A7A0000010000000103000000010000000800000000FE43FAC654...).

I used 'testwaarde' as naam and 1 as id.

Any idea what is going wrong here?

Regards,



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user