python - KML parsing child elements using django -


i trying parse kml file using django. using pykml parser module. have completed following steps.

 root = parser.fromstring(open('myfile.kml').read()) 

the content of file is:

 <document>     <placemark>       <name>t1</name>        <point><coordinates>v1</coordinates>        </point>     </placemark>     <placemark>      <name>t2</name>      <polygon>        <outerboundaryis>          <linearring><coordinates>v2</coordinates>         </linearring>       </outerboundaryis>     </polgon>    </placemark>   </document> 

i able retrieve names using following:

name = []  ele in root.document.placemark:     name.append(ele.name) 

but dont know how retrieve coordinates values different placemark. please me here.

try this:

for pm in root.document.placemark:     point = [p p in pm.getchildren() if p.tag.endswith('point')]     if point:         coords = point[0].coordinates.text     else:         poly = [p p in pm.getchildren() if p.tag.endswith('polygon')]         if poly:             coords = poly[0].outerboundaryis.linearring.coordinates.text     print pm.name, coords 

Comments