Skip to content

Commit

Permalink
[processing] port spatial index algorithm to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy authored and nyalldawson committed Nov 25, 2019
1 parent 7033c31 commit aed17ba
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 83 deletions.
3 changes: 0 additions & 3 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -93,9 +93,6 @@ qgis:countpointsinpolygon: >
qgis:creategrid: >
This algorithm creates a vector layer with a grid covering a given extent. Elements in the grid can be points, lines or polygons.The size and/or placement of each element in the grid is defined using a horizontal and vertical spacing. The CRS of the output layer must be defined. The grid extent and the spacing values must be expressed in the coordinates and units of this CRS. The top-left point (minX, maxY) is used as the reference point. That means that, at that point, an element is guaranteed to be placed. Unless the width and height of the selected extent is a multiple of the selected spacing, that is not true for the other points that define that extent.

qgis:createspatialindex: >
Creates an index to speed up access to the features in a layer based on their spatial location. Support for spatial index creation is dependent on the layer's data provider.

qgis:createpointslayerfromtable: >
This algorithm creates points layer from a table with columns that contain coordinates fields. Besides X and Y coordinates you can also specify Z and M fields.

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -103,7 +103,6 @@
from .SingleSidedBuffer import SingleSidedBuffer
from .SnapGeometries import SnapGeometriesToLayer
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
from .SpatialIndex import SpatialIndex
from .SpatialJoin import SpatialJoin
from .SpatialJoinSummary import SpatialJoinSummary
from .StatisticsByCategories import StatisticsByCategories
Expand Down Expand Up @@ -207,7 +206,6 @@ def getAlgs(self):
SingleSidedBuffer(),
SnapGeometriesToLayer(),
SpatialiteExecuteSQL(),
SpatialIndex(),
SpatialJoin(),
SpatialJoinSummary(),
StatisticsByCategories(),
Expand Down
77 changes: 0 additions & 77 deletions python/plugins/processing/algs/qgis/SpatialIndex.py

This file was deleted.

Expand Up @@ -1919,7 +1919,7 @@ tests:
type: vector
in_place_result: true

- algorithm: qgis:createspatialindex
- algorithm: native:createspatialindex
name: Create spatial index
params:
INPUT:
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -116,6 +116,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmslope.cpp
processing/qgsalgorithmsmooth.cpp
processing/qgsalgorithmsnaptogrid.cpp
processing/qgsalgorithmspatialindex.cpp
processing/qgsalgorithmsplitfeaturesbyattributecharacter.cpp
processing/qgsalgorithmsplitlineantimeridian.cpp
processing/qgsalgorithmsplitlinesbylength.cpp
Expand Down
101 changes: 101 additions & 0 deletions src/analysis/processing/qgsalgorithmspatialindex.cpp
@@ -0,0 +1,101 @@
/***************************************************************************
qgsalgorithmspatialraster.cpp
---------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsalgorithmspatialindex.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"

///@cond PRIVATE

QString QgsSpatialIndexAlgorithm::name() const
{
return QStringLiteral( "createspatialindex" );
}

QString QgsSpatialIndexAlgorithm::displayName() const
{
return QObject::tr( "Create spatial index" );
}

QStringList QgsSpatialIndexAlgorithm::tags() const
{
return QObject::tr( "table,spatial,geometry,index,create,vector" ).split( ',' );
}

QString QgsSpatialIndexAlgorithm::group() const
{
return QObject::tr( "Vector general" );
}

QString QgsSpatialIndexAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeneral" );
}

QgsProcessingAlgorithm::Flags QgsSpatialIndexAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading;
}


QString QgsSpatialIndexAlgorithm::shortHelpString() const
{
return QObject::tr( "Creates an index to speed up access to the features "
"in a layer based on their spatial location. Support "
"for spatial index creation is dependent on the layer's "
"data provider." );
}

QgsSpatialIndexAlgorithm *QgsSpatialIndexAlgorithm::createInstance() const
{
return new QgsSpatialIndexAlgorithm();
}

void QgsSpatialIndexAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );

addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Indexed layer" ) ) );
}

QVariantMap QgsSpatialIndexAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );

if ( !layer )
throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QStringLiteral( "INPUT" ) ) );

QgsVectorDataProvider *provider = layer->dataProvider();

if ( provider->capabilities() & QgsVectorDataProvider::CreateSpatialIndex )
{
if ( !provider->createSpatialIndex() )
{
feedback->pushInfo( QObject::tr( "Could not create spatial index" ) );
}
}
else
{
feedback->pushInfo( QObject::tr( "Layer's data provider does not support spatial indexes" ) );
}

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
return outputs;
}

///@endcond
55 changes: 55 additions & 0 deletions src/analysis/processing/qgsalgorithmspatialindex.h
@@ -0,0 +1,55 @@
/***************************************************************************
qgsalgorithmspatialindex.h
------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSALGORITHMSPATIALINDEX_H
#define QGSALGORITHMSPATIALINDEX_H

#define SIP_NO_FILE

#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"

///@cond PRIVATE

/**
* Native constant raster algorithm.
*/
class QgsSpatialIndexAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsSpatialIndexAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
Flags flags() const override;
QString shortHelpString() const override;
QgsSpatialIndexAlgorithm *createInstance() const override SIP_FACTORY;

protected:

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};

///@endcond PRIVATE

#endif // QGSALGORITHMSPATIALINDEX_H
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -110,6 +110,7 @@
#include "qgsalgorithmslope.h"
#include "qgsalgorithmsmooth.h"
#include "qgsalgorithmsnaptogrid.h"
#include "qgsalgorithmspatialindex.h"
#include "qgsalgorithmsplitlineantimeridian.h"
#include "qgsalgorithmsplitlinesbylength.h"
#include "qgsalgorithmsplitwithlines.h"
Expand Down Expand Up @@ -281,6 +282,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsSlopeAlgorithm() );
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSnapToGridAlgorithm() );
addAlgorithm( new QgsSpatialIndexAlgorithm() );
addAlgorithm( new QgsSplitFeaturesByAttributeCharacterAlgorithm() );
addAlgorithm( new QgsSplitGeometryAtAntimeridianAlgorithm() );
addAlgorithm( new QgsSplitLinesByLengthAlgorithm() );
Expand Down

0 comments on commit aed17ba

Please sign in to comment.