mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
Optimize extract nodes algorithm
This commit is contained in:
parent
82644fb37f
commit
7cb15c08b9
@ -1,122 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
ExtractNodes.py
|
||||
---------------------
|
||||
Date : August 2012
|
||||
Copyright : (C) 2012 by Victor Olaya
|
||||
Email : volayaf 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. *
|
||||
* *
|
||||
***************************************************************************
|
||||
"""
|
||||
|
||||
__author__ = 'Victor Olaya'
|
||||
__date__ = 'August 2012'
|
||||
__copyright__ = '(C) 2012, Victor Olaya'
|
||||
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import os
|
||||
import math
|
||||
|
||||
from qgis.PyQt.QtGui import QIcon
|
||||
from qgis.PyQt.QtCore import QVariant
|
||||
|
||||
from qgis.core import (QgsFeature,
|
||||
QgsGeometry,
|
||||
QgsWkbTypes,
|
||||
QgsField,
|
||||
QgsFeatureSink,
|
||||
QgsProcessing,
|
||||
QgsProcessingParameterFeatureSource,
|
||||
QgsProcessingParameterFeatureSink)
|
||||
|
||||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
||||
|
||||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
||||
|
||||
|
||||
class ExtractNodes(QgisAlgorithm):
|
||||
|
||||
INPUT = 'INPUT'
|
||||
OUTPUT = 'OUTPUT'
|
||||
|
||||
def icon(self):
|
||||
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'extract_nodes.png'))
|
||||
|
||||
def group(self):
|
||||
return self.tr('Vector geometry')
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initAlgorithm(self, config=None):
|
||||
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
||||
self.tr('Input layer')))
|
||||
|
||||
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Nodes'), QgsProcessing.TypeVectorPoint))
|
||||
|
||||
def name(self):
|
||||
return 'extractnodes'
|
||||
|
||||
def displayName(self):
|
||||
return self.tr('Extract nodes')
|
||||
|
||||
def processAlgorithm(self, parameters, context, feedback):
|
||||
source = self.parameterAsSource(parameters, self.INPUT, context)
|
||||
|
||||
fields = source.fields()
|
||||
fields.append(QgsField('node_index', QVariant.Int))
|
||||
fields.append(QgsField('distance', QVariant.Double))
|
||||
fields.append(QgsField('angle', QVariant.Double))
|
||||
|
||||
out_wkb = QgsWkbTypes.Point
|
||||
if QgsWkbTypes.hasM(source.wkbType()):
|
||||
out_wkb = QgsWkbTypes.addM(out_wkb)
|
||||
if QgsWkbTypes.hasZ(source.wkbType()):
|
||||
out_wkb = QgsWkbTypes.addZ(out_wkb)
|
||||
|
||||
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
||||
fields, out_wkb, source.sourceCrs())
|
||||
|
||||
features = source.getFeatures()
|
||||
total = 100.0 / source.featureCount() if source.featureCount() else 0
|
||||
for current, f in enumerate(features):
|
||||
if feedback.isCanceled():
|
||||
break
|
||||
|
||||
input_geometry = f.geometry()
|
||||
if not input_geometry:
|
||||
sink.addFeature(f, QgsFeatureSink.FastInsert)
|
||||
else:
|
||||
i = 0
|
||||
for part in input_geometry.constGet().coordinateSequence():
|
||||
for ring in part:
|
||||
if feedback.isCanceled():
|
||||
break
|
||||
|
||||
for point in ring:
|
||||
distance = input_geometry.distanceToVertex(i)
|
||||
angle = math.degrees(input_geometry.angleAtVertex(i))
|
||||
attrs = f.attributes()
|
||||
attrs.append(i)
|
||||
attrs.append(distance)
|
||||
attrs.append(angle)
|
||||
output_feature = QgsFeature()
|
||||
output_feature.setAttributes(attrs)
|
||||
output_feature.setGeometry(QgsGeometry(point.clone()))
|
||||
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
|
||||
i += 1
|
||||
|
||||
feedback.setProgress(int(current * total))
|
||||
|
||||
return {self.OUTPUT: dest_id}
|
@ -64,7 +64,6 @@ from .Explode import Explode
|
||||
from .ExportGeometryInfo import ExportGeometryInfo
|
||||
from .ExtendLines import ExtendLines
|
||||
from .ExtentFromLayer import ExtentFromLayer
|
||||
from .ExtractNodes import ExtractNodes
|
||||
from .ExtractSpecificNodes import ExtractSpecificNodes
|
||||
from .FieldPyculator import FieldsPyculator
|
||||
from .FieldsCalculator import FieldsCalculator
|
||||
@ -190,7 +189,6 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
|
||||
ExportGeometryInfo(),
|
||||
ExtendLines(),
|
||||
ExtentFromLayer(),
|
||||
ExtractNodes(),
|
||||
ExtractSpecificNodes(),
|
||||
FieldsCalculator(),
|
||||
FieldsMapper(),
|
||||
|
@ -35,6 +35,7 @@ SET(QGIS_ANALYSIS_SRCS
|
||||
processing/qgsalgorithmextractbyexpression.cpp
|
||||
processing/qgsalgorithmextractbyextent.cpp
|
||||
processing/qgsalgorithmextractbylocation.cpp
|
||||
processing/qgsalgorithmextractnodes.cpp
|
||||
processing/qgsalgorithmfiledownloader.cpp
|
||||
processing/qgsalgorithmfixgeometries.cpp
|
||||
processing/qgsalgorithmjoinbyattribute.cpp
|
||||
|
137
src/analysis/processing/qgsalgorithmextractnodes.cpp
Normal file
137
src/analysis/processing/qgsalgorithmextractnodes.cpp
Normal file
@ -0,0 +1,137 @@
|
||||
/***************************************************************************
|
||||
qgsalgorithmextractnodes.cpp
|
||||
--------------------------
|
||||
begin : November 2017
|
||||
copyright : (C) 2017 by Mathieu Pellerin
|
||||
email : nirvn dot asia 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 "qgsalgorithmextractnodes.h"
|
||||
|
||||
///@cond PRIVATE
|
||||
|
||||
QString QgsExtractNodesAlgorithm::name() const
|
||||
{
|
||||
return QStringLiteral( "extractnodes" );
|
||||
}
|
||||
|
||||
QString QgsExtractNodesAlgorithm::displayName() const
|
||||
{
|
||||
return QObject::tr( "Extract nodes" );
|
||||
}
|
||||
|
||||
QStringList QgsExtractNodesAlgorithm::tags() const
|
||||
{
|
||||
return QObject::tr( "points,vertex,vertices" ).split( ',' );
|
||||
}
|
||||
|
||||
QString QgsExtractNodesAlgorithm::group() const
|
||||
{
|
||||
return QObject::tr( "Vector geometry" );
|
||||
}
|
||||
|
||||
QString QgsExtractNodesAlgorithm::shortHelpString() const
|
||||
{
|
||||
return QObject::tr( "This algorithm takes a line or polygon layer and generates a point layer with points representing the nodes in the input lines or polygons. The attributes associated to each point are the same ones associated to the line or polygon that the point belongs to." ) +
|
||||
QStringLiteral( "\n\n" ) +
|
||||
QObject::tr( "Additional fields are added to the nodes indicating the node index (beginning at 0), distance along original geometry and bisector angle of node for original geometry." );
|
||||
}
|
||||
|
||||
QgsExtractNodesAlgorithm *QgsExtractNodesAlgorithm::createInstance() const
|
||||
{
|
||||
return new QgsExtractNodesAlgorithm();
|
||||
}
|
||||
|
||||
void QgsExtractNodesAlgorithm::initAlgorithm( const QVariantMap & )
|
||||
{
|
||||
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
|
||||
|
||||
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Nodes" ) ) );
|
||||
}
|
||||
|
||||
QVariantMap QgsExtractNodesAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
|
||||
{
|
||||
std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
|
||||
if ( !featureSource )
|
||||
return QVariantMap();
|
||||
|
||||
QgsWkbTypes::Type outputWkbType = QgsWkbTypes::Point;
|
||||
if ( QgsWkbTypes::hasM( featureSource->wkbType() ) )
|
||||
{
|
||||
outputWkbType = QgsWkbTypes::addM( outputWkbType );
|
||||
}
|
||||
if ( QgsWkbTypes::hasZ( featureSource->wkbType() ) )
|
||||
{
|
||||
outputWkbType = QgsWkbTypes::addZ( outputWkbType );
|
||||
}
|
||||
|
||||
QgsFields outputFields = featureSource->fields();
|
||||
outputFields.append( QgsField( QStringLiteral( "node_index" ), QVariant::Int, QString(), 10, 0 ) );
|
||||
outputFields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double, QString(), 20, 6 ) );
|
||||
outputFields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 6 ) );
|
||||
|
||||
QString dest;
|
||||
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, outputWkbType, featureSource->sourceCrs() ) );
|
||||
if ( !sink )
|
||||
return QVariantMap();
|
||||
|
||||
double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
|
||||
QgsFeatureIterator it = featureSource->getFeatures( QgsFeatureRequest() );
|
||||
QgsFeature f;
|
||||
int i = -1;
|
||||
while ( it.nextFeature( f ) )
|
||||
{
|
||||
i++;
|
||||
if ( feedback->isCanceled() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
QgsGeometry inputGeom = f.geometry();
|
||||
if ( inputGeom.isNull() )
|
||||
{
|
||||
sink->addFeature( f, QgsFeatureSink::FastInsert );
|
||||
}
|
||||
else
|
||||
{
|
||||
const QgsCoordinateSequence sequence = inputGeom.constGet()->coordinateSequence();
|
||||
for ( const QgsRingSequence &part : sequence )
|
||||
{
|
||||
int vertexPos = 0;
|
||||
for ( const QgsPointSequence &ring : part )
|
||||
{
|
||||
for ( int j = 0; j < ring.count(); ++ j )
|
||||
{
|
||||
double distance = inputGeom.distanceToVertex( vertexPos );
|
||||
double angle = inputGeom.angleAtVertex( vertexPos ) * 180 / M_PI_2;
|
||||
QgsAttributes attrs = f.attributes();
|
||||
attrs << vertexPos
|
||||
<< distance
|
||||
<< angle;
|
||||
QgsFeature outputFeature = QgsFeature();
|
||||
outputFeature.setAttributes( attrs );
|
||||
outputFeature.setGeometry( QgsGeometry( ring.at( j ).clone() ) );
|
||||
sink->addFeature( outputFeature, QgsFeatureSink::FastInsert );
|
||||
vertexPos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
feedback->setProgress( i * step );
|
||||
}
|
||||
|
||||
QVariantMap outputs;
|
||||
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
|
||||
return outputs;
|
||||
}
|
||||
|
||||
///@endcond
|
56
src/analysis/processing/qgsalgorithmextractnodes.h
Normal file
56
src/analysis/processing/qgsalgorithmextractnodes.h
Normal file
@ -0,0 +1,56 @@
|
||||
/***************************************************************************
|
||||
qgsalgorithmextractnodes.h
|
||||
-------------------------
|
||||
begin : November 2017
|
||||
copyright : (C) 2017 by Mathieu Pellerin
|
||||
email : nirvn dot asia 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 QGSALGORITHMEXTRACTNODES_H
|
||||
#define QGSALGORITHMEXTRACTNODES_H
|
||||
|
||||
#define SIP_NO_FILE
|
||||
|
||||
#include "qgis.h"
|
||||
#include "qgsprocessingalgorithm.h"
|
||||
|
||||
///@cond PRIVATE
|
||||
|
||||
/**
|
||||
* Native extract nodes algorithm.
|
||||
*/
|
||||
class QgsExtractNodesAlgorithm : public QgsProcessingAlgorithm
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
QgsExtractNodesAlgorithm() = default;
|
||||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
|
||||
QString name() const override;
|
||||
QString displayName() const override;
|
||||
virtual QStringList tags() const override;
|
||||
QString group() const override;
|
||||
QString shortHelpString() const override;
|
||||
QgsExtractNodesAlgorithm *createInstance() const override SIP_FACTORY;
|
||||
|
||||
protected:
|
||||
|
||||
virtual QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
||||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
|
||||
|
||||
};
|
||||
|
||||
///@endcond PRIVATE
|
||||
|
||||
#endif // QGSALGORITHMEXTRACTNODES_H
|
||||
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "qgsalgorithmextractbyexpression.h"
|
||||
#include "qgsalgorithmextractbyextent.h"
|
||||
#include "qgsalgorithmextractbylocation.h"
|
||||
#include "qgsalgorithmextractnodes.h"
|
||||
#include "qgsalgorithmfiledownloader.h"
|
||||
#include "qgsalgorithmfixgeometries.h"
|
||||
#include "qgsalgorithmjoinbyattribute.h"
|
||||
@ -106,6 +107,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
|
||||
addAlgorithm( new QgsExtractByExpressionAlgorithm() );
|
||||
addAlgorithm( new QgsExtractByExtentAlgorithm() );
|
||||
addAlgorithm( new QgsExtractByLocationAlgorithm() );
|
||||
addAlgorithm( new QgsExtractNodesAlgorithm() );
|
||||
addAlgorithm( new QgsFileDownloaderAlgorithm() );
|
||||
addAlgorithm( new QgsFixGeometriesAlgorithm() );
|
||||
addAlgorithm( new QgsJoinByAttributeAlgorithm() );
|
||||
|
Loading…
x
Reference in New Issue
Block a user