0

I have a KML file called Layers.KML which has multiple sub folders - Tree_Heights_Elevations, 20190516 - Chain Link Fence, 20190516 - Clearing Limits, 2016-0516 - Solar Array .

Tree_Heights_Elevations subfolders contain multiple points, each of which contains tree heights. Here is the screen shot of the KML file

enter image description here

As you can see in the image above -

Layers.KML has another folder which is Tree_Heights_Elevations which has multiple points. Eg: ASSPOTX which has some information. I want to extract value of Elevation.

I am using BeautifulSoup Python package but unable to extract the Elevation. Could anyone guide me the right way to extract data. I do not know if this is the simplest way to extract data into CSV

import csv, os
from bs4 import BeautifulSoup

inputfile = "Layers.KML"
with open(inputfile, 'r') as f:
  soup = BeautifulSoup(f)


  for node in soup.select('Tree_Heights_Elevations'):
       print(node)
  • 1
    I think the simplest way of extracting this information would be using OGR – Marcelo Villa Jul 16 at 23:13
  • @MarceloVilla these tables are stored as HTML in a <![CDATA[ block in the <description> tag of the KML. OGR doesn't parse the HTML, it just dumps it into the feature description property. – user2856 Jul 17 at 0:58
  • @AakashRaj please edit your question to include some sample data. – user2856 Jul 17 at 0:59
  • @user2856 I dont know how can I add the data. But if you look at the screenshot of GoogleEarth there is a KML file Layers.KML which has subfolder Tree_Height_Elevations which has over 400 files. One such files selected ASSPOTX which has Elevation info (highlighed in yellow). I was to parse it and extract data into a csv form. – Aakash Raj Jul 17 at 14:24
  • As mentioned by @user2856, the data you're trying to extract is in an HTML table in each KML feature's description tag. This is an unfortunate result of using ESRI's ArcGIS and its KML export tools, which cram all the attribute data into an HTML blob. A better practice, which many other KML exporters do, is to write the attributes into ExtendedData tags in the KML feature itself, and then use a balloon template to construct the balloon content. To extract the data, you'll want to parse the description tags out of each KML feature, and then the HTML from each of those. – Christiaan Adams Jul 18 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.