Skip to content

Commit

Permalink
Merge pull request #4782 from nirvn/shapefile_wkbtype
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jul 10, 2017
2 parents 18c3634 + 5b149fb commit 1693873
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsmessagelog.h"
#include "qgssettings.h"
#include "qgsexception.h"
#include "qgswkbtypes.h"

#include <QTextCodec>
#include <QFile>
Expand Down Expand Up @@ -334,7 +335,15 @@ bool QgsOgrFeatureIterator::readFeature( OGRFeatureH fet, QgsFeature &feature )

if ( geom )
{
feature.setGeometry( QgsOgrUtils::ogrGeometryToQgsGeometry( geom ) );
QgsGeometry g = QgsOgrUtils::ogrGeometryToQgsGeometry( geom );

// Insure that multipart datasets return multipart geometry
if ( QgsWkbTypes::isMultiType( mSource->mWkbType ) && !g.isMultipart() )
{
g.convertToMultiType();
}

feature.setGeometry( g );
}
else
feature.clearGeometry();
Expand Down Expand Up @@ -390,6 +399,7 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider *p )
, mOgrGeometryTypeFilter( QgsOgrProvider::ogrWkbSingleFlatten( p->mOgrGeometryTypeFilter ) )
, mDriverName( p->ogrDriverName )
, mCrs( p->crs() )
, mWkbType( p->wkbType() )
{
for ( int i = ( p->mFirstFieldIsFid ) ? 1 : 0; i < mFields.size(); i++ )
mFieldsWithoutFid.append( mFields.at( i ) );
Expand Down
1 change: 1 addition & 0 deletions src/providers/ogr/qgsogrfeatureiterator.h
Expand Up @@ -44,6 +44,7 @@ class QgsOgrFeatureSource : public QgsAbstractFeatureSource
OGRwkbGeometryType mOgrGeometryTypeFilter;
QString mDriverName;
QgsCoordinateReferenceSystem mCrs;
QgsWkbTypes::Type mWkbType = QgsWkbTypes::Unknown;

friend class QgsOgrFeatureIterator;
friend class QgsOgrExpressionCompiler;
Expand Down
8 changes: 7 additions & 1 deletion src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -30,6 +30,7 @@ email : sherman at mrcc.com
#include "qgsgeometry.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsvectorlayerexporter.h"
#include "qgswkbtypes.h"
#include "qgis.h"


Expand Down Expand Up @@ -1175,7 +1176,12 @@ size_t QgsOgrProvider::layerCount() const
*/
QgsWkbTypes::Type QgsOgrProvider::wkbType() const
{
return static_cast<QgsWkbTypes::Type>( mOGRGeomType );
QgsWkbTypes::Type wkb = static_cast<QgsWkbTypes::Type>( mOGRGeomType );
if ( ogrDriverName == QLatin1String( "ESRI Shapefile" ) && ( wkb == QgsWkbTypes::LineString || wkb == QgsWkbTypes::Polygon ) )
{
wkb = QgsWkbTypes::multiType( wkb );
}
return wkb;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/src/python/test_provider_virtual.py
Expand Up @@ -181,7 +181,7 @@ def test_Query(self):
# the same, without specifying the geometry column name
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3)
self.assertEqual(l2.dataProvider().wkbType(), 6)
ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures())
ref_sum3 = sum(f.id() for f in l2.getFeatures())
# check we have the same rows
Expand All @@ -204,7 +204,7 @@ def test_Query(self):
# with two geometry columns, but no geometry column specified (will take the first)
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3)
self.assertEqual(l2.dataProvider().wkbType(), 6)
ref_sum2 = sum(f.attributes()[0] for f in l2.getFeatures())
ref_sum3 = sum(f.id() for f in l2.getFeatures())
# check we have the same rows
Expand Down Expand Up @@ -343,7 +343,7 @@ def test_reopen(self):

l2 = QgsVectorLayer(tmp, "tt", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3)
self.assertEqual(l2.dataProvider().wkbType(), 6)
self.assertEqual(l2.dataProvider().featureCount(), 4)

def test_reopen2(self):
Expand Down Expand Up @@ -448,7 +448,7 @@ def test_sql2(self):
l4 = QgsVectorLayer("?query=%s&uid=ObjectId" % query, "tt", "virtual")
self.assertEqual(l4.isValid(), True)

self.assertEqual(l4.dataProvider().wkbType(), 3)
self.assertEqual(l4.dataProvider().wkbType(), 6)
self.assertEqual(l4.dataProvider().crs().postgisSrid(), 4326)

n = 0
Expand Down

0 comments on commit 1693873

Please sign in to comment.