def main(): feature = kmlengine.GetRootFeature(kmldom.ParseKml(ReadFile(inputkml))) if feature: VisitFeature(feature, 0) # Python deletes the feature and all of its descendant elements in turn. else: # The file is a KML fragment. print 'No root feature in %s' % inputkml
def parseKML(kml, debug=False): """Here kml is a filestream prehaps generated by .read(). This function Assumes that The KML contains a Document which contains only Points and LineStrings. If there is at least one LineString, the points contained in these LineStrings will be parsed and returned as one long string which is readable by the Google Maps and Elevation APIs. If Parsing fails, an attribute error should be thrown.""" # Use the convenience function to return the root feature of the parsed KML. root_feature = kmlengine.GetRootFeature(kmldom.ParseKml(kml)) # We know the root feature is a Document which contains only Placemarks. document = kmldom.AsDocument(root_feature) numPlacemarks = document.get_feature_array_size() coordinates = [[]] coordCounter = 0 for i in range(numPlacemarks): placemark = kmldom.AsPlacemark(document.get_feature_array_at(i)) geometry = placemark.get_geometry() # Assume that the document contains only Points and LineStrings try: coordsDom = kmldom.AsLineString(geometry).get_coordinates() numCoords = coordsDom.get_coordinates_array_size() except AttributeError: coordsDom = kmldom.AsPoint(geometry).get_coordinates() numCoords = coordsDom.get_coordinates_array_size() # Put all the coordinates in a list for j in range(numCoords): coordDom = coordsDom.get_coordinates_array_at(j) coordinates[-1].append( (coordDom.get_latitude(), coordDom.get_longitude())) coordCounter += 1 # URLs can only be 2048 characters which works out to ~100 # coordinates if coordCounter % 50 == 0: coordinates.append([]) if coordCounter >= 25000: raise Exception('Too many coordinates for Elevation API.') if debug: print "Number of coordinates: %d" % coordCounter paths = [] for coords in coordinates: # Get the coordinates in the format that the Google APIs want coordinatesString = [coordToString(coord) for coord in coords] coordinatesString = string.join(coordinatesString, '|') paths.append(coordinatesString) return paths
def LoadFile(self, filename): # Public, can be called indirectly from command line. if zipfile.is_zipfile(filename): kml = self.__ReadKmlFromKmz(filename) else: kml = open(filename).read() # consume the entire file dom = self.__GetRootFeature(kmldom.ParseKml(kml)) if dom is None: showwarning('Bad KML', 'The KML data could not be parsed') return self.__SetDom(dom)
def runTest(self): kml = '<Placemark>' \ '<Point><coordinates>1,2,3</coordinates></Point>' \ '</Placemark>' element = kmldom.ParseKml(kml) assert element feature = kmldom.AsFeature(element) assert feature status,lat,lon = kmlengine.GetFeatureLatLon(feature) assert True == status assert 1 == lon assert 2 == lat
def main(inputkml): feature = kmlengine.GetRootFeature(kmldom.ParseKml(ReadFile(inputkml))) (status, lat, lon) = kmlengine.GetFeatureLatLon(feature) if status: print 'center', lat, lon bbox = kmlengine.Bbox() status = kmlengine.GetFeatureBounds(feature, bbox) if status: print 'north', bbox.get_north() print 'south', bbox.get_south() print 'east', bbox.get_east() print 'west', bbox.get_west()
def runTest(self): kml = '<Placemark>' \ '<LineString><coordinates>1,2 -1,-2</coordinates></LineString>' \ '</Placemark>' element = kmldom.ParseKml(kml) assert element feature = kmldom.AsFeature(element) assert feature bbox = kmlengine.Bbox() status = kmlengine.GetFeatureBounds(feature, bbox) assert status assert 2 == bbox.get_north() assert -2 == bbox.get_south() assert 1 == bbox.get_east() assert -1 == bbox.get_west()
def main(): print '== This is %s' % sys.argv[0] kml = ('<kml>\n<Placemark>\n<name>foo</name>\n' '<Point>\n<coordinates>-122, 37</coordinates>\n</Point>\n' '</Placemark>\n</kml>') print 'kml is:\n%s' % kml print 'parsing kml' # Use the convenience function to return the root feature of the parsed KML. root_feature = kmlengine.GetRootFeature(kmldom.ParseKml(kml)) # We know the root feature is a Placemark. placemark = kmldom.AsPlacemark(root_feature) # Extract the coordinates and print them. point = kmldom.AsPoint(placemark.get_geometry()) coordinates = point.get_coordinates().get_coordinates_array_at(0) print 'lat: %0.3f, lng %0.3f' % (coordinates.get_latitude(), coordinates.get_longitude())
# TODO: argparse for arguments kmlfile = sys.argv[1] kml_data = "" if kmlfile.endswith('.kml'): with open(kmlfile) as f: kml_data = f.read() elif kmlfile.endswith('.kmz'): (success, kml_data) = kmlengine.KmzFile_OpenFromFile(kmlfile).ReadKml() # check return code if not success: print "Failed to read KML from kmz file" sys.exit(-1) kml = kmldom.ParseKml(kml_data) kml_root = kmlengine.GetRootFeature(kml) placemarks = [] nodes = [kml_root] while len(nodes) > 0: node = nodes.pop() if node.Type() == kmldom.Type_Document: doc = kmldom.AsDocument(node) for i in range(doc.get_feature_array_size()): nodes.append(doc.get_feature_array_at(i)) if node.Type() == kmldom.Type_Placemark: p = kmldom.AsPlacemark(node) placemarks.append(p)