Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Jan 4, 2024
2 parents 7cb78d9 + 1d71ef1 commit fab30fc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 26 deletions.
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.3.3"
__version__ = "0.4.0"
__doc__ = r"""
.. module:: jord
:platform: Unix, Windows
Expand Down
12 changes: 6 additions & 6 deletions jord/qgis_utilities/categorisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import random
from itertools import cycle
from typing import Iterable, Sized, Callable, Generator
from typing import Callable, Generator, Iterable, Sized

import qgis
from PyQt5.Qt import QColor

# noinspection PyUnresolvedReferences
from qgis.core import (
QgsVectorLayer,
QgsSymbol,
QgsRendererCategory,
QgsCategorizedSymbolRenderer,
QgsRendererCategory,
QgsSymbol,
QgsVectorLayer,
)
import qgis
from warg import TripleNumber, QuadNumber, n_uint_mix_generator_builder
from warg import QuadNumber, TripleNumber, n_uint_mix_generator_builder

__all__ = ["categorise_layer"]

Expand Down
8 changes: 5 additions & 3 deletions jord/qgis_utilities/layer_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from warg import passes_kws_to

from jord.geojson_utilities import GeoJsonGeometryTypesEnum
from .categorisation import categorise_layer

APPEND_TIMESTAMP = True
SKIP_MEMORY_LAYER_CHECK_AT_CLOSE = True
Expand Down Expand Up @@ -62,6 +61,7 @@ def add_qgis_single_feature_layer(
QgsRasterLayer,
QgsProject,
)
from .categorisation import categorise_layer

# noinspection PyUnresolvedReferences
import qgis
Expand Down Expand Up @@ -283,6 +283,7 @@ def add_qgis_multi_feature_layer(

# noinspection PyUnresolvedReferences
from qgis.core import QgsVectorLayer, QgsFeature
from .categorisation import categorise_layer

# noinspection PyUnresolvedReferences
import qgis
Expand Down Expand Up @@ -374,9 +375,10 @@ def add_qgis_multi_feature_layer(
feat = QgsFeature()

if attr_generator:
if sample_row:
row = next(attr_generator, None)
if row:
feat.initAttributes(num_cols)
feat.setAttributes(list(next(attr_generator).values()))
feat.setAttributes(list(row.values()))

feat.setGeometry(geom)
features.append(feat)
Expand Down
62 changes: 46 additions & 16 deletions jord/shapely_utilities/morphology.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,79 @@
#!/usr/bin/env python3


import shapely
from shapely.geometry.base import BaseGeometry

__all__ = ["closing", "opening", "erode", "erosion", "dilate", "dilation", "close"]

from warg import passes_kws_to

FALLBACK_CAPSTYLE = shapely.BufferCapStyle.round # CAN BE OVERRIDEN


def erosion(geom: BaseGeometry, eps: float = 1e-7) -> BaseGeometry:
@passes_kws_to(shapely.geometry.base.BaseGeometry.buffer)
def erosion(
geom: BaseGeometry,
distance: float = 1e-7,
cap_style: shapely.BufferCapStyle = shapely.BufferCapStyle.flat,
join_style: shapely.BufferJoinStyle = shapely.BufferJoinStyle.mitre,
**kwargs
) -> BaseGeometry:
"""
:param distance:
:param cap_style:
:param join_style:
:param geom: The geometry to be eroded
:param eps: Erosion amount
:return: The eroded geometry
"""
return geom.buffer(-eps)


def dilation(geom: BaseGeometry, eps: float = 1e-7) -> BaseGeometry:
return geom.buffer(
distance=-distance, cap_style=cap_style, join_style=join_style, **kwargs
)


@passes_kws_to(shapely.geometry.base.BaseGeometry.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:
"""
:param cap_style:
:param join_style:
:param geom: The geometry to be dilated
:param eps: Dilation amount
:param distance: Dilation amount
:return: The dilated geometry
"""
return geom.buffer(eps)
if (
isinstance(geom, shapely.Point) and cap_style == shapely.BufferCapStyle.flat
): # NONSENSE, propably not what is intended
cap_style = FALLBACK_CAPSTYLE

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


def closing(geom: BaseGeometry, eps: float = 1e-7) -> BaseGeometry:
@passes_kws_to(shapely.geometry.base.BaseGeometry.buffer)
def closing(geom: BaseGeometry, **kwargs) -> BaseGeometry:
"""
:param geom: The geometry to be closed
:param eps: Dilation and Erosion amount
:return: The closed geometry
"""
return erode(dilate(geom, eps), eps)
return erode(dilate(geom, **kwargs), **kwargs)


def opening(geom: BaseGeometry, eps: float = 1e-7) -> BaseGeometry:
@passes_kws_to(shapely.geometry.base.BaseGeometry.buffer)
def opening(geom: BaseGeometry, **kwargs) -> BaseGeometry:
"""
:param geom: The geometry to be opened
:param eps: Erosion and Dilation amount
:return: The opened geometry
"""
return dilate(erode(geom, eps), eps)
return dilate(erode(geom, **kwargs), **kwargs)


# open = opening # keyword clash
Expand Down
17 changes: 17 additions & 0 deletions samples/shapely_samples/morph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import shapely
from shapely import LineString, Point
from shapely.geometry import Polygon

point = Point([(0.0, 0.0)])
line = LineString([(0.5, 0.5), (0.5, 1.5), (1.5, 1.5), (1.5, 0.5)])
poly = Polygon([(0.25, 0.25), (0.25, 0.75), (0.75, 0.75), (0.75, 0.25)])

print(point.buffer(1, cap_style="flat")) # WHA
print(line.buffer(1, cap_style="flat"))
print(poly.buffer(1, cap_style="flat"))

print(point.buffer(-1, cap_style="flat"))
print(line.buffer(-1, cap_style="flat"))
print(poly.buffer(-1, cap_style="flat"))

print(point.buffer(1, cap_style=shapely.BufferCapStyle.square))
26 changes: 26 additions & 0 deletions samples/shapely_samples/relations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from shapely.geometry import Polygon

poly1 = Polygon([(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)])
poly2 = Polygon([(0.5, 0.5), (0.5, 1.5), (1.5, 1.5), (1.5, 0.5)])
poly3 = Polygon([(0.25, 0.25), (0.25, 0.75), (0.75, 0.75), (0.75, 0.25)])

print(poly1.intersection(poly2))
print(poly1.intersects(poly2))

print(poly1.union(poly2))
print(poly1.overlaps(poly2))

print(poly1.touches(poly2))

print(poly1.contains(poly2))

print("wah")
print(poly1.intersection(poly3))
print(poly1.intersects(poly3))

print(poly1.union(poly3))
print(poly1.overlaps(poly3))

print(poly1.touches(poly3))

print(poly1.contains(poly3))

0 comments on commit fab30fc

Please sign in to comment.