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 5829d90
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 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,7 +74,7 @@ def centerObject(feature, zoom=None):
rect_proj = geo2proj.transform(rect)

# center geometry
iface.mapCanvas().zoomToFeatureExtent(rect_proj)
get_iface().mapCanvas().zoomToFeatureExtent(rect_proj)
else:
# set map center to feature centroid at a specified zoom
center = feature.geometry().centroid().coordinates().getInfo()
Expand All @@ -85,7 +92,7 @@ def getBounds(asGeoJSON=False):
>>> bounds = Map.getBounds(True)
>>> Map.addLayer(bounds, {}, 'bounds')
"""
ex = iface.mapCanvas().extent()
ex = get_iface().mapCanvas().extent()
# return ex
xmax = ex.xMaximum()
ymax = ex.yMaximum()
Expand All @@ -97,7 +104,7 @@ def getBounds(asGeoJSON=False):
return [xmin, ymin, xmax, ymax]

# return as geometry
crs = iface.mapCanvas().mapSettings().destinationCrs().authid()
crs = get_iface().mapCanvas().mapSettings().destinationCrs().authid()

return ee.Geometry.Rectangle([xmin, ymin, xmax, ymax], crs, False)

Expand All @@ -113,9 +120,9 @@ def getCenter():
>>> center = Map.getCenter()
>>> Map.addLayer(center, { 'color': 'red' }, 'center')
"""
center = iface.mapCanvas().center()
center = get_iface().mapCanvas().center()

crs = iface.mapCanvas().mapSettings().destinationCrs().authid()
crs = get_iface().mapCanvas().mapSettings().destinationCrs().authid()

return ee.Geometry.Point([center.x(), center.y()], crs)

Expand Down Expand Up @@ -143,13 +150,13 @@ def setCenter(lon, lat, zoom=None):
xform = QgsCoordinateTransform(crsSrc, crsDest, QgsProject.instance())
# forward transformation: src -> dest
center_point = xform.transform(center_point_in)
iface.mapCanvas().setCenter(center_point)
get_iface().mapCanvas().setCenter(center_point)

### zoom
if zoom is not None:
# transform the zoom level to scale
scale_value = 591657550.5 / 2 ** (zoom - 1)
iface.mapCanvas().zoomScale(scale_value)
get_iface().mapCanvas().zoomScale(scale_value)


def getScale():
Expand All @@ -163,7 +170,7 @@ def getScale():
>>> print(Map.getScale())
"""

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


def getZoom():
Expand All @@ -180,8 +187,8 @@ def getZoom():
"""

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

0 comments on commit 5829d90

Please sign in to comment.