File: feature_id_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 (87 lines) | stat: -rw-r--r-- 3,051 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python

import os

from nose.tools import eq_
from nose.plugins.skip import SkipTest

import mapnik

from .utilities import execution_path, run_all

try:
    import itertools.izip as zip
except ImportError:
    pass


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


def compare_shape_between_mapnik_and_ogr(shapefile, query=None):
    if not os.path.exists(shapefile):
        raise SkipTest

    plugins = mapnik.DatasourceCache.plugin_names()
    if 'shape' in plugins and 'ogr' in plugins:
        ds1 = mapnik.Ogr(file=shapefile, layer_by_index=0)
        ds2 = mapnik.Shapefile(file=shapefile)
        if query:
            fs1 = ds1.features(query)
            fs2 = ds2.features(query)
        else:
            fs1 = ds1.featureset()
            fs2 = ds2.featureset()
        count = 0
        for feat1, feat2 in zip(fs1, fs2):
            count += 1
            eq_(feat1.id(), feat2.id(),
                '%s : ogr feature id %s "%s" does not equal shapefile feature id %s "%s"'
                % (count, feat1.id(), str(feat1.attributes), feat2.id(), str(feat2.attributes)))
    return True


def test_shapefile_line_featureset_id():
    compare_shape_between_mapnik_and_ogr('../data/shp/polylines.shp')


def test_shapefile_polygon_featureset_id():
    compare_shape_between_mapnik_and_ogr('../data/shp/poly.shp')


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

    bbox = (15523428.2632, 4110477.6323, -11218494.8310, 7495720.7404)
    query = mapnik.Query(mapnik.Box2d(*bbox))
    if 'ogr' in mapnik.DatasourceCache.plugin_names():
        ds = mapnik.Ogr(file='../data/shp/world_merc.shp', layer_by_index=0)
        for fld in ds.fields():
            query.add_property_name(fld)
        compare_shape_between_mapnik_and_ogr(
            '../data/shp/world_merc.shp', query)


def test_feature_hit_count():
    pass
    #raise Todo("need to optimize multigeom bbox handling in shapeindex: https://github.com/mapnik/mapnik/issues/783")
    # results in different results between shp and ogr!
    #bbox = (-14284551.8434, 2074195.1992, -7474929.8687, 8140237.7628)
    #bbox = (1113194.91,4512803.085,2226389.82,6739192.905)
    #query = mapnik.Query(mapnik.Box2d(*bbox))
    # if 'ogr' in mapnik.DatasourceCache.plugin_names():
    #    ds1 = mapnik.Ogr(file='../data/shp/world_merc.shp',layer_by_index=0)
    #    for fld in ds1.fields():
    #        query.add_property_name(fld)
    #    ds2 = mapnik.Shapefile(file='../data/shp/world_merc.shp')
    #    count1 = len(ds1.features(query).features)
    #    count2 = len(ds2.features(query).features)
    #    eq_(count1,count2,"Feature count differs between OGR driver (%s features) and Shapefile Driver (%s features) when querying the same bbox" % (count1,count2))

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