[FEATURE][processing] New algorithms "Extract Z values" and "Extract m values"

These algorithms allow users to convert z or m values present in feature
geometries to attributes in the layer. By default the z/m value from the
first vertex in the feature is extracted, but optionally statistics
can be calculated on ALL the z/m values from the geometry (e.g. calculating
mean/min/max/sum/etc of z values).
This commit is contained in:
Nyall Dawson 2019-01-02 07:01:02 +10:00
parent e99072b161
commit cef9c4eaec
45 changed files with 545 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

View File

@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

View File

@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]

View File

@ -6430,6 +6430,178 @@ tests:
name: expected/force_rhr_multipolys.gml
type: vector
- algorithm: native:extractzvalues
name: Extract z, first value only
params:
COLUMN_PREFIX: z_
INPUT:
name: lines_z.shp
type: vector
SUMMARIES:
- 0
results:
OUTPUT:
name: expected/extract_z_first.shp
type: vector
- algorithm: native:extractzvalues
name: Extract z, all stats
params:
COLUMN_PREFIX: zs_
INPUT:
name: lines_z.shp
type: vector
SUMMARIES:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
results:
OUTPUT:
name: expected/extract_z_all.shp
type: vector
- algorithm: native:extractzvalues
name: Extract z, points
params:
COLUMN_PREFIX: z_
INPUT:
name: custom/pointszm.shp
type: vector
SUMMARIES:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
results:
OUTPUT:
name: expected/extract_z_points.shp
type: vector
- algorithm: native:extractzvalues
name: Extract z, no z values
params:
COLUMN_PREFIX: z_
INPUT:
name: lines.gml|layername=lines
type: vector
SUMMARIES:
- 0
results:
OUTPUT:
name: expected/extract_z_none.shp
type: vector
- algorithm: native:extractmvalues
name: Extract m first only
params:
COLUMN_PREFIX: m_
INPUT:
name: lines_m.shp
type: vector
SUMMARIES:
- 0
results:
OUTPUT:
name: expected/extract_m_first.shp
type: vector
- algorithm: native:extractmvalues
name: Extract m, all stats
params:
COLUMN_PREFIX: ms_
INPUT:
name: lines_m.shp
type: vector
SUMMARIES:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
results:
OUTPUT:
name: expected/extract_m_all.shp
type: vector
- algorithm: native:extractmvalues
name: Extract m, points
params:
COLUMN_PREFIX: mp_
INPUT:
name: custom/pointszm.shp
type: vector
SUMMARIES:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
results:
OUTPUT:
name: expected/extract_m_points.shp
type: vector
- algorithm: native:extractmvalues
name: Extract m, no m values present
params:
COLUMN_PREFIX: m_
INPUT:
name: points.gml|layername=points
type: vector
SUMMARIES:
- 0
results:
OUTPUT:
name: expected/extract_m_none.shp
type: vector
- algorithm: native:splitlinesbylength
name: Split multilines by length
params:

View File

@ -46,6 +46,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmextractbyexpression.cpp
processing/qgsalgorithmextractbyextent.cpp
processing/qgsalgorithmextractbylocation.cpp
processing/qgsalgorithmextractzmvalues.cpp
processing/qgsalgorithmextractvertices.cpp
processing/qgsalgorithmfiledownloader.cpp
processing/qgsalgorithmfilter.cpp

View File

@ -0,0 +1,245 @@
/***************************************************************************
qgsalgorithmextractzmvalues.cpp
---------------------------------
begin : January 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson 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 "qgsalgorithmextractzmvalues.h"
#include "qgsfeaturerequest.h"
#include <vector>
///@cond PRIVATE
const std::vector< QgsStatisticalSummary::Statistic > STATS
{
QgsStatisticalSummary::First,
QgsStatisticalSummary::Last,
QgsStatisticalSummary::Count,
QgsStatisticalSummary::Sum,
QgsStatisticalSummary::Mean,
QgsStatisticalSummary::Median,
QgsStatisticalSummary::StDev,
QgsStatisticalSummary::Min,
QgsStatisticalSummary::Max,
QgsStatisticalSummary::Range,
QgsStatisticalSummary::Minority,
QgsStatisticalSummary::Majority,
QgsStatisticalSummary::Variety,
QgsStatisticalSummary::FirstQuartile,
QgsStatisticalSummary::ThirdQuartile,
QgsStatisticalSummary::InterQuartileRange,
};
QString QgsExtractZMValuesAlgorithmBase::group() const
{
return QObject::tr( "Vector geometry" );
}
QString QgsExtractZMValuesAlgorithmBase::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}
QString QgsExtractZMValuesAlgorithmBase::outputName() const
{
return QObject::tr( "Extracted" );
}
QList<int> QgsExtractZMValuesAlgorithmBase::inputLayerTypes() const
{
return QList<int>() << QgsProcessing::TypeVectorAnyGeometry;
}
QgsProcessingFeatureSource::Flag QgsExtractZMValuesAlgorithmBase::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}
void QgsExtractZMValuesAlgorithmBase::initParameters( const QVariantMap & )
{
QStringList statChoices;
statChoices.reserve( STATS.size() );
for ( QgsStatisticalSummary::Statistic stat : STATS )
{
statChoices << QgsStatisticalSummary::displayName( stat );
}
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SUMMARIES" ),
QObject::tr( "Summaries to calculate" ),
statChoices, true, QVariantList() << 0 ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "COLUMN_PREFIX" ), QObject::tr( "Output column prefix" ), mDefaultFieldPrefix, false, true ) );
}
QgsFields QgsExtractZMValuesAlgorithmBase::outputFields( const QgsFields &inputFields ) const
{
return QgsProcessingUtils::combineFields( inputFields, mNewFields );
}
bool QgsExtractZMValuesAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mPrefix = parameterAsString( parameters, QStringLiteral( "COLUMN_PREFIX" ), context );
const QList< int > stats = parameterAsEnums( parameters, QStringLiteral( "SUMMARIES" ), context );
mStats = nullptr;
for ( int s : stats )
{
mStats |= STATS.at( s );
mSelectedStats << STATS.at( s );
mNewFields.append( QgsField( mPrefix + QgsStatisticalSummary::shortName( STATS.at( s ) ), STATS.at( s ) == QgsStatisticalSummary::Count || STATS.at( s ) == QgsStatisticalSummary::Variety ? QVariant::Int : QVariant::Double ) );
}
return true;
}
QgsFeatureList QgsExtractZMValuesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
{
QgsFeature f = feature;
QgsAttributes attrs = f.attributes();
attrs.reserve( attrs.count() + mSelectedStats.count() );
if ( !f.hasGeometry() || f.geometry().isEmpty() || !mTestGeomFunc( f.geometry() ) )
{
attrs.resize( attrs.count() + mNewFields.size() );
}
else
{
const QgsGeometry g = f.geometry();
QgsStatisticalSummary stat( mStats );
for ( auto it = g.vertices_begin(); it != g.vertices_end(); ++it )
{
stat.addValue( mExtractValFunc( *it ) );
if ( mStats == QgsStatisticalSummary::First )
{
// only retrieving first vertex info (default behavior), so short cut and
// don't iterate remaining vertices
break;
}
}
stat.finalize();
for ( QgsStatisticalSummary::Statistic s : qgis::as_const( mSelectedStats ) )
attrs.append( stat.statistic( s ) );
}
f.setAttributes( attrs );
return QgsFeatureList() << f;
}
bool QgsExtractZMValuesAlgorithmBase::supportInPlaceEdit( const QgsMapLayer *layer ) const
{
Q_UNUSED( layer );
return false;
}
//
// QgsExtractZValuesAlgorithm
//
QgsExtractZValuesAlgorithm::QgsExtractZValuesAlgorithm()
{
mExtractValFunc = []( const QgsPoint & p ) -> double
{
return p.z();
};
mTestGeomFunc = []( const QgsGeometry & g ) -> bool
{
return QgsWkbTypes::hasZ( g.wkbType() );
};
mDefaultFieldPrefix = QStringLiteral( "z_" );
}
QString QgsExtractZValuesAlgorithm::name() const
{
return QStringLiteral( "extractzvalues" );
}
QString QgsExtractZValuesAlgorithm::displayName() const
{
return QObject::tr( "Extract Z values" );
}
QgsProcessingAlgorithm *QgsExtractZValuesAlgorithm::createInstance() const
{
return new QgsExtractZValuesAlgorithm();
}
QStringList QgsExtractZValuesAlgorithm::tags() const
{
return QObject::tr( "add,z,value,elevation,height,attribute,statistics,stats" ).split( ',' );
}
QString QgsExtractZValuesAlgorithm::shortHelpString() const
{
return QObject::tr( "Extracts z values from geometries into feature attributes.\n\n"
"By default only the z value from the first vertex of each feature is extracted, however the algorithm "
"can optionally calculate statistics on all of the geometry's z values, including sums, means, and minimums and maximums" );
}
QString QgsExtractZValuesAlgorithm::shortDescription() const
{
return QObject::tr( "Extracts z values (or z value statistics) from geometries into feature attributes." );
}
//
// QgsExtractMValuesAlgorithm
//
QgsExtractMValuesAlgorithm::QgsExtractMValuesAlgorithm()
{
mExtractValFunc = []( const QgsPoint & p ) -> double
{
return p.m();
};
mTestGeomFunc = []( const QgsGeometry & g ) -> bool
{
return QgsWkbTypes::hasM( g.wkbType() );
};
mDefaultFieldPrefix = QStringLiteral( "m_" );
}
QString QgsExtractMValuesAlgorithm::name() const
{
return QStringLiteral( "extractmvalues" );
}
QString QgsExtractMValuesAlgorithm::displayName() const
{
return QObject::tr( "Extract M values" );
}
QgsProcessingAlgorithm *QgsExtractMValuesAlgorithm::createInstance() const
{
return new QgsExtractMValuesAlgorithm();
}
QStringList QgsExtractMValuesAlgorithm::tags() const
{
return QObject::tr( "add,m,value,measure,attribute,statistics,stats" ).split( ',' );
}
QString QgsExtractMValuesAlgorithm::shortHelpString() const
{
return QObject::tr( "Extracts m values from geometries into feature attributes.\n\n"
"By default only the m value from the first vertex of each feature is extracted, however the algorithm "
"can optionally calculate statistics on all of the geometry's m values, including sums, means, and minimums and maximums" );
}
QString QgsExtractMValuesAlgorithm::shortDescription() const
{
return QObject::tr( "Extracts m values (or m value statistics) from geometries into feature attributes." );
}
///@endcond

View File

@ -0,0 +1,108 @@
/***************************************************************************
qgsalgorithmextractzmvalues.h
---------------------------------
begin : January 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson 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 QGSALGORITHMEXTRACTZMVALUES_H
#define QGSALGORITHMEXTRACTZMVALUES_H
#define SIP_NO_FILE
#include "qgis.h"
#include "qgsprocessingalgorithm.h"
#include "qgsstatisticalsummary.h"
#include <functional>
///@cond PRIVATE
/**
* Native add incremental field algorithm.
*/
class QgsExtractZMValuesAlgorithmBase : public QgsProcessingFeatureBasedAlgorithm
{
public:
QgsExtractZMValuesAlgorithmBase() = default;
QString group() const override;
QString groupId() const override;
QList<int> inputLayerTypes() const override;
protected:
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
QString outputName() const override;
QgsFields outputFields( const QgsFields &inputFields ) const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
protected:
std::function<double( const QgsPoint & )> mExtractValFunc;
std::function<bool( const QgsGeometry & )> mTestGeomFunc;
QString mDefaultFieldPrefix;
private:
QList< QgsStatisticalSummary::Statistic > mSelectedStats;
QgsStatisticalSummary::Statistics mStats = QgsStatisticalSummary::All;
QString mPrefix;
QgsFields mNewFields;
};
/**
* Native extract z values algorithm.
*/
class QgsExtractZValuesAlgorithm : public QgsExtractZMValuesAlgorithmBase
{
public:
QgsExtractZValuesAlgorithm();
QString name() const override;
QString displayName() const override;
QgsProcessingAlgorithm *createInstance() const override;
QStringList tags() const override;
QString shortHelpString() const override;
QString shortDescription() const override;
};
/**
* Native extract m values algorithm.
*/
class QgsExtractMValuesAlgorithm : public QgsExtractZMValuesAlgorithmBase
{
public:
QgsExtractMValuesAlgorithm();
QString name() const override;
QString displayName() const override;
QgsProcessingAlgorithm *createInstance() const override;
QStringList tags() const override;
QString shortHelpString() const override;
QString shortDescription() const override;
};
///@endcond PRIVATE
#endif // QGSALGORITHMEXTRACTZMVALUES_H

View File

@ -42,6 +42,7 @@
#include "qgsalgorithmextractbyextent.h"
#include "qgsalgorithmextractbylocation.h"
#include "qgsalgorithmextractvertices.h"
#include "qgsalgorithmextractzmvalues.h"
#include "qgsalgorithmfiledownloader.h"
#include "qgsalgorithmfilter.h"
#include "qgsalgorithmfiltervertices.h"
@ -171,7 +172,9 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsExtractByExpressionAlgorithm() );
addAlgorithm( new QgsExtractByExtentAlgorithm() );
addAlgorithm( new QgsExtractByLocationAlgorithm() );
addAlgorithm( new QgsExtractMValuesAlgorithm() );
addAlgorithm( new QgsExtractVerticesAlgorithm() );
addAlgorithm( new QgsExtractZValuesAlgorithm() );
addAlgorithm( new QgsFileDownloaderAlgorithm() );
addAlgorithm( new QgsFilterAlgorithm() );
addAlgorithm( new QgsFilterVerticesByM() );