def test_series_contour_to_xml(self): xml_in = etree.parse(os.path.join(DATA_LOC, "_series_contour.xml")).getroot() contour = Contour( **reconstruct_reader.extract_series_contour_attributes(xml_in)) xml_out = reconstruct_writer.series_contour_to_xml(contour) self.assertEqual( xml_element_to_dict(xml_in), xml_element_to_dict(xml_out), )
def test_shape_point(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) point_contour = Contour( closed=False, points=[(13.5904, 16.6472)], transform=transform, ) self.assertEqual(point_contour.shape.type, "Point")
def process_section_file(path): """Return a Section object from a Section XML file.""" tree = etree.parse(path) root = tree.getroot() # Create Section and populate with metadata data = extract_section_attributes(root) data["name"] = os.path.basename(path) data["_path"] = os.path.dirname(path) section = Section(**data) # Process Images, Contours, Transforms for node in root: # make Transform object transform_data = extract_transform_attributes(node) transform = Transform(**transform_data) children = [child for child in node] # Image node images = [child for child in children if child.tag == "Image"] if len(images) > 1: raise Exception("No support for Sections with more than one Image.") elif images: image_data = extract_image_attributes(images[0]) image_data["_path"] = section._path image_data["transform"] = transform # Image contours image_contours = [child for child in children if child.tag == "Contour"] if len(image_contours) > 1: raise Exception( "No support for Images with more than one Contour.") elif not image_contours: raise Exception("No support for Images with out a Contour.") else: image_contour_data = extract_section_contour_attributes( image_contours[0]) image_data.update(image_contour_data) image = Image(**image_data) section.images.append(image) # Non-Image Node else: for child in children: if child.tag == "Contour": contour_data = extract_section_contour_attributes(child) contour_data["transform"] = transform contour = Contour(**contour_data) section.contours.append(contour) return section
def processSectionFile(tree): '''Returns attribute dictionary, image object, and contour list associated with a Section's XML <tree>''' # Process attributes root = tree.getroot() attributes = sectionAttributes(root) # Process images and contours images = [] contours = None for transform in root: # make Transform object tForm = Transform(transformAttributes(transform)) children = [child for child in transform] # Image transform node img = [child for child in children if child.tag == 'Image'] if len(img) > 0: img = img.pop() img = Image(imageAttributes(img)) imgContour = [ child for child in children if child.tag == 'Contour' ] if len(imgContour) > 0: imgContour = imgContour.pop() contour = Contour(contourAttributes(imgContour), tForm) contour.image = img # set contour's image to the image img.contour = contour # set image's contour to the contour images.append(img) # Non-Image Transform Node else: for child in children: if child.tag == 'Contour': cont = Contour(contourAttributes(child), tForm) if contours == None: contours = [] contours.append(cont) return attributes, images, contours
def test_shape_polygon(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) poly_contour = Contour( closed=True, points=[ (19.2342, 15.115), (19.2826, 15.115), (19.2584, 15.1593), ], transform=transform, ) self.assertEqual(poly_contour.shape.type, "Polygon")
def test_shape_weird_line(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) line_contour = Contour( closed=False, points=[ (15.9352, 10.8615), (15.9332, 10.8508), (15.9352, 10.8615), ], transform=transform, ) self.assertEqual(line_contour.shape.type, "LineString")
def processSeriesFile(tree): root = tree.getroot() attributes = seriesAttributes(root) contours = None zcontours = None for elem in root: if elem.tag == 'Contour': contour = Contour(contourAttributes(elem), None) if contours == None: contours = [] contours.append(contour) elif elem.tag == 'ZContour': zcontour = ZContour(zContourAttributes(elem)) #=== if zcontours == None: zcontours = [] zcontours.append(zcontour) return attributes, contours, zcontours
def test_shape_line(self): transform = Transform( dim=0, xcoef=[0, 1, 0, 0, 0, 0], ycoef=[0, 0, 1, 0, 0, 0], ) line_contour = Contour( closed=False, points=[ (24.6589, 17.3004), (24.7018, 17.3489), (24.7634, 17.3917), (24.8174, 17.4197), ], transform=transform, ) self.assertEqual(line_contour.shape.type, "LineString")