Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/PyQt6/core/auto_additions/qgslocaleutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The following has been generated automatically from src/core/qgslocaleutils.h
try:
QgsLocaleUtils.countryBounds = staticmethod(QgsLocaleUtils.countryBounds)
QgsLocaleUtils.territoryBounds = staticmethod(QgsLocaleUtils.territoryBounds)
QgsLocaleUtils.systemLocaleGeographicBounds = staticmethod(QgsLocaleUtils.systemLocaleGeographicBounds)
except (NameError, AttributeError):
pass
63 changes: 63 additions & 0 deletions python/PyQt6/core/auto_generated/qgslocaleutils.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslocaleutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/



class QgsLocaleUtils
{
%Docstring(signature="appended")
Contains locale related utility functions.

.. versionadded:: 4.0
%End

%TypeHeaderCode
#include "qgslocaleutils.h"
%End
public:

static QgsRectangle countryBounds( const QString &country );
%Docstring
Returns the approximate bounds of a ``country`` (in EPSG:4326).

Returns an invalid rectangle if the country could not be matched.

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

static QgsRectangle territoryBounds( const QLocale &locale );
%Docstring
Returns the approximate bounds of a locale, by territory (in EPSG:4326).

Returns an invalid rectangle if the territory could not be matched.

.. seealso:: :py:func:`countryBounds`

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

static QgsRectangle systemLocaleGeographicBounds();
%Docstring
Returns the approximate bounds of the current system locale (in
EPSG:4326).

This method retrieves the territory from the system locale, and attempts
to retrieve the approximate bounds of the matching country.

Returns an invalid rectangle if the system locale territory could not be
matched.
%End
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslocaleutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/
1 change: 1 addition & 0 deletions python/PyQt6/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
%Include auto_generated/qgslegendsettings.sip
%Include auto_generated/qgslegendstyle.sip
%Include auto_generated/qgslocaldefaultsettings.sip
%Include auto_generated/qgslocaleutils.sip
%Include auto_generated/qgslocalizeddatapathregistry.sip
%Include auto_generated/qgslogger.sip
%Include auto_generated/qgsmapclippingregion.sip
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_additions/qgslocaleutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The following has been generated automatically from src/core/qgslocaleutils.h
try:
QgsLocaleUtils.countryBounds = staticmethod(QgsLocaleUtils.countryBounds)
QgsLocaleUtils.territoryBounds = staticmethod(QgsLocaleUtils.territoryBounds)
QgsLocaleUtils.systemLocaleGeographicBounds = staticmethod(QgsLocaleUtils.systemLocaleGeographicBounds)
except (NameError, AttributeError):
pass
63 changes: 63 additions & 0 deletions python/core/auto_generated/qgslocaleutils.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslocaleutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/



class QgsLocaleUtils
{
%Docstring(signature="appended")
Contains locale related utility functions.

.. versionadded:: 4.0
%End

%TypeHeaderCode
#include "qgslocaleutils.h"
%End
public:

static QgsRectangle countryBounds( const QString &country );
%Docstring
Returns the approximate bounds of a ``country`` (in EPSG:4326).

Returns an invalid rectangle if the country could not be matched.

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

static QgsRectangle territoryBounds( const QLocale &locale );
%Docstring
Returns the approximate bounds of a locale, by territory (in EPSG:4326).

Returns an invalid rectangle if the territory could not be matched.

.. seealso:: :py:func:`countryBounds`

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

static QgsRectangle systemLocaleGeographicBounds();
%Docstring
Returns the approximate bounds of the current system locale (in
EPSG:4326).

This method retrieves the territory from the system locale, and attempts
to retrieve the approximate bounds of the matching country.

Returns an invalid rectangle if the system locale territory could not be
matched.
%End
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgslocaleutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
%Include auto_generated/qgslegendsettings.sip
%Include auto_generated/qgslegendstyle.sip
%Include auto_generated/qgslocaldefaultsettings.sip
%Include auto_generated/qgslocaleutils.sip
%Include auto_generated/qgslocalizeddatapathregistry.sip
%Include auto_generated/qgslogger.sip
%Include auto_generated/qgsmapclippingregion.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ set(QGIS_CORE_SRCS
qgsmessageoutput.cpp
qgsmimedatautils.cpp
qgsmultirenderchecker.cpp
qgslocaleutils.cpp
qgsobjectcustomproperties.cpp
qgsofflineediting.cpp
qgsogcutils.cpp
Expand Down Expand Up @@ -1226,6 +1227,7 @@ set(QGIS_CORE_HDRS
qgslegendstyle.h
qgslocaldefaultsettings.h
qgslocalec.h
qgslocaleutils.h
qgslocalizeddatapathregistry.h
qgslogger.h
qgsmapclippingregion.h
Expand Down
76 changes: 76 additions & 0 deletions src/core/qgslocaleutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/***************************************************************************
qgslocaleutils.cpp
-------------
Date : September 2025
Copyright : (C) 2025 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslocaleutils.h"
#include "qgsapplication.h"
#include "qgsvectorlayer.h"
#include "qgsreadwritelocker.h"
#include "qgsfeaturerequest.h"
#include "qgsexpression.h"
#include <QReadWriteLock>
#include <mutex>

QgsRectangle QgsLocaleUtils::countryBounds( const QString &country )
{
static QReadWriteLock sMutex;
static QMap< QString, QgsRectangle > sCache;
QgsReadWriteLocker locker( sMutex, QgsReadWriteLocker::Read );
{
auto it = sCache.constFind( country );
if ( it != sCache.constEnd() )
return it.value();
}

locker.changeMode( QgsReadWriteLocker::Write );
const QString fileName = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );
QgsVectorLayer::LayerOptions options;
options.forceReadOnly = true;
auto layer = std::make_unique< QgsVectorLayer >( fileName, QStringLiteral( "World Map" ), QStringLiteral( "ogr" ), options );

QgsFeatureRequest req = QgsFeatureRequest().setFilterExpression( QgsExpression::createFieldEqualityExpression( QStringLiteral( "Name" ), country ) ).setNoAttributes();
QgsFeatureIterator featureIt = layer->getFeatures( req );
QgsFeature f;
QgsRectangle rect;
if ( featureIt.nextFeature( f ) )
{
rect = f.geometry().boundingBox();
}

sCache.insert( country, rect );
return rect;
}

QgsRectangle QgsLocaleUtils::territoryBounds( const QLocale &locale )
{
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
const QString territoryName = QLocale::countryToString( locale.country() );
#else
const QString territoryName = QLocale::territoryToCode( locale.territory() );
#endif

return countryBounds( territoryName );
}

QgsRectangle QgsLocaleUtils::systemLocaleGeographicBounds()
{
static std::once_flag initialized;
static QgsRectangle sRes;
std::call_once( initialized, []
{
sRes = territoryBounds( QLocale::system() );
} );

return sRes;
}
62 changes: 62 additions & 0 deletions src/core/qgslocaleutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/***************************************************************************
qgslocaleutils.h
-------------
Date : September 2025
Copyright : (C) 2025 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLOCALEUTILS_H
#define QGSLOCALEUTILS_H

#include "qgis_core.h"
#include "qgsrectangle.h"

/**
* \ingroup core
* \class QgsLocaleUtils
* \brief Contains locale related utility functions.
* \since QGIS 4.0
*/
class CORE_EXPORT QgsLocaleUtils
{
public:

/**
* Returns the approximate bounds of a \a country (in EPSG:4326).
*
* Returns an invalid rectangle if the country could not be matched.
*
* \see territoryBounds()
*/
static QgsRectangle countryBounds( const QString &country );

/**
* Returns the approximate bounds of a locale, by territory (in EPSG:4326).
*
* Returns an invalid rectangle if the territory could not be matched.
*
* \see countryBounds()
* \see systemLocaleGeographicBounds()
*/
static QgsRectangle territoryBounds( const QLocale &locale );

/**
* Returns the approximate bounds of the current system locale (in EPSG:4326).
*
* This method retrieves the territory from the system locale, and attempts to
* retrieve the approximate bounds of the matching country.
*
* Returns an invalid rectangle if the system locale territory could not be matched.
*/
static QgsRectangle systemLocaleGeographicBounds();
};

#endif // QGSLOCALEUTILS_H
28 changes: 26 additions & 2 deletions src/gui/layertree/qgslayertreemapcanvasbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "qgsproject.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgslocaleutils.h"
#include "qgsreferencedgeometry.h"

QgsLayerTreeMapCanvasBridge::QgsLayerTreeMapCanvasBridge( QgsLayerTree *root, QgsMapCanvas *canvas, QObject *parent )
: QObject( parent )
Expand Down Expand Up @@ -74,13 +76,20 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
const QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
int currentSpatialLayerCount = 0;
int currentValidSpatialLayerCount = 0;
bool allAddedLayersAreBasemaps = true;
for ( QgsLayerTreeLayer *layerNode : layerNodes )
{
if ( layerNode->layer() && layerNode->layer()->isSpatial() )
{
currentSpatialLayerCount++;
if ( layerNode->layer()->isValid() )
{
currentValidSpatialLayerCount++;
if ( !layerNode->layer()->properties().testFlag( Qgis::MapLayerProperty::IsBasemapLayer ) )
{
allAddedLayersAreBasemaps = false;
}
}
}
}

Expand All @@ -93,8 +102,23 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()

if ( firstValidLayers )
{
// if we are moving from zero to non-zero layers, let's zoom to those data (only consider valid layers here!)
mCanvas->zoomToProjectExtent();
if ( allAddedLayersAreBasemaps )
{
const QgsRectangle localeRect = QgsLocaleUtils::systemLocaleGeographicBounds();
if ( !localeRect.isNull() )
{
mCanvas->setReferencedExtent( QgsReferencedRectangle( localeRect, QgsCoordinateReferenceSystem( "EPSG:4326" ) ) );
}
else
{
mCanvas->zoomToProjectExtent();
}
}
else
{
// if we are moving from zero to non-zero layers, let's zoom to those data (only consider valid layers here!)
mCanvas->zoomToProjectExtent();
}
}

if ( !mFirstCRS.isValid() )
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ ADD_PYTHON_TEST(PyQgsLineString test_qgslinestring.py)
ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py)
ADD_PYTHON_TEST(PyQgsLineburstSymbolLayer test_qgslineburstsymbollayer.py)
ADD_PYTHON_TEST(PyQgsLocalDefaultSettings test_qgslocaldefaultsettings.py)
ADD_PYTHON_TEST(PyQgsLocaleUtils test_qgslocaleutils.py)
ADD_PYTHON_TEST(PyQgsLocalizedDataPathRegistry test_qgslocalizeddatapathregistry.py)
ADD_PYTHON_TEST(PyQgsLocator test_qgslocator.py)
ADD_PYTHON_TEST(PyQgsMapBoxGlStyleConverter test_qgsmapboxglconverter.py)
Expand Down
Loading
Loading