Skip to content

Commit

Permalink
Import iface in lazy manner
Browse files Browse the repository at this point in the history
  • Loading branch information
alukach committed Feb 11, 2025
1 parent a6c9b4a commit 2b3d323
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions ee_plugin/Map.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@
QgsProject,
QgsRectangle,
)
from qgis import gui
from qgis.PyQt.QtCore import QEventLoop, QTimer, QCoreApplication, QThread
from qgis.utils import iface

from . import utils


def get_iface() -> gui.QgisInterface:
# Lazy import to ensure that iface is available after gqis is initialized
from qgis.utils import iface

return iface


def addLayer(eeObject, visParams=None, name=None, shown=True, opacity=1.0):
Expand All @@ -27,8 +36,6 @@ def addLayer(eeObject, visParams=None, name=None, shown=True, opacity=1.0):
>>> from ee_plugin import Map
>>> Map.addLayer(.....)
"""
from . import utils

utils.add_or_update_ee_layer(eeObject, visParams, name, shown, opacity)


Expand Down Expand Up @@ -67,6 +74,7 @@ def centerObject(feature, zoom=None):
rect_proj = geo2proj.transform(rect)

# center geometry
iface = get_iface()
iface.mapCanvas().zoomToFeatureExtent(rect_proj)
else:
# set map center to feature centroid at a specified zoom
Expand All @@ -85,6 +93,7 @@ def getBounds(asGeoJSON=False):
>>> bounds = Map.getBounds(True)
>>> Map.addLayer(bounds, {}, 'bounds')
"""
iface = get_iface()
ex = iface.mapCanvas().extent()
# return ex
xmax = ex.xMaximum()
Expand Down Expand Up @@ -113,6 +122,7 @@ def getCenter():
>>> center = Map.getCenter()
>>> Map.addLayer(center, { 'color': 'red' }, 'center')
"""
iface = get_iface()
center = iface.mapCanvas().center()

crs = iface.mapCanvas().mapSettings().destinationCrs().authid()
Expand Down Expand Up @@ -143,6 +153,8 @@ def setCenter(lon, lat, zoom=None):
xform = QgsCoordinateTransform(crsSrc, crsDest, QgsProject.instance())
# forward transformation: src -> dest
center_point = xform.transform(center_point_in)

iface = get_iface()
iface.mapCanvas().setCenter(center_point)

### zoom
Expand All @@ -162,7 +174,7 @@ def getScale():
>>> from ee_plugin import Map
>>> print(Map.getScale())
"""

iface = get_iface()
return iface.mapCanvas().scale() / 1000


Expand All @@ -180,6 +192,7 @@ def getZoom():
"""

# from https://gis.stackexchange.com/questions/268890/get-current-zoom-level-from-qgis-map-canvas
iface = get_iface()
scale = iface.mapCanvas().scale()
dpi = iface.mainWindow().physicalDpiX()
maxScalePerPixel = 156543.04
Expand Down

0 comments on commit 2b3d323

Please sign in to comment.