mapnik/tests/python_tests/datasource_test.py
Artem Pavlenko 14700dba16 switch back from mapnik2 to mapnik
add mapnik2 module which redirects to mapnik and issues DeprecationWarning
2011-11-23 11:33:58 +00:00

114 lines
4.2 KiB
Python

#!/usr/bin/env python
from nose.tools import *
from utilities import execution_path
import os, mapnik
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 test_that_datasources_exist():
if len(mapnik.DatasourceCache.instance().plugin_names()) == 0:
print '***NOTICE*** - no datasource plugins have been loaded'
def test_field_listing():
lyr = mapnik.Layer('test')
if 'shape' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Shapefile(file='../data/shp/poly.shp')
fields = lyr.datasource.fields()
eq_(fields, ['AREA', 'EAS_ID', 'PRFEDEA'])
def test_total_feature_count_shp():
lyr = mapnik.Layer('test')
if 'shape' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Shapefile(file='../data/shp/poly.shp')
features = lyr.datasource.all_features()
num_feats = len(features)
eq_(num_feats, 10)
def test_total_feature_count_json():
lyr = mapnik.Layer('test')
if 'ogr' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Ogr(file='../data/json/points.json',layer_by_index=0)
features = lyr.datasource.all_features()
num_feats = len(features)
eq_(num_feats, 5)
def test_reading_json_from_string():
json = open('../data/json/points.json','r').read()
lyr = mapnik.Layer('test')
if 'ogr' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Ogr(file=json,layer_by_index=0)
features = lyr.datasource.all_features()
num_feats = len(features)
eq_(num_feats, 5)
def test_feature_envelope():
lyr = mapnik.Layer('test')
if 'shape' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Shapefile(file='../data/shp/poly.shp')
features = lyr.datasource.all_features()
for feat in features:
env = feat.envelope()
contains = lyr.envelope().contains(env)
eq_(contains, True)
intersects = lyr.envelope().contains(env)
eq_(intersects, True)
def test_feature_attributes():
lyr = mapnik.Layer('test')
if 'shape' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Shapefile(file='../data/shp/poly.shp')
features = lyr.datasource.all_features()
feat = features[0]
attrs = {'PRFEDEA': u'35043411', 'EAS_ID': 168, 'AREA': 215229.266}
eq_(feat.attributes, attrs)
eq_(lyr.datasource.fields(),['AREA', 'EAS_ID', 'PRFEDEA'])
eq_(lyr.datasource.field_types(),['float','int','str'])
def test_ogr_layer_by_sql():
lyr = mapnik.Layer('test')
if 'ogr' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Ogr(file='../data/shp/poly.shp', layer_by_sql='SELECT * FROM poly WHERE EAS_ID = 168')
features = lyr.datasource.all_features()
num_feats = len(features)
eq_(num_feats, 1)
def test_hit_grid():
import os
from itertools import groupby
def rle_encode(l):
""" encode a list of strings with run-length compression """
return ["%d:%s" % (len(list(group)), name) for name, group in groupby(l)]
m = mapnik.Map(256,256);
try:
mapnik.load_map(m,'../data/good_maps/agg_poly_gamma_map.xml');
m.zoom_all()
join_field = 'NAME'
fg = [] # feature grid
for y in range(0, 256, 4):
for x in range(0, 256, 4):
featureset = m.query_map_point(0,x,y)
added = False
for feature in featureset.features:
fg.append(feature[join_field])
added = True
if not added:
fg.append('')
hit_list = '|'.join(rle_encode(fg))
eq_(hit_list[:16],'730:|2:Greenland')
eq_(hit_list[-12:],'1:Chile|812:')
except RuntimeError, e:
# only test datasources that we have installed
if not 'Could not create datasource' in str(e):
raise RuntimeError(str(e))
if __name__ == '__main__':
setup()
[eval(run)() for run in dir() if 'test_' in run]