Skip to content

Commit

Permalink
Merge branch 'release/0.5.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Apr 17, 2024
2 parents bee89f6 + cf22c41 commit 3438683
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 34 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
fail_fast: true
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.15.2
hooks:
- id: pyupgrade
args:
- --py38-plus
- --keep-runtime-typing

- repo: https://github.com/ambv/black
rev: 24.3.0
rev: 24.4.0
hooks:
- id: black
language_version: python3.10

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.5
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types: [yaml]

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.15
rev: v0.16
hooks:
- id: validate-pyproject

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.9.0
hooks:
- id: mypy
verbose: true
Expand All @@ -35,7 +35,7 @@ repos:
#args: [ --strict ]

- repo: https://github.com/pycqa/flake8
rev: 6.1.0 # pick a git hash / tag to point to
rev: 7.0.0 # pick a git hash / tag to point to
hooks:
- id: flake8 # stop the build if there are Python syntax errors or undefined names
additional_dependencies: [flake8-docstrings]
Expand All @@ -56,7 +56,7 @@ repos:
- --statistics

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 # Use the ref you want to point at
rev: v4.6.0 # Use the ref you want to point at
hooks:
- id: check-added-large-files
name: check for added large files
Expand Down
2 changes: 1 addition & 1 deletion jord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

__project__ = "Jord"
__author__ = "Christian Heider Lindbjerg"
__version__ = "0.5.1"
__version__ = "0.5.2"
__doc__ = r"""
.. module:: jord
:platform: Unix, Windows
Expand Down
3 changes: 2 additions & 1 deletion jord/qgis_utilities/categorisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from itertools import cycle
from typing import Callable, Generator, Iterable, Sized

from PyQt5.Qt import QColor
# noinspection PyUnresolvedReferences
from qgis.PyQt.QtGui import QColor

# noinspection PyUnresolvedReferences
from qgis.core import (
Expand Down
1 change: 1 addition & 0 deletions jord/qgis_utilities/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
from .actions import *
from .groups import *
from .logging import *
from .randomize import *
27 changes: 26 additions & 1 deletion jord/qgis_utilities/helpers/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# noinspection PyUnresolvedReferences
from qgis.core import QgsLayerTreeGroup

__all__ = ["duplicate_groups"]
__all__ = ["duplicate_groups", "select_layer_in_group", "is_group_selected"]


def duplicate_groups(
Expand All @@ -29,3 +29,28 @@ def duplicate_groups(
new_group_parent.addChildNode(original_group_child.clone())

return new_group_parent


def select_layer_in_group(layer_name, group_name):
# noinspection PyUnresolvedReferences
from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer, QgsProject

# noinspection PyUnresolvedReferences
from qgis.utils import iface

group = QgsProject.instance().layerTreeRoot().findGroup(group_name)
if group is not None:
for child in group.children():
if child.name() == layer_name:
iface.setActiveLayer(child.layer())


def is_group_selected(group_name):
# noinspection PyUnresolvedReferences
from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer, QgsProject

# noinspection PyUnresolvedReferences
from qgis.utils import iface

group = QgsProject.instance().layerTreeRoot().findGroup(group_name)
return group in iface.layerTreeView().selectedNodes()
42 changes: 42 additions & 0 deletions jord/qgis_utilities/helpers/randomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import uuid

__all__ = ["randomize_field", "randomize_sub_tree_field"]


def randomize_sub_tree_field(selected_nodes, field_name: str) -> None:
# noinspection PyUnresolvedReferences
from qgis.core import QgsLayerTreeGroup, QgsLayerTreeLayer

for group in selected_nodes:
if isinstance(group, QgsLayerTreeLayer):
randomize_field(group, field_name=field_name)
elif isinstance(group, QgsLayerTreeGroup):
randomize_sub_tree_field(group.children(), field_name=field_name)
else:
...


def randomize_field(tree_layer, field_name: str) -> None: #: QgsLayerTreeLayer
"""
https://qgis.org/pyqgis/3.28/core/QgsVectorLayer.html#qgis.core.QgsVectorLayer.changeAttributeValues
changeAttributeValues
fid: int, newValues: Dict[int, Any], oldValues: Dict[int, Any] = {}, skipDefaultValues: bool = False
:param field_name:
:param tree_layer:
:return:
"""
layer = tree_layer.layer()

field_idx = layer.fields().indexFromName(field_name)

if field_idx >= 0:
layer.startEditing()
layer.beginEditCommand(f"Regenerate {field_name}")
for i in range(layer.featureCount() + 1):
layer.changeAttributeValue(i, field_idx, uuid.uuid4().hex)

# layer.rollBack()
layer.endEditCommand()
layer.commitChanges()
12 changes: 6 additions & 6 deletions jord/qgis_utilities/layer_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def add_qgis_single_feature_layer(
categorise_by_attribute in fields
), f"{categorise_by_attribute} was not found in {fields}"

if not isinstance(qgis_instance_handle, QgsProject):
qgis_project = qgis_instance_handle.qgis_project
elif qgis_instance_handle is None:
if qgis_instance_handle is None:
qgis_project = QgsProject.instance()
elif not isinstance(qgis_instance_handle, QgsProject):
qgis_project = qgis_instance_handle.qgis_project
else:
qgis_project = qgis_instance_handle

Expand Down Expand Up @@ -429,10 +429,10 @@ def add_qgis_multi_feature_layer(
layer.updateFields()
layer.updateExtents()

if not isinstance(qgis_instance_handle, QgsProject):
qgis_project = qgis_instance_handle.qgis_project
elif qgis_instance_handle is None:
if qgis_instance_handle is None:
qgis_project = QgsProject.instance()
elif not isinstance(qgis_instance_handle, QgsProject):
qgis_project = qgis_instance_handle.qgis_project
else:
qgis_project = qgis_instance_handle

Expand Down
3 changes: 2 additions & 1 deletion jord/qgis_utilities/styling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import logging
from typing import Mapping

from PyQt5.Qt import QColor
# noinspection PyUnresolvedReferences
from qgis.PyQt.QtGui import QColor

# noinspection PyUnresolvedReferences
from qgis.core import (
Expand Down
1 change: 1 addition & 0 deletions jord/qt_utilities/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
]

try:

from PyQt6.QtCore import Qt

class AlignmentFlag(Flag):
Expand Down
25 changes: 14 additions & 11 deletions jord/shapely_utilities/morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from warg import passes_kws_to

FALLBACK_CAPSTYLE = shapely.BufferCapStyle.round # CAN BE OVERRIDDEN
FALLBACK_JOINSTYLE = shapely.BufferCapStyle.round # CAN BE OVERRIDDEN


@passes_kws_to(shapely.geometry.base.BaseGeometry.buffer)
Expand All @@ -23,7 +24,7 @@ def morphology_buffer(
if isinstance(geom, shapely.GeometryCollection):
return shapely.GeometryCollection(
[
dilation(
morphology_buffer(
g,
distance=distance,
cap_style=cap_style,
Expand All @@ -34,16 +35,19 @@ def morphology_buffer(
]
)

if not isinstance(geom, (shapely.Polygon, shapely.MultiPolygon)):
if not isinstance(
geom, (shapely.Polygon, shapely.MultiPolygon)
): # So if line(s) or point(s)
return geom

if (
isinstance(geom, shapely.Point) and cap_style == shapely.BufferCapStyle.flat
): # parameter NONSENSE, probably not what is intended
cap_style = FALLBACK_CAPSTYLE
join_style = FALLBACK_JOINSTYLE

return geom.buffer(
distance=-distance, cap_style=cap_style, join_style=join_style, **kwargs
distance=distance, cap_style=cap_style, join_style=join_style, **kwargs
)


Expand All @@ -61,13 +65,7 @@ def erosion(geom: BaseGeometry, distance: float = 1e-7, **kwargs) -> BaseGeometr


@passes_kws_to(morphology_buffer)
def dilation(
geom: BaseGeometry,
distance: float = 1e-7,
cap_style: shapely.BufferCapStyle = shapely.BufferCapStyle.flat,
join_style: shapely.BufferJoinStyle = shapely.BufferJoinStyle.mitre,
**kwargs
) -> BaseGeometry:
def dilation(geom: BaseGeometry, distance: float = 1e-7, **kwargs) -> BaseGeometry:
"""
:param cap_style:
Expand Down Expand Up @@ -301,6 +299,11 @@ def ahfuas3232hdu():
print(dilate(lr))
print(lr.buffer(0))

ahfuas3232hdu()
def simple_dilate_example():
print(dilate(shapely.Point(0, 0)))
print(dilate(shapely.Point(0, 0), distance=0))

simple_dilate_example()
# ahfuas3232hdu()
# ahfuashdu()
# aishdjauisd()
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apppath>=1.0.2
draugr>=1.0.9
numpy>=1.20.0
pyzmq
sorcery
sympy
tqdm
pyzmq>=1.1.1
sorcery>=0.2.0
sympy>=1.1.1
tqdm>=1.1.1
warg>=1.1.6
2 changes: 1 addition & 1 deletion requirements/requirements_fiona.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fiona
fiona>=1.1.1
2 changes: 1 addition & 1 deletion requirements/requirements_qt.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pyqt5-tools
PySide2
40 changes: 40 additions & 0 deletions tests/shapely/morphology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
import shapely

from jord.shapely_utilities import dilate, erode, opening, closing


@pytest.mark.parametrize(
"shape",
(
shapely.Point(0, 0),
shapely.LinearRing([(-1, -1), (1, 1), (1, 1), (1, -1), (-1, -1)]),
shapely.LineString([(-1, -1), (1, 1), (1, 1), (1, -1), (-1, -1)]),
shapely.Polygon(
shapely.LinearRing([(-1, -1), (1, 1), (1, 1), (1, -1), (-1, -1)])
),
shapely.MultiPolygon(
[
shapely.Polygon(
shapely.LinearRing([(-1, -1), (1, 1), (1, 1), (1, -1), (-1, -1)])
)
]
),
shapely.MultiPoint([shapely.Point(0, 0)]),
shapely.MultiLineString(
[shapely.LineString([(-1, -1), (1, 1), (1, 1), (1, -1), (-1, -1)])]
),
shapely.GeometryCollection(
[
shapely.Point(0, 0),
shapely.LineString([(-1, -1), (1, 1), (1, 1), (1, -1), (-1, -1)]),
]
),
),
)
@pytest.mark.parametrize(
"operation",
((dilate, erode, closing, opening)),
)
def test_operation_zero(operation, shape):
shape.equals(operation(shape, distance=0))

0 comments on commit 3438683

Please sign in to comment.