I have been battling for days to read coordinates LineString
using fastkml library
in python.
I have imported geometry module in my main program. I am reading like this.
My kml file is stored in string called doc
X = Geometry
Z=x._get,_coordinates(doc)
I got an error that says
the module object has no attributes find
from fastkml import kml import re from fastkml.geometry import Geometry from lxml import etree from xml.etree import ElementTree from io import StringIO, BytesIO
def print_child_features(element):
if not getattr(element, 'features', None):
return
for feature in element.features():
y = feature.to_string()
return y
def print_child2_features(element): """ Prints the name of every child node of the given element, recursively """ if not getattr(element, 'features', None): return for feature in element.features(): print feature.name print_child2_features(feature)
def get_distance(coordinates_str): """gets distance of one path from coordinates string in form of: 14.81363432237944,53.57016581501523,0 14.81411766813742,53.56923005549378,0 14.81880340335202,53.56879451890311 ... look at: http://code.google.com/intl/pl-PL/apis/kml/documentation/kmlreference.html#coordinates """ sum_distance = 0.0 arr = [] coordinates = [] if ' ' in coordinates_str: arr = coordinates_str.split(' ') if len(arr) > 1: for s in arr: if ',' in s: pt = s.split(',') pos_latt = (float(pt[0].strip()), 0, 0) pos_long = (float(pt[1].strip()), 0, 0) position = (pos_latt, pos_long) coordinates.append(position) if coordinates: for i in range(len(coordinates) - 1): start = coordinates[i] stop = coordinates[i + 1] sum_distance += distance.points2distance(start, stop) return sum_distance
if name == ' main ':
#fname = input("Enter KML file name ")
fname = "DBN.kml"
k = kml.KML()
with open(fname) as kmlFile:
k.from_string(kmlFile.read())
x = Geometry()
doc = print_child_features(k)
z= x._get_coordinates(doc)
print z
length=0
if length <= 10:
print("Transciver is LR")
elif length > 10 and length <= 40:
print("Transciver is ER")
else:
print("Transciver is ER")
I did not get the code you have written. But I have written code to read coordinates from Linestrings and other Geometries using fastkml library, so I can help.
# import necessary modules
from fastkml import kml, geometry
k = kml.KML() # create fastkml object
k.from_string(doc.encode('utf-8')) # read doc string
document = list(k.features())
self.parse_placemarks(document)
# parse features throughout the KML File
def parse_placemarks(self, document):
for feature in document:
if isinstance(feature, kml.Placemark): # when there is no folder
placemark = feature
self.parse_geometries(placemark)
for feature in document:
if isinstance(feature, kml.Folder):
self.parse_placemarks(list(feature.features()))
if isinstance(feature, kml.Document):
self.parse_placemarks(list(feature.features()))
# parse geometry
def parse_geometries(self, placemark):
if hasattr(placemark, "geometry"):
if isinstance(placemark.geometry, geometry.LineString):
self.linestring(placemark)
# parse linestring
def linestring(self, line):
x, y = self.compute_coordinates(line.geometry)
# find coordinates
def compute_coordinates(self, geometry):
lons = []
lats = []
for coordinates in geometry.coords:
lons.append(coordinates[0])
lats.append(coordinates[1])
return (lons, lats)
You can find how to extract coordinates of other geometries and more about fastkml here : https://medium.com/@wwaryan/the-definite-only-guide-to-fastkml-58b8e19b8454
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.