Skip to content

Commit

Permalink
toJsonV2
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Apr 16, 2019
1 parent 7222828 commit 6967a21
Show file tree
Hide file tree
Showing 43 changed files with 316 additions and 16 deletions.
13 changes: 13 additions & 0 deletions python/core/auto_generated/geometry/qgsabstractgeometry.sip.in
Expand Up @@ -256,6 +256,19 @@ Returns a GeoJSON representation of the geometry.

.. seealso:: :py:func:`asGml2`

.. seealso:: :py:func:`asGml3`
%End

virtual QJsonObject asJsonV2( ) const = 0;
%Docstring
Returns a GeoJSON object of the geometry.

.. seealso:: :py:func:`asWkb`

.. seealso:: :py:func:`asWkt`

.. seealso:: :py:func:`asGml2`

.. seealso:: :py:func:`asGml3`
%End

Expand Down
Expand Up @@ -81,6 +81,7 @@ to ``p2`` will be used (i.e. winding the other way around the circle).

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual bool isEmpty() const;

Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgscompoundcurve.sip.in
Expand Up @@ -52,6 +52,8 @@ Compound curve geometry type

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;


virtual double length() const;

Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgscurvepolygon.sip.in
Expand Up @@ -56,6 +56,8 @@ Curve polygon geometry type

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2() const;


virtual double area() const;

Expand Down
5 changes: 5 additions & 0 deletions python/core/auto_generated/geometry/qgsgeometry.sip.in
Expand Up @@ -1439,6 +1439,11 @@ Exports the geometry to WKT
QString asJson( int precision = 17 ) const;
%Docstring
Exports the geometry to a GeoJSON string.
%End

QJsonObject asJsonV2( ) const;
%Docstring
Exports the geometry to a GeoJSON object.
%End

QgsGeometry convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart = false ) const /Factory/;
Expand Down
Expand Up @@ -154,6 +154,8 @@ An IndexError will be raised if no geometry with the specified index exists.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;


virtual QgsRectangle boundingBox() const;

Expand Down
Expand Up @@ -375,6 +375,7 @@ Interpolate a value at given angle on circular arc given values (zm1, zm2, zm3)




static double normalizedAngle( double angle );
%Docstring
Ensures that an angle is in the range 0 <= angle < 2 pi.
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgslinestring.sip.in
Expand Up @@ -414,6 +414,8 @@ segment in the line.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;


virtual double length() const;

Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgsmulticurve.sip.in
Expand Up @@ -37,6 +37,8 @@ Multi curve geometry collection.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual bool addGeometry( QgsAbstractGeometry *g /Transfer/ );

virtual bool insertGeometry( QgsAbstractGeometry *g /Transfer/, int index );
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgsmultilinestring.sip.in
Expand Up @@ -36,6 +36,8 @@ Multi line string geometry collection.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual bool addGeometry( QgsAbstractGeometry *g /Transfer/ );

virtual bool insertGeometry( QgsAbstractGeometry *g /Transfer/, int index );
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgsmultipoint.sip.in
Expand Up @@ -38,6 +38,8 @@ Multi point geometry collection.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual int nCoordinates() const;

virtual bool addGeometry( QgsAbstractGeometry *g /Transfer/ );
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgsmultipolygon.sip.in
Expand Up @@ -35,6 +35,8 @@ Multi polygon geometry collection.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual bool addGeometry( QgsAbstractGeometry *g /Transfer/ );

virtual bool insertGeometry( QgsAbstractGeometry *g /Transfer/, int index );
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgsmultisurface.sip.in
Expand Up @@ -37,6 +37,8 @@ Multi surface geometry collection.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual bool addGeometry( QgsAbstractGeometry *g /Transfer/ );

virtual bool insertGeometry( QgsAbstractGeometry *g /Transfer/, int index );
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/geometry/qgspoint.sip.in
Expand Up @@ -357,6 +357,8 @@ M value is preserved.

virtual QString asJson( int precision = 17 ) const;

virtual QJsonObject asJsonV2( ) const;

virtual void draw( QPainter &p ) const;

virtual void transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform, bool transformZ = false ) throw( QgsCsException );
Expand Down
9 changes: 9 additions & 0 deletions src/core/geometry/qgsabstractgeometry.h
Expand Up @@ -280,6 +280,15 @@ class CORE_EXPORT QgsAbstractGeometry
*/
virtual QString asJson( int precision = 17 ) const = 0;

/**
* Returns a GeoJSON object of the geometry.
* \see asWkb()
* \see asWkt()
* \see asGml2()
* \see asGml3()
*/
virtual QJsonObject asJsonV2( ) const = 0;

//render pipeline

/**
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgscircularstring.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgspoint.h"
#include "qgswkbptr.h"
#include "qgslogger.h"
#include <QJsonObject>
#include <QPainter>
#include <QPainterPath>
#include <memory>
Expand Down Expand Up @@ -374,6 +375,13 @@ QString QgsCircularString::asJson( int precision ) const
return json;
}

QJsonObject QgsCircularString::asJsonV2() const
{
// GeoJSON does not support curves
std::unique_ptr< QgsLineString > line( curveToLine() );
return line->asJsonV2( );
}

bool QgsCircularString::isEmpty() const
{
return mX.isEmpty();
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgscircularstring.h
Expand Up @@ -80,7 +80,7 @@ class CORE_EXPORT QgsCircularString: 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;
QString asJson( int precision = 17 ) const override;

QJsonObject asJsonV2( ) const override;
bool isEmpty() const override;
int numPoints() const override;

Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgscompoundcurve.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsgeometryutils.h"
#include "qgslinestring.h"
#include "qgswkbptr.h"
#include <QJsonObject>
#include <QPainter>
#include <QPainterPath>
#include <memory>
Expand Down Expand Up @@ -298,6 +299,13 @@ QString QgsCompoundCurve::asJson( int precision ) const
return json;
}

QJsonObject QgsCompoundCurve::asJsonV2() const
{
// GeoJSON does not support curves
std::unique_ptr< QgsLineString > line( curveToLine() );
return line->asJsonV2();
}

double QgsCompoundCurve::length() const
{
double length = 0;
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgscompoundcurve.h
Expand Up @@ -51,6 +51,7 @@ class CORE_EXPORT QgsCompoundCurve: 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;
QString asJson( int precision = 17 ) const override;
QJsonObject asJsonV2( ) const override;

//curve interface
double length() const override;
Expand Down
28 changes: 28 additions & 0 deletions src/core/geometry/qgscurvepolygon.cpp
Expand Up @@ -24,6 +24,8 @@
#include "qgspolygon.h"
#include "qgswkbptr.h"
#include "qgsmulticurve.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QPainter>
#include <QPainterPath>
#include <memory>
Expand Down Expand Up @@ -426,6 +428,32 @@ QString QgsCurvePolygon::asJson( int precision ) const
return json;
}

QJsonObject QgsCurvePolygon::asJsonV2() const
{
QJsonArray coordinates;
if ( exteriorRing() )
{
std::unique_ptr< QgsLineString > exteriorLineString( exteriorRing()->curveToLine() );
QgsPointSequence exteriorPts;
exteriorLineString->points( exteriorPts );
coordinates.append( QgsGeometryUtils::pointsToJsonV2( exteriorPts ) );

std::unique_ptr< QgsLineString > interiorLineString;
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
{
interiorLineString.reset( interiorRing( i )->curveToLine() );
QgsPointSequence interiorPts;
interiorLineString->points( interiorPts );
coordinates.append( QgsGeometryUtils::pointsToJsonV2( interiorPts ) );
}
}
return
{
{ QLatin1String( "type" ), QLatin1String( "Point" ) },
{ QLatin1String( "coordinates" ), coordinates }
};
}

double QgsCurvePolygon::area() const
{
if ( !mExteriorRing )
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgscurvepolygon.h
Expand Up @@ -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;
QString asJson( int precision = 17 ) const override;
QJsonObject asJsonV2() const override;

//surface interface
double area() const override;
Expand Down
9 changes: 9 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -1268,6 +1268,15 @@ QString QgsGeometry::asJson( int precision ) const
return d->geometry->asJson( precision );
}

QJsonObject QgsGeometry::asJsonV2() const
{
if ( !d->geometry )
{
return QJsonObject( );
}
return d->geometry->asJsonV2( );
}

QgsGeometry QgsGeometry::convertToType( QgsWkbTypes::GeometryType destType, bool destMultipart ) const
{
switch ( destType )
Expand Down
6 changes: 6 additions & 0 deletions src/core/geometry/qgsgeometry.h
Expand Up @@ -19,6 +19,7 @@ email : morb at ozemail dot com dot au
#include <functional>

#include <QDomDocument>
#include <QJsonObject>
#include <QSet>
#include <QString>
#include <QVector>
Expand Down Expand Up @@ -1429,6 +1430,11 @@ class CORE_EXPORT QgsGeometry
*/
QString asJson( int precision = 17 ) const;

/**
* Exports the geometry to a GeoJSON object.
*/
QJsonObject asJsonV2( ) const;

/**
* Try to convert the geometry to the requested type
* \param destType the geometry type to be converted to
Expand Down
14 changes: 14 additions & 0 deletions src/core/geometry/qgsgeometrycollection.cpp
Expand Up @@ -431,6 +431,20 @@ QString QgsGeometryCollection::asJson( int precision ) const
return json;
}

QJsonObject QgsGeometryCollection::asJsonV2() const
{
QJsonArray coordinates;
for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
{
coordinates.append( geom->asJsonV2() );
}
return
{
{ QLatin1String( "type" ), QLatin1String( "GeometryCollection" ) },
{ QLatin1String( "coordinates" ), coordinates }
};
}

QgsRectangle QgsGeometryCollection::boundingBox() const
{
if ( mBoundingBox.isNull() )
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgsgeometrycollection.h
Expand Up @@ -174,6 +174,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;
QString asJson( int precision = 17 ) const override;
QJsonObject asJsonV2( ) const override;

QgsRectangle boundingBox() const override;

Expand Down
11 changes: 11 additions & 0 deletions src/core/geometry/qgsgeometryutils.cpp
Expand Up @@ -27,6 +27,7 @@ email : marco.hugentobler at sourcepole dot com
#include <QVector>
#include <QRegularExpression>


QVector<QgsLineString *> QgsGeometryUtils::extractLineStrings( const QgsAbstractGeometry *geom )
{
QVector< QgsLineString * > linestrings;
Expand Down Expand Up @@ -1194,6 +1195,16 @@ QString QgsGeometryUtils::pointsToJSON( const QgsPointSequence &points, int prec
return json;
}

QJsonArray QgsGeometryUtils::pointsToJsonV2( const QgsPointSequence &points )
{
QJsonArray coordinates;
for ( const QgsPoint &p : points )
{
coordinates.append( QJsonArray( { p.x(), p.y() } ) );
}
return coordinates;
}

double QgsGeometryUtils::normalizedAngle( double angle )
{
double clippedAngle = angle;
Expand Down
7 changes: 7 additions & 0 deletions src/core/geometry/qgsgeometryutils.h
Expand Up @@ -24,6 +24,7 @@ email : marco.hugentobler at sourcepole dot com
#include "qgsabstractgeometry.h"
#include "qgsvector3d.h"

#include <QJsonArray>

class QgsLineString;

Expand Down Expand Up @@ -420,6 +421,12 @@ class CORE_EXPORT QgsGeometryUtils
*/
static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;

/**
* Returns a geoJSON coordinates object.
* \note not available in Python bindings
*/
static QJsonArray pointsToJsonV2( const QgsPointSequence &points ) SIP_SKIP;

/**
* Ensures that an angle is in the range 0 <= angle < 2 pi.
* \param angle angle in radians
Expand Down

0 comments on commit 6967a21

Please sign in to comment.