def printKML(doc, outfile): schema_ogc = Schema("ogckml22.xsd") schema_ogc.assertValid(doc) f = open(outfile, "w") f.write(etree.tostring(doc, pretty_print=True)) f.close()
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 = urllib.request.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))
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)
) doc.append(style3) pointFolder = KML.Folder() pointList = [] print "Creating KML" for y in range(len(points)): if(points[y][2] <=10): colorvalue = "red" elif(points[y][2] >10 and points[y][2] <=14): colorvalue = "yellow" else: colorvalue = "white" coord = points[y][1],points[y][0] pm = KML.Placemark( KML.name(points[y][2]), KML.styleUrl(colorvalue), KML.Point( KML.coordinates(str(coord).strip('() ')) ) ) pointFolder.append(pm) doc.append(pointFolder) schema_ogc = Schema("ogckml22.xsd") schema_ogc.assertValid(pointFolder) fout = open(str(args.outfile).strip('[]\''),'w') fout.write(etree.tostring(doc, pretty_print=True)) fout.close()
def main(): """ Create a KML document with a folder and a style for each earthquake magnitude """ doc = KML.Document() icon_styles = [ [2, 'ff000000'], [3, 'ffff0000'], [4, 'ff00ff55'], [5, 'ffff00aa'], [6, 'ff00ffff'], [7, 'ff0000ff'], ] # create a series of Icon Styles for threshold, color in icon_styles: 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'), ), get_balloon_style(), 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/feed/v1.0/summary/2.5_week.csv' fileobject = urlopen(url) if six.PY3: # fileobject is bytes, csv requires string import codecs fileobject = codecs.getreader('utf-8')(fileobject) for row in csv.DictReader(fileobject): timestamp = datetime.strptime(row['time'], '%Y-%m-%dT%H:%M:%S.%fZ') pm = KML.Placemark( KML.name('Magnitude={0}'.format(row['mag'])), KML.TimeStamp( KML.when(timestamp.strftime('%Y-%m-%dT%H:%M:%SZ')), ), KML.styleUrl( '#earthquake-style-{thresh}'.format( thresh=int(float(row['mag'])) ) ), make_extended_data_elements(row), KML.Point( KML.coordinates('{0},{1}'.format(row['longitude'], row['latitude'])) ) ) doc.Folder.append(pm) # check if the schema is valid schema_gx = Schema('kml22gx.xsd') schema_gx.assertValid(doc) kml = KML.kml(doc) print(etree.tostring(format_xml_with_cdata(kml), pretty_print=True, encoding='utf-8', xml_declaration=True).decode())