Python and XML (LandXML) -
ok, have xml file looks this, landxml file, works other xml. need parse python (currently i'm using elementtree lib) in way cycle through sub-element <coordgeom></coordgeom>
, make lists dependent on child - can linelist, spirallist , curvelist. content of lists atribute values (don't need names) nested list every object. like:
linelist=[[10.014571204947,209.285340662374,776.719431311241 -399.949629732524,813.113864060552 -193.853052659974],[287.308329990254,277.363320844698,558.639337133827 380.929458057393,293.835705515579 463.448840215686]]
here sample code:
<?xml version="1.0"?> <landxml xmlns="http://www.landxml.org/schema/landxml-1.2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.landxml.org/schema/landxml-1.2 http://www.landxml.org/schema/landxml-1.2/landxml-1.2.xsd" date="2015-05-15" time="09:47:43" version="1.2" language="english" readonly="false"> <units> <metric areaunit="squaremeter" linearunit="meter" volumeunit="cubicmeter" temperatureunit="celsius" pressureunit="millibars" diameterunit="millimeter" angularunit="decimal degrees" directionunit="decimal degrees"></metric> </units> <project name="c:\users\rade\downloads\situacija profili.dwg"></project> <application name="autocad civil 3d" desc="civil 3d" manufacturer="autodesk, inc." version="2014" manufacturerurl="www.autodesk.com/civil" timestamp="2015-05-15t09:47:43"></application> <alignments name=""> <alignment name="proba" length="1201.057158008475" stastart="0." desc=""> <coordgeom> <line dir="10.014571204947" length="209.285340662374"> <start>776.719431311241 -399.949629732524</start> <end>813.113864060552 -193.853052659974</end> </line> <spiral length="435.309621307305" radiusend="300." radiusstart="inf" rot="cw" spitype="clothoid" theta="41.569006803911" totaly="101.382259815422" totalx="412.947724836996" tanlong="298.633648469722" tanshort="152.794210168398"> <start>813.113864060552 -193.853052659974</start> <pi>865.04584458778 100.230482065888</pi> <end>785.087350093002 230.433054310499</end> </spiral> <curve rot="cw" chord="150.078507004323" crvtype="arc" delta="28.970510103309" dirend="299.475054297727" dirstart="328.445564401036" external="9.849481983234" length="151.689236185509" midord="9.536387074322" radius="300." tangent="77.502912753511"> <start>785.087350093002 230.433054310499</start> <center>529.44434090873 73.440532656728</center> <end>677.05771309169 334.61153478517</end> <pi>744.529424397382 296.476647100012</pi> </curve> <spiral length="127.409639008589" radiusend="inf" radiusstart="300." rot="cw" spitype="clothoid" theta="12.166724307463" totaly="8.989447716697" totalx="126.8363181841" tanlong="85.141254974713" tanshort="42.653117896421"> <start>677.05771309169 334.61153478517</start> <pi>639.925187941987 355.598770007863</pi> <end>558.639337133827 380.929458057393</end> </spiral> <line dir="287.308329990254" length="277.363320844698"> <start>558.639337133827 380.929458057393</start> <end>293.835705515579 463.448840215686</end> </line> </coordgeom> <profile name="proba"> <profalign name="niveleta"> <pvi>0. 329.48636525895</pvi> <circcurve length="69.993187715052" radius="5000.">512.581836381869 330.511528931714</circcurve> <circcurve length="39.994027682446" radius="5000.">948.834372016349 337.491569501865</circcurve> <pvi>1201.057158008475 339.509351789802</pvi> </profalign> </profile> </alignment> </alignments> </landxml>
this example far production ready, should contain need need implementing full solution. searches lines only, not know attributes needs on other kinds of geometry , hasn't got kind of error handling.
and it's not pretty.
from lxml import etree doc = etree.parse('/tmp/stuff.xml') geometry = doc.find('.//{http://www.landxml.org/schema/landxml-1.2}coordgeom') line_list = [] child in geometry: child_list = [] if child.tag == '{http://www.landxml.org/schema/landxml-1.2}line': child_list.append(float(child.attrib['dir'])) child_list.append(float(child.attrib['length'])) start, end = child.find('{http://www.landxml.org/schema/landxml-1.2}start'), child.find('{http://www.landxml.org/schema/landxml-1.2}end') child_list.extend([float(coord) coord in start.text.split(' ')]) child_list.extend([float(coord) coord in end.text.split(' ')]) line_list.append(child_list) print line_list
if want extend example, have thorough read-thru of lxml tutorial. i've used in tutorial.
Comments
Post a Comment