[QGIS Commit] r14246 - trunk/qgis/src/providers/wfs

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Sep 18 06:03:56 EDT 2010


Author: mhugent
Date: 2010-09-18 10:03:56 +0000 (Sat, 18 Sep 2010)
New Revision: 14246

Modified:
   trunk/qgis/src/providers/wfs/qgswfsdata.cpp
   trunk/qgis/src/providers/wfs/qgswfsprovider.cpp
   trunk/qgis/src/providers/wfs/qgswfsprovider.h
Log:
Implement featureAtId for wfs provider

Modified: trunk/qgis/src/providers/wfs/qgswfsdata.cpp
===================================================================
--- trunk/qgis/src/providers/wfs/qgswfsdata.cpp	2010-09-18 08:59:37 UTC (rev 14245)
+++ trunk/qgis/src/providers/wfs/qgswfsdata.cpp	2010-09-18 10:03:56 UTC (rev 14246)
@@ -111,7 +111,6 @@
     progressDialog->show();
   }
 
-  QByteArray readData;
   int atEnd = 0;
   while ( !atEnd )
   {
@@ -119,10 +118,10 @@
     {
       atEnd = 1;
     }
-    readData = reply->readAll();
+    QByteArray readData = reply->readAll();
     if ( readData.size() > 0 )
     {
-      XML_Parse( p, readData.data(), readData.size(), atEnd );
+      XML_Parse( p, readData.constData(), readData.size(), atEnd );
     }
     QCoreApplication::processEvents();
   }

Modified: trunk/qgis/src/providers/wfs/qgswfsprovider.cpp
===================================================================
--- trunk/qgis/src/providers/wfs/qgswfsprovider.cpp	2010-09-18 08:59:37 UTC (rev 14245)
+++ trunk/qgis/src/providers/wfs/qgswfsprovider.cpp	2010-09-18 10:03:56 UTC (rev 14246)
@@ -76,6 +76,54 @@
   delete mSpatialIndex;
 }
 
+void QgsWFSProvider::copyFeature( QgsFeature* f, QgsFeature& feature, bool fetchGeometry, QgsAttributeList fetchAttributes )
+{
+  if ( !f )
+  {
+    return;
+  }
+
+  //copy the geometry
+  QgsGeometry* geometry = f->geometry();
+  unsigned char *geom = geometry->asWkb();
+  int geomSize = geometry->wkbSize();
+  unsigned char* copiedGeom = new unsigned char[geomSize];
+  memcpy( copiedGeom, geom, geomSize );
+  feature.setGeometryAndOwnership( copiedGeom, geomSize );
+
+  //and the attributes
+  const QgsAttributeMap& attributes = f->attributeMap();
+  for ( QgsAttributeList::const_iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it )
+  {
+    feature.addAttribute( *it, attributes[*it] );
+  }
+
+  //id and valid
+  feature.setValid( true );
+  feature.setFeatureId( f->id() );
+}
+
+bool QgsWFSProvider::featureAtId( int featureId,
+                                  QgsFeature& feature,
+                                  bool fetchGeometry,
+                                  QgsAttributeList fetchAttributes )
+{
+  QMap<int, QgsFeature* >::iterator it = mFeatures.find( featureId );
+  if ( it == mFeatures.end() )
+  {
+    return false;
+  }
+
+  QgsFeature* f = it.value();
+  if ( !f )
+  {
+    return false;
+  }
+
+  copyFeature( f, feature, fetchGeometry, fetchAttributes );
+  return true;
+}
+
 bool QgsWFSProvider::nextFeature( QgsFeature& feature )
 {
   feature.setValid( false );
@@ -87,27 +135,19 @@
       return 0;
     }
 
-    feature.setFeatureId( mFeatures[*mFeatureIterator]->id() );
+    QgsFeature* f = mFeatures[*mFeatureIterator];
+    ++mFeatureIterator;
+    if ( !f )
+    {
+      continue;
+    }
 
-    //we need geometry anyway, e.g. for intersection tests
-    QgsGeometry* geometry = mFeatures[*mFeatureIterator]->geometry();
-    unsigned char *geom = geometry->asWkb();
-    int geomSize = geometry->wkbSize();
-    unsigned char* copiedGeom = new unsigned char[geomSize];
-    memcpy( copiedGeom, geom, geomSize );
-    feature.setGeometryAndOwnership( copiedGeom, geomSize );
+    copyFeature( f, feature, true, mAttributesToFetch );
 
-    const QgsAttributeMap& attributes = mFeatures[*mFeatureIterator]->attributeMap();
-    for ( QgsAttributeList::const_iterator it = mAttributesToFetch.begin(); it != mAttributesToFetch.end(); ++it )
-    {
-      feature.addAttribute( *it, attributes[*it] );
-    }
-    ++mFeatureIterator;
     if ( mUseIntersect )
     {
       if ( feature.geometry() && feature.geometry()->intersects( mSpatialFilter ) )
       {
-        feature.setValid( true );
         return true;
       }
       else
@@ -117,7 +157,6 @@
     }
     else
     {
-      feature.setValid( true );
       return true;
     }
   }

Modified: trunk/qgis/src/providers/wfs/qgswfsprovider.h
===================================================================
--- trunk/qgis/src/providers/wfs/qgswfsprovider.h	2010-09-18 08:59:37 UTC (rev 14245)
+++ trunk/qgis/src/providers/wfs/qgswfsprovider.h	2010-09-18 10:03:56 UTC (rev 14246)
@@ -57,6 +57,22 @@
                          bool useIntersect = false );
 
     /**
+     * Gets the feature at the given feature ID.
+     * @param featureId of the feature to be returned
+     * @param feature which will receive the data
+     * @param fetchGeometry flag which if true, will cause the geometry to be fetched from the provider
+     * @param fetchAttributes a list containing the indexes of the attribute fields to copy
+     * @return True when feature was found, otherwise false
+     *
+     * Default implementation traverses all features until it finds the one with correct ID.
+     * In case the provider supports reading the feature directly, override this function.
+     */
+    virtual bool featureAtId( int featureId,
+                              QgsFeature& feature,
+                              bool fetchGeometry = true,
+                              QgsAttributeList fetchAttributes = QgsAttributeList() );
+
+    /**
      * Get the next feature resulting from a select operation.
      * @param feature feature which will receive data from the provider
      * @return true when there was a feature to fetch, false when end was hit
@@ -189,6 +205,9 @@
     /**This method tries to guess the geometry attribute and the other attribute names from the .gml file if no schema is present. Returns 0 in case of success*/
     int guessAttributesFromFile( const QString& uri, QString& geometryAttribute, std::list<QString>& thematicAttributes ) const;
 
+    /**Copies feature attributes / geometry from f to feature*/
+    void copyFeature( QgsFeature* f, QgsFeature& feature, bool fetchGeometry, QgsAttributeList fetchAttributes );
+
     //GML2 specific methods
     int getExtentFromGML2( QgsRectangle* extent, const QDomElement& wfsCollectionElement ) const;
 



More information about the QGIS-commit mailing list