Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ci #182

Merged
merged 11 commits into from
Dec 27, 2024
Merged
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
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: QGIS Plugin CI

on:
push:
branches:
- '**'
pull_request:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up QGIS environment
run: |
sudo apt update
sudo apt install -y qgis python3-qgis python3-pyqt5 python3-pytest

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
pip install -r requirements.txt

- name: pre-commit
run: |
pip install pre-commit
pre-commit run --all-files

- name: Run tests
run: python -m pytest test/
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
- id: ruff-format
111 changes: 59 additions & 52 deletions Map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,56 @@
"""
functions to use GEE within Qgis python script
"""

import math
import ee

from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsProject, QgsPointXY, QgsRectangle
from qgis.utils import iface
import ee
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsPointXY,
QgsProject,
QgsRectangle,
)
from qgis.PyQt.QtCore import QEventLoop, QTimer
from qgis.utils import iface


def addLayer(eeObject, visParams=None, name=None, shown=True, opacity=1.0):
"""
Adds a given EE object to the map as a layer.
Adds a given EE object to the map as a layer.

https://developers.google.com/earth-engine/api_docs#mapaddlayer
https://developers.google.com/earth-engine/api_docs#mapaddlayer

Uses:
>>> from ee_plugin import Map
>>> Map.addLayer(.....)
Uses:
>>> from ee_plugin import Map
>>> Map.addLayer(.....)
"""
from ee_plugin.utils import add_or_update_ee_layer
from .utils import add_or_update_ee_layer

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


def centerObject(feature, zoom=None):
"""
Centers the map view on a given object.
Centers the map view on a given object.

https://developers.google.com/earth-engine/api_docs#mapcenterobject
https://developers.google.com/earth-engine/api_docs#mapcenterobject

Uses:
>>> from ee_plugin import Map
>>> Map.centerObject(feature)
Uses:
>>> from ee_plugin import Map
>>> Map.centerObject(feature)
"""

if not hasattr(feature, 'geometry'):
if not hasattr(feature, "geometry"):
feature = ee.Feature(feature)

if not zoom:
# make sure our geometry is in geo
rect = feature.geometry().transform(ee.Projection('EPSG:4326'), 1)
rect = feature.geometry().transform(ee.Projection("EPSG:4326"), 1)

# get coordinates
coords = rect.bounds().getInfo()['coordinates'][0]
coords = rect.bounds().getInfo()["coordinates"][0]
xmin = coords[0][0]
ymin = coords[0][1]
xmax = coords[2][0]
Expand All @@ -54,7 +61,7 @@ def centerObject(feature, zoom=None):
rect = QgsRectangle(xmin, ymin, xmax, ymax)

# transform rect to a crs used by current project
crs_src = QgsCoordinateReferenceSystem('EPSG:4326')
crs_src = QgsCoordinateReferenceSystem("EPSG:4326")
crs_dst = QgsCoordinateReferenceSystem(QgsProject.instance().crs())
geo2proj = QgsCoordinateTransform(crs_src, crs_dst, QgsProject.instance())
rect_proj = geo2proj.transform(rect)
Expand All @@ -69,14 +76,14 @@ def centerObject(feature, zoom=None):

def getBounds(asGeoJSON=False):
"""
Returns the bounds of the current map view, as a list in the format [west, south, east, north] in degrees.
Returns the bounds of the current map view, as a list in the format [west, south, east, north] in degrees.

https://developers.google.com/earth-engine/api_docs#mapgetbounds
https://developers.google.com/earth-engine/api_docs#mapgetbounds

Uses:
>>> from ee_plugin import Map
>>> bounds = Map.getBounds(True)
>>> Map.addLayer(bounds, {}, 'bounds')
Uses:
>>> from ee_plugin import Map
>>> bounds = Map.getBounds(True)
>>> Map.addLayer(bounds, {}, 'bounds')
"""
ex = iface.mapCanvas().extent()
# return ex
Expand All @@ -97,14 +104,14 @@ def getBounds(asGeoJSON=False):

def getCenter():
"""
Returns the coordinates at the center of the map.
Returns the coordinates at the center of the map.

https://developers.google.com/earth-engine/api_docs#mapgetcenter
https://developers.google.com/earth-engine/api_docs#mapgetcenter

Uses:
>>> from ee_plugin import Map
>>> center = Map.getCenter()
>>> Map.addLayer(center, { 'color': 'red' }, 'center')
Uses:
>>> from ee_plugin import Map
>>> center = Map.getCenter()
>>> Map.addLayer(center, { 'color': 'red' }, 'center')
"""
center = iface.mapCanvas().center()

Expand All @@ -115,13 +122,13 @@ def getCenter():

def setCenter(lon, lat, zoom=None):
"""
Centers the map view at the given coordinates with the given zoom level. If no zoom level is provided, it uses the most recent zoom level on the map.
Centers the map view at the given coordinates with the given zoom level. If no zoom level is provided, it uses the most recent zoom level on the map.

https://developers.google.com/earth-engine/api_docs#mapsetcenter
https://developers.google.com/earth-engine/api_docs#mapsetcenter

Uses:
>>> from ee_plugin import Map
>>> Map.setCenter(lon, lat, zoom)
Uses:
>>> from ee_plugin import Map
>>> Map.setCenter(lon, lat, zoom)
"""
# wait 100 milliseconds while load the ee image
loop = QEventLoop()
Expand All @@ -130,7 +137,7 @@ def setCenter(lon, lat, zoom=None):
### center
center_point_in = QgsPointXY(lon, lat)
# convert coordinates
crsSrc = QgsCoordinateReferenceSystem('EPSG:4326')
crsSrc = QgsCoordinateReferenceSystem("EPSG:4326")
crsDest = QgsCoordinateReferenceSystem(QgsProject.instance().crs())
xform = QgsCoordinateTransform(crsSrc, crsDest, QgsProject.instance())
# forward transformation: src -> dest
Expand All @@ -146,29 +153,29 @@ def setCenter(lon, lat, zoom=None):

def getScale():
"""
Returns the approximate pixel scale of the current map view, in meters.
Returns the approximate pixel scale of the current map view, in meters.

https://developers.google.com/earth-engine/api_docs#mapgetscale
https://developers.google.com/earth-engine/api_docs#mapgetscale

Uses:
>>> from ee_plugin import Map
>>> print(Map.getScale())
Uses:
>>> from ee_plugin import Map
>>> print(Map.getScale())
"""

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


def getZoom():
"""
Returns the current zoom level of the map.
Returns the current zoom level of the map.

https://developers.google.com/earth-engine/api_docs#mapgetzoom
https://developers.google.com/earth-engine/api_docs#mapgetzoom

Note that in QGIS zoom is a floating point number
Note that in QGIS zoom is a floating point number

Uses:
>>> from ee_plugin import Map
>>> print(Map.getZoom())
Uses:
>>> from ee_plugin import Map
>>> print(Map.getZoom())
"""

# from https://gis.stackexchange.com/questions/268890/get-current-zoom-level-from-qgis-map-canvas
Expand All @@ -183,13 +190,13 @@ def getZoom():

def setZoom(zoom):
"""
Sets the zoom level of the map.
Sets the zoom level of the map.

https://developers.google.com/earth-engine/api_docs#mapsetzoom
https://developers.google.com/earth-engine/api_docs#mapsetzoom

Uses:
>>> from ee_plugin import Map
>>> Map.setZoom(15)
Uses:
>>> from ee_plugin import Map
>>> Map.setZoom(15)
"""

center = getCenter()
Expand Down
13 changes: 9 additions & 4 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- coding: utf-8 -*-
import os
import site

import pkg_resources

__version__ = '0.0.7'
__version__ = "0.0.7"


def add_ee_dependencies():
extra_libs_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'extlibs'))
extra_libs_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), "extlibs")
)

if os.path.isdir(extra_libs_path):
# add to python path
Expand All @@ -28,13 +32,14 @@ def classFactory(iface): # pylint: disable=invalid-name

# set User Agent for all calls
import ee
user_agent = f'QGIS_EE/{__version__}'

user_agent = f"QGIS_EE/{__version__}"
if ee.data.getUserAgent() != user_agent:
ee.data.setUserAgent(user_agent)


# authenticate and initialize EE
from . import ee_auth

ee_auth.authenticate_and_set_project()

# start
Expand Down
Loading
Loading