Skip to content

Commit

Permalink
Implement layer extent calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2020
1 parent f0d2f3d commit f2d172f
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 5 deletions.
Expand Up @@ -58,6 +58,13 @@ Returns a clone of the item. Ownership is transferred to the caller.
virtual QString type() const = 0;
%Docstring
Returns a unique (untranslated) string identifying the type of item.
%End

virtual QgsRectangle boundingBox() const = 0;
%Docstring
Returns the bounding box of the item's geographic location.

The coordinate reference system for the bounding box is retrieved via :py:func:`~QgsAnnotationItem.crs`.
%End

QgsCoordinateReferenceSystem crs() const;
Expand Down
Expand Up @@ -38,6 +38,8 @@ Constructor for QgsAnnotationLineStringItem, with the specified ``linestring`` i
static QgsAnnotationLineStringItem *create() /Factory/;
virtual bool readXml( const QDomElement &element, const QgsReadWriteContext &context );

virtual QgsRectangle boundingBox() const;


virtual QgsAnnotationLineStringItem *clone() /Factory/;

Expand Down
Expand Up @@ -40,6 +40,8 @@ Constructor for QgsAnnotationMarkerItem, at the specified ``point`` in the given

virtual QgsAnnotationMarkerItem *clone() /Factory/;

virtual QgsRectangle boundingBox() const;


QgsPointXY point() const;
%Docstring
Expand Down
Expand Up @@ -40,6 +40,8 @@ Constructor for QgsAnnotationPolygonItem, with the specified ``polygon`` in the

virtual QgsAnnotationPolygonItem *clone() /Factory/;

virtual QgsRectangle boundingBox() const;


QgsPolygon polygon() const;
%Docstring
Expand Down
7 changes: 7 additions & 0 deletions src/core/annotations/qgsannotationitem.h
Expand Up @@ -86,6 +86,13 @@ class CORE_EXPORT QgsAnnotationItem
*/
virtual QString type() const = 0;

/**
* Returns the bounding box of the item's geographic location.
*
* The coordinate reference system for the bounding box is retrieved via crs().
*/
virtual QgsRectangle boundingBox() const = 0;

/**
* Returns the CRS used for storing the location of the item.
*
Expand Down
8 changes: 3 additions & 5 deletions src/core/annotations/qgsannotationlayer.cpp
Expand Up @@ -77,17 +77,15 @@ QgsRectangle QgsAnnotationLayer::extent() const
QgsRectangle rect;
for ( auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
{
#if 0
QgsCoordinateTransform trans( it->crs(), crs(), mTransformContext );
QgsCoordinateTransform trans( it.value()->crs(), crs(), mTransformContext );
if ( rect.isNull() )
{
rect = trans.transform( item->boundingBox() );
rect = trans.transform( it.value()->boundingBox() );
}
else
{
rect.combineExtentWith( trans.transform( item->boundingBox() ) );
rect.combineExtentWith( trans.transform( it.value()->boundingBox() ) );
}
#endif
}
return rect;
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/annotations/qgsannotationlinestringitem.cpp
Expand Up @@ -103,6 +103,11 @@ bool QgsAnnotationLineStringItem::readXml( const QDomElement &element, const Qgs
return true;
}

QgsRectangle QgsAnnotationLineStringItem::boundingBox() const
{
return mLineString.boundingBox();
}

QgsAnnotationLineStringItem *QgsAnnotationLineStringItem::clone()
{
std::unique_ptr< QgsAnnotationLineStringItem > item = qgis::make_unique< QgsAnnotationLineStringItem >( mLineString, crs() );
Expand Down
1 change: 1 addition & 0 deletions src/core/annotations/qgsannotationlinestringitem.h
Expand Up @@ -44,6 +44,7 @@ class CORE_EXPORT QgsAnnotationLineStringItem : public QgsAnnotationItem
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
static QgsAnnotationLineStringItem *create() SIP_FACTORY;
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
QgsRectangle boundingBox() const override;

QgsAnnotationLineStringItem *clone() override SIP_FACTORY;

Expand Down
5 changes: 5 additions & 0 deletions src/core/annotations/qgsannotationmarkeritem.cpp
Expand Up @@ -98,6 +98,11 @@ QgsAnnotationMarkerItem *QgsAnnotationMarkerItem::clone()
return item.release();
}

QgsRectangle QgsAnnotationMarkerItem::boundingBox() const
{
return QgsRectangle( mPoint.x(), mPoint.y(), mPoint.x(), mPoint.y() );
}

const QgsMarkerSymbol *QgsAnnotationMarkerItem::symbol() const
{
return mSymbol.get();
Expand Down
1 change: 1 addition & 0 deletions src/core/annotations/qgsannotationmarkeritem.h
Expand Up @@ -45,6 +45,7 @@ class CORE_EXPORT QgsAnnotationMarkerItem : public QgsAnnotationItem
static QgsAnnotationMarkerItem *create() SIP_FACTORY;
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
QgsAnnotationMarkerItem *clone() override SIP_FACTORY;
QgsRectangle boundingBox() const override;

/**
* Returns the point location of the marker.
Expand Down
5 changes: 5 additions & 0 deletions src/core/annotations/qgsannotationpolygonitem.cpp
Expand Up @@ -124,6 +124,11 @@ QgsAnnotationPolygonItem *QgsAnnotationPolygonItem::clone()
return item.release();
}

QgsRectangle QgsAnnotationPolygonItem::boundingBox() const
{
return mPolygon.boundingBox();
}

const QgsFillSymbol *QgsAnnotationPolygonItem::symbol() const
{
return mSymbol.get();
Expand Down
1 change: 1 addition & 0 deletions src/core/annotations/qgsannotationpolygonitem.h
Expand Up @@ -45,6 +45,7 @@ class CORE_EXPORT QgsAnnotationPolygonItem : public QgsAnnotationItem
static QgsAnnotationPolygonItem *create() SIP_FACTORY;
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
QgsAnnotationPolygonItem *clone() override SIP_FACTORY;
QgsRectangle boundingBox() const override;

/**
* Returns the polygon geometry of the item.
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsannotationitemregistry.cpp
Expand Up @@ -61,6 +61,8 @@ class TestItem : public QgsAnnotationItem
{
return true;
}

QgsRectangle boundingBox() const override { return QgsRectangle(); }
};


Expand Down
24 changes: 24 additions & 0 deletions tests/src/python/test_qgsannotationlayer.py
Expand Up @@ -89,6 +89,30 @@ def testItems(self):
self.assertTrue(layer.removeItem(marker_item_id))
self.assertEqual(len(layer.items()), 0)

def testExtent(self):
layer = QgsAnnotationLayer('test', QgsAnnotationLayer.LayerOptions(QgsProject.instance().transformContext()))
self.assertTrue(layer.isValid())

polygon_item_id = layer.addItem(QgsAnnotationPolygonItem(QgsPolygon(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13)])),
QgsCoordinateReferenceSystem('EPSG:4326')))
linestring_item_id = layer.addItem(QgsAnnotationLineStringItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)]),
QgsCoordinateReferenceSystem('EPSG:4326')))
marker_item_id = layer.addItem(QgsAnnotationMarkerItem(QgsPointXY(12, 13), QgsCoordinateReferenceSystem('EPSG:4326')))

# no crs set, so item bounds are taken direct...
extent = layer.extent()
self.assertEqual(extent.xMinimum(), 11.0)
self.assertEqual(extent.xMaximum(), 14.0)
self.assertEqual(extent.yMinimum(), 13.0)
self.assertEqual(extent.yMaximum(), 15.0)

layer.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
extent = layer.extent()
self.assertAlmostEqual(extent.xMinimum(), 1224514.0, -3)
self.assertAlmostEqual(extent.xMaximum(), 1558472.0, -3)
self.assertAlmostEqual(extent.yMinimum(), 1459732.0, -3)
self.assertAlmostEqual(extent.yMaximum(), 1689200.0, -3)

def testReadWriteXml(self):
doc = QDomDocument("testdoc")

Expand Down

0 comments on commit f2d172f

Please sign in to comment.