[Qgis-developer] CSV Layers cannot be picked as input in Sextante
Victor Olaya
volayaf at gmail.com
Sat Dec 15 14:53:25 PST 2012
Anita,
You are not using the geometry of the second layer, so instead of a
vector layer, you could define it as a table.
You should replace "##input2=vector" with "#input2=table".
However, you are not going to be able to select vector layers in this
case, just independent csv and dbf files loaded into QGIS. I think I
can solve that by changing the behaviour of SEXTANTE, and allowing it
to consider any vector layer also as a table. There is no reason for
it to be like it is now. If I change that, I will tell you.
Cheers
Victor
2012/12/15 Anita Graser <anitagraser at gmx.at>:
> Hi,
>
> I just finished my join script for Sextante but it isn't as useful as
> I would like it to be because I cannot pick CSV files (loaded using
> Add Vector Layer) as input.
> Can I fix this using a different input definition? Script follows.
>
> On a related note: It's possible to select CSVs loaded using Add
> Delimited Text layer. But the join will have only one result. It seems
> like provider.nextFeature(inFeat) doesn't work properly in this case.
>
> Best wishes,
> Anita
>
>
>
> #Definition of inputs and outputs
> #==================================
> ##[my scripts]=group
> ##input1=vector
> ##join_field1=field input1
> ##input2=vector
> ##join_field2=field input2
> ##output=output vector
>
> #Algorithm body
> #==================================
> from sextante.core.QGisLayers import QGisLayers
> from qgis.core import *
> from PyQt4.QtCore import *
> from sextante.core.SextanteVectorWriter import SextanteVectorWriter
>
>
> # Layer 1
> layer = QGisLayers.getObjectFromUri(input1)
> provider = layer.dataProvider()
> allAttrs = provider.attributeIndexes()
> provider.select( allAttrs )
> join_field1_index = provider.fieldNameIndex(join_field1)
> # Layer 2
> layer2 = QGisLayers.getObjectFromUri(input2)
> provider2 = layer2.dataProvider()
> allAttrs = provider2.attributeIndexes()
> provider2.select( allAttrs )
> fields2 = provider2.fields()
> join_field2_index = provider2.fieldNameIndex(join_field2)
>
> # Output
> outFields = provider.fields()
> for (i,f) in fields2.iteritems():
> f.setName("x_"+f.name())
> outFields[len(outFields)] = f
>
> writer = SextanteVectorWriter(output, None, outFields,
> provider.geometryType(), provider.crs() )
>
> inFeat = QgsFeature()
> inFeat2 = QgsFeature()
> outFeat = QgsFeature()
>
> # Create output vector layer with additional attribute
> while provider.nextFeature(inFeat):
> inGeom = inFeat.geometry()
> atMap = inFeat.attributeMap()
> join_value1 = atMap[join_field1_index].toString()
>
> while provider2.nextFeature(inFeat2):
> inGeom2 = inFeat2.geometry()
> atMap2 = inFeat2.attributeMap()
> join_value2 = atMap2[join_field2_index].toString()
>
> if join_value1 == join_value2:
> # create the new feature
> outFeat.setGeometry( inGeom )
> outFeat.setAttributeMap( atMap )
> l = len(provider.fields())
> for (i,a) in atMap2.iteritems():
> outFeat.addAttribute( l+i, a )
>
> writer.addFeature( outFeat )
>
> del writer
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
More information about the Qgis-developer
mailing list