Skip to content

Commit 9080e5e

Browse files
author
wonder
committedMar 4, 2007
- 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

File tree

2 files changed

+93
-38
lines changed

2 files changed

+93
-38
lines changed
 

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,41 @@ QString QgsOgrProvider::storageType() const
233233
}
234234

235235

236+
237+
bool QgsOgrProvider::getFeatureAtId(int featureId,
238+
QgsFeature& feature,
239+
bool fetchGeometry,
240+
QgsAttributeList fetchAttributes)
241+
{
242+
OGRFeature *fet = ogrLayer->GetFeature(featureId);
243+
if (fet == NULL)
244+
return false;
245+
246+
feature.setFeatureId(fet->GetFID());
247+
248+
/* fetch geometry */
249+
if (fetchGeometry)
250+
{
251+
OGRGeometry *geom = fet->GetGeometryRef();
252+
253+
// get the wkb representation
254+
unsigned char *wkb = new unsigned char[geom->WkbSize()];
255+
geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb);
256+
257+
feature.setGeometryAndOwnership(wkb, geom->WkbSize());
258+
}
259+
260+
/* fetch attributes */
261+
for(QgsAttributeList::iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it)
262+
{
263+
getFeatureAttribute(fet,feature,*it);
264+
}
265+
266+
return true;
267+
}
268+
269+
270+
236271
bool QgsOgrProvider::getNextFeature(QgsFeature& feature,
237272
bool fetchGeometry,
238273
QgsAttributeList fetchAttributes,
@@ -251,49 +286,57 @@ bool QgsOgrProvider::getNextFeature(QgsFeature& feature,
251286
while ((fet = ogrLayer->GetNextFeature()) != NULL)
252287
{
253288
// skip features without geometry
254-
if (fet->GetGeometryRef() != NULL || mFetchFeaturesWithoutGeom)
289+
if (fet->GetGeometryRef() == NULL && !mFetchFeaturesWithoutGeom)
255290
{
256-
OGRFeatureDefn * featureDefinition = fet->GetDefnRef();
257-
QString featureTypeName = featureDefinition ? QString(featureDefinition->GetName()) : QString("");
258-
feature.setFeatureId(fet->GetFID());
259-
feature.setTypeName(featureTypeName);
260-
261-
if (fetchGeometry)
262-
{
263-
OGRGeometry *geom = fet->GetGeometryRef();
291+
delete fet;
292+
continue;
293+
}
264294

265-
// get the wkb representation
266-
unsigned char *wkb = new unsigned char[geom->WkbSize()];
267-
geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb);
268-
269-
feature.setGeometryAndOwnership(wkb, geom->WkbSize());
295+
OGRFeatureDefn * featureDefinition = fet->GetDefnRef();
296+
QString featureTypeName = featureDefinition ? QString(featureDefinition->GetName()) : QString("");
297+
feature.setFeatureId(fet->GetFID());
298+
feature.setTypeName(featureTypeName);
270299

271-
if(mUseIntersect)
272-
{
273-
//precise test for intersection with search rectangle
274-
//first make QgsRect from OGRPolygon
275-
OGREnvelope env;
276-
mSelectionRectangle->getEnvelope(&env);
277-
if(env.IsInit()) //if envelope is invalid, skip the precise intersection test
278-
{
279-
selectionRect.set(env.MinX, env.MinY, env.MaxX, env.MaxY);
280-
if(!feature.geometry()->fast_intersects(selectionRect))
281-
{
282-
delete fet;
283-
continue;
284-
}
285-
}
286-
}
287-
}
300+
/* fetch geometry */
301+
if (fetchGeometry)
302+
{
303+
OGRGeometry *geom = fet->GetGeometryRef();
304+
305+
// get the wkb representation
306+
unsigned char *wkb = new unsigned char[geom->WkbSize()];
307+
geom->exportToWkb((OGRwkbByteOrder) QgsApplication::endian(), wkb);
308+
309+
feature.setGeometryAndOwnership(wkb, geom->WkbSize());
310+
311+
if (mUseIntersect)
312+
{
313+
//precise test for intersection with search rectangle
314+
//first make QgsRect from OGRPolygon
315+
OGREnvelope env;
316+
mSelectionRectangle->getEnvelope(&env);
317+
if(env.IsInit()) //if envelope is invalid, skip the precise intersection test
318+
{
319+
selectionRect.set(env.MinX, env.MinY, env.MaxX, env.MaxY);
320+
if(!feature.geometry()->fast_intersects(selectionRect))
321+
{
322+
delete fet;
323+
continue;
324+
}
325+
}
326+
327+
}
328+
}
288329

289-
for(QgsAttributeList::iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it)
290-
{
291-
getFeatureAttribute(fet,feature,*it);
292-
}
293-
294-
break;
330+
/* fetch attributes */
331+
for(QgsAttributeList::iterator it = fetchAttributes.begin(); it != fetchAttributes.end(); ++it)
332+
{
333+
getFeatureAttribute(fet,feature,*it);
295334
}
296-
}
335+
336+
/* we have a feature, end this cycle */
337+
break;
338+
339+
} /* while */
297340

298341
if (fet)
299342
{

‎src/providers/ogr/qgsogrprovider.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ class QgsOgrProvider : public QgsVectorDataProvider
7878
*/
7979
virtual void select(QgsRect mbr, bool useIntersect = false);
8080

81+
/**
82+
* Gets the feature at the given feature ID.
83+
* @param featureId id of the feature
84+
* @param feature feature which will receive the data
85+
* @param fetchGeoemtry if true, geometry will be fetched from the provider
86+
* @param fetchAttributes a list containing the indexes of the attribute fields to copy
87+
* @return True when feature was found, otherwise false
88+
*/
89+
virtual bool getFeatureAtId(int featureId,
90+
QgsFeature& feature,
91+
bool fetchGeometry = true,
92+
QgsAttributeList fetchAttributes = QgsAttributeList());
8193
/**
8294
* Get the next feature resulting from a select operation.
8395
* @param feature feature which will receive data from the provider

0 commit comments

Comments
 (0)
Please sign in to comment.