Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_boundaries():
""" Parse the KML file to get the boundaries
"""
with open(kml_file) as f:
tree = parser.parse(f)
root = tree.getroot()
N = 0
placemarks = {}
for ch in root.Document.Folder.getchildren():
if 'Placemark' in ch.tag:
# Found a placemark
N += 1
pname = int(ch.name.text)
# Get the coordinates
pcoords = ch.MultiGeometry.Polygon.outerBoundaryIs.LinearRing.coordinates.text
pcoords = pcoords.strip()
pcoords = re.split(',| ', pcoords)
pcoords = [float(c) for c in pcoords]
from lxml import etree
# define variables for the namespace URL strings
kmlns = '{}' #'{' + nsmap['kml'] + '}'
gxns = '{' + nsmap['gx'] + '}'
# start with a base KML tour and playlist
tour_doc = KML.kml(
GX.Tour(
KML.name("Play me!"),
GX.Playlist(),
)
)
with open("colorado_river_linestring.kml") as f:
linestring_doc = parse(f)
# get the coordinate string of the first KML coordinate element
coord_str = str(
linestring_doc.getroot().find(".//{http://www.opengis.net/kml/2.2}coordinates")
).strip()
for vertex in coord_str.split(' '):
(lon,lat,alt) = vertex.split(',')
flyto = GX.FlyTo(
GX.duration(2),
GX.flyToMode("smooth"),
KML.Camera(
KML.longitude(lon),
KML.latitude(lat),
KML.altitude(0),
KML.heading(-129.7),
- if data_fields requested:
a struct with attributes:
* 'geometry': a |shapely| Polygon/MultiPolygon or Point/MultiPoint
* the requested data_fields as attributes. The value are string, or None
if the data fields is unset in the KML. If several identical data_fields are
found, they are put in a list.
"""
if kml_path.endswith('kmz'):
with zipfile.ZipFile(kml_path) as kmz:
kml_name = [info.filename for info in kmz.infolist()
if os.path.splitext(info.filename)[1] == '.kml'][0]
with kmz.open(kml_name) as kml_file:
root = parser.parse(kml_file).getroot()
else:
with open(kml_path, 'r') as kml_file:
root = parser.parse(kml_file).getroot()
tag = root.tag[:root.tag.rfind('}')+1]
zones = {}
for element in root.findall('.//' + tag + root_id_zone):
# Ignore nested root_id within root_id
if element.find('.//' + tag + root_id_zone) is not None:
continue
if ignore_if_parent is not None and element.getparent().tag.endswith(ignore_if_parent):
continue
name = element.name.text
# Read the zone geometry
geometry = None
polygons = [_GetPolygon(poly)
for poly in element.findall('.//' + tag + 'Polygon')]
if polygons:
if len(polygons) == 1:
def ReadKML(filename):
with open(filename, 'r') as kml_file:
doc = parser.parse(kml_file).getroot()
return doc
def _ReadKmlBorder(kml_path, root_id='Placemark'):
"""Gets the border defined in a KML.
Args:
kml_path: The path name to the border file KML or KMZ.
root_id_zone: The root id defininig a zone. Usually it is 'Placemark'.
Returns:
A dictionary of |shapely| LineString keyed by their names.
"""
if kml_path.endswith('kmz'):
with zipfile.ZipFile(kml_path) as kmz:
kml_name = [info.filename for info in kmz.infolist()
if os.path.splitext(info.filename)[1] == '.kml'][0]
with kmz.open(kml_name) as kml_file:
root = parser.parse(kml_file).getroot()
else:
with open(kml_path, 'r') as kml_file:
root = parser.parse(kml_file).getroot()
tag = root.tag[:root.tag.rfind('}') + 1]
linetrings_dict = {}
for element in root.findall('.//' + tag + root_id):
# Ignore nested root_id within root_id
if element.find('.//' + tag + root_id) is not None:
continue
name = element.name.text
linestrings = [
_GetLineString(l)
for l in element.findall('.//' + tag + 'LineString')
]
if not linestrings:
def ReadKML(filename):
with open(filename, 'r') as kml_file:
doc = parser.parse(kml_file).getroot()
return doc