My favorites | Sign in
Project Home Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
'''Example of generating KML from data in a CSV file

References:

'''
import csv
import urllib2
from datetime import datetime
from lxml import etree
from pykml.factory import KML_ElementMaker as KML

def makeExtendedDataElements(datadict):
'''Converts a dictionary to ExtendedData/Data elements'''
edata = KML.ExtendedData()
for key, value in datadict.iteritems():
edata.append(KML.Data(KML.value(value), name=key + "_"))
return edata

# create a KML document with a folder and a default style
stylename = "earthquake-balloon-style"
balloonstyle = KML.BalloonStyle(
KML.text("""
<table Border=1>
<tr><th>Earthquake ID</th><td>$[Eqid_]</td></tr>
<tr><th>Magnitude</th><td>$[Magnitude_]</td></tr>
<tr><th>Depth</th><td>$[Depth_]</td></tr>
<tr><th>Datetime</th><td>$[Datetime_]</td></tr>
<tr><th>Coordinates</th><td>($[Lat_],$[Lat_])</td></tr>
<tr><th>Region</th><td>$[Region_]</td></tr>
</table>"""
),
)

doc = KML.Document()

iconstyles = [
[2,'ff000000'],
[3,'ffff0000'],
[4,'ff00ff55'],
[5,'ffff00aa'],
[6,'ff00ffff'],
[7,'ff0000ff'],
]

# create a series of Icon Styles
for threshold,color in iconstyles:
doc.append(
KML.Style(
KML.IconStyle(
KML.color(color),
KML.scale(threshold/2),
KML.Icon(
KML.href("http://maps.google.com/mapfiles/kml/shapes/earthquake.png"),
),
KML.hotSpot(x="0.5",y="0",xunits="fraction",yunits="fraction"),
),
balloonstyle,
id="earthquake-style-{threshold}".format(threshold=threshold),
)
)

doc.append(KML.Folder())

# read in a csv file, and create a placemark for each record
url="http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M2.5.txt"
fileobject = urllib2.urlopen(url)
for row in csv.DictReader(fileobject):
timestamp = datetime.strptime(row["Datetime"], "%A, %B %d, %Y %H:%M:%S %Z")
pm = KML.Placemark(
KML.name("Magnitude={0}".format(row['Magnitude'])),
KML.TimeStamp(
KML.when(timestamp.strftime('%Y-%m-%dT%H:%M:%SZ')),
),
KML.styleUrl(
"#earthquake-style-{thresh}".format(
thresh=int(float(row['Magnitude']))
)
),
makeExtendedDataElements(row),
KML.Point(
KML.coordinates("{0},{1}".format(row["Lon"],row["Lat"]))
)
)
doc.Folder.append(pm)

# check if the schema is valid
from pykml.parser import Schema
schema_gx = Schema("kml22gx.xsd")
schema_gx.assertValid(doc)

print etree.tostring(doc, pretty_print=True)

Change log

be16a1cb0094 by taeri...@precisionm6500b on Nov 4, 2011   Diff
minor improvements to the
example_csv_to_kml script
Go to: 
Project members, sign in to write a code review

Older revisions

7bd3217df37f by taeri...@precisionm6500b on Oct 19, 2011   Diff
added examples (csv2kml and spline
camera)
All revisions of this file

File info

Size: 2665 bytes, 92 lines
Powered by Google Project Hosting