File: feature_test.py

package info (click to toggle)
python-mapnik 1%3A0.0~20240222-5ab32f020-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,364 kB
  • sloc: python: 11,685; cpp: 5,776; sh: 242; makefile: 10
file content (115 lines) | stat: -rw-r--r-- 3,589 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from binascii import unhexlify
import mapnik
import pytest

def test_default_constructor():
    f = mapnik.Feature(mapnik.Context(), 1)
    assert f is not None


def test_feature_geo_interface():
    ctx = mapnik.Context()
    feat = mapnik.Feature(ctx, 1)
    feat.geometry = mapnik.Geometry.from_wkt('Point (0 0)')
    assert feat.__geo_interface__['geometry'] == {u'type': u'Point', u'coordinates': [0, 0]}


def test_python_extended_constructor():
    context = mapnik.Context()
    context.push('foo')
    context.push('foo')
    f = mapnik.Feature(context, 1)
    wkt = 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))'
    f.geometry = mapnik.Geometry.from_wkt(wkt)
    f['foo'] = 'bar'
    assert f['foo'] ==  'bar'
    assert f.envelope(), mapnik.Box2d(10.0, 10.0, 45.0 ==  45.0)
    # reset
    f['foo'] = u"avión"
    assert f['foo'] ==  u"avión"
    f['foo'] = 1.4
    assert f['foo'] ==  1.4
    f['foo'] = True
    assert f['foo'] ==  True


def test_add_geom_wkb():
    # POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
    wkb = '010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440'
    geometry = mapnik.Geometry.from_wkb(unhexlify(wkb))
    if hasattr(geometry, 'is_valid'):
        # Those are only available when python-mapnik has been built with
        # boost >= 1.56.
        assert geometry.is_valid() ==  True
        assert geometry.is_simple() ==  True
    assert geometry.envelope(), mapnik.Box2d(10.0, 10.0, 40.0 ==  40.0)
    geometry.correct()
    if hasattr(geometry, 'is_valid'):
        # valid after calling correct
        assert geometry.is_valid() ==  True


def test_feature_expression_evaluation():
    context = mapnik.Context()
    context.push('name')
    f = mapnik.Feature(context, 1)
    f['name'] = 'a'
    assert f['name'] ==  u'a'
    expr = mapnik.Expression("[name]='a'")
    evaluated = expr.evaluate(f)
    assert evaluated ==  True
    num_attributes = len(f)
    assert num_attributes ==  1
    assert f.id() ==  1

# https://github.com/mapnik/mapnik/issues/933


def test_feature_expression_evaluation_missing_attr():
    context = mapnik.Context()
    context.push('name')
    f = mapnik.Feature(context, 1)
    f['name'] = u'a'
    assert f['name'] ==  u'a'
    expr = mapnik.Expression("[fielddoesnotexist]='a'")
    assert not 'fielddoesnotexist' in f
    try:
        expr.evaluate(f)
    except Exception as e:
        assert "Key does not exist" in str(e) ==  True
    num_attributes = len(f)
    assert num_attributes ==  1
    assert f.id() ==  1

# https://github.com/mapnik/mapnik/issues/934


def test_feature_expression_evaluation_attr_with_spaces():
    context = mapnik.Context()
    context.push('name with space')
    f = mapnik.Feature(context, 1)
    f['name with space'] = u'a'
    assert f['name with space'] ==  u'a'
    expr = mapnik.Expression("[name with space]='a'")
    assert str(expr) ==  "([name with space]='a')"
    assert expr.evaluate(f) ==  True

# https://github.com/mapnik/mapnik/issues/2390

def test_feature_from_geojson():
    with pytest.raises(RuntimeError):
        ctx = mapnik.Context()
        inline_string = """
        {
        "geometry" : {
        "coordinates" : [ 0,0 ]
        "type" : "Point"
        },
        "type" : "Feature",
        "properties" : {
        "this":"that"
        "known":"nope because missing comma"
        }
        }
        """
        mapnik.Feature.from_geojson(inline_string, ctx)