more fTools api updates

This commit is contained in:
Juergen E. Fischer 2013-02-03 21:17:23 +01:00
parent 57e753ae87
commit 7f7b5dd63e
12 changed files with 106 additions and 224 deletions

View File

@ -111,7 +111,7 @@ class QgsFeature
const QgsAttributes& attributes() const;
//QgsAttributes& attributes();
void setAttributes(const QgsAttributes& attrs);
void setAttribute( int field, const QVariant& attr );
bool setAttribute( int field, const QVariant& attr );
void initAttributes( int fieldCount );
/**Deletes an attribute and its value*/

View File

@ -458,7 +458,7 @@ class geometryThread( QThread ):
def polygons_to_lines( self ):
vprovider = self.vlayer.dataProvider()
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vproviders.fields(),
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
QGis.WKBLineString, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
@ -488,7 +488,7 @@ class geometryThread( QThread ):
def lines_to_polygons( self ):
vprovider = self.vlayer.dataProvider()
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vproviders.fields(),
writer = QgsVectorFileWriter( self.myName, self.myEncoding, vprovider.fields(),
QGis.WKBPolygon, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
@ -545,47 +545,45 @@ class geometryThread( QThread ):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0)
self.emit( SIGNAL( "runRange( PyQt_PyObject )" ), ( 0, vprovider.featureCount() ) )
( fields, index1, index2 ) = self.checkMeasurementFields( self.vlayer, not self.writeShape )
if self.writeShape:
( fields, index1, index2 ) = self.checkGeometryFields( self.vlayer )
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
vprovider.geometryType(), vprovider.crs() )
fit = vprovider.getFeatures()
while fit.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1
inGeom = inFeat.geometry()
if self.myCalcType == 1:
inGeom.transform( coordTransform )
fit = vprovider.getFeatures()
while fit.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1
inGeom = inFeat.geometry()
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
if self.myCalcType == 1:
inGeom.transform( coordTransform )
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
if self.writeShape:
outFeat.setGeometry( inGeom )
atMap = inFeat.attributes()
maxIndex = index1 if index1>index2 else index2
if maxIndex>len(atMap):
atMap += [ QVariant() ] * ( index2+1 - len(atMap) )
atMap[ index1 ] = attr1
if index1!=index2:
atMap[ index2 ] = attr2
outFeat.setAttributes( atMap )
outFeat.addAttribute( index1, QVariant( attr1 ) )
outFeat.addAttribute( index2, QVariant( attr2 ) )
writer.addFeature( outFeat )
del writer
return True
else: # update existing file
( index1, index2 ) = self.findOrCreateFields( self.vlayer )
fit = vprovider.getFeatures()
while fit.nextFeature(inFeat):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), nElement )
nElement += 1
inGeom = inFeat.geometry()
if self.myCalcType == 1:
inGeom.transform( coordTransform )
( attr1, attr2 ) = self.simpleMeasure( inGeom, self.myCalcType, ellips, crs )
else:
changeMap = {}
changeAttributeMap = { index1 : QVariant( attr1 ),
index2 : QVariant( attr2 ) }
changeMap[ inFeat.id() ] = changeAttributeMap
changeMap[ inFeat.id() ] = {}
changeMap[ inFeat.id() ][ index1 ] = QVariant( attr1 )
if index1!=index2:
changeMap[ inFeat.id() ][ index2 ] = QVariant( attr2 )
vprovider.changeAttributeValues( changeMap )
if self.writeShape:
del writer
return True
def polygon_centroids( self ):
@ -659,7 +657,7 @@ class geometryThread( QThread ):
geom = QgsGeometry( inFeat.geometry() )
point = QgsPoint( geom.asPoint() )
polygon.append( point )
if step <= 3: feat.addAttribute( step, QVariant( ids[ index ] ) )
if step <= 3: feat.setAttribute( step, QVariant( ids[ index ] ) )
step += 1
geometry = QgsGeometry().fromPolygon( [ polygon ] )
feat.setGeometry( geometry )
@ -869,16 +867,16 @@ class geometryThread( QThread ):
self.emit( SIGNAL( "runStatus( PyQt_PyObject )" ), 0 )
fields = { 0 : QgsField( "MINX", QVariant.Double ),
1 : QgsField( "MINY", QVariant.Double ),
2 : QgsField( "MAXX", QVariant.Double ),
3 : QgsField( "MAXY", QVariant.Double ),
4 : QgsField( "CNTX", QVariant.Double ),
5 : QgsField( "CNTY", QVariant.Double ),
6 : QgsField( "AREA", QVariant.Double ),
7 : QgsField( "PERIM", QVariant.Double ),
8 : QgsField( "HEIGHT", QVariant.Double ),
9 : QgsField( "WIDTH", QVariant.Double ) }
fields = [ QgsField( "MINX", QVariant.Double ),
QgsField( "MINY", QVariant.Double ),
QgsField( "MAXX", QVariant.Double ),
QgsField( "MAXY", QVariant.Double ),
QgsField( "CNTX", QVariant.Double ),
QgsField( "CNTY", QVariant.Double ),
QgsField( "AREA", QVariant.Double ),
QgsField( "PERIM", QVariant.Double ),
QgsField( "HEIGHT", QVariant.Double ),
QgsField( "WIDTH", QVariant.Double ) ]
writer = QgsVectorFileWriter( self.myName, self.myEncoding, fields,
QGis.WKBPolygon, self.vlayer.crs() )
@ -1000,171 +998,36 @@ class geometryThread( QThread ):
value = value + measure.measureLine( k )
return value
def checkForField( self, L, e ):
e = QString( e ).toLower()
fieldRange = range( 0, len( L ) )
for item in fieldRange:
if L[ item ].toLower() == e:
return True, item
return False, len( L )
def doubleFieldIndex( self, name, desc, fieldList, f ):
i = 0
for f in fieldList:
if name == f.name().toUpper():
return (i, fieldList )
i += 1
def checkGeometryFields( self, vlayer ):
fieldList.append( QgsField( name, QVariant.Double, "double precision", 21, 6, desc ) )
return ( len(fieldList)-1, fieldList )
def checkMeasurementFields( self, vlayer, add ):
vprovider = vlayer.dataProvider()
nameList = []
fieldList = vprovider.fields()
geomType = vlayer.geometryType()
fieldKeys = fieldList.keys()
for i in fieldKeys:
nameList.append( fieldList[ i ].name().toLower() )
if geomType == QGis.Polygon:
if len( fieldKeys ) == max( fieldKeys ): # if equal, then the field geometry is not at the end of the fields list
( found, index ) = self.checkForField( nameList, "AREA" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "AREA" )
if not found:
field = QgsField( "AREA", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon area" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldList ) + 1
else:
index1 = len( fieldList )
fieldList[ index1 ] = field
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "PERIMETER" )
index2 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
if not found:
field = QgsField( "PERIMETER", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon perimeter" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldList ) + 1
else:
index2 = len( fieldList )
fieldList[ index2 ] = field
(index1, fieldList) = doubleFieldIndex( "AREA", self.tr( "Polygon area" ), fieldList )
(index2, fieldList) = doubleFieldIndex( "PERIMETER", self.tr( "Polygon perimeter" ), fieldList )
elif geomType == QGis.Line:
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "LENGTH" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if not found:
field = QgsField( "LENGTH", QVariant.Double, "double precision", 21, 6, self.tr( "Line length" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldList ) + 1
else:
index1 = len( fieldList )
fieldList[ index1 ] = field
(index1, fieldList) = doubleFieldIndex( "LENGTH", self.tr( "Line length" ), fieldList )
index2 = index1
else:
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "XCOORD" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if not found:
field = QgsField( "XCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point x coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldList ) + 1
else:
index1 = len( fieldList )
fieldList[ index1 ] = field
(index1, fieldList) = doubleFieldIndex( "XCOORD", self.tr( "Point x ordinate" ), fieldList )
(index2, fieldList) = doubleFieldIndex( "YCOORD", self.tr( "Point y ordinate" ), fieldList )
if add:
vprovider.addAttributes( newFields )
vlayer.updateFieldMap()
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "YCOORD" )
index2 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if not found:
field = QgsField( "YCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point y coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldList ) + 2
else:
index2 = len( fieldList )
fieldList[ index2 ] = field
return ( fieldList, index1, index2 )
def findOrCreateFields( self, vlayer ):
vprovider = vlayer.dataProvider()
fieldList = vprovider.fields()
geomType = vlayer.geometryType()
newFields = []
nameList = []
fieldKeys = fieldList.keys()
for i in fieldKeys:
nameList.append( fieldList[ i ].name().toLower() )
if geomType == QGis.Polygon:
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "AREA" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "AREA" )
if not found:
field = QgsField( "AREA", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon area" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldKeys ) + 1
else:
index1 = len( fieldKeys )
newFields.append( field )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "PERIMETER" )
index1 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "PERIMETER" )
if not found:
field = QgsField( "PERIMETER", QVariant.Double, "double precision", 21, 6, self.tr( "Polygon perimeter" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldKeys ) + 2
else:
index2 = len( fieldKeys ) + 1
newFields.append( field )
elif geomType == QGis.Line:
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "LENGTH" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "LENGTH" )
if not found:
field = QgsField( "LENGTH", QVariant.Double, "double precision", 21, 6, self.tr( "Line length" ) )
if len( fieldKeys ) == max( fieldKeys ):
index1 = len( fieldKeys ) + 1
else:
index1 = len( fieldKeys )
newFields.append( field )
index2 = index1
else:
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "XCOORD" )
index1 = index + 1
else:
( found, index1 ) = self.checkForField( nameList, "XCOORD" )
if not found:
field = QgsField( "XCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point x coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index = len( fieldKeys ) + 1
else:
index1 = len( fieldKeys )
newFields.append( field )
if len( fieldKeys ) == max( fieldKeys ):
( found, index ) = self.checkForField( nameList, "YCOORD" )
index2 = index + 1
else:
( found, index2 ) = self.checkForField( nameList, "YCOORD" )
if not found:
field = QgsField( "YCOORD", QVariant.Double, "double precision", 21, 6, self.tr( "Point y coordinate" ) )
if len( fieldKeys ) == max( fieldKeys ):
index2 = len( fieldKeys ) + 2
else:
index2 = len( fieldKeys ) + 1
newFields.append( field )
# FIXME: addAttributes was deprecated and removed
vprovider.addAttributes( newFields )
vlayer.updateFieldMap()
return ( index1, index2 )
def extractAsLine( self, geom ):
multi_geom = QgsGeometry()
temp_geom = []

View File

@ -507,9 +507,9 @@ class geoprocessingThread( QThread ):
outGeom = tmpGeom.convexHull()
outFeat.setGeometry( outGeom )
(area, perim) = self.simpleMeasure( outGeom )
outFeat.addAttribute( 0, QVariant( outID ) )
outFeat.addAttribute( 1, QVariant( area ) )
outFeat.addAttribute( 2, QVariant( perim ) )
outFeat.setAttribute( 0, QVariant( outID ) )
outFeat.setAttribute( 1, QVariant( area ) )
outFeat.setAttribute( 2, QVariant( perim ) )
writer.addFeature( outFeat )
except:
GEOS_EXCEPT = False
@ -563,9 +563,9 @@ class geoprocessingThread( QThread ):
outGeom = tmpGeom.convexHull()
outFeat.setGeometry( outGeom )
(area, perim) = self.simpleMeasure( outGeom )
outFeat.addAttribute( 0, QVariant( outID ) )
outFeat.addAttribute( 1, QVariant( area ) )
outFeat.addAttribute( 2, QVariant( perim ) )
outFeat.setAttribute( 0, QVariant( outID ) )
outFeat.setAttribute( 1, QVariant( area ) )
outFeat.setAttribute( 2, QVariant( perim ) )
writer.addFeature( outFeat )
except:
GEOS_EXCEPT = False

View File

@ -165,8 +165,8 @@ class Dialog(QDialog, Ui_Dialog):
for j in tempList:
outFeat.setGeometry(tempGeom.fromPoint(j))
outFeat.addAttribute(0, v1)
outFeat.addAttribute(1, v2)
outFeat.setAttribute(0, v1)
outFeat.setAttribute(1, v2)
writer.addFeature(outFeat)
start = start + add

View File

@ -193,13 +193,13 @@ class Dialog(QDialog, Ui_Dialog):
sd += (i-md)*(i-md)
sd = sqrt(sd/item)
outfeat.setGeometry(QgsGeometry.fromPoint(meanPoint).buffer(sd * times, 10))
outfeat.addAttribute(0, QVariant(sd))
outfeat.addAttribute(1, QVariant(j))
outfeat.setAttribute(0, QVariant(sd))
outfeat.setAttribute(1, QVariant(j))
else:
outfeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outfeat.addAttribute(0, QVariant(cx))
outfeat.addAttribute(1, QVariant(cy))
outfeat.addAttribute(2, QVariant(j))
outfeat.setAttribute(0, QVariant(cx))
outfeat.setAttribute(1, QVariant(cy))
outfeat.setAttribute(2, QVariant(j))
writer.addFeature(outfeat)
if single:
break

View File

@ -219,7 +219,7 @@ class PointsInPolygonThread(QThread):
interrupted = True
break
outFeat.addAttribute(index, QVariant(count))
outFeat.setAttribute(index, QVariant(count))
writer.addFeature(outFeat)
self.emit( SIGNAL( "updateProgress()" ) )

View File

@ -225,7 +225,7 @@ class Dialog(QDialog, Ui_Dialog):
add = ( 100.00 - 70.00 ) / len(points)
for i in points:
outFeat.setGeometry(i)
outFeat.addAttribute(0, QVariant(idVar))
outFeat.setAttribute(0, QVariant(idVar))
writer.addFeature(outFeat)
idVar = idVar + 1
count = count + add

View File

@ -147,7 +147,7 @@ class Dialog(QDialog, Ui_Dialog):
pGeom = QgsGeometry().fromPoint(QgsPoint(x, y))
if pGeom.intersects(bound):
outFeat.setGeometry(pGeom)
outFeat.addAttribute(0, QVariant(idVar))
outFeat.setAttribute(0, QVariant(idVar))
writer.addFeature(outFeat)
idVar = idVar + 1
x = x + pointSpacing

View File

@ -143,7 +143,7 @@ class Dialog(QDialog, Ui_Dialog):
length = length + distArea.measure(outGeom)
outFeat.setGeometry(inGeom)
outFeat.setAttributes(atMap)
outFeat.addAttribute(index, QVariant(length))
outFeat.setAttribute(index, QVariant(length))
writer.addFeature(outFeat)
start = start + 1
progressBar.setValue(start)

View File

@ -193,8 +193,8 @@ class Dialog(QDialog, Ui_Dialog):
pt2 = QgsPoint(bound.xMaximum(), y)
line = [pt1, pt2]
outFeat.setGeometry(outGeom.fromPolyline(line))
outFeat.addAttribute(0, QVariant(idVar))
outFeat.addAttribute(1, QVariant(y))
outFeat.setAttribute(0, QVariant(idVar))
outFeat.setAttribute(1, QVariant(y))
writer.addFeature(outFeat)
y = y - yOffset
idVar = idVar + 1
@ -213,8 +213,8 @@ class Dialog(QDialog, Ui_Dialog):
pt2 = QgsPoint(x, bound.yMinimum())
line = [pt1, pt2]
outFeat.setGeometry(outGeom.fromPolyline(line))
outFeat.addAttribute(0, QVariant(idVar))
outFeat.addAttribute(1, QVariant(x))
outFeat.setAttribute(0, QVariant(idVar))
outFeat.setAttribute(1, QVariant(x))
writer.addFeature(outFeat)
x = x + xOffset
idVar = idVar + 1
@ -238,11 +238,11 @@ class Dialog(QDialog, Ui_Dialog):
pt5 = QgsPoint(x, y)
polygon = [[pt1, pt2, pt3, pt4, pt5]]
outFeat.setGeometry(outGeom.fromPolygon(polygon))
outFeat.addAttribute(0, QVariant(idVar))
outFeat.addAttribute(1, QVariant(x))
outFeat.addAttribute(2, QVariant(x + xOffset))
outFeat.addAttribute(3, QVariant(y - yOffset))
outFeat.addAttribute(4, QVariant(y))
outFeat.setAttribute(0, QVariant(idVar))
outFeat.setAttribute(1, QVariant(x))
outFeat.setAttribute(2, QVariant(x + xOffset))
outFeat.setAttribute(3, QVariant(y - yOffset))
outFeat.setAttribute(4, QVariant(y))
writer.addFeature(outFeat)
idVar = idVar + 1
x = x + xOffset

View File

@ -18,6 +18,11 @@ email : sherman at mrcc.com
#include "qgsgeometry.h"
#include "qgsrectangle.h"
#ifdef QGISDEBUG
#include "qgsmessagelog.h"
#include <QObject>
#endif
/** \class QgsFeature
* \brief Encapsulates a spatial feature with attributes
*/
@ -167,6 +172,20 @@ void QgsFeature::initAttributes( int fieldCount )
}
bool QgsFeature::setAttribute( int idx, const QVariant &value )
{
#ifdef QGISDEBUG
if ( idx < 0 || idx >= mAttributes.size() )
{
QgsMessageLog::logMessage( QObject::tr( "Attribute index %1 out of bounds [0;%2[" ).arg( idx ).arg( mAttributes.size() ), QString::null, QgsMessageLog::WARNING );
return false;
}
#endif
mAttributes[idx] = value;
return true;
}
bool QgsFeature::setAttribute( const QString& name, QVariant value )
{
int fieldIdx = fieldNameIndex( name );

View File

@ -140,7 +140,7 @@ class CORE_EXPORT QgsFeature
const QgsAttributes& attributes() const { return mAttributes; }
QgsAttributes& attributes() { return mAttributes; }
void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; }
void setAttribute( int field, const QVariant& attr ) { mAttributes[field] = attr; }
bool setAttribute( int field, const QVariant& attr );
void initAttributes( int fieldCount );
/**Deletes an attribute and its value*/