Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_untyped_extended_data_nested(self):
ns = '{http://www.opengis.net/kml/2.2}'
k = kml.KML(ns=ns)
d = kml.Document(ns, 'docid', 'doc name', 'doc description')
d.extended_data = kml.UntypedExtendedData(elements=[
kml.UntypedExtendedDataElement(name='type', value='Document')
])
f = kml.Folder(ns, 'fid', 'f name', 'f description')
f.extended_data = kml.UntypedExtendedData(elements=[
kml.UntypedExtendedDataElement(name='type', value='Folder')
])
k.append(d)
d.append(f)
k2 = kml.KML()
k2.from_string(k.to_string())
document_data = list(k2.features())[0].extended_data
folder_data = list(list(k2.features())[0].features())[0].extended_data
def test_placemark(self):
ns = '{http://www.opengis.net/kml/2.2}'
k = kml.KML(ns=ns)
p = kml.Placemark(ns, 'id', 'name', 'description')
p.geometry = Point(0.0, 0.0, 0.0)
p2 = kml.Placemark(ns, 'id2', 'name2', 'description2')
p2.geometry = LineString([(0, 0, 0), (1, 1, 1)])
k.append(p)
k.append(p2)
self.assertEqual(len(list(k.features())), 2)
k2 = kml.KML()
k2.from_string(k.to_string(prettyprint=True))
self.assertEqual(k.to_string(), k2.to_string())
def test_read_timespan(self):
ts = kml.TimeSpan(ns='')
doc = """
1876-08-01
1997-07-16T07:30:15Z
"""
ts.from_string(doc)
self.assertEqual(ts.begin[1], 'date')
self.assertEqual(ts.begin[0], datetime.datetime(1876, 8, 1, 0, 0))
self.assertEqual(ts.end[1], 'dateTime')
self.assertEqual(ts.end[0], datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc()))
def handle(self, **options):
filename = "/Users/symroe/Downloads/Polling_Boundaries (1).kmz"
kmz = ZipFile(filename, 'r')
kml_file = kmz.open('doc.kml', 'r')
k = kml.KML()
k.from_string(kml_file.read())
main = next(k.features())
districts = next(main.features())
for district in districts.features():
# station.geometry.wkt
# station.name
# station.description
# print district.geometry.wkt
polygons = district.geometry.wkt[18:-3].split(')), ((')
WKT = ""
for polygon in polygons:
points = polygon.split(',')
cleaned_points = ""
for point in points:
split_points = point.strip().split(' ')
def kmlToGeoJsonDict(kmlfile=None, data=None, encoding=None):
""" Convert a KML file to a GeoJSON dict """
import xml.dom.minidom as md
from fastkml import kml
import kml2geojson
k = kml.KML()
with open(kmlfile) as thefile:
kmlf = thefile.read()
# Handle encoding
if not encoding:
try:
import re
match = re.search('encoding=".+"', kmlf).group()
encoding = match.split('=')[1][1:-1]
except:
encoding = 'utf-8'
kmlf = kmlf.encode(encoding)
k.from_string(kmlf)
kmlStr = k.to_string()
def import_areas(self):
filename = os.path.abspath(
'data/bristol_areas.kmz'
)
kmz = ZipFile(filename, 'r')
kml_file = kmz.open('doc.kml', 'r')
k = kml.KML()
k.from_string(kml_file.read())
main = next(k.features())
districts = next(main.features())
council_id = "E06000023"
for district in districts.features():
polygons = district.geometry.wkt[18:-3].split(')), ((')
WKT = ""
for polygon in polygons:
points = polygon.split(',')
cleaned_points = ""
for point in points:
split_points = point.strip().split(' ')
x = split_points[0]
y = split_points[1]
def parse_kml_features(self, data):
k = kml.KML()
k.from_string(data.encode("utf-8"))
document = next(k.features())
folder = next(document.features())
return list(folder.features())
def export(filename: str):
global current_document
kml_tree = kml.KML()
current_document = kml.Document()
yield current_document
kml_tree.append(current_document)
with open(filename, "w", encoding="utf8") as kml_file:
kml_file.write(kml_tree.to_string(prettyprint=True))
_stylemap.clear()
current_document = None
def write_kml(geom_objects,
filename="output.kml",
comment=""):
""" Write out flight path geometry objects to a kml file. """
kml_root = fastkml.kml.KML()
kml_doc = fastkml.kml.Document(
ns=ns,
name=comment)
if type(geom_objects) is not list:
geom_objects = [geom_objects]
for _flight in geom_objects:
kml_doc.append(_flight)
with open(filename,'w') as kml_file:
kml_file.write(kml_doc.to_string())
kml_file.close()