def load_kml_string(kml_string, schema_type = "kml22gx.xsd"): schema_gx = Schema(schema_type) doc = parser.fromstring(kml_string, schema_gx) if (schema_gx.validate(doc)): return doc else: print "Invalid kml format for string passed in" return None
def load_kml_file(filename, schema_type = "kml22gx.xsd"): with open(filename) as fh: schema_gx = Schema(schema_type) doc = parser.parse(fh) if (schema_gx.validate(doc)): return doc.getroot() else: print "Invalid kml format for file: "+str(filename) return None
def test_trivial_kml_document(self): """Tests the creation of a trivial OGC KML document.""" doc = KML.kml() schema = Schema("ogckml22.xsd") self.assertTrue(schema.validate(doc)) self.assertEquals( etree.tostring(doc), '<kml xmlns:gx="http://www.google.com/kml/ext/2.2" ' 'xmlns:atom="http://www.w3.org/2005/Atom" ' 'xmlns="http://www.opengis.net/kml/2.2"/>' )
def test_trivial_kml_document(self): """Tests the creation of a trivial OGC KML document.""" doc = KML.kml() schema = Schema("ogckml22.xsd") self.assertTrue(schema.validate(doc)) target = etree.fromstring( '<kml ' 'xmlns:atom="http://www.w3.org/2005/Atom" ' 'xmlns:gx="http://www.google.com/kml/ext/2.2" ' 'xmlns="http://www.opengis.net/kml/2.2"/>') self.assertTrue(compare_xml(target, doc))
def test_trivial_kml_document(self): """Tests the creation of a trivial OGC KML document.""" doc = KML.kml() schema = Schema('ogckml22.xsd') self.assertTrue(schema.validate(doc)) data = etree.tostring(doc, encoding='ascii') expected = \ b'<kml xmlns:gx="http://www.google.com/kml/ext/2.2" ' \ b'xmlns:atom="http://www.w3.org/2005/Atom" ' \ b'xmlns="http://www.opengis.net/kml/2.2"/>' self.assertXmlEquivalentOutputs(data, expected)
def schema_invalid(file): 'Check if it is a valid schema' with open(file, 'r') as f: #The files supplied are most likely part of the google extension schema, so validate it against that root = parser.fromstring(f.read()) #Put the read cursor back at the start of the file f.seek(0) schema_gx = Schema("kml22gx.xsd") #If the kml file does not use a valid schema if not schema_gx.validate(root): return True else: return False
), GX.AnimatedUpdate( GX.duration(5), KML.Update( KML.targetHref(), KML.Change( KML.IconStyle( KML.scale(10.0), targetId="mystyle" ) ) ) ), GX.Wait( GX.duration(5) ) ) ) ) ) print etree.tostring(doc, pretty_print=True) # output a KML file (named based on the Python script) outfile = file(__file__.rstrip('.py')+'.kml','w') outfile.write(etree.tostring(doc, pretty_print=True)) schema = Schema('kml22gx.xsd') import ipdb; ipdb.set_trace() schema.validate(doc)
KML.name("Play me!"), GX.Playlist( GX.FlyTo( GX.duration(3), GX.flyToMode("bounce"), KML.Camera( KML.longitude(170.157), KML.latitude(-43.671), KML.altitude(9700), KML.heading(-6.333), KML.tilt(33.5), )), GX.AnimatedUpdate( GX.duration(5), KML.Update( KML.targetHref(), KML.Change( KML.IconStyle(KML.scale(10.0), targetId="mystyle")))), GX.Wait(GX.duration(5)))))) print(etree.tostring(doc, pretty_print=True)) # output a KML file (named based on the Python script) outfile = file(__file__.rstrip('.py') + '.kml', 'w') outfile.write(etree.tostring(doc, pretty_print=True)) schema = Schema('kml22gx.xsd') import ipdb ipdb.set_trace() schema.validate(doc)
from os import path from pykml import parser from pykml.parser import Schema ### create schemas schema_ogc = Schema("ogckml22.xsd") schema_gx = Schema("kml22gx.xsd") ### load file kml_file = path.join('data/countyDisplay.kml') with open(kml_file) as f: doc = parser.parse(f) # validate it against the OGC KML schema print(schema_ogc.validate(doc)) # validate it against the Google Extension schema print(schema_gx.validate(doc))