Skip to content
Permalink
Browse files
- added getFeatureAtId() for OGR provider (for fast random access)
- more sane indenting in getNextFeature()


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6764 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Mar 4, 2007
1 parent 19f18f0 commit 9080e5e9fab1e6860d442f33e27ec52d9b6884eb
Showing with 93 additions and 38 deletions.
  1. +81 −38 src/providers/ogr/qgsogrprovider.cpp
  2. +12 −0 src/providers/ogr/qgsogrprovider.h
@@ -233,6 +233,41 @@ QString QgsOgrProvider::storageType() const
}



bool QgsOgrProvider::getFeatureAtId(int featureId,
QgsFeature& feature,
bool fetchGeometry,
QgsAttributeList fetchAttributes)
{
OGRFeature *fet = ogrLayer->GetFeature(featureId);
if (fet == NULL)
return false;

feature.setFeatureId(fet->GetFID());

/* fetch geometry */
if (fetchGeometry)
{
OGRGeometry *geom = fet->GetGeometryRef();

// get the wkb representation
unsigned char *wkb = new unsigned char[geom->WkbSize()];
geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb);

feature.setGeometryAndOwnership(wkb, geom->WkbSize());
}

/* fetch attributes */
for(QgsAttributeList::iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it)
{
getFeatureAttribute(fet,feature,*it);
}

return true;
}



bool QgsOgrProvider::getNextFeature(QgsFeature& feature,
bool fetchGeometry,
QgsAttributeList fetchAttributes,
@@ -251,49 +286,57 @@ bool QgsOgrProvider::getNextFeature(QgsFeature& feature,
while ((fet = ogrLayer->GetNextFeature()) != NULL)
{
// skip features without geometry
if (fet->GetGeometryRef() != NULL || mFetchFeaturesWithoutGeom)
if (fet->GetGeometryRef() == NULL && !mFetchFeaturesWithoutGeom)
{
OGRFeatureDefn * featureDefinition = fet->GetDefnRef();
QString featureTypeName = featureDefinition ? QString(featureDefinition->GetName()) : QString("");
feature.setFeatureId(fet->GetFID());
feature.setTypeName(featureTypeName);

if (fetchGeometry)
{
OGRGeometry *geom = fet->GetGeometryRef();
delete fet;
continue;
}

// get the wkb representation
unsigned char *wkb = new unsigned char[geom->WkbSize()];
geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb);

feature.setGeometryAndOwnership(wkb, geom->WkbSize());
OGRFeatureDefn * featureDefinition = fet->GetDefnRef();
QString featureTypeName = featureDefinition ? QString(featureDefinition->GetName()) : QString("");
feature.setFeatureId(fet->GetFID());
feature.setTypeName(featureTypeName);

if(mUseIntersect)
{
//precise test for intersection with search rectangle
//first make QgsRect from OGRPolygon
OGREnvelope env;
mSelectionRectangle->getEnvelope(&env);
if(env.IsInit()) //if envelope is invalid, skip the precise intersection test
{
selectionRect.set(env.MinX, env.MinY, env.MaxX, env.MaxY);
if(!feature.geometry()->fast_intersects(selectionRect))
{
delete fet;
continue;
}
}
}
}
/* fetch geometry */
if (fetchGeometry)
{
OGRGeometry *geom = fet->GetGeometryRef();

// get the wkb representation
unsigned char *wkb = new unsigned char[geom->WkbSize()];
geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb);

feature.setGeometryAndOwnership(wkb, geom->WkbSize());

if (mUseIntersect)
{
//precise test for intersection with search rectangle
//first make QgsRect from OGRPolygon
OGREnvelope env;
mSelectionRectangle->getEnvelope(&env);
if(env.IsInit()) //if envelope is invalid, skip the precise intersection test
{
selectionRect.set(env.MinX, env.MinY, env.MaxX, env.MaxY);
if(!feature.geometry()->fast_intersects(selectionRect))
{
delete fet;
continue;
}
}

}
}

for(QgsAttributeList::iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it)
{
getFeatureAttribute(fet,feature,*it);
}

break;
/* fetch attributes */
for(QgsAttributeList::iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it)
{
getFeatureAttribute(fet,feature,*it);
}
}

/* we have a feature, end this cycle */
break;

} /* while */

if (fet)
{
@@ -78,6 +78,18 @@ class QgsOgrProvider : public QgsVectorDataProvider
*/
virtual void select(QgsRect mbr, bool useIntersect = false);

/**
* Gets the feature at the given feature ID.
* @param featureId id of the feature
* @param feature feature which will receive the data
* @param fetchGeoemtry if true, geometry will 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
*/
virtual bool getFeatureAtId(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

0 comments on commit 9080e5e

Please sign in to comment.