QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsinternalgeometryengine.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsinternalgeometryengine.cpp - QgsInternalGeometryEngine
3 
4  ---------------------
5  begin : 13.1.2016
6  copyright : (C) 2016 by Matthias Kuhn
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 
19 #include "qgslinestringv2.h"
20 #include "qgsmultipolygonv2.h"
21 #include "qgspolygonv2.h"
22 #include "qgsmulticurvev2.h"
23 
24 #include <QTransform>
25 
27  : mGeometry( geometry.geometry() )
28 {
29 
30 }
31 
32 /***************************************************************************
33  * This class is considered CRITICAL and any change MUST be accompanied with
34  * full unit tests.
35  * See details in QEP #17
36  ****************************************************************************/
37 
39 {
40  QList<QgsLineStringV2*> linesToProcess;
41 
42  const QgsMultiCurveV2* multiCurve = dynamic_cast< const QgsMultiCurveV2* >( mGeometry );
43  if ( multiCurve )
44  {
45  for ( int i = 0; i < multiCurve->partCount(); ++i )
46  {
47  linesToProcess << static_cast<QgsLineStringV2*>( multiCurve->geometryN( i )->clone() );
48  }
49  }
50 
51  const QgsCurveV2* curve = dynamic_cast< const QgsCurveV2* >( mGeometry );
52  if ( curve )
53  {
54  linesToProcess << static_cast<QgsLineStringV2*>( curve->segmentize() );
55  }
56 
57  QgsMultiPolygonV2 *multipolygon = linesToProcess.size() > 1 ? new QgsMultiPolygonV2() : nullptr;
58  QgsPolygonV2 *polygon = nullptr;
59 
60  if ( !linesToProcess.empty() )
61  {
62  Q_FOREACH ( QgsLineStringV2* line, linesToProcess )
63  {
64  QTransform transform = QTransform::fromTranslate( x, y );
65 
66  QgsLineStringV2* secondline = line->reversed();
67  secondline->transform( transform );
68 
69  line->append( secondline );
70  line->addVertex( line->pointN( 0 ) );
71 
72  polygon = new QgsPolygonV2();
73  polygon->setExteriorRing( line );
74 
75  if ( multipolygon )
76  multipolygon->addGeometry( polygon );
77 
78  delete secondline;
79  }
80 
81  if ( multipolygon )
82  return QgsGeometry( multipolygon );
83  else
84  return QgsGeometry( polygon );
85  }
86 
87  return QgsGeometry();
88 }
QTransform fromTranslate(qreal dx, qreal dy)
QgsInternalGeometryEngine(const QgsGeometry &geometry)
The caller is responsible that the geometry is available and unchanged for the whole lifetime of this...
Multi curve geometry collection.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
void append(const QgsLineStringV2 *line)
Appends the contents of another line string to the end of this line string.
int size() const
Polygon geometry type.
Definition: qgspolygonv2.h:29
const QgsAbstractGeometryV2 * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
virtual void setExteriorRing(QgsCurveV2 *ring) override
Sets the exterior ring of the polygon.
bool empty() const
Line string geometry type, with support for z-dimension and m-values.
QgsGeometry extrude(double x, double y)
Will extrude a line or (segmentized) curve by a given offset and return a polygon representation of i...
virtual int partCount() const override
Returns count of parts contained in the geometry.
void addVertex(const QgsPointV2 &pt)
Adds a new vertex to the end of the line string.
void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) override
Transforms the geometry using a coordinate transform.
virtual QgsLineStringV2 * reversed() const override
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
Multi polygon geometry collection.
virtual QgsAbstractGeometryV2 * clone() const =0
Clones the geometry by performing a deep copy.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
QgsPointV2 pointN(int i) const
Returns the specified point from inside the line string.