diff --git a/Makefile b/Makefile index 8f30a9114..e5503fee9 100755 --- a/Makefile +++ b/Makefile @@ -84,8 +84,9 @@ demo: pep8: # https://gist.github.com/1903033 # gsed on osx - @pep8 -r --select=W293 -q --filename=*.py `pwd`/tests/ | xargs gsed -i 's/^[ \r\t]*$//' - @pep8 -r --select=W391 -q --filename=*.py `pwd`/tests/ | xargs gsed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}' + @pep8 -r --select=W293 -q --filename=*.py `pwd`/tests/ | xargs gsed -i 's/^[ \r\t]*$$//' + @pep8 -r --select=W391 -q --filename=*.py `pwd`/tests/ | xargs gsed -i -e :a -e '/^\n*$$/{$$d;N;ba' -e '}' + @pep8 -r --select=W391 -q --filename=*.py `pwd`/tests/ | xargs ged -i '/./,/^$$/!d' grind: @for FILE in tests/cpp_tests/*-bin; do \ diff --git a/tests/python_tests/render_grid_test.py b/tests/python_tests/render_grid_test.py index 2c887139e..1d15fd5ee 100644 --- a/tests/python_tests/render_grid_test.py +++ b/tests/python_tests/render_grid_test.py @@ -36,7 +36,7 @@ if mapnik.has_grid_renderer(): val += ' ✓ keys match\n' val += '\n\t%s\n\t%s' % (g1_file,g2_file) return val - + def show_grids2(name,g1,g2): g2_expected = '../data/grids/mapnik-%s-actual.json' % name if not os.path.exists(g2_expected): @@ -60,15 +60,15 @@ if mapnik.has_grid_renderer(): val += ' ✓ keys match\n' val += '\n\t%s\n\t%s' % (g1_file,g2_expected) return val - + # previous rendering using agg ellipse directly grid_correct_new = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]} - + # newer rendering using svg grid_correct_new2 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]} - + grid_correct_new3 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]} - + def resolve(grid,row,col): """ Resolve the attributes for a given pixel in a grid. """ @@ -83,8 +83,8 @@ if mapnik.has_grid_renderer(): codepoint -= 32 key = grid['keys'][codepoint] return grid['data'].get(key) - - + + def create_grid_map(width,height,sym): ds = mapnik.MemoryDatasource() context = mapnik.Context() @@ -93,17 +93,17 @@ if mapnik.has_grid_renderer(): f['Name'] = 'South East' f.add_geometries_from_wkt('POINT (143.10 -38.60)') ds.add_feature(f) - + f = mapnik.Feature(context,2) f['Name'] = 'South West' f.add_geometries_from_wkt('POINT (142.48 -38.60)') ds.add_feature(f) - + f = mapnik.Feature(context,3) f['Name'] = 'North West' f.add_geometries_from_wkt('POINT (142.48 -38.38)') ds.add_feature(f) - + f = mapnik.Feature(context,4) f['Name'] = 'North East' f.add_geometries_from_wkt('POINT (143.10 -38.38)') @@ -120,8 +120,8 @@ if mapnik.has_grid_renderer(): m.append_style('places_labels',s) m.layers.append(lyr) return m - - + + def test_render_grid(): """ test render_grid method""" width,height = 256,256 @@ -132,13 +132,13 @@ if mapnik.has_grid_renderer(): ul_lonlat = mapnik.Coord(142.30,-38.20) lr_lonlat = mapnik.Coord(143.40,-38.80) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) - + # new method grid = mapnik.Grid(m.width,m.height,key='Name') mapnik.render_layer(m,grid,layer=0,fields=['Name']) utf1 = grid.encode('utf',resolution=4) eq_(utf1,grid_correct_new3,show_grids('new-markers',utf1,grid_correct_new3)) - + # check a full view is the same as a full image grid_view = grid.view(0,0,width,height) # for kicks check at full res too @@ -147,9 +147,9 @@ if mapnik.has_grid_renderer(): eq_(utf3['grid'],utf4['grid']) eq_(utf3['keys'],utf4['keys']) eq_(utf3['data'],utf4['data']) - + eq_(resolve(utf4,0,0),None) - + # resolve some center points in the # resampled view utf5 = grid_view.encode('utf',resolution=4) @@ -157,14 +157,14 @@ if mapnik.has_grid_renderer(): eq_(resolve(utf5,25,46),{"Name": "North East"}) eq_(resolve(utf5,38,10),{"Name": "South West"}) eq_(resolve(utf5,38,46),{"Name": "South East"}) - - + + grid_feat_id = {'keys': ['', '3', '4', '2', '1'], 'data': {'1': {'Name': 'South East'}, '3': {'Name': u'North West'}, '2': {'Name': 'South West'}, '4': {'Name': 'North East'}}, 'grid': [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' !! ## ', ' !!! ### ', ' !! ## ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' $$$ %% ', ' $$$ %%% ', ' $$ %% ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']} - + grid_feat_id2 = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]} - + grid_feat_id3 = {"data": {"1": {"Name": "South East", "__id__": 1}, "2": {"Name": "South West", "__id__": 2}, "3": {"Name": "North West", "__id__": 3}, "4": {"Name": "North East", "__id__": 4}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]} - + def test_render_grid3(): """ test using feature id""" width,height = 256,256 @@ -175,7 +175,7 @@ if mapnik.has_grid_renderer(): ul_lonlat = mapnik.Coord(142.30,-38.20) lr_lonlat = mapnik.Coord(143.40,-38.80) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) - + grid = mapnik.Grid(m.width,m.height,key='__id__') mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name']) utf1 = grid.encode('utf',resolution=4) @@ -188,9 +188,9 @@ if mapnik.has_grid_renderer(): eq_(utf3['grid'],utf4['grid']) eq_(utf3['keys'],utf4['keys']) eq_(utf3['data'],utf4['data']) - + eq_(resolve(utf4,0,0),None) - + # resolve some center points in the # resampled view utf5 = grid_view.encode('utf',resolution=4) @@ -198,8 +198,8 @@ if mapnik.has_grid_renderer(): eq_(resolve(utf5,25,46),{"Name": "North East","__id__": 4}) eq_(resolve(utf5,38,10),{"Name": "South West","__id__": 2}) eq_(resolve(utf5,38,46),{"Name": "South East","__id__": 1}) - - + + def gen_grid_for_id(pixel_key): ds = mapnik.MemoryDatasource() context = mapnik.Context() @@ -224,13 +224,13 @@ if mapnik.has_grid_renderer(): grid = mapnik.Grid(m.width,m.height,key='__id__') mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name']) return grid - + def test_negative_id(): grid = gen_grid_for_id(-1) eq_(grid.get_pixel(128,128),-1) utf1 = grid.encode('utf',resolution=4) eq_(utf1['keys'],['-1']) - + def test_32bit_int_id(): int32 = 2147483647 grid = gen_grid_for_id(int32) @@ -242,7 +242,7 @@ if mapnik.has_grid_renderer(): eq_(grid.get_pixel(128,128),max_neg) utf1 = grid.encode('utf',resolution=4) eq_(utf1['keys'],[str(max_neg)]) - + def test_64bit_int_id(): int64 = 0x7FFFFFFFFFFFFFFF grid = gen_grid_for_id(int64) @@ -254,15 +254,15 @@ if mapnik.has_grid_renderer(): eq_(grid.get_pixel(128,128),max_neg) utf1 = grid.encode('utf',resolution=4) eq_(utf1['keys'],[str(max_neg)]) - + def test_id_zero(): grid = gen_grid_for_id(0) eq_(grid.get_pixel(128,128),0) utf1 = grid.encode('utf',resolution=4) eq_(utf1['keys'],['0']) - + line_expected = {"keys": ["", "1"], "data": {"1": {"Name": "1"}}, "grid": [" !", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", "!! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! "]} - + def test_line_rendering(): ds = mapnik.MemoryDatasource() context = mapnik.Context() @@ -290,9 +290,9 @@ if mapnik.has_grid_renderer(): mapnik.render_layer(m,grid,layer=0,fields=['Name']) utf1 = grid.encode() eq_(utf1,line_expected,show_grids('line',utf1,line_expected)) - + point_expected = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]} - + def test_point_symbolizer_grid(): width,height = 256,256 sym = mapnik.PointSymbolizer() @@ -305,8 +305,8 @@ if mapnik.has_grid_renderer(): mapnik.render_layer(m,grid,layer=0,fields=['Name']) utf1 = grid.encode() eq_(utf1,point_expected,show_grids('point-sym',utf1,point_expected)) - - + + # should throw because this is a mis-usage # https://github.com/mapnik/mapnik/issues/1325 @raises(RuntimeError) @@ -320,7 +320,7 @@ if mapnik.has_grid_renderer(): r.symbols.append(sym) s.rules.append(r) m.append_style('points',s) - + # NOTE: we use a csv datasource here # because the memorydatasource fails silently for # queries requesting fields that do not exist in the datasource @@ -331,7 +331,7 @@ if mapnik.has_grid_renderer(): lyr1.datasource = ds1 lyr1.styles.append('points') m.layers.append(lyr1) - + ds2 = mapnik.Datasource(**{"type":"csv","inline":''' wkt,Value "POINT (142.48 -38.60)",South West'''}) @@ -339,7 +339,7 @@ if mapnik.has_grid_renderer(): lyr2.datasource = ds2 lyr2.styles.append('points') m.layers.append(lyr2) - + ul_lonlat = mapnik.Coord(142.30,-38.20) lr_lonlat = mapnik.Coord(143.40,-38.80) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) diff --git a/tests/visual_tests/test_python.py b/tests/visual_tests/test_python.py index 9c2b53aa2..4fd7c2cbe 100755 --- a/tests/visual_tests/test_python.py +++ b/tests/visual_tests/test_python.py @@ -105,4 +105,3 @@ for format_tree in format_trees: diff = compare(actual,expected) if diff > 0: print 'comparision failed between:\n %s (actual)\n %s (expected)' % (actual,expected) - diff --git a/utils/stats/mapdef_stats.py b/utils/stats/mapdef_stats.py index df4fc189a..29c79f17a 100755 --- a/utils/stats/mapdef_stats.py +++ b/utils/stats/mapdef_stats.py @@ -22,7 +22,7 @@ if __name__ == "__main__": for r in style.rules: print " Filter: %s" % r.filter num_sym += len(r.symbols) - + print "Total number of layers %s" % len(m.layers) print "Total number of styles %s" % len(set(styles)) # unique styles print "Total number of rules %s" % num_rules diff --git a/utils/upgrade_map_xml/upgrade_map_xml.py b/utils/upgrade_map_xml/upgrade_map_xml.py index f69131ba7..45561a7d3 100755 --- a/utils/upgrade_map_xml/upgrade_map_xml.py +++ b/utils/upgrade_map_xml/upgrade_map_xml.py @@ -40,7 +40,7 @@ def indent(elem, level=0): else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i - + def name2expr(sym): if 'name' not in sym.attrib: return name = sym.attrib['name'] @@ -71,12 +71,12 @@ def handle_attr_changes(sym): sym.attrib.pop('text_convert') if sym.attrib.get('text_transform'): sym.attrib.pop('text_transform') - + # https://github.com/mapnik/mapnik/issues/807 justify_alignment = sym.attrib.get('justify_alignment',sym.attrib.get('justify-alignment')) if justify_alignment and justify_alignment == "middle": sym.attrib['justify-alignment'] = 'center' - + minimum_distance = sym.attrib.get('min_distance') if minimum_distance: sym.attrib['minimum-distance'] = minimum_distance @@ -122,7 +122,7 @@ def upgrade(input_xml,output_xml=None,indent_xml=True): if not os.path.exists(input_xml): sys.stderr.write('input xml "%s" does not exist' % input_xml) sys.exit(1) - + pre_read = open(input_xml,'r') if '!ENTITY' in pre_read.read() and not HAS_LXML: sys.stderr.write('\nSorry, it appears the xml you are trying to upgrade has entities, which requires lxml (python bindings to libxml2)\n') @@ -135,7 +135,7 @@ def upgrade(input_xml,output_xml=None,indent_xml=True): except: print 'Could not parse "%s" invalid XML' % input_xml return - + if hasattr(tree,'xinclude'): tree.xinclude() root = tree.getroot() @@ -144,12 +144,12 @@ def upgrade(input_xml,output_xml=None,indent_xml=True): if root.attrib.get('bgcolor'): root.attrib['background-color'] = root.attrib.get('bgcolor') root.attrib.pop('bgcolor') - + # underscores to spaces for underscore2dash(root) - + root.set('minimum-version', '0.7.2') - + # underscores to spaces for fontset = root.findall('FontSet') or root.findall('*/FontSet') for f in fontset: @@ -161,8 +161,8 @@ def upgrade(input_xml,output_xml=None,indent_xml=True): layers = root.findall('Layer') or root.findall('*/Layer') for l in layers: underscore2dash(l) - - + + styles = root.findall('Style') or root.findall('*/Style') if not len(styles): sys.stderr.write('### Warning, no styles encountered and nothing able to be upgraded!\n') @@ -210,7 +210,7 @@ def upgrade(input_xml,output_xml=None,indent_xml=True): if indent_xml: indent(root) - + if output_xml: tree.write(output_xml) else: @@ -248,20 +248,20 @@ if __name__ == "__main__": (options, args) = parser.parse_args() if not len(args) > 0: parser.error("Please provide the path to a map.xml and a new xml to write") - + input_xml = args[0] output_xml = None if len(args) < 3 and not options.update_in_place: if len(args) == 2: output_xml = args[1] - + if (len(args) == 1) or (input_xml == output_xml): parser.error(color_text(1,'\n\nAre you sure you want to overwrite "%s"?\nIf so, then pass --in-place to confirm.\nOtherwise pass a different filename to write an upgraded copy to.\n' % input_xml)) print 'Upgrading "%s" to "%s"...' % (input_xml,output_xml) upgrade(input_xml,output_xml=output_xml,indent_xml=options.indent_xml) - + elif len(args) == 1: print 'Upgrading "%s"...' % (input_xml) upgrade(input_xml,output_xml=output_xml,indent_xml=options.indent_xml) diff --git a/utils/upgrade_map_xml/upgrade_map_xml_keep_ent.py b/utils/upgrade_map_xml/upgrade_map_xml_keep_ent.py index 31885c034..74330de9c 100755 --- a/utils/upgrade_map_xml/upgrade_map_xml_keep_ent.py +++ b/utils/upgrade_map_xml/upgrade_map_xml_keep_ent.py @@ -41,7 +41,7 @@ def name2expr(sym): name = '[%s]' % name sym.attrib.pop('name') sym._setText(name) - + def fixup_pointsym(sym): if sym.attrib.get('width'): sym.attrib.pop('width') @@ -61,10 +61,10 @@ def fixup_sym_attributes(sym): sym.clear() # remove CssParameter elements for k,v in attrib.items(): # insert attributes instead sym.attrib[k] = v - - + + if __name__ == "__main__": - + # Required parameters: # map_xml_file: outdated stylesheet file # output_file: new stylesheet file @@ -74,7 +74,7 @@ if __name__ == "__main__": print >> sys.stderr,'Usage: %s ' % sys.argv[0] sys.exit(1) xml = sys.argv[1] - + if sys.argv[3] is not None: includes_folder = sys.argv[3] @@ -97,7 +97,7 @@ if __name__ == "__main__": # This dummy tree expands the entities that I found and puts them # in a dictionary (entity) => resolved_entity # NOTE: `findall' makes the script very slow - + # First get the entities declared in the header temp_xml = ''.join([good_doctype, dummy_map % '']) expanded_tree = objectify.parse(StringIO.StringIO(temp_xml)) @@ -135,7 +135,7 @@ if __name__ == "__main__": # Tree to be updated to be mapnik compliant tree = objectify.parse(StringIO.StringIO(fixed_xml_string), parser=parser) root = tree.getroot() - + for layer in layer_entities: file = open("%s/%s" % (includes_folder, doctype_entities[layer])) layer_xml_string = file.read() @@ -177,7 +177,7 @@ if __name__ == "__main__": if hasattr(rule,'BuildingSymbolizer') : for sym in rule.BuildingSymbolizer: fixup_sym_attributes(sym) - + updated_xml = etree.tostring(tree, pretty_print=True, xml_declaration=True, @@ -186,9 +186,9 @@ if __name__ == "__main__": # Insert the original doctype declaration fixed_updated_xml = re.sub(r'(?ims)^.*DOCTYPE.*\[.*\]\>', good_doctype, updated_xml) - + output_file = open(sys.argv[2], 'w') output_file.write(fixed_updated_xml) output_file.close() - + # print fixed_updated_xml