Skip to content
Permalink
Browse files

Merge pull request #33820 from kadas-albireo/kml

Add QgsAbstractGeometry::asKml
  • Loading branch information
manisandro committed Jan 16, 2020
2 parents fa568a5 + 6c32a6a commit f39d53bf715b0b697840b7f6a7000d89dc402c2c
@@ -273,6 +273,13 @@ Returns a GeoJSON representation of the geometry as a QString.
%End


virtual QString asKml( int precision = 17 ) const = 0;
%Docstring
Returns a KML representation of the geometry.

.. versionadded:: 3.12
%End



virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) throw( QgsCsException ) = 0;
@@ -150,6 +150,9 @@ Returns a reversed copy of the curve, where the direction of the curve has been
virtual QgsAbstractGeometry *boundary() const /Factory/;


virtual QString asKml( int precision = 17 ) const;


virtual QgsCurve *segmentize( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const /Factory/;

%Docstring
@@ -54,6 +54,8 @@ Curve polygon geometry type

virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;

virtual QString asKml( int precision = 17 ) const;


virtual double area() const;

@@ -162,6 +162,8 @@ An IndexError will be raised if no geometry with the specified index exists.

virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;

virtual QString asKml( int precision = 17 ) const;


virtual QgsRectangle boundingBox() const;

@@ -431,6 +431,8 @@ segment in the line.

virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;

virtual QString asKml( int precision = 17 ) const;


virtual double length() const;

@@ -369,6 +369,8 @@ M value is preserved.

virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;

virtual QString asKml( int precision = 17 ) const;

virtual void draw( QPainter &p ) const;

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) throw( QgsCsException );
@@ -307,6 +307,12 @@ class CORE_EXPORT QgsAbstractGeometry
*/
virtual json asJsonObject( int precision = 17 ) SIP_SKIP const;

/**
* Returns a KML representation of the geometry.
* \since QGIS 3.12
*/
virtual QString asKml( int precision = 17 ) const = 0;


//render pipeline

@@ -145,6 +145,17 @@ QgsAbstractGeometry *QgsCurve::boundary() const
return multiPoint;
}

QString QgsCurve::asKml( int precision ) const
{
std::unique_ptr<QgsLineString> lineString( curveToLine() );
if ( !lineString )
{
return QString();
}
QString kml = lineString->asKml( precision );
return kml;
}

QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
{
return curveToLine( tolerance, toleranceType );
@@ -149,6 +149,8 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry

QgsAbstractGeometry *boundary() const override SIP_FACTORY;

QString asKml( int precision = 17 ) const override;

/**
* Returns a geometry without curves. Caller takes ownership
* \param tolerance segmentation tolerance
@@ -437,6 +437,27 @@ json QgsCurvePolygon::asJsonObject( int precision ) const
};
}

QString QgsCurvePolygon::asKml( int precision ) const
{
QString kml;
kml.append( QLatin1String( "<Polygon>" ) );
if ( mExteriorRing )
{
kml.append( QLatin1String( "<outerBoundaryIs>" ) );
kml.append( mExteriorRing->asKml( precision ) );
kml.append( QLatin1String( "</outerBoundaryIs>" ) );
}
const QVector<QgsCurve *> &interiorRings = mInteriorRings;
for ( const QgsCurve *ring : interiorRings )
{
kml.append( QLatin1String( "<innerBoundaryIs>" ) );
kml.append( ring->asKml( precision ) );
kml.append( QLatin1String( "</innerBoundaryIs>" ) );
}
kml.append( QLatin1String( "</Polygon>" ) );
return kml;
}

double QgsCurvePolygon::area() const
{
if ( !mExteriorRing )
@@ -56,6 +56,7 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;
QString asKml( int precision = 17 ) const override;

//surface interface
double area() const override;
@@ -445,6 +445,19 @@ json QgsGeometryCollection::asJsonObject( int precision ) const
};
}

QString QgsGeometryCollection::asKml( int precision ) const
{
QString kml;
kml.append( QLatin1String( "<MultiGeometry>" ) );
const QVector< QgsAbstractGeometry * > &geometries = mGeometries;
for ( const QgsAbstractGeometry *geometry : geometries )
{
kml.append( geometry->asKml( precision ) );
}
kml.append( QLatin1String( "</MultiGeometry>" ) );
return kml;
}

QgsRectangle QgsGeometryCollection::boundingBox() const
{
if ( mBoundingBox.isNull() )
@@ -184,6 +184,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;
QString asKml( int precision = 17 ) const override;

QgsRectangle boundingBox() const override;

@@ -518,6 +518,62 @@ json QgsLineString::asJsonObject( int precision ) const
};
}

QString QgsLineString::asKml( int precision ) const
{
QString kml;
if ( isRing() )
{
kml.append( QLatin1String( "<LinearRing>" ) );
}
else
{
kml.append( QLatin1String( "<LineString>" ) );
}
bool z = is3D();
kml.append( QLatin1String( "<altitudeMode>" ) );
if ( z )
{
kml.append( QLatin1String( "absolute" ) );
}
else
{
kml.append( QLatin1String( "clampToGround" ) );
}
kml.append( QLatin1String( "</altitudeMode>" ) );
kml.append( QLatin1String( "<coordinates>" ) );

int nPoints = mX.size();
for ( int i = 0; i < nPoints; ++i )
{
if ( i > 0 )
{
kml.append( QLatin1String( " " ) );
}
kml.append( qgsDoubleToString( mX[i], precision ) );
kml.append( QLatin1String( "," ) );
kml.append( qgsDoubleToString( mY[i], precision ) );
if ( z )
{
kml.append( QLatin1String( "," ) );
kml.append( qgsDoubleToString( mZ[i], precision ) );
}
else
{
kml.append( QLatin1String( ",0" ) );
}
}
kml.append( QLatin1String( "</coordinates>" ) );
if ( isRing() )
{
kml.append( QLatin1String( "</LinearRing>" ) );
}
else
{
kml.append( QLatin1String( "</LineString>" ) );
}
return kml;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests.
@@ -580,6 +580,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;
QString asKml( int precision = 17 ) const override;

//curve interface
double length() const override;
@@ -306,6 +306,11 @@ json QgsPoint::asJsonObject( int precision ) const
return j;
}

QString QgsPoint::asKml( int precision ) const
{
return QStringLiteral( "<Point><coordinates>%1,%2</coordinates></Point>" ).arg( qgsDoubleToString( mX, precision ), qgsDoubleToString( mY, precision ) );
}

void QgsPoint::draw( QPainter &p ) const
{
p.drawRect( QRectF( mX - 2, mY - 2, 4, 4 ) );
@@ -488,6 +488,7 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;
QString asKml( int precision = 17 ) const override;
void draw( QPainter &p ) const override;
void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) override SIP_THROW( QgsCsException );
void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;

0 comments on commit f39d53b

Please sign in to comment.
You can’t perform that action at this time.