Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][processing] new algorithm point to layer
  • Loading branch information
olivierdalang authored and nyalldawson committed Jun 21, 2019
1 parent acda364 commit 8f629ca
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ point_to_layer.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>1000000</gml:X><gml:Y>1550000</gml:Y></gml:coord>
<gml:coord><gml:X>1000000</gml:X><gml:Y>1550000</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:point_to_layer fid="point_to_layer.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:3857"><gml:coordinates>1000000,1550000</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>1</ogr:id>
</ogr:point_to_layer>
</gml:featureMember>
</ogr:FeatureCollection>
9 changes: 9 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml 100755 → 100644
Expand Up @@ -1847,6 +1847,15 @@ tests:
name: expected/polygon_from_extent_rounded_2.gml
type: vector

- algorithm: native:pointtolayer
name: Test (native:pointtolayer)
params:
INPUT: 1000000.000000,1550000.000000 [EPSG:3857]
results:
OUTPUT:
name: expected/point_to_layer.gml
type: vector

- algorithm: qgis:climbalongline
name: Climb line layer with DTM
params:
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -80,6 +80,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmarrayoffsetlines.cpp
processing/qgsalgorithmpolygonstolines.cpp
processing/qgsalgorithmpointonsurface.cpp
processing/qgsalgorithmpointtolayer.cpp
processing/qgsalgorithmpointsalonggeometry.cpp
processing/qgsalgorithmprojectpointcartesian.cpp
processing/qgsalgorithmpromotetomultipart.cpp
Expand Down
71 changes: 71 additions & 0 deletions src/analysis/processing/qgsalgorithmpointtolayer.cpp
@@ -0,0 +1,71 @@
/***************************************************************************
qgsalgorithmpointtolayer.cpp
---------------------
begin : May 2019
copyright : (C) 2017 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 "qgsalgorithmpointtolayer.h"

///@cond PRIVATE

QString QgsPointToLayerAlgorithm::name() const
{
return QStringLiteral( "pointtolayer" );
}

void QgsPointToLayerAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterPoint( QStringLiteral( "INPUT" ), QObject::tr( "Point" ) ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Point" ), QgsProcessing::TypeVectorPolygon ) );
}

QString QgsPointToLayerAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm creates a new vector layer that contains a single feature with geometry matching a point parameter.\n\n"
"It can be used in models to convert a point into a layer which can be used for other algorithms which require "
"a layer based input." );
}

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

QVariantMap QgsPointToLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsCoordinateReferenceSystem crs = parameterAsPointCrs( parameters, QStringLiteral( "INPUT" ), context );
QgsGeometry geom = QgsGeometry::fromPointXY( parameterAsPoint( parameters, QStringLiteral( "INPUT" ), context ) );

QgsFields fields;
fields.append( QgsField( QStringLiteral( "id" ), QVariant::Int ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Point, crs ) );
if ( !sink )
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );

QgsFeature f;
f.setAttributes( QgsAttributes() << 1 );
f.setGeometry( geom );
sink->addFeature( f, QgsFeatureSink::FastInsert );

feedback->setProgress( 100 );

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}

///@endcond

57 changes: 57 additions & 0 deletions src/analysis/processing/qgsalgorithmpointtolayer.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgsalgorithmpointtolayer.h
---------------------
begin : May 2019
copyright : (C) 2017 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 QGSALGORITHMPOINTTOLAYER_H
#define QGSALGORITHMPOINTTOLAYER_H

#define SIP_NO_FILE

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

///@cond PRIVATE

/**
* Native point to layer algorithm.
*/
class QgsPointToLayerAlgorithm : public QgsProcessingAlgorithm
{

public:

QgsPointToLayerAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override { return QObject::tr( "Create layer from point" ); }
QStringList tags() const override { return QObject::tr( "point,layer,polygon,create,new" ).split( ',' ); }
QString group() const override { return QObject::tr( "Vector geometry" ); }
QString groupId() const override { return QStringLiteral( "vectorgeometry" ); }
QString shortHelpString() const override;
QgsPointToLayerAlgorithm *createInstance() const override SIP_FACTORY;

protected:

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

};

///@endcond PRIVATE

#endif // QGSALGORITHMPOINTTOLAYER_H


2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -74,6 +74,7 @@
#include "qgsalgorithmpackage.h"
#include "qgsalgorithmarrayoffsetlines.h"
#include "qgsalgorithmpointonsurface.h"
#include "qgsalgorithmpointtolayer.h"
#include "qgsalgorithmpointsalonggeometry.h"
#include "qgsalgorithmprojectpointcartesian.h"
#include "qgsalgorithmpromotetomultipart.h"
Expand Down Expand Up @@ -215,6 +216,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsPackageAlgorithm() );
addAlgorithm( new QgsCreateArrayOffsetLinesAlgorithm() );
addAlgorithm( new QgsPointOnSurfaceAlgorithm() );
addAlgorithm( new QgsPointToLayerAlgorithm() );
addAlgorithm( new QgsPointsAlongGeometryAlgorithm() );
addAlgorithm( new QgsProjectPointCartesianAlgorithm() );
addAlgorithm( new QgsPromoteToMultipartAlgorithm() );
Expand Down

0 comments on commit 8f629ca

Please sign in to comment.