Skip to content

Commit

Permalink
Update tests for geometry classes
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed May 2, 2019
1 parent ba81645 commit bbd836f
Show file tree
Hide file tree
Showing 42 changed files with 105 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ Returns a GML3 representation of the geometry.
.. seealso:: :py:func:`asJson`
%End

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

:param precision: number of decimal places for coordinates

Expand All @@ -259,6 +259,8 @@ Returns a GeoJSON representation of the geometry.
.. seealso:: :py:func:`asGml2`

.. seealso:: :py:func:`asGml3`

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


Expand Down
2 changes: 0 additions & 2 deletions python/core/auto_generated/geometry/qgscircularstring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ to ``p2`` will be used (i.e. winding the other way around the circle).

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

virtual QString asJson( int precision = 17 ) const;

virtual bool isEmpty() const;

virtual int numPoints() const;
Expand Down
2 changes: 0 additions & 2 deletions python/core/auto_generated/geometry/qgscompoundcurve.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ Compound curve geometry type

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

virtual QString asJson( int precision = 17 ) const;


virtual double length() const;

Expand Down
2 changes: 0 additions & 2 deletions python/core/auto_generated/geometry/qgscurvepolygon.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ 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 asJson( int precision = 17 ) const;


virtual double area() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ 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 asJson( int precision = 17 ) const;


virtual QgsRectangle boundingBox() const;

Expand Down
2 changes: 0 additions & 2 deletions python/core/auto_generated/geometry/qgslinestring.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,6 @@ 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 asJson( int precision = 17 ) const;


virtual double length() const;

Expand Down
2 changes: 0 additions & 2 deletions python/core/auto_generated/geometry/qgsmulticurve.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ Multi curve geometry collection.

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

virtual QString asJson( int precision = 17 ) const;

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

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

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

virtual QString asJson( int precision = 17 ) const;

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

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

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

virtual QString asJson( int precision = 17 ) const;

virtual int nCoordinates() const;

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

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

virtual QString asJson( int precision = 17 ) const;

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

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

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

virtual QString asJson( int precision = 17 ) const;

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

virtual bool insertGeometry( QgsAbstractGeometry *g /Transfer/, int index );
Expand Down
2 changes: 0 additions & 2 deletions python/core/auto_generated/geometry/qgspoint.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ 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 asJson( 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 );
Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgsabstractgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ QString QgsAbstractGeometry::wktTypeStr() const
return wkt;
}

QString QgsAbstractGeometry::asJson( int precision )
{
return QString::fromStdString( asJsonObject( precision ).dump() );
}

QgsPoint QgsAbstractGeometry::centroid() const
{
// http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
Expand Down
10 changes: 6 additions & 4 deletions src/core/geometry/qgsabstractgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,25 @@ class CORE_EXPORT QgsAbstractGeometry
virtual QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const = 0;

/**
* Returns a GeoJSON representation of the geometry.
* Returns a GeoJSON representation of the geometry as a QString.
* \param precision number of decimal places for coordinates
* \see asWkb()
* \see asWkt()
* \see asGml2()
* \see asGml3()
* \see asJsonObject()
*/
virtual QString asJson( int precision = 17 ) const = 0;
QString asJson( int precision = 17 );

/**
* Returns a json obejct representation of the geometry.
* Returns a json object representation of the geometry.
* \see asWkb()
* \see asWkt()
* \see asGml2()
* \see asGml3()
* \see asJson()
* \since QGIS 3.8
* \note not available in Python bindings
* \since QGIS 3.10
*/
virtual json asJsonObject( int precision = 17 ) const SIP_SKIP { Q_UNUSED( precision ); return nullptr; }

Expand Down
7 changes: 0 additions & 7 deletions src/core/geometry/qgscircularstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,6 @@ QDomElement QgsCircularString::asGml3( QDomDocument &doc, int precision, const Q
return elemCurve;
}

QString QgsCircularString::asJson( int precision ) const
{
// GeoJSON does not support curves
std::unique_ptr< QgsLineString > line( curveToLine() );
QString json = line->asJson( precision );
return json;
}

json QgsCircularString::asJsonObject( int precision ) const
{
Expand Down
1 change: 0 additions & 1 deletion src/core/geometry/qgscircularstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
QString asWkt( int precision = 17 ) const override;
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;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;
bool isEmpty() const override;
int numPoints() const override;
Expand Down
8 changes: 0 additions & 8 deletions src/core/geometry/qgscompoundcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,6 @@ QDomElement QgsCompoundCurve::asGml3( QDomDocument &doc, int precision, const QS
return compoundCurveElem;
}

QString QgsCompoundCurve::asJson( int precision ) const
{
// GeoJSON does not support curves
std::unique_ptr< QgsLineString > line( curveToLine() );
QString json = line->asJson( precision );
return json;
}

json QgsCompoundCurve::asJsonObject( int precision ) const
{
// GeoJSON does not support curves
Expand Down
1 change: 0 additions & 1 deletion src/core/geometry/qgscompoundcurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
QString asWkt( int precision = 17 ) const override;
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;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;

//curve interface
Expand Down
31 changes: 1 addition & 30 deletions src/core/geometry/qgscurvepolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,38 +401,9 @@ QDomElement QgsCurvePolygon::asGml3( QDomDocument &doc, int precision, const QSt
return elemCurvePolygon;
}

QString QgsCurvePolygon::asJson( int precision ) const
{
// GeoJSON does not support curves
QString json = QStringLiteral( "{\"type\": \"Polygon\", \"coordinates\": [" );

if ( exteriorRing() )
{
std::unique_ptr< QgsLineString > exteriorLineString( exteriorRing()->curveToLine() );
QgsPointSequence exteriorPts;
exteriorLineString->points( exteriorPts );
json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + QLatin1String( ", " );

std::unique_ptr< QgsLineString > interiorLineString;
for ( int i = 0, n = numInteriorRings(); i < n; ++i )
{
interiorLineString.reset( interiorRing( i )->curveToLine() );
QgsPointSequence interiorPts;
interiorLineString->points( interiorPts );
json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + QLatin1String( ", " );
}
if ( json.endsWith( QLatin1String( ", " ) ) )
{
json.chop( 2 ); // Remove last ", "
}
}
json += QLatin1String( "] }" );
return json;
}

json QgsCurvePolygon::asJsonObject( int precision ) const
{
json coordinates;
json coordinates { json::array( )};
if ( exteriorRing() )
{
std::unique_ptr< QgsLineString > exteriorLineString( exteriorRing()->curveToLine() );
Expand Down
1 change: 0 additions & 1 deletion src/core/geometry/qgscurvepolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
QString asWkt( int precision = 17 ) const override;
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;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;

//surface interface
Expand Down
6 changes: 1 addition & 5 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,11 +1263,7 @@ QString QgsGeometry::asWkt( int precision ) const

QString QgsGeometry::asJson( int precision ) const
{
if ( !d->geometry )
{
return QStringLiteral( "null" );
}
return d->geometry->asJson( precision );
return QString::fromStdString( asJsonObject( precision ).dump() );
}

json QgsGeometry::asJsonObject( int precision ) const
Expand Down
19 changes: 2 additions & 17 deletions src/core/geometry/qgsgeometrycollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,32 +417,17 @@ QDomElement QgsGeometryCollection::asGml3( QDomDocument &doc, int precision, con
return elemMultiGeometry;
}

QString QgsGeometryCollection::asJson( int precision ) const
{
QString json = QStringLiteral( "{\"type\": \"GeometryCollection\", \"geometries\": [" );
for ( const QgsAbstractGeometry *geom : mGeometries )
{
json += geom->asJson( precision ) + QLatin1String( ", " );
}
if ( json.endsWith( QLatin1String( ", " ) ) )
{
json.chop( 2 ); // Remove last ", "
}
json += QLatin1String( "] }" );
return json;
}

json QgsGeometryCollection::asJsonObject( int precision ) const
{
json coordinates;
json coordinates { json::array( ) };
for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
{
coordinates.push_back( geom->asJsonObject( precision ) );
}
return
{
{ "type", "GeometryCollection" },
{ "coordinates", coordinates }
{ "geometries", coordinates }
};
}

Expand Down
1 change: 0 additions & 1 deletion src/core/geometry/qgsgeometrycollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
QString asWkt( int precision = 17 ) const override;
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;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;

QgsRectangle boundingBox() const override;
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgsgeometryutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ QString QgsGeometryUtils::pointsToJSON( const QgsPointSequence &points, int prec

json QgsGeometryUtils::pointsToJson( const QgsPointSequence &points, int precision )
{
json coordinates;
json coordinates { json::array() };
for ( const QgsPoint &p : points )
{
if ( p.is3D() )
Expand Down
8 changes: 0 additions & 8 deletions src/core/geometry/qgslinestring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,6 @@ QDomElement QgsLineString::asGml3( QDomDocument &doc, int precision, const QStri
return elemLineString;
}

QString QgsLineString::asJson( int precision ) const
{
QgsPointSequence pts;
points( pts );

return "{\"type\": \"LineString\", \"coordinates\": " + QgsGeometryUtils::pointsToJSON( pts, precision ) + '}';
}

json QgsLineString::asJsonObject( int precision ) const
{
QgsPointSequence pts;
Expand Down
1 change: 0 additions & 1 deletion src/core/geometry/qgslinestring.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ class CORE_EXPORT QgsLineString: public QgsCurve
QString asWkt( int precision = 17 ) const override;
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;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;

//curve interface
Expand Down
24 changes: 1 addition & 23 deletions src/core/geometry/qgsmulticurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,9 @@ QDomElement QgsMultiCurve::asGml3( QDomDocument &doc, int precision, const QStri
return elemMultiCurve;
}

QString QgsMultiCurve::asJson( int precision ) const
{
// GeoJSON does not support curves
QString json = QStringLiteral( "{\"type\": \"MultiLineString\", \"coordinates\": [" );
for ( const QgsAbstractGeometry *geom : mGeometries )
{
if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
{
std::unique_ptr< QgsLineString > lineString( static_cast<const QgsCurve *>( geom )->curveToLine() );
QgsPointSequence pts;
lineString->points( pts );
json += QgsGeometryUtils::pointsToJSON( pts, precision ) + QLatin1String( ", " );
}
}
if ( json.endsWith( QLatin1String( ", " ) ) )
{
json.chop( 2 ); // Remove last ", "
}
json += QLatin1String( "] }" );
return json;
}

json QgsMultiCurve::asJsonObject( int precision ) const
{
json coordinates;
json coordinates { json::array( ) };
for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
{
if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
Expand Down
1 change: 0 additions & 1 deletion src/core/geometry/qgsmulticurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class CORE_EXPORT QgsMultiCurve: public QgsGeometryCollection
bool fromWkt( const QString &wkt ) override;
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;
json asJsonObject( int precision = 17 ) const override SIP_SKIP;
bool addGeometry( QgsAbstractGeometry *g SIP_TRANSFER ) override;
bool insertGeometry( QgsAbstractGeometry *g SIP_TRANSFER, int index ) override;
Expand Down

0 comments on commit bbd836f

Please sign in to comment.