def __init__(self, document, **args):
     self.tool = SvgTool()
     self.x = args.get("x")
     self.y = args.get("y")
     self.w = args.get("w")
     self.h = args.get("h")
     self.document = document
class SvgExtractRegion(object):
    """ """
    def __init__(self, document, **args):
        self.tool = SvgTool()
        self.x = args.get("x")
        self.y = args.get("y")
        self.w = args.get("w")
        self.h = args.get("h")
        self.document = document

    def extractRegion(self):
        content_file = self.tool.makeNewSvgStringIO(
            self.document.getFilename(), self.document.getData())
        image = self.tool.makeNewSvgImage(content_file)

        new_content_file = self.tool.makeNewSvgStringIO(
            "new_" + self.document.getFilename(), StringIO.StringIO(''))
        new_svg = self.tool.makeNewSvgImage(new_content_file)

        region = Box(Point(self.x, self.y), self.w, self.h)

        new_image = self.tool.selectGrainsInRegion(region, image, new_svg)
        objGran = Grain(content=new_image.getContentFile(),
                        mimetype="image/svg+xml",
                        graintype='svg')
        return objGran
示例#3
0
 def setUp(self):
     self.tool = SvgTool()
     self.xml_tool = SvgXmlTool()
             
     self.region0 = Box(Point(0,0),10,10)
     self.region1 = Box(Point(1,1),8,8)
     self.bbox0 = Box(Point(1,1),8,8)
     self.bbox1 = Box(Point(0,0),18,18)
示例#4
0
class SvgToolTest(unittest.TestCase):

    def setUp(self):
        self.tool = SvgTool()
        self.xml_tool = SvgXmlTool()
                
        self.region0 = Box(Point(0,0),10,10)
        self.region1 = Box(Point(1,1),8,8)
        self.bbox0 = Box(Point(1,1),8,8)
        self.bbox1 = Box(Point(0,0),18,18)

    def test_make_new_svg_image_with_file(self):
        content_file = self.tool.makeNewSvgFile()
        assert(51, len(self.tool.makeNewSvgImage(content_file)._content.content))
        assert("noname" in os.listdir("/tmp"))
        os.remove("/tmp/noname")
        content_file = self.tool.makeNewSvgFile("testsvgtool1.svg")
        assert(51, len(self.tool.makeNewSvgImage(content_file)._content.content))
        assert("testsvgtool1.svg" in os.listdir("/tmp"))
        os.remove("/tmp/testsvgtool1.svg")
        content_file = self.tool.makeNewSvgFile("test2.svg",DATA_DIR)
        assert(51, len(content_file.content))
        assert((content_file.name) in os.listdir(content_file.directory))
        os.remove(content_file.getAbsolutePath())  

    def test_make_new_svg_image_with_string_io(self):
        content_string_io = self.tool.makeNewSvgStringIO("test.svg", StringIO.StringIO(SVG_HEAD_EXPECTED))
        assert(268, len(self.tool.makeNewSvgImage(content_string_io)._content.content))
        content_string_io = self.tool.makeNewSvgStringIO("test.svg", StringIO.StringIO(''))
        image = self.tool.makeNewSvgImage(content_string_io)
        assert(28, len(image._content.content))
        #image.toPng()

    def test_make_new_svg_file(self):
        assert(51, len(self.tool.makeNewSvgFile().content))
        assert("noname" in os.listdir("/tmp"))
        os.remove("/tmp/noname")
        assert(51, len(self.tool.makeNewSvgFile("testsvgtool1.svg").content))
        assert("testsvgtool1.svg" in os.listdir("/tmp"))
        os.remove("/tmp/testsvgtool1.svg")
        imgFile = self.tool.makeNewSvgFile("test2.svg",DATA_DIR)      
        assert(51, len(imgFile.content))
        assert((imgFile.name) in os.listdir(imgFile.directory))
        os.remove(imgFile.getAbsolutePath())
        
    def test_alter_svg_data(self):
        content_file = self.tool.makeNewSvgFile("testsvgtool.svg")
        im = self.tool.makeNewSvgImage(content_file)
        assert("testsvgtool.svg" in os.listdir("/tmp"))
        im = self.tool.alterSvgData(im, "newname.svg")
        assert("newname.svg" in os.listdir("/tmp"))
        if(not "testsvgtool" in os.listdir("/tmp")):
            os.mkdir('/tmp/testsvgtool')
            
        im = self.tool.alterSvgData(im, directory='/tmp/testsvgtool')
        assert("newname.svg" in os.listdir(im._content.directory))
        os.remove("/tmp/testsvgtool/newname.svg")
        os.rmdir("/tmp/testsvgtool")
        os.remove("/tmp/newname.svg")       
        os.remove("/tmp/testsvgtool.svg")  
        
    def test_verify_point_into_region(self):
        assert(self.tool.isBBoxInRegion(self.bbox0, self.region0))
        assert(self.tool.isBBoxInRegion(self.region0, self.region0))
        assert(not self.tool.isBBoxInRegion(self.bbox1, self.region1))


    def test_handle_tag_in_svg_file(self):
        """
        content_file = self.tool.makeNewSvgFile("test.svg",DATA_DIR)
        img = self.tool.makeNewSvgImage(content_file)
        tag_list = ["g7","rect9", "g15", "rect17"]
        self.assertEquals(["rect9", "rect17"], self.tool._SvgTool__handleTagIdList(img.getCode(), tag_list))
        """

    def test_handle_tag_in_svg_string_io(self):
        """
        content_file = self.tool.makeNewSvgStringIO("test.svg",StringIO.StringIO(SVG_CODE_TEST))
        img = self.tool.makeNewSvgImage(content_file)
        tag_list = ["g7","rect9", "g15", "rect17"]
        self.assertEquals(["rect9", "rect17"], self.tool._SvgTool__handleTagIdList(img.getCode(), tag_list))
        """

    def test_generate_bounding_box(self):
        grains = [['svg2', '25', '25', '100', '100'], \
        ['rectangle', '0', '0', '50', '50'], \
        ['g7', '25', '25', '50', '50'], \
        ['rect9', '25', '25', '50', '50'], \
        ['g11', '50', '50', '50', '50'], \
        ['rect13', '50', '50', '50', '50'], \
        ['g15', '75', '75', '50', '50'], \
        ['rect17', '75', '75', '50', '50']]
        self.assertEquals(grains, self.tool.generateBoundingBox(os.path.join(DATA_DIR, 'test.svg')))
        
        bbox = [['svg2', '39.5', '19.5', '481', '160.5'], \
        ['uprightPost', '-0.5', '-0.5', '11', '151'], \
        ['column', '11.5', '-0.5', '149', '151'], \
        ['beam', '-0.5', '-0.5', '137', '17'], \
        ['rect11', '-0.5', '-0.5', '4', '17'], \
        ['rect13', '4.5', '-0.5', '127', '9'], \
        ['rect15', '132.5', '-0.5', '4', '17'], \
        ['use17', '11.5', '-0.5', '137', '17'], \
        ['use19', '11.5', '49.5', '137', '17'], \
        ['use21', '11.5', '99.5', '137', '17'], \
        ['use23', '149.5', '-0.5', '11', '151'], \
        ['rack', '-10.5', '-0.5', '481', '160.5'], \
        ['floor', '-10.5', '149.5', '481', '10.5'], \
        ['rect27', '-10', '150', '480', '10'], \
        ['line29', '-10.5', '149.5', '481', '1'], \
        ['use31', '-0.5', '-0.5', '11', '151'], \
        ['use33', '11.5', '-0.5', '149', '151'], \
        ['use35', '161.5', '-0.5', '149', '151'], \
        ['use37', '311.5', '-0.5', '149', '151'], \
        ['use39', '39.5', '19.5', '481', '160.5']]
        self.assertEquals(bbox, self.tool.generateBoundingBox(os.path.join(DATA_DIR, 'shelves.svg')))

    def test_copy_svg_head_file(self):
        content_file = self.tool.makeNewSvgFile("test.svg",DATA_DIR)
        new_content_file = self.tool.makeNewSvgFile("new_file.svg")
        img = self.tool.makeNewSvgImage(content_file)
        new_svg = self.tool.copySvgHead(img.getCode(), self.tool.makeNewSvgImage(new_content_file).getCode())
        self.assertEquals(SVG_HEAD_EXPECTED, new_svg.toxml())
        os.remove("/tmp/new_file.svg")
        

    def test_copy_svg_head_string_io(self):
        """
        content_file = self.tool.makeNewSvgStringIO("test.svg",StringIO.StringIO(SVG_CODE_TEST))
        new_content_file = self.tool.makeNewSvgStringIO("new_file.svg", StringIO.StringIO(''))
        img = self.tool.makeNewSvgImage(content_file)
        new_svg = self.tool.copySvgHead(img.getCode(), self.tool.makeNewSvgImage(new_content_file).getCode())
        self.assertEquals(SVG_HEAD_EXPECTED, new_svg.toxml())
        """

    def test_select_grain_in_region_test(self):
        content_file = self.tool.makeNewSvgFile("test.svg",DATA_DIR)
        img = self.tool.makeNewSvgImage(content_file)
        region = Box(Point(20,20),70,70)
        new_content_file = self.tool.makeNewSvgFile("selec_test.svg")
        newSvg = SvgImage(new_content_file)        
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)
        #self.assertEquals(['g7', 'rect9'], tagList)
        self.assertEquals(newSvg._content.content, SVG_FILE_TEST_CONTENT)
        os.remove("/tmp/selec_test.svg")

    def test_select_grain_in_region_test(self):
        """
        content_file = self.tool.makeNewSvgFile("test.svg",DATA_DIR)
        content_string_io = self.tool.makeNewSvgStringIO("test.svg", StringIO.StringIO(content_file.content))
        img = self.tool.makeNewSvgImage(content_string_io)
        region = Box(Point(20,20),70,70)
        new_content_string_io = self.tool.makeNewSvgStringIO("selec_test.svg", StringIO.StringIO(''))
        newSvg = SvgImage(new_content_string_io)        
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)
        #self.assertEquals(['g7', 'rect9'], tagList)
        self.assertEquals(newSvg._content.content, SVG_FILE_TEST_CONTENT)
        os.remove("/tmp/selec_test.svg")
        """

    def test_select_grain_in_region_horse(self):
        content_file = self.tool.makeNewSvgFile("1_Cav_Shoulder_Insignia.svg",DATA_DIR)
        img = self.tool.makeNewSvgImage(content_file)
        region = Box(Point(0,0),130,130)
        new_content_file = self.tool.makeNewSvgFile('selec_horse.svg')
        newSvg = SvgImage(new_content_file)        
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)
        #self.assertEquals(['path3154', 'path3156'], tag_list)
        #self.assertEquals(newSvg._file.content, SVG_FILE_HORSE_CONTENT)
        #os.remove("/tmp/selec_horse.svg")
        
        img.toPng('horse.png', '/tmp')
        newSvg.toPng()

    def test_select_grain_in_region_blason(self):
        content_file = self.tool.makeNewSvgFile("Blason_54.svg",DATA_DIR)    
        img = self.tool.makeNewSvgImage(content_file)
        region = Box(Point(120,0),460,450)
        new_content_file = self.tool.makeNewSvgFile('selec_blason.svg')        
        newSvg = SvgImage(new_content_file)
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)
        """
        TAGS_IN_REGION = ['path3954', 'path3952', 'path3950', 'path3946', \
        'path3942', 'path3938', 'path3934', 'path3930', 'path3926', \
        'path3924', 'path3922', 'path3920', 'path3918', 'path3916', \
        'path1932', 'g5398', 'rect5400', 'path5402', 'path5404', 'path5406', \
        'path5408', 'path5410', 'path5412', 'path5414', 'path5416', \
        'path5418', 'path5420', 'path5422', 'path5424', 'path5426', \
        'path5428', 'path5430', 'path5432', 'path5434', 'path5436', \
        'path5438', 'path5440', 'path5442', 'path5444', 'path5446', \
        'path5462', 'path5464', 'path5466', 'path5468', 'path5472', \
        'path5476', 'path5480', 'path5484', 'path5488', 'path5492', \
        'path5496', 'path8232']
        """
        
        img.toPng('blason.png', '/tmp')
        newSvg.toPng()
        #self.assertEquals(TAGS_IN_REGION, tagList)
        #os.remove("/tmp/selec_blason.svg")

    def test_select_grain_in_region_heart(self):
        content_file = self.tool.makeNewSvgFile("15_Eunomia_symbol.svg",DATA_DIR)
        img = self.tool.makeNewSvgImage(content_file)
        region = Box(Point(4,150),250,260)
        new_content_file = self.tool.makeNewSvgFile('selec_heart.svg')
        newSvg = SvgImage(new_content_file)
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)
        #self.assertEquals(['path1872'], tag_list)
        #os.remove("/tmp/selec_heart.svg")

    def test_select_grain_in_region_cat(self):
        content_file = self.tool.makeNewSvgFile("3Stormo-Patch.svg",DATA_DIR)
        img = self.tool.makeNewSvgImage(content_file)
        region = Box(Point(80,170),90,90)
        new_content_file = self.tool.makeNewSvgFile('selec_cat.svg')
        newSvg = SvgImage(new_content_file)
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)
        #self.assertEquals(['g4297', 'path2306', 'path3291', 'path4290'], tag_list)
        #os.remove("/tmp/selec_cat.svg")

    def test_select_grain_in_region_brazil(self):
        content_file = self.tool.makeNewSvgFile("2006_Brazilian_Election_2nd_round_States.svg",DATA_DIR)
        img = self.tool.makeNewSvgImage(content_file)
        region = Box(Point(360,310),150,190)
        new_content_file = self.tool.makeNewSvgFile('selec_brazil.svg')
        newSvg = SvgImage(new_content_file)
        newSvg = self.tool.selectGrainsInRegion(region, img, newSvg)