From 247755df3fbfc273b6f4de9237363bf60c31e554 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 28 Oct 2011 17:06:23 -0700 Subject: [PATCH] ensure the tests can be run even with no datasource plugins --- tests/data/good_maps/point_json_inline.xml | 1 + tests/python_tests/datasource_test.py | 113 +++--- tests/python_tests/feature_id_test.py | 68 ++-- tests/python_tests/feature_test.py | 2 +- tests/python_tests/glyph_symbolizer_test.py | 107 ++--- tests/python_tests/layer_modification_test.py | 71 ++-- tests/python_tests/load_map_test.py | 19 +- tests/python_tests/multi_tile_raster_test.py | 99 ++--- tests/python_tests/object_test.py | 58 +-- tests/python_tests/raster_colorizer_test.py | 13 +- tests/python_tests/raster_symbolizer_test.py | 274 ++++++------- tests/python_tests/render_test.py | 16 +- tests/python_tests/sqlite_test.py | 364 +++++++++--------- 13 files changed, 621 insertions(+), 584 deletions(-) diff --git a/tests/data/good_maps/point_json_inline.xml b/tests/data/good_maps/point_json_inline.xml index 9632de486..38bcfb9d8 100644 --- a/tests/data/good_maps/point_json_inline.xml +++ b/tests/data/good_maps/point_json_inline.xml @@ -62,6 +62,7 @@ } + GeoJson 0 ogr diff --git a/tests/python_tests/datasource_test.py b/tests/python_tests/datasource_test.py index a1d4e089b..12ded0e84 100644 --- a/tests/python_tests/datasource_test.py +++ b/tests/python_tests/datasource_test.py @@ -12,59 +12,66 @@ def setup(): def test_field_listing(): lyr = mapnik2.Layer('test') - lyr.datasource = mapnik2.Shapefile(file='../data/shp/poly.shp') - fields = lyr.datasource.fields() - eq_(fields, ['AREA', 'EAS_ID', 'PRFEDEA']) + if 'shape' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.Shapefile(file='../data/shp/poly.shp') + fields = lyr.datasource.fields() + eq_(fields, ['AREA', 'EAS_ID', 'PRFEDEA']) def test_total_feature_count_shp(): lyr = mapnik2.Layer('test') - lyr.datasource = mapnik2.Shapefile(file='../data/shp/poly.shp') - features = lyr.datasource.all_features() - num_feats = len(features) - eq_(num_feats, 10) + if 'shape' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.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 = mapnik2.Layer('test') - lyr.datasource = mapnik2.Ogr(file='../data/json/points.json',layer_by_index=0) - features = lyr.datasource.all_features() - num_feats = len(features) - eq_(num_feats, 5) + if 'ogr' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.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 = mapnik2.Layer('test') - lyr.datasource = mapnik2.Ogr(file=json,layer_by_index=0) - features = lyr.datasource.all_features() - num_feats = len(features) - eq_(num_feats, 5) + if 'ogr' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.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 = mapnik2.Layer('test') - lyr.datasource = mapnik2.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) + if 'shape' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.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 = mapnik2.Layer('test') - lyr.datasource = mapnik2.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']) + if 'shape' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.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 = mapnik2.Layer('test') - lyr.datasource = mapnik2.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) + if 'ogr' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.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 @@ -75,22 +82,28 @@ def test_hit_grid(): return ["%d:%s" % (len(list(group)), name) for name, group in groupby(l)] m = mapnik2.Map(256,256); - mapnik2.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:') + try: + mapnik2.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() diff --git a/tests/python_tests/feature_id_test.py b/tests/python_tests/feature_id_test.py index 2db7e9bcc..02c30f2d4 100644 --- a/tests/python_tests/feature_id_test.py +++ b/tests/python_tests/feature_id_test.py @@ -12,27 +12,29 @@ def setup(): os.chdir(execution_path('.')) def compare_shape_between_mapnik_and_ogr(shapefile,query=None): - ds1 = mapnik2.Ogr(file=shapefile,layer_by_index=0) - ds2 = mapnik2.Shapefile(file=shapefile) - if query: - fs1 = ds1.features(query) - fs2 = ds2.features(query) - else: - fs1 = ds1.featureset() - fs2 = ds2.featureset() - count = 0; - while(True): - count += 1 - feat1 = fs1.next() - feat2 = fs2.next() - if not feat1: - break - #import pdb;pdb.set_trace() - 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 + plugins = mapnik2.DatasourceCache.instance().plugin_names() + if 'shape' in plugins and 'ogr' in plugins: + ds1 = mapnik2.Ogr(file=shapefile,layer_by_index=0) + ds2 = mapnik2.Shapefile(file=shapefile) + if query: + fs1 = ds1.features(query) + fs2 = ds2.features(query) + else: + fs1 = ds1.featureset() + fs2 = ds2.featureset() + count = 0; + while(True): + count += 1 + feat1 = fs1.next() + feat2 = fs2.next() + if not feat1: + break + #import pdb;pdb.set_trace() + 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 @@ -45,10 +47,11 @@ def test_shapefile_polygon_featureset_id(): def test_shapefile_polygon_feature_query_id(): bbox = (15523428.2632, 4110477.6323, -11218494.8310, 7495720.7404) query = mapnik2.Query(mapnik2.Box2d(*bbox)) - ds = mapnik2.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) + if 'ogr' in mapnik2.DatasourceCache.instance().plugin_names(): + ds = mapnik2.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(): raise Todo("need to optimize multigeom bbox handling in shapeindex") @@ -56,13 +59,14 @@ def test_feature_hit_count(): #bbox = (-14284551.8434, 2074195.1992, -7474929.8687, 8140237.7628) bbox = (1113194.91,4512803.085,2226389.82,6739192.905) query = mapnik2.Query(mapnik2.Box2d(*bbox)) - ds1 = mapnik2.Ogr(file='../data/shp/world_merc.shp',layer_by_index=0) - for fld in ds1.fields(): - query.add_property_name(fld) - ds2 = mapnik2.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 'ogr' in mapnik2.DatasourceCache.instance().plugin_names(): + ds1 = mapnik2.Ogr(file='../data/shp/world_merc.shp',layer_by_index=0) + for fld in ds1.fields(): + query.add_property_name(fld) + ds2 = mapnik2.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() diff --git a/tests/python_tests/feature_test.py b/tests/python_tests/feature_test.py index 669c1d3ec..b51ea532d 100644 --- a/tests/python_tests/feature_test.py +++ b/tests/python_tests/feature_test.py @@ -99,7 +99,7 @@ def test_feature_expression_evaluation_attr_with_spaces(): f['name with space'] = u'a' eq_(f['name with space'],u'a') expr = mapnik2.Expression("[name with space]='a'") - eq_(str(expr),"[name with space]='a'") + eq_(str(expr),"([name with space]='a')") eq_(expr.evaluate(f),True) if __name__ == "__main__": diff --git a/tests/python_tests/glyph_symbolizer_test.py b/tests/python_tests/glyph_symbolizer_test.py index 48c35cc2c..cd5fec579 100644 --- a/tests/python_tests/glyph_symbolizer_test.py +++ b/tests/python_tests/glyph_symbolizer_test.py @@ -16,10 +16,11 @@ def test_renders_with_agg(): sym.color = mapnik2.Expression("'#ff0000'") _map = create_map_and_append_symbolyzer(sym) - im = mapnik2.Image(_map.width,_map.height) - mapnik2.render(_map, im) - save_data('agg_glyph_symbolizer.png', im.tostring('png')) - assert contains_word('\xff\x00\x00\xff', im.tostring()) + if _map: + im = mapnik2.Image(_map.width,_map.height) + mapnik2.render(_map, im) + save_data('agg_glyph_symbolizer.png', im.tostring('png')) + assert contains_word('\xff\x00\x00\xff', im.tostring()) def test_renders_with_cairo(): if not mapnik2.has_pycairo(): @@ -31,31 +32,36 @@ def test_renders_with_cairo(): sym.size = mapnik2.Expression("[value]") sym.color = mapnik2.Expression("'#ff0000'") _map = create_map_and_append_symbolyzer(sym) - - from cStringIO import StringIO - import cairo - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256) - mapnik2.render(_map, surface) - im = mapnik2.Image.from_cairo(surface) - save_data('cairo_glyph_symbolizer.png', im.tostring('png')) - assert contains_word('\xff\x00\x00\xff', im.tostring()) + if _map: + from cStringIO import StringIO + import cairo + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256) + mapnik2.render(_map, surface) + im = mapnik2.Image.from_cairo(surface) + save_data('cairo_glyph_symbolizer.png', im.tostring('png')) + assert contains_word('\xff\x00\x00\xff', im.tostring()) def test_load_save_load_map(): map = mapnik2.Map(256,256) in_map = "../data/good_maps/glyph_symbolizer.xml" - mapnik2.load_map(map, in_map) - style = map.find_style('arrows') - sym = style.rules[0].symbols[0] - assert isinstance(sym, mapnik2.GlyphSymbolizer) - assert sym.angle_mode == mapnik2.angle_mode.AZIMUTH - - out_map = mapnik2.save_map_to_string(map).decode('utf8') - map = mapnik2.Map(256,256) - mapnik2.load_map_from_string(map, out_map.encode('utf8')) - assert 'GlyphSymbolizer' in out_map - # make sure non-ascii characters are well supported since most interesting - # glyphs for symbology are usually in that range - assert u'í' in out_map, out_map + try: + mapnik2.load_map(map, in_map) + style = map.find_style('arrows') + sym = style.rules[0].symbols[0] + assert isinstance(sym, mapnik2.GlyphSymbolizer) + assert sym.angle_mode == mapnik2.angle_mode.AZIMUTH + + out_map = mapnik2.save_map_to_string(map).decode('utf8') + map = mapnik2.Map(256,256) + mapnik2.load_map_from_string(map, out_map.encode('utf8')) + assert 'GlyphSymbolizer' in out_map + # make sure non-ascii characters are well supported since most interesting + # glyphs for symbology are usually in that range + assert u'í' in out_map, out_map + except RuntimeError, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(e) # # Utilities and setup code @@ -69,29 +75,34 @@ def setup(): def create_map_and_append_symbolyzer(sym): srs = '+init=epsg:32630' lyr = mapnik2.Layer('arrows') - lyr.datasource = mapnik2.Shapefile( - file = '../data/shp/arrows.shp', - ) - lyr.srs = srs - _map = mapnik2.Map(256,256, srs) - style = mapnik2.Style() - rule = mapnik2.Rule() - rule.symbols.append(sym) - - # put a test symbolizer to see what is the azimuth being read - ts = mapnik2.TextSymbolizer(mapnik2.Expression('[azimuth]'), - "DejaVu Sans Book", - 10, - mapnik2.Color("black")) - ts.allow_overlap = True - rule.symbols.append(ts) - - style.rules.append(rule) - _map.append_style('foo', style) - lyr.styles.append('foo') - _map.layers.append(lyr) - _map.zoom_to_box(mapnik2.Box2d(0,0,8,8)) - return _map + try: + lyr.datasource = mapnik2.Shapefile( + file = '../data/shp/arrows.shp', + ) + lyr.srs = srs + _map = mapnik2.Map(256,256, srs) + style = mapnik2.Style() + rule = mapnik2.Rule() + rule.symbols.append(sym) + + # put a test symbolizer to see what is the azimuth being read + ts = mapnik2.TextSymbolizer(mapnik2.Expression('[azimuth]'), + "DejaVu Sans Book", + 10, + mapnik2.Color("black")) + ts.allow_overlap = True + rule.symbols.append(ts) + + style.rules.append(rule) + _map.append_style('foo', style) + lyr.styles.append('foo') + _map.layers.append(lyr) + _map.zoom_to_box(mapnik2.Box2d(0,0,8,8)) + return _map + except RuntimeError, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(e) if __name__ == "__main__": setup() diff --git a/tests/python_tests/layer_modification_test.py b/tests/python_tests/layer_modification_test.py index f25cbb2c4..113e0a8b8 100644 --- a/tests/python_tests/layer_modification_test.py +++ b/tests/python_tests/layer_modification_test.py @@ -32,40 +32,45 @@ def test_adding_datasource_to_layer(): ''' m = mapnik2.Map(256, 256) - mapnik2.load_map_from_string(m, map_string) + try: + mapnik2.load_map_from_string(m, map_string) + + # validate it loaded fine + eq_(m.layers[0].styles[0],'world_borders_style') + eq_(m.layers[0].styles[1],'point_style') + eq_(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] - # validate it loaded fine - eq_(m.layers[0].styles[0],'world_borders_style') - eq_(m.layers[0].styles[1],'point_style') - eq_(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... - eq_(m.layers[0].datasource,None) - eq_(lyr.datasource,None) - - # also note that since the srs was black it defaulted to wgs84 - eq_(m.layers[0].srs,'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') - eq_(lyr.srs,'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') - - # now add a datasource one... - ds = mapnik2.Shapefile(file='../data/shp/world_merc.shp') - m.layers[0].datasource = ds - - # now ensure it is attached - eq_(m.layers[0].datasource.name(),"shape") - eq_(lyr.datasource.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 - eq_(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') - eq_(lyr.srs,'+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs') + # ensure that there was no datasource for the layer... + eq_(m.layers[0].datasource,None) + eq_(lyr.datasource,None) + + # also note that since the srs was black it defaulted to wgs84 + eq_(m.layers[0].srs,'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') + eq_(lyr.srs,'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') + + # now add a datasource one... + ds = mapnik2.Shapefile(file='../data/shp/world_merc.shp') + m.layers[0].datasource = ds + + # now ensure it is attached + eq_(m.layers[0].datasource.name(),"shape") + eq_(lyr.datasource.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 + eq_(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') + eq_(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, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(e) if __name__ == "__main__": setup() diff --git a/tests/python_tests/load_map_test.py b/tests/python_tests/load_map_test.py index 6b902dfc4..2c6e510df 100644 --- a/tests/python_tests/load_map_test.py +++ b/tests/python_tests/load_map_test.py @@ -15,13 +15,18 @@ def setup(): def assert_loads_successfully(file): m = mapnik2.Map(512, 512) - strict = True - mapnik2.load_map(m, file, strict) - - # libxml2 is not smart about paths, and clips the last directory off - # of a path if it does not end in a trailing slash - base_path = os.path.dirname(file) + '/' - mapnik2.load_map_from_string(m,open(file,'rb').read(),strict,base_path) + try: + strict = True + mapnik2.load_map(m, file, strict) + + # libxml2 is not smart about paths, and clips the last directory off + # of a path if it does not end in a trailing slash + base_path = os.path.dirname(file) + '/' + mapnik2.load_map_from_string(m,open(file,'rb').read(),strict,base_path) + except RuntimeError, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(e) # We expect these files to raise a RuntimeError diff --git a/tests/python_tests/multi_tile_raster_test.py b/tests/python_tests/multi_tile_raster_test.py index 65a29675f..41764754d 100644 --- a/tests/python_tests/multi_tile_raster_test.py +++ b/tests/python_tests/multi_tile_raster_test.py @@ -13,57 +13,58 @@ def setup(): def test_multi_tile_policy(): srs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' lyr = mapnik2.Layer('raster') - lyr.datasource = mapnik2.Raster( - file = '../data/raster_tiles/${x}/${y}.tif', - lox = -180, - loy = -90, - hix = 180, - hiy = 90, - multi = 1, - tile_size = 256, - x_width = 2, - y_width = 2 - ) - lyr.srs = srs - _map = mapnik2.Map(256, 256, srs) - style = mapnik2.Style() - rule = mapnik2.Rule() - sym = mapnik2.RasterSymbolizer() - rule.symbols.append(sym) - style.rules.append(rule) - _map.append_style('foo', style) - lyr.styles.append('foo') - _map.layers.append(lyr) - _map.zoom_to_box(lyr.envelope()) + if 'raster' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.Raster( + file = '../data/raster_tiles/${x}/${y}.tif', + lox = -180, + loy = -90, + hix = 180, + hiy = 90, + multi = 1, + tile_size = 256, + x_width = 2, + y_width = 2 + ) + lyr.srs = srs + _map = mapnik2.Map(256, 256, srs) + style = mapnik2.Style() + rule = mapnik2.Rule() + sym = mapnik2.RasterSymbolizer() + rule.symbols.append(sym) + style.rules.append(rule) + _map.append_style('foo', style) + lyr.styles.append('foo') + _map.layers.append(lyr) + _map.zoom_to_box(lyr.envelope()) + + im = mapnik2.Image(_map.width, _map.height) + mapnik2.render(_map, im) + + save_data('test_multi_tile_policy.png', im.tostring('png')) - im = mapnik2.Image(_map.width, _map.height) - mapnik2.render(_map, im) + # test green chunk + assert im.view(0,64,1,1).tostring() == '\x00\xff\x00\xff' + assert im.view(127,64,1,1).tostring() == '\x00\xff\x00\xff' + assert im.view(0,127,1,1).tostring() == '\x00\xff\x00\xff' + assert im.view(127,127,1,1).tostring() == '\x00\xff\x00\xff' - save_data('test_multi_tile_policy.png', im.tostring('png')) - - # test green chunk - assert im.view(0,64,1,1).tostring() == '\x00\xff\x00\xff' - assert im.view(127,64,1,1).tostring() == '\x00\xff\x00\xff' - assert im.view(0,127,1,1).tostring() == '\x00\xff\x00\xff' - assert im.view(127,127,1,1).tostring() == '\x00\xff\x00\xff' - - # test blue chunk - assert im.view(128,64,1,1).tostring() == '\x00\x00\xff\xff' - assert im.view(255,64,1,1).tostring() == '\x00\x00\xff\xff' - assert im.view(128,127,1,1).tostring() == '\x00\x00\xff\xff' - assert im.view(255,127,1,1).tostring() == '\x00\x00\xff\xff' - - # test red chunk - assert im.view(0,128,1,1).tostring() == '\xff\x00\x00\xff' - assert im.view(127,128,1,1).tostring() == '\xff\x00\x00\xff' - assert im.view(0,191,1,1).tostring() == '\xff\x00\x00\xff' - assert im.view(127,191,1,1).tostring() == '\xff\x00\x00\xff' - - # test magenta chunk - assert im.view(128,128,1,1).tostring() == '\xff\x00\xff\xff' - assert im.view(255,128,1,1).tostring() == '\xff\x00\xff\xff' - assert im.view(128,191,1,1).tostring() == '\xff\x00\xff\xff' - assert im.view(255,191,1,1).tostring() == '\xff\x00\xff\xff' + # test blue chunk + assert im.view(128,64,1,1).tostring() == '\x00\x00\xff\xff' + assert im.view(255,64,1,1).tostring() == '\x00\x00\xff\xff' + assert im.view(128,127,1,1).tostring() == '\x00\x00\xff\xff' + assert im.view(255,127,1,1).tostring() == '\x00\x00\xff\xff' + + # test red chunk + assert im.view(0,128,1,1).tostring() == '\xff\x00\x00\xff' + assert im.view(127,128,1,1).tostring() == '\xff\x00\x00\xff' + assert im.view(0,191,1,1).tostring() == '\xff\x00\x00\xff' + assert im.view(127,191,1,1).tostring() == '\xff\x00\x00\xff' + + # test magenta chunk + assert im.view(128,128,1,1).tostring() == '\xff\x00\xff\xff' + assert im.view(255,128,1,1).tostring() == '\xff\x00\xff\xff' + assert im.view(128,191,1,1).tostring() == '\xff\x00\xff\xff' + assert im.view(255,191,1,1).tostring() == '\xff\x00\xff\xff' if __name__ == "__main__": setup() diff --git a/tests/python_tests/object_test.py b/tests/python_tests/object_test.py index bc8a7c8a4..ca7b251f8 100644 --- a/tests/python_tests/object_test.py +++ b/tests/python_tests/object_test.py @@ -247,35 +247,6 @@ def test_linesymbolizer_pickle(): eq_(s.line_cap, s2.line_cap) eq_(s.line_join, s2.line_join) -# Shapefile initialization -def test_shapefile_init(): - s = mapnik2.Shapefile(file='../../demo/data/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(): - s = mapnik2.Shapefile(file='../../demo/data/boundaries', encoding='latin1') - f = s.features_at_point(s.envelope().center()).features[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) - - # Check that the deprecated interface still works, - # remove me once the deprecated code is cleaned up - eq_(f.properties['Shape_Leng'], 19218883.724300001) # TextSymbolizer initialization def test_textsymbolizer_init(): @@ -389,19 +360,24 @@ def test_map_init_from_string(): m = mapnik2.Map(600, 300) eq_(m.base, '') - mapnik2.load_map_from_string(m, map_string) - eq_(m.base, './') - mapnik2.load_map_from_string(m, map_string, False, "") # this "" will have no effect - eq_(m.base, './') try: - mapnik2.load_map_from_string(m, map_string, False, "/tmp") - except RuntimeError: - pass # runtime error expected because shapefile path should be wrong and datasource will throw - eq_(m.base, '/tmp') # /tmp will be set despite the exception because load_map mostly worked - m.base = 'foo' - mapnik2.load_map_from_string(m, map_string, True, ".") - eq_(m.base, '.') - raise(Todo("Need to write more map property tests in 'object_test.py'...")) + mapnik2.load_map_from_string(m, map_string) + eq_(m.base, './') + mapnik2.load_map_from_string(m, map_string, False, "") # this "" will have no effect + eq_(m.base, './') + try: + mapnik2.load_map_from_string(m, map_string, False, "/tmp") + except RuntimeError: + pass # runtime error expected because shapefile path should be wrong and datasource will throw + eq_(m.base, '/tmp') # /tmp will be set despite the exception because load_map mostly worked + m.base = 'foo' + mapnik2.load_map_from_string(m, map_string, True, ".") + eq_(m.base, '.') + raise(Todo("Need to write more map property tests in 'object_test.py'...")) + except RuntimeError, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(e) # Map pickling def test_map_pickle(): diff --git a/tests/python_tests/raster_colorizer_test.py b/tests/python_tests/raster_colorizer_test.py index 3e65a2d60..e2fa11604 100644 --- a/tests/python_tests/raster_colorizer_test.py +++ b/tests/python_tests/raster_colorizer_test.py @@ -15,10 +15,15 @@ def test_gen_map(): outputfile = 'raster_colorizer_test.png' m = mapnik2.Map(800, 600) - mapnik2.load_map(m, mapxmlfile) - mapnik2.save_map(m, mapxmloutputfile) - m.zoom_all() - mapnik2.render_to_file(m, outputfile) + try: + mapnik2.load_map(m, mapxmlfile) + mapnik2.save_map(m, mapxmloutputfile) + m.zoom_all() + mapnik2.render_to_file(m, outputfile) + except RuntimeError,e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(str(e)) #test discrete colorizer mode def test_get_color_discrete(): diff --git a/tests/python_tests/raster_symbolizer_test.py b/tests/python_tests/raster_symbolizer_test.py index 228219238..cef5de169 100644 --- a/tests/python_tests/raster_symbolizer_test.py +++ b/tests/python_tests/raster_symbolizer_test.py @@ -14,86 +14,93 @@ def setup(): def test_dataraster_coloring(): srs = '+init=epsg:32630' lyr = mapnik2.Layer('dataraster') - lyr.datasource = mapnik2.Gdal( - file = '../data/raster/dataraster.tif', - band = 1, - ) - lyr.srs = srs - _map = mapnik2.Map(256,256, srs) - style = mapnik2.Style() - rule = mapnik2.Rule() - sym = mapnik2.RasterSymbolizer() - # Assigning a colorizer to the RasterSymbolizer tells the later - # that it should use it to colorize the raw data raster - sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color("transparent")) + if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.Gdal( + file = '../data/raster/dataraster.tif', + band = 1, + ) + lyr.srs = srs + _map = mapnik2.Map(256,256, srs) + style = mapnik2.Style() + rule = mapnik2.Rule() + sym = mapnik2.RasterSymbolizer() + # Assigning a colorizer to the RasterSymbolizer tells the later + # that it should use it to colorize the raw data raster + sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color("transparent")) + + for value, color in [ + ( 0, "#0044cc"), + ( 10, "#00cc00"), + ( 20, "#ffff00"), + ( 30, "#ff7f00"), + ( 40, "#ff0000"), + ( 50, "#ff007f"), + ( 60, "#ff00ff"), + ( 70, "#cc00cc"), + ( 80, "#990099"), + ( 90, "#660066"), + ( 200, "transparent"), + ]: + sym.colorizer.add_stop(value, mapnik2.Color(color)) + rule.symbols.append(sym) + style.rules.append(rule) + _map.append_style('foo', style) + lyr.styles.append('foo') + _map.layers.append(lyr) + _map.zoom_to_box(lyr.envelope()) - for value, color in [ - ( 0, "#0044cc"), - ( 10, "#00cc00"), - ( 20, "#ffff00"), - ( 30, "#ff7f00"), - ( 40, "#ff0000"), - ( 50, "#ff007f"), - ( 60, "#ff00ff"), - ( 70, "#cc00cc"), - ( 80, "#990099"), - ( 90, "#660066"), - ( 200, "transparent"), - ]: - sym.colorizer.add_stop(value, mapnik2.Color(color)) - rule.symbols.append(sym) - style.rules.append(rule) - _map.append_style('foo', style) - lyr.styles.append('foo') - _map.layers.append(lyr) - _map.zoom_to_box(lyr.envelope()) - - im = mapnik2.Image(_map.width,_map.height) - mapnik2.render(_map, im) - # save a png somewhere so we can see it - save_data('test_dataraster_coloring.png', im.tostring('png')) - imdata = im.tostring() - # we have some values in the [20,30) interval so check that they're colored - assert contains_word('\xff\xff\x00\xff', imdata) + im = mapnik2.Image(_map.width,_map.height) + mapnik2.render(_map, im) + # save a png somewhere so we can see it + save_data('test_dataraster_coloring.png', im.tostring('png')) + imdata = im.tostring() + # we have some values in the [20,30) interval so check that they're colored + assert contains_word('\xff\xff\x00\xff', imdata) def test_dataraster_query_point(): srs = '+init=epsg:32630' lyr = mapnik2.Layer('dataraster') - lyr.datasource = mapnik2.Gdal( - file = '../data/raster/dataraster.tif', - band = 1, - ) - lyr.srs = srs - _map = mapnik2.Map(256,256, srs) - _map.layers.append(lyr) - - # point inside raster extent with valid data - x, y = 427417, 4477517 - features = _map.query_point(0,x,y).features - assert len(features) == 1 - feat = features[0] - center = feat.envelope().center() - assert center.x==x and center.y==y, center - value = feat['value'] - assert value == 21.0, value - - # point outside raster extent - features = _map.query_point(0,-427417,4477517).features - assert len(features) == 0 - - # point inside raster extent with nodata - features = _map.query_point(0,126850,4596050).features - assert len(features) == 0 + if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.Gdal( + file = '../data/raster/dataraster.tif', + band = 1, + ) + lyr.srs = srs + _map = mapnik2.Map(256,256, srs) + _map.layers.append(lyr) + + # point inside raster extent with valid data + x, y = 427417, 4477517 + features = _map.query_point(0,x,y).features + assert len(features) == 1 + feat = features[0] + center = feat.envelope().center() + assert center.x==x and center.y==y, center + value = feat['value'] + assert value == 21.0, value + + # point outside raster extent + features = _map.query_point(0,-427417,4477517).features + assert len(features) == 0 + + # point inside raster extent with nodata + features = _map.query_point(0,126850,4596050).features + assert len(features) == 0 def test_load_save_map(): map = mapnik2.Map(256,256) in_map = "../data/good_maps/raster_symbolizer.xml" - mapnik2.load_map(map, in_map) - - out_map = mapnik2.save_map_to_string(map) - assert 'RasterSymbolizer' in out_map - assert 'RasterColorizer' in out_map - assert 'stop' in out_map + try: + mapnik2.load_map(map, in_map) + + out_map = mapnik2.save_map_to_string(map) + assert 'RasterSymbolizer' in out_map + assert 'RasterColorizer' in out_map + assert 'stop' in out_map + except RuntimeError, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(str(e)) def test_raster_with_alpha_blends_correctly_with_background(): WIDTH = 500 @@ -117,77 +124,80 @@ def test_raster_with_alpha_blends_correctly_with_background(): map_layer = mapnik2.Layer('test_layer') filepath = '../data/raster/white-alpha.png' - map_layer.datasource = mapnik2.Gdal(file=filepath) - map_layer.styles.append('raster_style') - map.layers.append(map_layer) - - map.zoom_all() - - mim = mapnik2.Image(WIDTH, HEIGHT) - - mapnik2.render(map, mim) - save_data('test_raster_with_alpha_blends_correctly_with_background.png', - mim.tostring('png')) - imdata = mim.tostring() - # All white is expected - assert contains_word('\xff\xff\xff\xff', imdata) + if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names(): + map_layer.datasource = mapnik2.Gdal(file=filepath) + map_layer.styles.append('raster_style') + map.layers.append(map_layer) + + map.zoom_all() + + mim = mapnik2.Image(WIDTH, HEIGHT) + + mapnik2.render(map, mim) + save_data('test_raster_with_alpha_blends_correctly_with_background.png', + mim.tostring('png')) + imdata = mim.tostring() + # All white is expected + assert contains_word('\xff\xff\xff\xff', imdata) def test_raster_warping(): lyrSrs = "+init=epsg:32630" mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' lyr = mapnik2.Layer('dataraster', lyrSrs) - lyr.datasource = mapnik2.Gdal( - file = '../data/raster/dataraster.tif', - band = 1, - ) - sym = mapnik2.RasterSymbolizer() - sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color(255,255,0)) - rule = mapnik2.Rule() - rule.symbols.append(sym) - style = mapnik2.Style() - style.rules.append(rule) - _map = mapnik2.Map(256,256, mapSrs) - _map.append_style('foo', style) - lyr.styles.append('foo') - _map.layers.append(lyr) - prj_trans = mapnik2.ProjTransform(mapnik2.Projection(mapSrs), - mapnik2.Projection(lyrSrs)) - _map.zoom_to_box(prj_trans.backward(lyr.envelope())) - - im = mapnik2.Image(_map.width,_map.height) - mapnik2.render(_map, im) - # save a png somewhere so we can see it - save_data('test_raster_warping.png', im.tostring('png')) - imdata = im.tostring() - assert contains_word('\xff\xff\x00\xff', imdata) + if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.Gdal( + file = '../data/raster/dataraster.tif', + band = 1, + ) + sym = mapnik2.RasterSymbolizer() + sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color(255,255,0)) + rule = mapnik2.Rule() + rule.symbols.append(sym) + style = mapnik2.Style() + style.rules.append(rule) + _map = mapnik2.Map(256,256, mapSrs) + _map.append_style('foo', style) + lyr.styles.append('foo') + _map.layers.append(lyr) + prj_trans = mapnik2.ProjTransform(mapnik2.Projection(mapSrs), + mapnik2.Projection(lyrSrs)) + _map.zoom_to_box(prj_trans.backward(lyr.envelope())) + + im = mapnik2.Image(_map.width,_map.height) + mapnik2.render(_map, im) + # save a png somewhere so we can see it + save_data('test_raster_warping.png', im.tostring('png')) + imdata = im.tostring() + assert contains_word('\xff\xff\x00\xff', imdata) def test_raster_warping_does_not_overclip_source(): lyrSrs = "+init=epsg:32630" mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' lyr = mapnik2.Layer('dataraster', lyrSrs) - lyr.datasource = mapnik2.Gdal( - file = '../data/raster/dataraster.tif', - band = 1, - ) - sym = mapnik2.RasterSymbolizer() - sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color(255,255,0)) - rule = mapnik2.Rule() - rule.symbols.append(sym) - style = mapnik2.Style() - style.rules.append(rule) - _map = mapnik2.Map(256,256, mapSrs) - _map.background=mapnik2.Color('white') - _map.append_style('foo', style) - lyr.styles.append('foo') - _map.layers.append(lyr) - _map.zoom_to_box(mapnik2.Box2d(3,42,4,43)) - - im = mapnik2.Image(_map.width,_map.height) - mapnik2.render(_map, im) - # save a png somewhere so we can see it - save_data('test_raster_warping_does_not_overclip_source.png', - im.tostring('png')) - assert im.view(0,200,1,1).tostring()=='\xff\xff\x00\xff' + if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names(): + lyr.datasource = mapnik2.Gdal( + file = '../data/raster/dataraster.tif', + band = 1, + ) + sym = mapnik2.RasterSymbolizer() + sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color(255,255,0)) + rule = mapnik2.Rule() + rule.symbols.append(sym) + style = mapnik2.Style() + style.rules.append(rule) + _map = mapnik2.Map(256,256, mapSrs) + _map.background=mapnik2.Color('white') + _map.append_style('foo', style) + lyr.styles.append('foo') + _map.layers.append(lyr) + _map.zoom_to_box(mapnik2.Box2d(3,42,4,43)) + + im = mapnik2.Image(_map.width,_map.height) + mapnik2.render(_map, im) + # save a png somewhere so we can see it + save_data('test_raster_warping_does_not_overclip_source.png', + im.tostring('png')) + assert im.view(0,200,1,1).tostring()=='\xff\xff\x00\xff' if __name__ == "__main__": setup() diff --git a/tests/python_tests/render_test.py b/tests/python_tests/render_test.py index 2724e1492..9679a8212 100644 --- a/tests/python_tests/render_test.py +++ b/tests/python_tests/render_test.py @@ -86,12 +86,16 @@ def get_paired_images(w,h,mapfile): return i,i2 def test_render_from_serialization(): - i,i2 = get_paired_images(100,100,'../data/good_maps/building_symbolizer.xml') - eq_(i.tostring(),i2.tostring()) - - i,i2 = get_paired_images(100,100,'../data/good_maps/polygon_symbolizer.xml') - eq_(i.tostring(),i2.tostring()) - + try: + i,i2 = get_paired_images(100,100,'../data/good_maps/building_symbolizer.xml') + eq_(i.tostring(),i2.tostring()) + + i,i2 = get_paired_images(100,100,'../data/good_maps/polygon_symbolizer.xml') + eq_(i.tostring(),i2.tostring()) + except RuntimeError, e: + # only test datasources that we have installed + if not 'Could not create datasource' in str(e): + raise RuntimeError(e) grid_correct = {"keys": ["", "North West", "North East", "South West", "South East"], "data": {"South East": {"Name": "South East"}, "North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!!!! ##### ", " !!!!! ##### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$$ %%%%% ", " $$$$$ %%%%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]} diff --git a/tests/python_tests/sqlite_test.py b/tests/python_tests/sqlite_test.py index 8c9e0fa3a..11d2bfdcc 100644 --- a/tests/python_tests/sqlite_test.py +++ b/tests/python_tests/sqlite_test.py @@ -10,189 +10,191 @@ def setup(): # from another directory we need to chdir() os.chdir(execution_path('.')) -def test_attachdb_with_relative_file(): - # The point table and index is in the qgis_spatiallite.sqlite - # database. If either is not found, then this fails - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='point', - attachdb='scratch@qgis_spatiallite.sqlite' - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['pkuid'],1) - -def test_attachdb_with_multiple_files(): - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='attachedtest', - attachdb='scratch1@:memory:,scratch2@:memory:', - initdb=''' - create table scratch1.attachedtest (the_geom); - create virtual table scratch2.idx_attachedtest_the_geom using rtree(pkid,xmin,xmax,ymin,ymax); - insert into scratch2.idx_attachedtest_the_geom values (1,-7799225.5,-7778571.0,1393264.125,1417719.375); - ''' - ) - fs = ds.featureset() - feature = fs.next() - # the above should not throw but will result in no features - eq_(feature,None) - -def test_attachdb_with_absolute_file(): - # The point table and index is in the qgis_spatiallite.sqlite - # database. If either is not found, then this fails - ds = mapnik2.SQLite(file=os.getcwd() + '/../data/sqlite/world.sqlite', - table='point', - attachdb='scratch@qgis_spatiallite.sqlite' - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['pkuid'],1) - -def test_attachdb_with_index(): - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='attachedtest', - attachdb='scratch@:memory:', - initdb=''' - create table scratch.attachedtest (the_geom); - create virtual table scratch.idx_attachedtest_the_geom using rtree(pkid,xmin,xmax,ymin,ymax); - insert into scratch.idx_attachedtest_the_geom values (1,-7799225.5,-7778571.0,1393264.125,1417719.375); - ''' - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature,None) +if 'sqlite' in mapnik2.DatasourceCache.instance().plugin_names(): -def test_attachdb_with_explicit_index(): - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='attachedtest', - index_table='myindex', - attachdb='scratch@:memory:', - initdb=''' - create table scratch.attachedtest (the_geom); - create virtual table scratch.myindex using rtree(pkid,xmin,xmax,ymin,ymax); - insert into scratch.myindex values (1,-7799225.5,-7778571.0,1393264.125,1417719.375); - ''' - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature,None) - -def test_attachdb_with_sql_join(): - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='(select * from world_merc INNER JOIN business on world_merc.iso3 = business.ISO3 limit 100)', - attachdb='busines@business.sqlite' - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature.id(),1) - expected = { - 1995:0, - 1996:0, - 1997:0, - 1998:0, - 1999:0, - 2000:0, - 2001:0, - 2002:0, - 2003:0, - 2004:0, - 2005:0, - 2006:0, - 2007:0, - 2008:0, - 2009:0, - 2010:0, - # this appears to be sqlites way of - # automatically handling clashing column names - 'ISO3:1':'ATG', - 'OGC_FID':1, - 'area':44, - 'fips':u'AC', - 'iso2':u'AG', - 'iso3':u'ATG', - 'lat':17.078, - 'lon':-61.783, - 'name':u'Antigua and Barbuda', - 'pop2005':83039, - 'region':19, - 'subregion':29, - 'un':28 - } - for k,v in expected.items(): - try: - eq_(feature[str(k)],v) - except: - #import pdb;pdb.set_trace() - print 'invalid key/v %s/%s for: %s' % (k,v,feature) - -def test_subqueries(): - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='world_merc', - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['OGC_FID'],1) - eq_(feature['fips'],u'AC') - eq_(feature['iso2'],u'AG') - eq_(feature['iso3'],u'ATG') - eq_(feature['un'],28) - eq_(feature['name'],u'Antigua and Barbuda') - eq_(feature['area'],44) - eq_(feature['pop2005'],83039) - eq_(feature['region'],19) - eq_(feature['subregion'],29) - eq_(feature['lon'],-61.783) - eq_(feature['lat'],17.078) - - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='(select * from world_merc)', - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['OGC_FID'],1) - eq_(feature['fips'],u'AC') - eq_(feature['iso2'],u'AG') - eq_(feature['iso3'],u'ATG') - eq_(feature['un'],28) - eq_(feature['name'],u'Antigua and Barbuda') - eq_(feature['area'],44) - eq_(feature['pop2005'],83039) - eq_(feature['region'],19) - eq_(feature['subregion'],29) - eq_(feature['lon'],-61.783) - eq_(feature['lat'],17.078) + def test_attachdb_with_relative_file(): + # The point table and index is in the qgis_spatiallite.sqlite + # database. If either is not found, then this fails + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='point', + attachdb='scratch@qgis_spatiallite.sqlite' + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['pkuid'],1) - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='(select OGC_FID,GEOMETRY as geom from world_merc)', - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['OGC_FID'],1) - eq_(len(feature),1) - - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='(select GEOMETRY,OGC_FID,fips from world_merc)', - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['OGC_FID'],1) - eq_(feature['fips'],u'AC') - - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='(select GEOMETRY,rowid as aliased_id,fips from world_merc)', - key_field='aliased_id' - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['aliased_id'],1) - eq_(feature['fips'],u'AC') - - ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', - table='(select GEOMETRY,OGC_FID,OGC_FID as rowid,fips from world_merc)', - ) - fs = ds.featureset() - feature = fs.next() - eq_(feature['rowid'],1) - eq_(feature['fips'],u'AC') + def test_attachdb_with_multiple_files(): + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='attachedtest', + attachdb='scratch1@:memory:,scratch2@:memory:', + initdb=''' + create table scratch1.attachedtest (the_geom); + create virtual table scratch2.idx_attachedtest_the_geom using rtree(pkid,xmin,xmax,ymin,ymax); + insert into scratch2.idx_attachedtest_the_geom values (1,-7799225.5,-7778571.0,1393264.125,1417719.375); + ''' + ) + fs = ds.featureset() + feature = fs.next() + # the above should not throw but will result in no features + eq_(feature,None) + + def test_attachdb_with_absolute_file(): + # The point table and index is in the qgis_spatiallite.sqlite + # database. If either is not found, then this fails + ds = mapnik2.SQLite(file=os.getcwd() + '/../data/sqlite/world.sqlite', + table='point', + attachdb='scratch@qgis_spatiallite.sqlite' + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['pkuid'],1) + + def test_attachdb_with_index(): + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='attachedtest', + attachdb='scratch@:memory:', + initdb=''' + create table scratch.attachedtest (the_geom); + create virtual table scratch.idx_attachedtest_the_geom using rtree(pkid,xmin,xmax,ymin,ymax); + insert into scratch.idx_attachedtest_the_geom values (1,-7799225.5,-7778571.0,1393264.125,1417719.375); + ''' + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature,None) + + def test_attachdb_with_explicit_index(): + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='attachedtest', + index_table='myindex', + attachdb='scratch@:memory:', + initdb=''' + create table scratch.attachedtest (the_geom); + create virtual table scratch.myindex using rtree(pkid,xmin,xmax,ymin,ymax); + insert into scratch.myindex values (1,-7799225.5,-7778571.0,1393264.125,1417719.375); + ''' + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature,None) + + def test_attachdb_with_sql_join(): + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='(select * from world_merc INNER JOIN business on world_merc.iso3 = business.ISO3 limit 100)', + attachdb='busines@business.sqlite' + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature.id(),1) + expected = { + 1995:0, + 1996:0, + 1997:0, + 1998:0, + 1999:0, + 2000:0, + 2001:0, + 2002:0, + 2003:0, + 2004:0, + 2005:0, + 2006:0, + 2007:0, + 2008:0, + 2009:0, + 2010:0, + # this appears to be sqlites way of + # automatically handling clashing column names + 'ISO3:1':'ATG', + 'OGC_FID':1, + 'area':44, + 'fips':u'AC', + 'iso2':u'AG', + 'iso3':u'ATG', + 'lat':17.078, + 'lon':-61.783, + 'name':u'Antigua and Barbuda', + 'pop2005':83039, + 'region':19, + 'subregion':29, + 'un':28 + } + for k,v in expected.items(): + try: + eq_(feature[str(k)],v) + except: + #import pdb;pdb.set_trace() + print 'invalid key/v %s/%s for: %s' % (k,v,feature) + + def test_subqueries(): + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='world_merc', + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['OGC_FID'],1) + eq_(feature['fips'],u'AC') + eq_(feature['iso2'],u'AG') + eq_(feature['iso3'],u'ATG') + eq_(feature['un'],28) + eq_(feature['name'],u'Antigua and Barbuda') + eq_(feature['area'],44) + eq_(feature['pop2005'],83039) + eq_(feature['region'],19) + eq_(feature['subregion'],29) + eq_(feature['lon'],-61.783) + eq_(feature['lat'],17.078) + + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='(select * from world_merc)', + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['OGC_FID'],1) + eq_(feature['fips'],u'AC') + eq_(feature['iso2'],u'AG') + eq_(feature['iso3'],u'ATG') + eq_(feature['un'],28) + eq_(feature['name'],u'Antigua and Barbuda') + eq_(feature['area'],44) + eq_(feature['pop2005'],83039) + eq_(feature['region'],19) + eq_(feature['subregion'],29) + eq_(feature['lon'],-61.783) + eq_(feature['lat'],17.078) + + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='(select OGC_FID,GEOMETRY as geom from world_merc)', + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['OGC_FID'],1) + eq_(len(feature),1) + + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='(select GEOMETRY,OGC_FID,fips from world_merc)', + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['OGC_FID'],1) + eq_(feature['fips'],u'AC') + + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='(select GEOMETRY,rowid as aliased_id,fips from world_merc)', + key_field='aliased_id' + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['aliased_id'],1) + eq_(feature['fips'],u'AC') + + ds = mapnik2.SQLite(file='../data/sqlite/world.sqlite', + table='(select GEOMETRY,OGC_FID,OGC_FID as rowid,fips from world_merc)', + ) + fs = ds.featureset() + feature = fs.next() + eq_(feature['rowid'],1) + eq_(feature['fips'],u'AC') if __name__ == "__main__": setup()