예제 #1
0
 def runTest(self):
   id = 'pm123'
   kml = '<Placemark id="%s"><name>hello</name></Placemark>' % id
   kmlfile,errors = kmlengine.KmlFile.CreateFromParse(kml)
   assert kmlfile
   object = kmlfile.GetObjectById(id)
   assert object
   placemark = kmldom.AsPlacemark(object)
   assert placemark
   assert placemark.has_id()
   assert id == placemark.get_id()
예제 #2
0
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
예제 #3
0
 def runTest(self):
   factory = kmldom.KmlFactory_GetFactory()
   placemark = factory.CreatePlacemark()
   name = 'hi'
   placemark.set_name(name)
   clone_element = kmlengine.Clone(placemark)
   assert clone_element
   clone_placemark = kmldom.AsPlacemark(clone_element)
   assert clone_placemark
   assert name == clone_placemark.get_name()
   # Change the name in the original
   new_name = 'new name'
   placemark.set_name(new_name)
   # Verify that the clone still has the old name
   assert name == clone_placemark.get_name()
예제 #4
0
 def runTest(self):
   factory = kmldom.KmlFactory_GetFactory()
   # TODO: This crashes CreateFromImport as do all non-Object complex elements
   # kml = factory.CreateKml()
   # This returns an ElementPtr for the given element and works fine in
   # CreateFromImport:
   kml_as_element = factory.CreateElementById(kmldom.Type_kml)
   kml = kmldom.AsKml(kml_as_element)
   kml.set_feature(factory.CreatePlacemark())
   kmlfile = kmlengine.KmlFile.CreateFromImport(kml_as_element)
   assert kmlfile
   root = kmlfile.get_root()
   assert root
   kml = kmldom.AsKml(root)
   assert kml
   assert kml.has_feature()
   placemark = kmldom.AsPlacemark(kml.get_feature())
   assert placemark
예제 #5
0
 def runTest(self):
   factory = kmldom.KmlFactory_GetFactory()
   placemark = factory.CreatePlacemark()
   id = 'placemark123'
   name = 'some name'
   placemark.set_id(id)
   placemark.set_name(name)
   folder = factory.CreateFolder()
   folder.add_feature(placemark)
   kmlfile = kmlengine.KmlFile.CreateFromImport(folder)
   assert kmlfile
   object = kmlfile.GetObjectById(id)
   assert object
   placemark = kmldom.AsPlacemark(object)
   assert placemark
   assert placemark.has_id()
   assert id == placemark.get_id()
   assert placemark.has_name()
   assert name == placemark.get_name()
예제 #6
0
 def runTest(self):
   kmz_filepath = os.path.join(os.path.dirname(__file__), '../../testdata/kmz/model-macky.kmz')
   kmzfile = kmlengine.KmzFile.OpenFromFile(kmz_filepath)
   assert kmzfile
   (ok, kml) = kmzfile.ReadKml()
   assert ok
   (kmlfile,errors) = kmlengine.KmlFile.CreateFromParse(kml)
   assert kmlfile
   root = kmldom.AsKml(kmlfile.get_root())
   assert root
   placemark = kmldom.AsPlacemark(root.get_feature())
   assert placemark
   assert 'SketchUp Model of Macky Auditorium' == placemark.get_name()
   assert placemark.has_geometry()
   model = kmldom.AsModel(placemark.get_geometry())
   assert model
   assert 'model_4' == model.get_id()
   (ok, dae) = kmzfile.ReadFile('geometry/CU-Macky.dae')
   assert ok
   assert 268477 == len(dae)
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())
예제 #8
0
 def runTest(self):
   factory = kmldom.KmlFactory_GetFactory()
   placemark = factory.CreatePlacemark()
   feature = kmlengine.GetRootFeature(placemark)
   assert feature
   assert kmldom.AsPlacemark(feature)
예제 #9
0
        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)

if len(placemarks) < 1:
    print "Error: no placemarks found in KML file"
    sys.exit(-1)
if len(placemarks) == 1:
    print "Found 1 placemark. Using it"
    placemark = placemarks[0]
else:
    print "Select placemark:"
    for i, p in enumerate(placemarks):
        print " %3d: %s" % (i + 1, p.get_name())
    pos = 0
    while pos < 1 or pos > len(placemarks):
        pos = raw_input("# ")