def from_xml_file(filename): xmldoc = parse(filename) width = get_track_root_info(xmldoc, 'width') default_profil_steps_length = get_track_root_info( xmldoc, 'profil steps length') segments = [] for section in get_main_track(xmldoc).childNodes: if section.nodeType == Node.ELEMENT_NODE: s = Segment.from_xml(section) if s.end_radius is None: segments.append(s) else: profil_steps_length = default_profil_steps_length if s.profil_steps_length is not None: profil_steps_length = s.profil_steps_length length = abs((s.radius + s.end_radius) / 2.0 * s.arc) num_steps = int(length / profil_steps_length) + 1 if num_steps == 1: segments.append(s) else: # rearange steps so: # - every part of the turn has the same length # - the first part has curvature given by ``radius`` # - the last part had curvature given by ``end_radius`` # - there are ``steps`` parts # - the total ``arc`` angle does not change dradius = (s.end_radius - s.radius) / float(num_steps - 1) tmp = 0.0 for i in xrange(num_steps): tmp += 1.0 / (s.radius + i * dradius) geom_average = 1.0 / tmp for i in xrange(num_steps): name = s.name + '.' + str(i) radius = s.radius + i * dradius arc = s.arc * geom_average / radius segments.append( Segment(name=name, arc=arc, radius=radius, end_radius=None)) return Track(segments, width)
def test_from_xml(self): xmldoc = parseString(""" <section name="s6"> <attstr name="type" val="lft" /> <attnum name="arc" unit="deg" val="102.0" /> <attnum name="radius" unit="m" val="32.7" /> <attnum name="end radius" unit="m" val="38.4" /> <attnum name="grade" unit="%" val="-3.0" /> <attnum name="profil end tangent" unit="%" val="-3.0" /> <attnum name="profil steps length" unit="m" val="4.0" /> <attnum name="banking start" unit="deg" val="0.0"/> <attnum name="banking end" unit="deg" val="-4.0"/> <attstr name="marks" val="50;100;150"/> <section name="Left Border"> <attnum name="width" unit="m" val="1.0" /> <attnum name="height" unit="m" val="0.05" /> <attstr name="surface" val="curb-left" /> <attstr name="style" val="curb" /> </section> <section name="Left Side"> <attnum name="start width" unit="m" val="10.0" /> <attnum name="end width" unit="m" val="12.0" /> <attstr name="surface" val="dirtA" /> </section> <section name="Right Side"> <attnum name="start width" unit="m" val="32.0" /> <attnum name="end width" unit="m" val="40.0" /> <attstr name="surface" val="sand" /> </section> </section> """) s = Segment.from_xml(xmldoc.childNodes[0]) self.assertEqual(s.name, 's6') self.assertIsNone(s.length) self.assertAlmostEqual(math.degrees(s.arc), 102.0) self.assertAlmostEqual(s.radius, 32.7) self.assertAlmostEqual(s.end_radius, 38.4) self.assertAlmostEqual(s.profil_steps_length, 4.0)