How to use the fastkml.kml function in fastkml

To help you get started, we’ve selected a few fastkml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cleder / fastkml / fastkml / test_main.py View on Github external
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
github cleder / fastkml / fastkml / test_main.py View on Github external
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())
github cleder / fastkml / fastkml / test_main.py View on Github external
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()))
github DemocracyClub / UK-Polling-Stations / polling_stations / apps / data_collection / management / commands / import_kmz.py View on Github external
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(' ')
github gee-community / gee_tools / geetools / batch / utils.py View on Github external
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()
github DemocracyClub / UK-Polling-Stations / polling_stations / apps / data_collection / management / commands / import_E06000023.py View on Github external
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]
github DemocracyClub / UK-Polling-Stations / polling_stations / apps / data_collection / management / commands / import_camden.py View on Github external
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())
github xoolive / traffic / traffic / drawing / kml.py View on Github external
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
github projecthorus / radiosonde_auto_rx / auto_rx / utils / log_to_kml.py View on Github external
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()