1#!/usr/bin/env python 2 3# Copyright 2008, Google Inc. All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are met: 7# 8# 1. Redistributions of source code must retain the above copyright notice, 9# this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright notice, 11# this list of conditions and the following disclaimer in the documentation 12# and/or other materials provided with the distribution. 13# 3. Neither the name of Google Inc. nor the names of its contributors may be 14# used to endorse or promote products derived from this software without 15# specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 18# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28# This program demonstrates use of the KML DOM Python SWIG bindings for 29# generating geometry. 30 31import sys 32import kmldom 33 34def CreatePointPlacemark(name, lat, lng): 35 factory = kmldom.KmlFactory_GetFactory() 36 placemark = factory.CreatePlacemark() 37 placemark.set_name(name) 38 coordinates = factory.CreateCoordinates() 39 coordinates.add_latlng(lat, lng) 40 point = factory.CreatePoint() 41 point.set_coordinates(coordinates) 42 placemark.set_geometry(point) 43 return placemark 44 45def CreateSimple2dLineStringPlacemark(name, latlng): 46 factory = kmldom.KmlFactory_GetFactory() 47 placemark = factory.CreatePlacemark() 48 placemark.set_name(name) 49 coordinates = factory.CreateCoordinates() 50 for (lat, lng) in latlng: 51 coordinates.add_latlng(lat, lng) 52 linestring = factory.CreateLineString() 53 linestring.set_tessellate(True) 54 linestring.set_coordinates(coordinates) 55 placemark.set_geometry(linestring) 56 return placemark 57 58def CreateSimple2dPolygonPlacemark(name): 59 factory = kmldom.KmlFactory_GetFactory() 60 61 # <outerBoundaryIs><LinearRing>... 62 linearing = factory.CreateLinearRing() 63 outerboundaryis = factory.CreateOuterBoundaryIs() 64 outerboundaryis.set_linearring(linearing) 65 66 # <Polygon><outerBoundaryIs>... <innerBoundaryIs>... 67 polygon = factory.CreatePolygon() 68 polygon.set_tessellate(True) 69 polygon.set_outerboundaryis(outerboundaryis) 70 71 # <Placemark><Polygon>... 72 placemark = factory.CreatePlacemark() 73 placemark.set_name(name) 74 placemark.set_geometry(polygon) 75 76 return placemark 77 78def Create2HolePolygonPlacemark(name): 79 factory = kmldom.KmlFactory_GetFactory() 80 81 # <Polygon> 82 polygon = factory.CreatePolygon() 83 84 # <outerBoundaryIs><LinearRing><coordinates> 85 outerboundaryis = factory.CreateOuterBoundaryIs() 86 linearring = factory.CreateLinearRing() 87 coordinates = factory.CreateCoordinates() 88 coordinates.add_latlngalt(37.823360,-122.374208,0) 89 coordinates.add_latlngalt(37.823360,-122.369120,0) 90 coordinates.add_latlngalt(37.827271,-122.369073,0) 91 coordinates.add_latlngalt(37.827259,-122.374655,0) 92 coordinates.add_latlngalt(37.823360,-122.374208,0) 93 linearring.set_coordinates(coordinates) 94 outerboundaryis.set_linearring(linearring) 95 polygon.set_outerboundaryis(outerboundaryis) 96 97 # <innerBoundaryIs><LinearRing><coordinates> 98 innerboundaryis = factory.CreateInnerBoundaryIs() 99 linearring = factory.CreateLinearRing() 100 coordinates = factory.CreateCoordinates() 101 coordinates.add_latlngalt(37.825728,-122.373670,0) 102 coordinates.add_latlngalt(37.825715,-122.372255,0) 103 coordinates.add_latlngalt(37.826578,-122.372886,0) 104 coordinates.add_latlngalt(37.825728,-122.373670,0) 105 linearring.set_coordinates(coordinates) 106 innerboundaryis.set_linearring(linearring) 107 polygon.add_innerboundaryis(innerboundaryis) 108 109 # <innerBoundaryIs><LinearRing><coordinates> 110 innerboundaryis = factory.CreateInnerBoundaryIs() 111 linearring = factory.CreateLinearRing() 112 coordinates = factory.CreateCoordinates() 113 coordinates.add_latlngalt(37.824914,-122.371487,0) 114 coordinates.add_latlngalt(37.824089,-122.372024,0) 115 coordinates.add_latlngalt(37.824065,-122.370626,0) 116 coordinates.add_latlngalt(37.824914,-122.371487,0) 117 linearring.set_coordinates(coordinates) 118 innerboundaryis.set_linearring(linearring) 119 polygon.add_innerboundaryis(innerboundaryis) 120 121 # <Placemark><name>... <Polygon>... 122 placemark = factory.CreatePlacemark() 123 placemark.set_name(name) 124 placemark.set_geometry(polygon) 125 126 return placemark 127 128def main(): 129 print '== This is %s' % sys.argv[0] 130 131 factory = kmldom.KmlFactory_GetFactory() 132 133 # <Document> 134 document = factory.CreateDocument() 135 136 # Each Create*Placemark() creates and returns a Placemark. 137 document.add_feature(CreatePointPlacemark('pt0',1,2)) 138 document.add_feature(CreatePointPlacemark('pt1',3,4)) 139 lnglat = [(1,2),(3,4),(5,6),(7,8)] 140 document.add_feature(CreateSimple2dLineStringPlacemark('line',lnglat)) 141 document.add_feature(CreateSimple2dPolygonPlacemark('box')) 142 document.add_feature(Create2HolePolygonPlacemark('2 holes')) 143 144 # <Placemark><MultiGeometry><Point>... <LineString>... 145 placemark = factory.CreatePlacemark() 146 multigeometry = factory.CreateMultiGeometry() 147 multigeometry.add_geometry(factory.CreatePoint()) 148 multigeometry.add_geometry(factory.CreateLineString()) 149 placemark.set_geometry(multigeometry) 150 document.add_feature(placemark) 151 152 # <kml> 153 kml = factory.CreateKml() 154 kml.set_feature(document) 155 156 print 'Serialize to xml...' 157 print kmldom.SerializePretty(kml) 158 159if __name__ == '__main__': 160 main() 161