[GIS] KML Network Links to Python Scripts

googlekmlNetworkpython

I'm trying to update a KML file using a network link and a python script that grabs and parses data from a csv file. The python script runs and creates the KML code when I run it on it's own but the KML file containing the network link doesn't seem to be able to run the script. The python script and data file are stored on my computer and not on a web server and I'm wondering if this is the problem. For my purposes I would need to run these files from a computer and not a web server in the field so is this possible? I've included the code.

The KML file:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">  
<NetworkLink>
    <name>Test Points</name>
    <Link>
        <href>addPoints.py</href>
        <refreshMode>onInterval</refreshMode>
        <refreshInterval>60</refreshInterval>
    </Link>
</NetworkLink>
</kml>

And the python script:

#!/Python27/ArcGIS10.1/python

import urllib2
from genshi.template import TemplateLoader

def collect_latest_fires():
     f = open('data_file.csv')

    for line in f:
        fields = line.split(',')
        latitude = fields[0]
        longitude = fields[1]
        altitude = fields[2]
        coords = '%s, %s, %s' % (longitude, latitude,altitude)

        yield {
            'Name': 'Wildland Fire at %s' % coords,
            'Description': 'test',
            'Coordinates': coords
            }

loader = TemplateLoader(['.'])
template = loader.load('GenshiTemplate.kml')
stream = template.generate(collection=collect_latest_fires())

kml = stream.render(method='xml',encoding='utf-8')

print 'Content-Type: application/vnd.google-earth.kml+xml\n'
print kml

The data file is just a csv file containing latitude, longitude and altitude.

Best Answer

I was trying to do similar thing to you. I was trying to read in latitude, longitude, and altitude data from a GPS sensor using python, and set up a network link in Google Earth to update the pin marker in Google Earth as new data came in. I just needed it to run locally on my computer, so I didn't need to create a URL, as I've seen other people doing. What I did was create a .kml file that acted as my network link by typing the following code into Notepad (text editor) and saved it as NetworkLinkfilename.kml

Notice in the line I am referring to a .kml file (Linkfilename.kml), while also specifying the document path. This network will update the position of the pin in Google Earth every 3 seconds.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
  <NetworkLink>
    <Link>
      <href>C:\PythonPrograms\Linkfilename.kml</href>
      <refreshMode>onInterval</refreshMode>
    <refreshInterval>3</refreshInterval>
    </Link>
  </NetworkLink>
</Document>
</kml>

In my Python program, I used a module called simplekml. I have the following code in my python program:

kml = simplekml.Kml()
doc = kml.newdocument(name = 'Linkfilename')
doc.newpoint(name="PCB", altitudemode='absolute' ,coords=[(lon[i],lat[i],alt[i])])
kml.save("Linkfilename.kml")

This code is within a loop, and longitude, latitude, and latitude are vectors, which is why they are indexed with i. This way the values change as the loop is iterated, and the position of the most current index will be shown in Google Earth. This will create a .kml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_52">
        <Document id="feat_53">
            <name>Linkfilename</name>
            <Placemark id="feat_54">
                <name>PCB</name>
                <Point id="geom_17">
                    <coordinates>-83.70903,42.29417,287.0</coordinates>
                    <altitudeMode>absolute</altitudeMode>
                </Point>
            </Placemark>
        </Document>
    </Document>
</kml>

To setup the link, open Google Earth, drag and drop the NetworkLinkfilename.kml file. Run the python script, then the Linkfilename.kml will be created, make sure it is created in the location you specified (correct document path) in the network link file. Once the linkfile is created, drag and drop it into Google Earth. The pin marker will update location every 3 seconds.

Related Question