# Tests SVG output and validity import os import unittest from xml.dom.minidom import parseString as parse_xml_string from shapely.geometry import ( LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, ) from shapely.geometry.collection import GeometryCollection class SvgTestCase(unittest.TestCase): def assertSVG(self, geom, expected, **kwrds): """Helper function to check XML and debug SVG""" svg_elem = geom.svg(**kwrds) try: parse_xml_string(svg_elem) except Exception: raise AssertionError("XML is not valid for SVG element: " + str(svg_elem)) svg_doc = geom._repr_svg_() try: doc = parse_xml_string(svg_doc) except Exception: raise AssertionError("XML is not valid for SVG document: " + str(svg_doc)) svg_output_dir = None # svg_output_dir = '.' # useful for debugging SVG files if svg_output_dir: fname = geom.geom_type if geom.is_empty: fname += "_empty" if not geom.is_valid: fname += "_invalid" if kwrds: fname += "_" + ",".join(str(k) + "=" + str(kwrds[k]) for k in kwrds) svg_path = os.path.join(svg_output_dir, fname + ".svg") with open(svg_path, "w") as fp: fp.write(doc.toprettyxml()) assert svg_elem == expected def test_point(self): # Empty self.assertSVG(Point(), "") # Valid g = Point(6, 7) self.assertSVG( g, '', ) self.assertSVG( g, '', scale_factor=5, ) def test_multipoint(self): # Empty self.assertSVG(MultiPoint(), "") # Valid g = MultiPoint([(6, 7), (3, 4)]) self.assertSVG( g, '' '', ) self.assertSVG( g, '' '', scale_factor=5, ) def test_linestring(self): # Empty self.assertSVG(LineString(), "") # Valid g = LineString([(5, 8), (496, -6), (530, 20)]) self.assertSVG( g, '', ) self.assertSVG( g, '', scale_factor=5, ) # Invalid self.assertSVG( LineString([(0, 0), (0, 0)]), '', ) def test_multilinestring(self): # Empty self.assertSVG(MultiLineString(), "") # Valid self.assertSVG( MultiLineString([[(6, 7), (3, 4)], [(2, 8), (9, 1)]]), '' '', ) # Invalid self.assertSVG( MultiLineString([[(2, 3), (2, 3)], [(2, 8), (9, 1)]]), '' '', ) def test_polygon(self): # Empty self.assertSVG(Polygon(), "") # Valid g = Polygon( [(35, 10), (45, 45), (15, 40), (10, 20), (35, 10)], [[(20, 30), (35, 35), (30, 20), (20, 30)]], ) self.assertSVG( g, '', ) self.assertSVG( g, '', scale_factor=5, ) # Invalid self.assertSVG( Polygon([(0, 40), (0, 0), (40, 40), (40, 0), (0, 40)]), '', ) def test_multipolygon(self): # Empty self.assertSVG(MultiPolygon(), "") # Valid self.assertSVG( MultiPolygon( [ Polygon([(40, 40), (20, 45), (45, 30), (40, 40)]), Polygon( [(20, 35), (10, 30), (10, 10), (30, 5), (45, 20), (20, 35)], [[(30, 20), (20, 15), (20, 25), (30, 20)]], ), ] ), '' '', ) # Invalid self.assertSVG( MultiPolygon( [ Polygon([(140, 140), (120, 145), (145, 130), (140, 140)]), Polygon([(0, 40), (0, 0), (40, 40), (40, 0), (0, 40)]), ] ), '' '', ) def test_collection(self): # Empty self.assertSVG(GeometryCollection(), "") # Valid self.assertSVG( GeometryCollection([Point(7, 3), LineString([(4, 2), (8, 4)])]), '' '', ) # Invalid self.assertSVG( Point(7, 3).union(LineString([(4, 2), (4, 2)])), '' '', )