Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Project: store geographic extent
  • Loading branch information
elpaso authored and pblottiere committed Mar 10, 2021
1 parent 10c3a1a commit b6e01f9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
34 changes: 32 additions & 2 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -1695,9 +1695,29 @@ Copies attributes like name, short name, ... into another layer.
.. versionadded:: 3.0
%End

virtual void setExtent( const QgsRectangle &rect );
virtual void setExtent( const QgsRectangle &extent );
%Docstring
Sets the extent
Sets the ``extent`` in layer CRS.

If the layer CRS is not EPSG:4326 and the geographic extent is
known, using :py:func:`~QgsMapLayer.setExtents` is faster.

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

virtual void setExtents( const QgsRectangle &extent, const QgsRectangle &geographicExtent );
%Docstring
Simultaneously sets the ``extent`` and the ``geographicExtent``.

This call can be used to skip the costly coordinate transformation when setting
the layer extent in case the geographic extent is already known (e.g. when loading
a project).

.. seealso:: :py:func:`setExtent`

.. seealso:: :py:func:`geographicExtent`

.. versionadded:: 3.18
%End

void setValid( bool valid );
Expand Down Expand Up @@ -1812,6 +1832,7 @@ Sets error message




bool hasDependencyCycle( const QSet<QgsMapLayerDependency> & ) const;
%Docstring
Checks whether a new set of dependencies will introduce a cycle
Expand All @@ -1826,6 +1847,15 @@ this method is now deprecated and always return ``False``, because circular depe



QgsRectangle geographicExtent() const;
%Docstring
Returns the geographic extent (EPSG:4326) of the layer.

.. seealso:: :py:func:`setExtent`

.. versionadded:: 3.18
%End

};

QFlags<QgsMapLayer::LayerFlag> operator|(QgsMapLayer::LayerFlag f1, QFlags<QgsMapLayer::LayerFlag> f2);
Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsxmlutils.sip.in
Expand Up @@ -50,7 +50,7 @@ Encodes a distance unit to a DOM element.
.. seealso:: :py:func:`readMapUnits`
%End

static QDomElement writeRectangle( const QgsRectangle &rect, QDomDocument &doc );
static QDomElement writeRectangle( const QgsRectangle &rect, QDomDocument &doc, const QString &elementName = QStringLiteral( "extent" ) );

static QDomElement writeVariant( const QVariant &value, QDomDocument &doc );
%Docstring
Expand Down
18 changes: 16 additions & 2 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -411,6 +411,7 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
if ( !extent().isNull() )
{
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document ) );
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document, QStringLiteral( "geographicExtent" ) ) );
}

layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
Expand Down Expand Up @@ -1899,9 +1900,17 @@ void QgsMapLayer::emitStyleChanged()
emit styleChanged();
}

void QgsMapLayer::setExtent( const QgsRectangle &r )
void QgsMapLayer::setExtent( const QgsRectangle &extent )
{
mExtent = r;
mExtent = extent;
const QgsCoordinateTransform transformer { crs(), QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), transformContext() };
mGeographicExtent = transformer.transform( extent );
}

void QgsMapLayer::setExtents( const QgsRectangle &extent, const QgsRectangle &geographicExtent )
{
mExtent = extent;
mGeographicExtent = geographicExtent;
}

bool QgsMapLayer::isReadOnly() const
Expand Down Expand Up @@ -2001,3 +2010,8 @@ void QgsMapLayer::onNotified( const QString &message )
emit dataChanged();
}
}

QgsRectangle QgsMapLayer::geographicExtent() const
{
return mGeographicExtent;
}
36 changes: 34 additions & 2 deletions src/core/qgsmaplayer.h
Expand Up @@ -1512,8 +1512,29 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
void clone( QgsMapLayer *layer ) const;

//! Sets the extent
virtual void setExtent( const QgsRectangle &rect );
/**
* Sets the \a extent in layer CRS.
*
* If the layer CRS is not EPSG:4326 and the geographic extent is
* known, using setExtents() is faster.
*
* \see setExtents()
*/
virtual void setExtent( const QgsRectangle &extent );

/**
* Simultaneously sets the \a extent and the \a geographicExtent.
*
* This call can be used to skip the costly coordinate transformation when setting
* the layer extent in case the geographic extent is already known (e.g. when loading
* a project).
*
* \see setExtent()
* \see geographicExtent()
*
* \since QGIS 3.18
*/
virtual void setExtents( const QgsRectangle &extent, const QgsRectangle &geographicExtent );

//! Sets whether layer is valid or not
void setValid( bool valid );
Expand Down Expand Up @@ -1605,6 +1626,9 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Extent of the layer
mutable QgsRectangle mExtent;

//! Extent of the layer in EPSG:4326
QgsRectangle mGeographicExtent;

//! Indicates if the layer is valid and can be drawn
bool mValid = false;

Expand Down Expand Up @@ -1676,6 +1700,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
double mLayerOpacity = 1.0;

/**
* Returns the geographic extent (EPSG:4326) of the layer.
*
* \see setExtent()
* \since QGIS 3.18
*/
QgsRectangle geographicExtent() const;

private:

virtual QString baseURI( PropertyType type ) const;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsxmlutils.cpp
Expand Up @@ -78,7 +78,7 @@ QDomElement QgsXmlUtils::writeMapUnits( QgsUnitTypes::DistanceUnit units, QDomDo
return unitsNode;
}

QDomElement QgsXmlUtils::writeRectangle( const QgsRectangle &rect, QDomDocument &doc )
QDomElement QgsXmlUtils::writeRectangle( const QgsRectangle &rect, QDomDocument &doc, const QString &elementName )
{
QDomElement xMin = doc.createElement( QStringLiteral( "xmin" ) );
QDomElement yMin = doc.createElement( QStringLiteral( "ymin" ) );
Expand All @@ -95,7 +95,7 @@ QDomElement QgsXmlUtils::writeRectangle( const QgsRectangle &rect, QDomDocument
xMax.appendChild( xMaxText );
yMax.appendChild( yMaxText );

QDomElement extentNode = doc.createElement( QStringLiteral( "extent" ) );
QDomElement extentNode = doc.createElement( elementName );
extentNode.appendChild( xMin );
extentNode.appendChild( yMin );
extentNode.appendChild( xMax );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsxmlutils.h
Expand Up @@ -59,7 +59,7 @@ class CORE_EXPORT QgsXmlUtils
*/
static QDomElement writeMapUnits( QgsUnitTypes::DistanceUnit units, QDomDocument &doc );

static QDomElement writeRectangle( const QgsRectangle &rect, QDomDocument &doc );
static QDomElement writeRectangle( const QgsRectangle &rect, QDomDocument &doc, const QString &elementName = QStringLiteral( "extent" ) );

/**
* Write a QVariant to a QDomElement.
Expand Down

0 comments on commit b6e01f9

Please sign in to comment.