def genSummaryStub(Ddeploy): # Start off with some background info root = K.kml( K.Document( ATOM.author(ATOM.name(KMLauthor)), ATOM.link(href=homepage), K.name(Ddeploy['name']), K.description(Ddeploy['purpose']), )) root.insert( 0, etree.Comment( '\nKML generated automatically by drifterSummaryKML.py\n')) doc = root.xpath('//default:Document', \ namespaces={'gx': 'http://www.google.com/kml/ext/2.2',\ 'atom': 'http://www.w3.org/2005/Atom',\ 'default': 'http://www.opengis.net/kml/2.2'}) # create and add the pieces of the stub doc[0].append( etree.Comment("\nStyle definition for drifter's positions\n")) doc[0].append(K.styleUrl('#drifter_info')) doc[0].append(genKMLstub.SurfaceStyle()) doc[0].append( etree.Comment("\nStyle definition for drifter's trackline\n")) doc[0].append(genKMLstub.TrackStyle(Ddeploy)) return root
def to_placemark(t): if t.route is None: return None geometries = [] for ls in t.route: g = [] for lon, lat in ls: g.append('{},{}'.format(lon, lat)) coordinates = ' '.join(g) geometry = KML.LineString( KML.coordinates(coordinates)) geometries.append(geometry) name = t.number if t.company is not None: name = '{} {}'.format(t.company, t.number) href = 'https://angkot.web.id/route/#/{}/'.format(t.id) return KML.Placemark( KML.name(name), ATOM.link(href=href), ATOM.updated(t.updated.isoformat()), KML.MultiGeometry(*geometries))
def genFinalStub(): # using information in the dictionary of deployment qualities, make KML # Start off with some background info root = K.kml( K.Document( ATOM.author(ATOM.name(KMLauthor)), ATOM.link(href=homepage), K.name(final_title), K.description( 'To track the ultimate fate of all released drifters'), )) root.insert( 0, etree.Comment( '\nKML generated automatically by drifterSummaryKML.py\n')) doc = root.xpath('//default:Document', \ namespaces={'gx': 'http://www.google.com/kml/ext/2.2',\ 'atom': 'http://www.w3.org/2005/Atom',\ 'default': 'http://www.opengis.net/kml/2.2'}) # create and add the pieces of the stub doc[0].append( etree.Comment("\nStyle definition for drifter's positions\n")) doc[0].append(K.styleUrl('#drifter_info')) doc[0].append(genKMLstub.SurfaceStyle()) return root
def test_write_python_script_for_kml_document(self): """Tests the creation of a trivial OGC KML document.""" from pykml.factory import write_python_script_for_kml_document doc = KML.kml( KML.Document( ATOM.author(ATOM.name("J. K. Rowling")), ATOM.link(href="http://www.harrypotter.com"), KML.Placemark(KML.name("Hogwarts"), KML.Point(KML.coordinates("1,1"))))) script = write_python_script_for_kml_document(doc) self.assertEqual( script, 'from lxml import etree\n' 'from pykml.factory import KML_ElementMaker as KML\n' 'from pykml.factory import ATOM_ElementMaker as ATOM\n' 'from pykml.factory import GX_ElementMaker as GX\n' '\n' 'doc = KML.kml(\n' ' KML.Document(\n' ' ATOM.author(\n' ' ATOM.name(\'J. K. Rowling\'),\n' ' ),\n' ' ATOM.link( href="http://www.harrypotter.com",\n' '),\n' ' KML.Placemark(\n' ' KML.name(\'Hogwarts\'),\n' ' KML.Point(\n' ' KML.coordinates(\'1,1\'),\n' ' ),\n' ' ),\n' ' ),\n' ')\n' 'print(etree.tostring(etree.ElementTree(doc),pretty_print=True).decode())\n' )
def test_kml_document_with_atom_element(self): """Tests the creation of a KML document with an ATOM element.""" doc = KML.kml( KML.Document( ATOM.author(ATOM.name("J. K. Rowling")), ATOM.link(href="http://www.harrypotter.com"), KML.Placemark(KML.name("Hogwarts"), KML.Point(KML.coordinates("1,1"))))) self.assertTrue(Schema("kml22gx.xsd").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">' '<Document>' '<atom:author>' '<atom:name>J. K. Rowling</atom:name>' '</atom:author>' '<atom:link href="http://www.harrypotter.com"/>' '<Placemark>' '<name>Hogwarts</name>' '<Point>' '<coordinates>1,1</coordinates>' '</Point>' '</Placemark>' '</Document>' '</kml>') self.assertTrue(compare_xml(target, doc))
def test_write_python_script_for_kml_document(self): """Tests the creation of a trivial OGC KML document.""" doc = KML.kml( KML.Document( ATOM.author( ATOM.name('J. K. Rowling') ), ATOM.link(href='http://www.harrypotter.com'), KML.Placemark( KML.name('Hogwarts'), KML.Point( KML.coordinates('1,1') ) ) ) ) script = write_python_script_for_kml_document(doc) expected = \ 'from lxml import etree\n' \ 'from pykml.factory import KML_ElementMaker as KML\n' \ 'from pykml.factory import ATOM_ElementMaker as ATOM\n' \ 'from pykml.factory import GX_ElementMaker as GX\n' \ '\n' \ 'doc = KML.kml(\n' \ ' KML.Document(\n' \ ' ATOM.author(\n' \ ' ATOM.name(\'J. K. Rowling\'),\n' \ ' ),\n' \ ' ATOM.link(href=\'http://www.harrypotter.com\',\n' \ '),\n' \ ' KML.Placemark(\n' \ ' KML.name(\'Hogwarts\'),\n' \ ' KML.Point(\n' \ ' KML.coordinates(\'1,1\'),\n' \ ' ),\n' \ ' ),\n' \ ' ),\n' \ ')\n' \ 'print(etree.tostring(etree.ElementTree(doc), \n' \ ' encoding=\'utf-8\', \n' \ ' xml_declaration=True, \n' \ ' pretty_print=True).decode(\'utf-8\'))\n' self.assertEqual(script, expected)
def test_write_python_script_for_kml_document(self): """Tests the creation of a trivial OGC KML document.""" from pykml.factory import write_python_script_for_kml_document doc = KML.kml( KML.Document( ATOM.author( ATOM.name("J. K. Rowling") ), ATOM.link(href="http://www.harrypotter.com"), KML.Placemark( KML.name("Hogwarts"), KML.Point( KML.coordinates("1,1") ) ) ) ) script = write_python_script_for_kml_document(doc) self.assertEquals( script, 'from lxml import etree\n' 'from pykml.factory import KML_ElementMaker as KML\n' 'from pykml.factory import ATOM_ElementMaker as ATOM\n' 'from pykml.factory import GX_ElementMaker as GX\n' '\n' 'doc = KML.kml(\n' ' KML.Document(\n' ' ATOM.author(\n' ' ATOM.name(\'J. K. Rowling\'),\n' ' ),\n' ' ATOM.link( href="http://www.harrypotter.com",\n' '),\n' ' KML.Placemark(\n' ' KML.name(\'Hogwarts\'),\n' ' KML.Point(\n' ' KML.coordinates(\'1,1\'),\n' ' ),\n' ' ),\n' ' ),\n' ')\n' 'print etree.tostring(etree.ElementTree(doc),pretty_print=True)\n' )
def to_placemark(t): if t.route is None: return None geometries = [] for ls in t.route: g = [] for lon, lat in ls: g.append('{},{}'.format(lon, lat)) coordinates = ' '.join(g) geometry = KML.LineString(KML.coordinates(coordinates)) geometries.append(geometry) name = t.number if t.company is not None: name = '{} {}'.format(t.company, t.number) href = 'https://angkot.web.id/route/#/{}/'.format(t.id) return KML.Placemark(KML.name(name), ATOM.link(href=href), ATOM.updated(t.updated.isoformat()), KML.MultiGeometry(*geometries))
def genStub(Ddeploy): # using information in the dictionary of deployment qualities, make KML timestamp = time.strptime(Ddeploy['startstr'], '%Y-%m-%d %H:%M:%S') date = time.strftime('%b. %Y', timestamp) description = 'The trackline for the %s deployment of %s' % ( Ddeploy['area'], Ddeploy['name']) #print description # Start off with some background info root = K.kml( K.Document( ATOM.author(ATOM.name(KMLauthor)), ATOM.link(href=homepage), K.name(Ddeploy['name']), K.description(description), )) root.insert( 0, etree.Comment('\nKML generated automatically by genKMLstub.py\n')) doc = root.xpath('//default:Document', \ namespaces={'gx': 'http://www.google.com/kml/ext/2.2',\ 'atom': 'http://www.w3.org/2005/Atom',\ 'default': 'http://www.opengis.net/kml/2.2'}) # create and add the pieces of the stub doc[0].append( etree.Comment("\nStyle definition for drifter's positions\n")) doc[0].append(K.styleUrl('#drifter_info')) doc[0].append(SurfaceStyle()) doc[0].append( etree.Comment("\nStyle definition for drifter's trackline\n")) doc[0].append(TrackStyle(Ddeploy)) doc[0].append( etree.Comment( "\nEach drifter position has data associated with it. To display that\n\ data, need separate placemarks for each. Contain in a Folder for\n\ cleanliness.\n")) doc[0].append(Folder(Ddeploy)) return root
def test_kml_document_with_atom_element(self): """Tests the creation of a KML document with an ATOM element.""" doc = KML.kml( KML.Document( ATOM.author( ATOM.name('J. K. Rowling') ), ATOM.link(href='http://www.harrypotter.com'), KML.Placemark( KML.name('Hogwarts'), KML.Point( KML.coordinates('1,1') ) ) ) ) self.assertTrue(Schema('kml22gx.xsd').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">' \ b'<Document>' \ b'<atom:author>' \ b'<atom:name>J. K. Rowling</atom:name>' \ b'</atom:author>' \ b'<atom:link href="http://www.harrypotter.com"/>' \ b'<Placemark>' \ b'<name>Hogwarts</name>' \ b'<Point>' \ b'<coordinates>1,1</coordinates>' \ b'</Point>' \ b'</Placemark>' \ b'</Document>' \ b'</kml>' self.assertXmlEquivalentOutputs(data, expected)
def test_kml_document_with_atom_element(self): """Tests the creation of a KML document with an ATOM element.""" doc = KML.kml( KML.Document( ATOM.author( ATOM.name("J. K. Rowling") ), ATOM.link(href="http://www.harrypotter.com"), KML.Placemark( KML.name("Hogwarts"), KML.Point( KML.coordinates("1,1") ) ) ) ) self.assertTrue(Schema("kml22gx.xsd").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">' '<Document>' '<atom:author>' '<atom:name>J. K. Rowling</atom:name>' '</atom:author>' '<atom:link href="http://www.harrypotter.com"/>' '<Placemark>' '<name>Hogwarts</name>' '<Point>' '<coordinates>1,1</coordinates>' '</Point>' '</Placemark>' '</Document>' '</kml>' )
def _export(self, ids): from angkot.route.models import Transportation from lxml import etree from pykml.factory import KML_ElementMaker as KML from pykml.factory import ATOM_ElementMaker as ATOM def to_placemark(t): if t.route is None: return None geometries = [] for ls in t.route: g = [] for lon, lat in ls: g.append('{},{}'.format(lon, lat)) coordinates = ' '.join(g) geometry = KML.LineString( KML.coordinates(coordinates)) geometries.append(geometry) name = t.number if t.company is not None: name = '{} {}'.format(t.company, t.number) href = 'https://angkot.web.id/route/#/{}/'.format(t.id) return KML.Placemark( KML.name(name), ATOM.link(href=href), ATOM.updated(t.updated.isoformat()), KML.MultiGeometry(*geometries)) def get_city(t): return '{}, {}'.format(t.city, t.get_province_display()) tt = Transportation.objects.filter(pk__in=ids, active=True) \ .order_by('province', 'city', 'company', 'number') folders = [] folder = None current_city = None updated = None for t in tt: p = to_placemark(t) if p is None: continue city = get_city(t) if city != current_city: current_city = city print('City:', city, file=sys.stderr) folder = KML.Folder( KML.name(city)) folders.append(folder) folder.append(p) if updated is None or updated < t.updated: updated = t.updated updated = updated.isoformat() description = ''' Public transportation path data by AngkotWebId project. Copyright © AngkotWebId contributors - https://angkot.web.id The data is licensed under Open Database License 1.0. '''.strip() kml = KML.kml(KML.Document( KML.Name('AngkotWebId'), ATOM.author('AngkotWebId Contributors'), ATOM.link(href='https://angkot.web.id'), ATOM.link(rel='license', href='http://opendatacommons.org/licenses/odbl/1-0/'), ATOM.updated(updated), KML.description(description), *folders)) print('<?xml version="1.0" encoding="UTF-8"?>') print(etree.tostring(kml).decode())
def _export(self, ids): from angkot.route.models import Transportation from lxml import etree from pykml.factory import KML_ElementMaker as KML from pykml.factory import ATOM_ElementMaker as ATOM def to_placemark(t): if t.route is None: return None geometries = [] for ls in t.route: g = [] for lon, lat in ls: g.append('{},{}'.format(lon, lat)) coordinates = ' '.join(g) geometry = KML.LineString(KML.coordinates(coordinates)) geometries.append(geometry) name = t.number if t.company is not None: name = '{} {}'.format(t.company, t.number) href = 'https://angkot.web.id/route/#/{}/'.format(t.id) return KML.Placemark(KML.name(name), ATOM.link(href=href), ATOM.updated(t.updated.isoformat()), KML.MultiGeometry(*geometries)) def get_city(t): return '{}, {}'.format(t.city, t.get_province_display()) tt = Transportation.objects.filter(pk__in=ids, active=True) \ .order_by('province', 'city', 'company', 'number') folders = [] folder = None current_city = None updated = None for t in tt: p = to_placemark(t) if p is None: continue city = get_city(t) if city != current_city: current_city = city print('City:', city, file=sys.stderr) folder = KML.Folder(KML.name(city)) folders.append(folder) folder.append(p) if updated is None or updated < t.updated: updated = t.updated updated = updated.isoformat() description = ''' Public transportation path data by AngkotWebId project. Copyright © AngkotWebId contributors - https://angkot.web.id The data is licensed under Open Database License 1.0. '''.strip() kml = KML.kml( KML.Document( KML.Name('AngkotWebId'), ATOM.author('AngkotWebId Contributors'), ATOM.link(href='https://angkot.web.id'), ATOM.link( rel='license', href='http://opendatacommons.org/licenses/odbl/1-0/'), ATOM.updated(updated), KML.description(description), *folders)) print('<?xml version="1.0" encoding="UTF-8"?>') print(etree.tostring(kml).decode())