[Qgis-developer] Messy field's Id handling with AttributeMap()and fieldmaps if oid/fid/geom fields

haubourg regis.haubourg at eau-adour-garonne.fr
Fri Aug 10 05:17:27 PDT 2012


Hi all, 
developping EasyCustomLabeling plugin that duplicates layers into memory
layers, and also testing brand new duplicate plugin,* we found a very
cheating behaviour with field Id's and AttributeMap() / AddAttribute methods
of QgsFeature()*. 

I'll try to be as clear as possible:

*Qgis provider masks some fields types to user*, such as *oid, fid or
geometry* (if used as displayed geom). This is true for postgis. I guess
other spatial databases are concerned too. 

*What happens: *  
     - if good data admin practice,* oid and geometry fields are generally
stored as last fields *in table or views . (test_oid_geom_at_the_end.jpeg)
:* Everything goes fine*
     /   ex: CREATE OR REPLACE VIEW dce.rwbody_ag_testoid_end AS  SELECT
rwbody.eu_cd, masse_eau.nom_masse_eau,  masse_eau.nature::character varying
AS nature, rwbody.region_cd, rwbody.system, rwbody.geoml93, rwbody.oid  FROM
rwbody; /

     - *If oid or fid or geom field are NOT last fields
(test_oid_third_geomend.jpeg )*
   /  ex: CREATE OR REPLACE VIEW dce.rwbody_ag_testoid_end AS  SELECT
rwbody.eu_cd, masse_eau.nom_masse_eau, rwbody.oid  
masse_eau.nature::character varying AS nature, rwbody.region_cd,
rwbody.system, rwbody.geoml93 FROM rwbody; /  
Fields are masked but ids are just omitted. Just have a look to field id's
in layer properties to check this by yourself.
        When copying fields or attributes from that source table to a target
table will not copy attributes in the right columns . Id handling seems is
different between fetch attribute with attributeMap() and SetAttributes().. 
First one increment ids without oid / fid /geom fields, the other one
does... 

As a workaround, it is necessary to handle its own incremented id, *but,
this workaround is not possible when attributes are NULL, because
SourceFeature.attributeMap() only returns NOT empty attributes*.  Looping on
that attributeMap is quite tricky:

-- this code is mixing attributes: 
for SourceFeature in sourcelayer.selectedFeatures():
   Targetfeature = QgsFeature()   
   sourceAttrs = SourceFeature.attributeMap()
   Targetfeature.setAttributeMap(sourceAttrs)
   targetProvider.addFeatures( [ Targetfeature] )


-- this is a workaround :
for SourceFeature in sourcelayer.selectedFeatures():
   Targetfeature = QgsFeature()   
   sourceattrs = SourceFeature.attributeMap()
   idx = 0
   for k,v in sourceattrs .iteritems():
        Targetfeature .changeAttribute(idx, v)
        idx = idx + 1
   targetProvider.addFeatures( [ Targetfeature] )

*My question :


Is possible to correct attributeMap() and  setAttributeMap() methods so that
they both handle the same way ids, even if some are masked?  * This would be
really easier to handle, especially with growing number of users with
SpatialDB. 


Cheers, 
Régis



--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Messy-field-s-Id-handling-with-AttributeMap-and-fieldmaps-if-oid-fid-geom-fields-tp4994389.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list