1

I am trying to extract coordinates from a KML file. However, I keep getting this error:

for e in doc.Document.Folder.Placemark:
  File "src\lxml\objectify.pyx", line 230, in lxml.objectify.ObjectifiedElement.__getattr__
  File "src\lxml\objectify.pyx", line 450, in lxml.objectify._lookupChildOrRaise
AttributeError: no such child: {http://www.opengis.net/kml/2.2}Placemark

My code for extracting the coordinates is as below:

kml_file = path.join(fileName_kml)

           with open(kml_file) as f:
               doc = parser.parse(f).getroot()
            for e in doc.Document.Folder.Placemark:
                 coor = e.Point.coordinates.text.split(',')
                 print(coor)

Not entirely sure what to do or how to fix it.

EDIT: This is the first bit of my kml file. Im also VERY new to Python so I dont really understand attritubes and children and stuff. So ansers as infformative as possible will be good thanks.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>loc</name>
    <description/>
    <Style id="poly-000000-1200-77-nodesc-normal">
      <LineStyle>

This is what the is:

 <Folder>
      <name>Untitled layer</name>
      <Placemark>
        <name>loc</name>
        <styleUrl>#poly-000000-1200-77-nodesc</styleUrl>
        <Polygon>
          <outerBoundaryIs>
            <LinearRing>
              <tessellate>1</tessellate>
              <coordinates>
                -1.0947725,53.9600505,0
                -1.0967252,53.95697,0
                -1.0909316,53.9562629,0
                -1.0887214,53.9598864,0
                -1.0947725,53.9600505,0
              </coordinates>
            </LinearRing>
          </outerBoundaryIs>
        </Polygon>
      </Placemark>
    </Folder>
Tee M
  • 11
  • 1
  • 3
  • Could you please provide a sample of the file that you are parsing, so that we can see its scructure? – Chillie Sep 19 '18 at 11:36
  • It's telling you that: `no such child: {http://www.opengis.net/kml/2.2}Placemark` - so are you sure it's a child of document or is elsewhere or nested deeper within Document? – Jon Clements Sep 19 '18 at 11:39
  • " loc – Tee M Sep 19 '18 at 11:46
  • 1
    @TeeM you'll need to [edit] and code block format that into your question... comments aren't useful for code... – Jon Clements Sep 19 '18 at 11:46
  • Sorry done now. Thanks – Tee M Sep 19 '18 at 11:50
  • @TeeM you'll need to show much more of the file than that... you see the `` there and how other stuff is indented in `<>`s as well (like `Style`) - can you find the bit that has `` and then show that until you see ``? – Jon Clements Sep 19 '18 at 11:51
  • Ahhh... looks like the `Placemark` is probably an immediate child of the document... going from https://developers.google.com/kml/documentation/kml_tut#placemarks - so you might be after `for e in doc.Placemark`... – Jon Clements Sep 19 '18 at 12:03
  • that doesnt seem to work either – Tee M Sep 19 '18 at 12:38
  • Please ensure that we can reproduce the problem. See [mcve]. – mzjn Sep 21 '18 at 06:18
  • I have solved it now thanks – Tee M Sep 21 '18 at 07:35
  • 3
    Then please post an answer that explains how you solved it. – mzjn Sep 21 '18 at 08:12

0 Answers0