File: shapefile_test.py

package info (click to toggle)
python-mapnik 1%3A0.0~20200224-7da019cf9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,784 kB
  • sloc: python: 12,085; cpp: 5,717; sh: 101; makefile: 18
file content (177 lines) | stat: -rw-r--r-- 6,078 bytes parent folder | download | duplicates (5)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from nose.tools import assert_almost_equal, eq_, raises
from nose.plugins.skip import SkipTest

import mapnik

from .utilities import execution_path, run_all


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('.'))

if 'shape' in mapnik.DatasourceCache.plugin_names():

    # Shapefile initialization
    def test_shapefile_init():
        if not os.path.exists('../data/shp/boundaries'):
            raise SkipTest

        s = mapnik.Shapefile(file='../data/shp/boundaries')

        e = s.envelope()

        assert_almost_equal(e.minx, -11121.6896651, places=7)
        assert_almost_equal(e.miny, -724724.216526, places=6)
        assert_almost_equal(e.maxx, 2463000.67866, places=5)
        assert_almost_equal(e.maxy, 1649661.267, places=3)

    # Shapefile properties
    def test_shapefile_properties():
        if not os.path.exists('../data/shp/boundaries'):
            raise SkipTest

        s = mapnik.Shapefile(file='../data/shp/boundaries', encoding='latin1')
        f = list(s.features_at_point(s.envelope().center()))[0]

        eq_(f['CGNS_FID'], u'6f733341ba2011d892e2080020a0f4c9')
        eq_(f['COUNTRY'], u'CAN')
        eq_(f['F_CODE'], u'FA001')
        eq_(f['NAME_EN'], u'Quebec')
        # this seems to break if icu data linking is not working
        eq_(f['NOM_FR'], u'Qu\xe9bec')
        eq_(f['NOM_FR'], u'Québec')
        eq_(f['Shape_Area'], 1512185733150.0)
        eq_(f['Shape_Leng'], 19218883.724300001)

    @raises(RuntimeError)
    def test_that_nonexistant_query_field_throws(**kwargs):
        if not os.path.exists('../data/shp/world_merc'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/world_merc')
        eq_(len(ds.fields()), 11)
        eq_(ds.fields(), ['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME',
                          'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT'])
        eq_(ds.field_types(),
            ['str',
             'str',
             'str',
             'int',
             'str',
             'int',
             'int',
             'int',
             'int',
             'float',
             'float'])
        query = mapnik.Query(ds.envelope())
        for fld in ds.fields():
            query.add_property_name(fld)
        # also add an invalid one, triggering throw
        query.add_property_name('bogus')
        ds.features(query)

    def test_dbf_logical_field_is_boolean():
        if not os.path.exists('../data/shp/long_lat'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/long_lat')
        eq_(len(ds.fields()), 7)
        eq_(ds.fields(), ['LONG', 'LAT', 'LOGICAL_TR',
                          'LOGICAL_FA', 'CHARACTER', 'NUMERIC', 'DATE'])
        eq_(ds.field_types(), ['str', 'str',
                               'bool', 'bool', 'str', 'float', 'str'])
        query = mapnik.Query(ds.envelope())
        for fld in ds.fields():
            query.add_property_name(fld)
        feat = list(ds.all_features())[0]
        eq_(feat.id(), 1)
        eq_(feat['LONG'], '0')
        eq_(feat['LAT'], '0')
        eq_(feat['LOGICAL_TR'], True)
        eq_(feat['LOGICAL_FA'], False)
        eq_(feat['CHARACTER'], '254')
        eq_(feat['NUMERIC'], 32)
        eq_(feat['DATE'], '20121202')

    # created by hand in qgis 1.8.0
    def test_shapefile_point2d_from_qgis():
        if not os.path.exists('../data/shp/points/qgis.shp'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/points/qgis.shp')
        eq_(len(ds.fields()), 2)
        eq_(ds.fields(), ['id', 'name'])
        eq_(ds.field_types(), ['int', 'str'])
        eq_(len(list(ds.all_features())), 3)

    # ogr2ogr tests/data/shp/3dpoint/ogr_zfield.shp
    # tests/data/shp/3dpoint/qgis.shp -zfield id
    def test_shapefile_point_z_from_qgis():
        if not os.path.exists('../data/shp/points/ogr_zfield.shp'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/points/ogr_zfield.shp')
        eq_(len(ds.fields()), 2)
        eq_(ds.fields(), ['id', 'name'])
        eq_(ds.field_types(), ['int', 'str'])
        eq_(len(list(ds.all_features())), 3)

    def test_shapefile_multipoint_from_qgis():
        if not os.path.exists('../data/shp/points/qgis_multi.shp'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/points/qgis_multi.shp')
        eq_(len(ds.fields()), 2)
        eq_(ds.fields(), ['id', 'name'])
        eq_(ds.field_types(), ['int', 'str'])
        eq_(len(list(ds.all_features())), 1)

    # pointzm from arcinfo
    def test_shapefile_point_zm_from_arcgis():
        if not os.path.exists('../data/shp/points/poi.shp'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/points/poi.shp')
        eq_(len(ds.fields()), 7)
        eq_(ds.fields(),
            ['interst_id',
             'state_d',
             'cnty_name',
             'latitude',
             'longitude',
             'Name',
             'Website'])
        eq_(ds.field_types(), ['str', 'str',
                               'str', 'float', 'float', 'str', 'str'])
        eq_(len(list(ds.all_features())), 17)

    # copy of the above with ogr2ogr that makes m record 14 instead of 18
    def test_shapefile_point_zm_from_ogr():
        if not os.path.exists('../data/shp/points/poi_ogr.shp'):
            raise SkipTest

        ds = mapnik.Shapefile(file='../data/shp/points/poi_ogr.shp')
        eq_(len(ds.fields()), 7)
        eq_(ds.fields(),
            ['interst_id',
             'state_d',
             'cnty_name',
             'latitude',
             'longitude',
             'Name',
             'Website'])
        eq_(ds.field_types(), ['str', 'str',
                               'str', 'float', 'float', 'str', 'str'])
        eq_(len(list(ds.all_features())), 17)

if __name__ == "__main__":
    setup()
    exit(run_all(eval(x) for x in dir() if x.startswith("test_")))