Skip to content

Commit f0c7438

Browse files
author
mhugent
committedMay 6, 2005
added a QgsOgrProvider::createSpatialIndex() method. This method creates a .qix index file for shape files. Note that there is currently no access from the GUI. If you already want to try this feature, just add a call to createSpatialIndex() to the constructor of QgsOgrProvider
git-svn-id: http://svn.osgeo.org/qgis/trunk@3326 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 4091dbb commit f0c7438

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed
 

‎providers/ogr/qgsogrprovider.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ email : sherman at mrcc.com
3131
#include <cpl_error.h>
3232
#include "ogr_api.h"//only for a test
3333

34+
#include <qfile.h>
3435
#include <qmessagebox.h>
3536
#include <qmap.h>
3637

@@ -78,7 +79,9 @@ QgsOgrProvider::QgsOgrProvider(QString uri): QgsVectorDataProvider(), dataSource
7879
std::cerr << "Data source is valid" << std::endl;
7980
#endif
8081
valid = true;
82+
8183
ogrLayer = ogrDataSource->GetLayer(0);
84+
8285
// get the extent_ (envelope) of the layer
8386
#ifdef QGISDEBUG
8487
std::cerr << "Starting get extent\n";
@@ -122,6 +125,8 @@ QgsOgrProvider::QgsOgrProvider(QString uri): QgsVectorDataProvider(), dataSource
122125
valid = false;
123126
}
124127

128+
//createSpatialIndex();
129+
125130
//resize the cache matrix
126131
minmaxcache=new double*[fieldCount()];
127132
for(int i=0;i<fieldCount();i++)
@@ -571,8 +576,9 @@ void QgsOgrProvider::select(QgsRect *rect, bool useIntersect)
571576
//TODO about it. If setting the filter fails, all records will be returned
572577
if (result == OGRERR_NONE)
573578
{
574-
std::cerr << "Setting spatial filter using " << wktExtent << std::endl;
575-
ogrLayer->SetSpatialFilter(filter);
579+
std::cerr << "Setting spatial filter using " << wktExtent << std::endl;
580+
//ogrLayer->SetSpatialFilter(filter);
581+
ogrLayer->SetSpatialFilterRect(rect->xMin(), rect->yMin(), rect->xMax(), rect->yMax());
576582
std::cerr << "Feature count: " << ogrLayer->GetFeatureCount() << std::endl;
577583
}else{
578584
#ifdef QGISDEBUG
@@ -1070,6 +1076,28 @@ bool QgsOgrProvider::changeAttributeValues(std::map<int,std::map<QString,QString
10701076
return true;
10711077
}
10721078

1079+
bool QgsOgrProvider::createSpatialIndex()
1080+
{
1081+
//experimental, try to create a spatial index
1082+
QString filename=dataSourceUri.section('/',-1,-1);//todo: find out the filename from the uri
1083+
QString layername=filename.section('.',0,0);
1084+
QString sql="CREATE SPATIAL INDEX ON "+layername;
1085+
ogrDataSource->ExecuteSQL (sql.ascii(), ogrLayer->GetSpatialFilter(),"");
1086+
//todo: find out, if the .qix file is there
1087+
QString indexname=dataSourceUri;
1088+
indexname.truncate(dataSourceUri.length()-filename.length());
1089+
indexname=indexname+layername+".qix";
1090+
QFile indexfile(indexname);
1091+
if(indexfile.exists())
1092+
{
1093+
return true;
1094+
}
1095+
else
1096+
{
1097+
return false;
1098+
}
1099+
}
1100+
10731101
int QgsOgrProvider::capabilities() const
10741102
{
10751103
return (QgsVectorDataProvider::AddFeatures

‎providers/ogr/qgsogrprovider.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ class QgsOgrProvider:public QgsVectorDataProvider
154154
return 0;
155155
}
156156

157+
/**Tries to create a .qix index file for faster access if only a subset of the features is required
158+
@return true in case of success*/
159+
bool createSpatialIndex();
160+
157161
/**Returns a bitmask containing the supported capabilities*/
158162
int capabilities() const;
159163

‎providers/postgres/qgspostgresprovider.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,27 @@ QgsPostgresProvider::QgsPostgresProvider(QString uri):dataSourceUri(uri)
322322
} */
323323
// tabledisplay=0;
324324

325+
//set client encoding to unicode because QString uses UTF-8 anyway
326+
#ifdef QGISDEBUG
327+
qWarning("setting client encoding to UNICODE");
328+
#endif
329+
int errcode=PQsetClientEncoding(connection, "UNICODE");
330+
331+
#ifdef QGISDEBUG
332+
if(errcode==0)
333+
{
334+
qWarning("encoding successfully set");
335+
}
336+
else if(errcode==-1)
337+
{
338+
qWarning("error in setting encoding");
339+
}
340+
else
341+
{
342+
qWarning("undefined return value from encoding setting");
343+
}
344+
#endif
345+
325346
//fill type names into lists
326347
mNumericalTypes.push_back("double precision");
327348
mNumericalTypes.push_back("int4");
@@ -727,7 +748,8 @@ void QgsPostgresProvider::getFeatureAttributes(int key, QgsFeature *f){
727748
// results
728749
if(fld != geometryColumn){
729750
// Add the attribute to the feature
730-
QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
751+
//QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
752+
QString val = QString::fromUtf8 (PQgetvalue(attr,0, i));
731753
f->addAttribute(fld, val);
732754
}
733755
}
@@ -755,7 +777,8 @@ void QgsPostgresProvider::getFeatureAttributes(int key,
755777
if(fld != geometryColumn)
756778
{
757779
// Add the attribute to the feature
758-
QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
780+
//QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
781+
QString val = QString::fromUtf8(PQgetvalue(attr,0, i));
759782
//qWarning(val);
760783
f->addAttribute(fld, val);
761784
}

‎src/qgsfeature.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
905905
}
906906

907907
#ifdef QGISDEBUG
908-
qWarning("In QgsFeature::geosGeometry()");
908+
//qWarning("In QgsFeature::geosGeometry()");
909909
#endif
910910

911911
double *x;
@@ -951,7 +951,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
951951
case QGis::WKBLineString:
952952
{
953953
#ifdef QGISDEBUG
954-
qWarning("Linestring found");
954+
//qWarning("Linestring found");
955955
#endif
956956
geos::DefaultCoordinateSequence* sequence=new geos::DefaultCoordinateSequence();
957957
ptr = geometry + 5;
@@ -964,7 +964,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
964964
y = (double *) ptr;
965965
ptr += sizeof(double);
966966
#ifdef QGISDEBUG
967-
qWarning("adding coordinate pair "+QString::number(*x)+"//"+QString::number(*y));
967+
//qWarning("adding coordinate pair "+QString::number(*x)+"//"+QString::number(*y));
968968
#endif
969969
sequence->add(geos::Coordinate(*x,*y));
970970
}
@@ -998,7 +998,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
998998
case QGis::WKBPolygon:
999999
{
10001000
#ifdef QGISDEBUG
1001-
qWarning("Polygon found");
1001+
//qWarning("Polygon found");
10021002
#endif
10031003
// get number of rings in the polygon
10041004
numRings = (int *) (geometry + 1 + sizeof(int));
@@ -1010,7 +1010,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
10101010
for (idx = 0; idx < *numRings; idx++)
10111011
{
10121012
#ifdef QGISDEBUG
1013-
qWarning("Ring nr: "+QString::number(idx));
1013+
//qWarning("Ring nr: "+QString::number(idx));
10141014
#endif
10151015
geos::DefaultCoordinateSequence* sequence=new geos::DefaultCoordinateSequence();
10161016
// get number of points in the ring
@@ -1041,7 +1041,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
10411041
case QGis::WKBMultiPolygon:
10421042
{
10431043
#ifdef QGISDEBUG
1044-
qWarning("Multipolygon found");
1044+
//qWarning("Multipolygon found");
10451045
#endif
10461046
std::vector<geos::Geometry *> *polygons=new std::vector<geos::Geometry *>;
10471047
// get the number of polygons

‎src/qgsvectorlayer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,9 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * th
789789
double scale = markerScaleFactor * symbolScale;
790790
drawFeature(p,fet,theMapToPixelTransform,&marker, scale,
791791
projectionsEnabledFlag);
792+
//test for geos performance
793+
//geos::Geometry* g=fet->geosGeometry();
794+
//delete g;
792795
++featureCount;
793796
delete fet;
794797
}

0 commit comments

Comments
 (0)
Please sign in to comment.