File: layer_modification_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 (76 lines) | stat: -rw-r--r-- 2,620 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
import os
import mapnik
import pytest

from .utilities import execution_path

@pytest.fixture(scope="module")
def setup():
    # All of the paths used are relative, if we run the tests
    # from another directory we need to chdir()
    os.chdir(execution_path('.'))
    yield

def test_adding_datasource_to_layer(setup):
    if not os.path.exists('../data/shp/world_merc.shp'):
        pytest.skip('Missing data')

    map_string = '''<?xml version="1.0" encoding="utf-8"?>
<Map>

    <Layer name="world_borders">
        <StyleName>world_borders_style</StyleName>
        <StyleName>point_style</StyleName>
        <!-- leave datasource empty -->
        <!--
        <Datasource>
            <Parameter name="file">../data/shp/world_merc.shp</Parameter>
            <Parameter name="type">shape</Parameter>
        </Datasource>
        -->
    </Layer>

</Map>
'''
    m = mapnik.Map(256, 256)

    try:
        mapnik.load_map_from_string(m, map_string)

        # validate it loaded fine
        assert m.layers[0].styles[0] ==  'world_borders_style'
        assert m.layers[0].styles[1] ==  'point_style'
        assert len(m.layers) ==  1

        # also assign a variable reference to that layer
        # below we will test that this variable references
        # the same object that is attached to the map
        lyr = m.layers[0]

        # ensure that there was no datasource for the layer...
        assert m.layers[0].datasource ==  None
        assert lyr.datasource ==  None

        # also note that since the srs was black it defaulted to wgs84
        assert m.layers[0].srs == 'epsg:4326'
        assert lyr.srs ==  'epsg:4326'

        # now add a datasource one...
        ds = mapnik.Shapefile(file='../data/shp/world_merc.shp')
        m.layers[0].datasource = ds

        # now ensure it is attached
        assert m.layers[0].datasource.describe()['name'] ==  "shape"
        assert lyr.datasource.describe()['name'] ==  "shape"

        # and since we have now added a shapefile in spherical mercator, adjust
        # the projection
        lyr.srs = '+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'

        # test that assignment
        assert m.layers[0].srs == '+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
        assert lyr.srs ==  '+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
    except RuntimeError as e:
        # only test datasources that we have installed
        if not 'Could not create datasource' in str(e):
            raise RuntimeError(e)