File: test_geo.py

package info (click to toggle)
ezdxf 0.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 46,952 kB
  • sloc: python: 158,141; javascript: 166; cpp: 138; makefile: 116; lisp: 20
file content (65 lines) | stat: -rw-r--r-- 1,836 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#  Copyright (c) 2020, Manfred Moitzi
#  License: MIT License
import pytest
from ezdxf.entities import factory
from ezdxf.render.forms import square, translate
from ezdxf.lldxf import const
from ezdxf.addons import geo

shapely_geometry = pytest.importorskip("shapely.geometry")


def test_shapely_geo_interface():
    point = shapely_geometry.shape(
        {
            "type": "Point",
            "coordinates": (0, 0),
        }
    )
    assert (point.x, point.y) == (0, 0)


def validate(geo_proxy: geo.GeoProxy) -> bool:
    return shapely_geometry.shape(geo_proxy).is_valid


def test_resolved_hatch_with_intersecting_holes():
    hatch = factory.new("HATCH")
    paths = hatch.paths
    paths.add_polyline_path(square(10), flags=const.BOUNDARY_PATH_EXTERNAL)
    paths.add_polyline_path(
        translate(square(3), (1, 1)), flags=const.BOUNDARY_PATH_DEFAULT
    )
    paths.add_polyline_path(
        translate(square(3), (2, 2)), flags=const.BOUNDARY_PATH_DEFAULT
    )
    p = geo.proxy(hatch)
    # Overlapping holes already resolved by fast_bbox_detection()
    polygon = shapely_geometry.shape(p)
    assert polygon.is_valid is True

    p.filter(validate)
    assert p.root["type"] == "Polygon"
    assert len(p.root["coordinates"]) == 2


def test_valid_hatch():
    hatch = factory.new("HATCH")
    paths = hatch.paths
    paths.add_polyline_path(square(10), flags=const.BOUNDARY_PATH_EXTERNAL)
    paths.add_polyline_path(
        translate(square(3), (1, 1)), flags=const.BOUNDARY_PATH_DEFAULT
    )
    paths.add_polyline_path(
        translate(square(3), (5, 1)), flags=const.BOUNDARY_PATH_DEFAULT
    )
    p = geo.proxy(hatch)
    polygon = shapely_geometry.shape(p)
    assert polygon.is_valid is True

    p.filter(validate)
    assert p.root != {}


if __name__ == "__main__":
    pytest.main([__file__])