Skip to content

Commit

Permalink
added a QgsOgrProvider::createSpatialIndex() method. This method crea…
Browse files Browse the repository at this point in the history
…tes 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
  • Loading branch information
mhugent committed May 6, 2005
1 parent 4091dbb commit f0c7438
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
32 changes: 30 additions & 2 deletions providers/ogr/qgsogrprovider.cpp
Expand Up @@ -31,6 +31,7 @@ email : sherman at mrcc.com
#include <cpl_error.h>
#include "ogr_api.h"//only for a test

#include <qfile.h>
#include <qmessagebox.h>
#include <qmap.h>

Expand Down Expand Up @@ -78,7 +79,9 @@ QgsOgrProvider::QgsOgrProvider(QString uri): QgsVectorDataProvider(), dataSource
std::cerr << "Data source is valid" << std::endl;
#endif
valid = true;

ogrLayer = ogrDataSource->GetLayer(0);

// get the extent_ (envelope) of the layer
#ifdef QGISDEBUG
std::cerr << "Starting get extent\n";
Expand Down Expand Up @@ -122,6 +125,8 @@ QgsOgrProvider::QgsOgrProvider(QString uri): QgsVectorDataProvider(), dataSource
valid = false;
}

//createSpatialIndex();

//resize the cache matrix
minmaxcache=new double*[fieldCount()];
for(int i=0;i<fieldCount();i++)
Expand Down Expand Up @@ -571,8 +576,9 @@ void QgsOgrProvider::select(QgsRect *rect, bool useIntersect)
//TODO about it. If setting the filter fails, all records will be returned
if (result == OGRERR_NONE)
{
std::cerr << "Setting spatial filter using " << wktExtent << std::endl;
ogrLayer->SetSpatialFilter(filter);
std::cerr << "Setting spatial filter using " << wktExtent << std::endl;
//ogrLayer->SetSpatialFilter(filter);
ogrLayer->SetSpatialFilterRect(rect->xMin(), rect->yMin(), rect->xMax(), rect->yMax());
std::cerr << "Feature count: " << ogrLayer->GetFeatureCount() << std::endl;
}else{
#ifdef QGISDEBUG
Expand Down Expand Up @@ -1070,6 +1076,28 @@ bool QgsOgrProvider::changeAttributeValues(std::map<int,std::map<QString,QString
return true;
}

bool QgsOgrProvider::createSpatialIndex()
{
//experimental, try to create a spatial index
QString filename=dataSourceUri.section('/',-1,-1);//todo: find out the filename from the uri
QString layername=filename.section('.',0,0);
QString sql="CREATE SPATIAL INDEX ON "+layername;
ogrDataSource->ExecuteSQL (sql.ascii(), ogrLayer->GetSpatialFilter(),"");
//todo: find out, if the .qix file is there
QString indexname=dataSourceUri;
indexname.truncate(dataSourceUri.length()-filename.length());
indexname=indexname+layername+".qix";
QFile indexfile(indexname);
if(indexfile.exists())
{
return true;
}
else
{
return false;
}
}

int QgsOgrProvider::capabilities() const
{
return (QgsVectorDataProvider::AddFeatures
Expand Down
4 changes: 4 additions & 0 deletions providers/ogr/qgsogrprovider.h
Expand Up @@ -154,6 +154,10 @@ class QgsOgrProvider:public QgsVectorDataProvider
return 0;
}

/**Tries to create a .qix index file for faster access if only a subset of the features is required
@return true in case of success*/
bool createSpatialIndex();

/**Returns a bitmask containing the supported capabilities*/
int capabilities() const;

Expand Down
27 changes: 25 additions & 2 deletions providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -322,6 +322,27 @@ QgsPostgresProvider::QgsPostgresProvider(QString uri):dataSourceUri(uri)
} */
// tabledisplay=0;

//set client encoding to unicode because QString uses UTF-8 anyway
#ifdef QGISDEBUG
qWarning("setting client encoding to UNICODE");
#endif
int errcode=PQsetClientEncoding(connection, "UNICODE");

#ifdef QGISDEBUG
if(errcode==0)
{
qWarning("encoding successfully set");
}
else if(errcode==-1)
{
qWarning("error in setting encoding");
}
else
{
qWarning("undefined return value from encoding setting");
}
#endif

//fill type names into lists
mNumericalTypes.push_back("double precision");
mNumericalTypes.push_back("int4");
Expand Down Expand Up @@ -727,7 +748,8 @@ void QgsPostgresProvider::getFeatureAttributes(int key, QgsFeature *f){
// results
if(fld != geometryColumn){
// Add the attribute to the feature
QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
//QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
QString val = QString::fromUtf8 (PQgetvalue(attr,0, i));
f->addAttribute(fld, val);
}
}
Expand Down Expand Up @@ -755,7 +777,8 @@ void QgsPostgresProvider::getFeatureAttributes(int key,
if(fld != geometryColumn)
{
// Add the attribute to the feature
QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
//QString val = mEncoding->toUnicode(PQgetvalue(attr,0, i));
QString val = QString::fromUtf8(PQgetvalue(attr,0, i));
//qWarning(val);
f->addAttribute(fld, val);
}
Expand Down
12 changes: 6 additions & 6 deletions src/qgsfeature.cpp
Expand Up @@ -905,7 +905,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
}

#ifdef QGISDEBUG
qWarning("In QgsFeature::geosGeometry()");
//qWarning("In QgsFeature::geosGeometry()");
#endif

double *x;
Expand Down Expand Up @@ -951,7 +951,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
case QGis::WKBLineString:
{
#ifdef QGISDEBUG
qWarning("Linestring found");
//qWarning("Linestring found");
#endif
geos::DefaultCoordinateSequence* sequence=new geos::DefaultCoordinateSequence();
ptr = geometry + 5;
Expand All @@ -964,7 +964,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
y = (double *) ptr;
ptr += sizeof(double);
#ifdef QGISDEBUG
qWarning("adding coordinate pair "+QString::number(*x)+"//"+QString::number(*y));
//qWarning("adding coordinate pair "+QString::number(*x)+"//"+QString::number(*y));
#endif
sequence->add(geos::Coordinate(*x,*y));
}
Expand Down Expand Up @@ -998,7 +998,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
case QGis::WKBPolygon:
{
#ifdef QGISDEBUG
qWarning("Polygon found");
//qWarning("Polygon found");
#endif
// get number of rings in the polygon
numRings = (int *) (geometry + 1 + sizeof(int));
Expand All @@ -1010,7 +1010,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
for (idx = 0; idx < *numRings; idx++)
{
#ifdef QGISDEBUG
qWarning("Ring nr: "+QString::number(idx));
//qWarning("Ring nr: "+QString::number(idx));
#endif
geos::DefaultCoordinateSequence* sequence=new geos::DefaultCoordinateSequence();
// get number of points in the ring
Expand Down Expand Up @@ -1041,7 +1041,7 @@ geos::Geometry* QgsFeature::geosGeometry() const
case QGis::WKBMultiPolygon:
{
#ifdef QGISDEBUG
qWarning("Multipolygon found");
//qWarning("Multipolygon found");
#endif
std::vector<geos::Geometry *> *polygons=new std::vector<geos::Geometry *>;
// get the number of polygons
Expand Down
3 changes: 3 additions & 0 deletions src/qgsvectorlayer.cpp
Expand Up @@ -789,6 +789,9 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * th
double scale = markerScaleFactor * symbolScale;
drawFeature(p,fet,theMapToPixelTransform,&marker, scale,
projectionsEnabledFlag);
//test for geos performance
//geos::Geometry* g=fet->geosGeometry();
//delete g;
++featureCount;
delete fet;
}
Expand Down

0 comments on commit f0c7438

Please sign in to comment.