| 1 |
Quickstart |
| 2 |
========== |
| 3 |
This quickstart guide will show you how to create a simple KML file from scratch and how |
| 4 |
to read a KML file from a string. The example demonstrates organizing geographic data using |
| 5 |
nested folders and placemarks - common structures used to group and display locations in |
| 6 |
Google Earth and other KML viewers. |
| 7 |
|
| 8 |
Build a KML from Scratch |
| 9 |
------------------------ |
| 10 |
|
| 11 |
Example how to build a simple KML file from the Python interpreter. |
| 12 |
|
| 13 |
First we import the necessary modules: |
| 14 |
|
| 15 |
.. code-block:: pycon |
| 16 |
|
| 17 |
>>> from fastkml import kml |
| 18 |
>>> from pygeoif.geometry import Polygon |
| 19 |
>>> from pathlib import Path |
| 20 |
|
| 21 |
Create a KML object: |
| 22 |
|
| 23 |
.. code-block:: pycon |
| 24 |
|
| 25 |
>>> k = kml.KML() |
| 26 |
|
| 27 |
Create a KML Document and add it to the KML root object: |
| 28 |
|
| 29 |
.. code-block:: pycon |
| 30 |
|
| 31 |
>>> d = kml.Document(id="docid", name="doc name", description="doc description") |
| 32 |
>>> k.append(d) |
| 33 |
|
| 34 |
Create a KML Folder and add it to the Document: |
| 35 |
|
| 36 |
.. code-block:: pycon |
| 37 |
|
| 38 |
>>> f = kml.Folder(id="fid", name="f name", description="f description") |
| 39 |
>>> d.append(f) |
| 40 |
|
| 41 |
Create a KML Folder and nest it in the first Folder: |
| 42 |
|
| 43 |
.. code-block:: pycon |
| 44 |
|
| 45 |
>>> nf = kml.Folder( |
| 46 |
... id="nested-fid", name="nested f name", description="nested f description" |
| 47 |
... ) |
| 48 |
>>> f.append(nf) |
| 49 |
|
| 50 |
Create a second KML Folder within the Document: |
| 51 |
|
| 52 |
.. code-block:: pycon |
| 53 |
|
| 54 |
>>> f2 = kml.Folder(id="id2", name="name2", description="description2") |
| 55 |
>>> d.append(f2) |
| 56 |
|
| 57 |
Create a KML Placemark with a simple polygon geometry and add it to the second Folder: |
| 58 |
|
| 59 |
.. code-block:: pycon |
| 60 |
|
| 61 |
>>> polygon = Polygon([(0, 0, 0), (1, 1, 0), (1, 0, 1)]) |
| 62 |
>>> p = kml.Placemark(id="id", name="name", description="description", geometry=polygon) |
| 63 |
>>> f2.append(p) |
| 64 |
|
| 65 |
Finally, print out the KML object as a string: |
| 66 |
|
| 67 |
.. code-block:: pycon |
| 68 |
|
| 69 |
>>> print(k.to_string(prettyprint=True, precision=6)) |
| 70 |
<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 71 |
<Document id="docid"> |
| 72 |
<name>doc name</name> |
| 73 |
<description>doc description</description> |
| 74 |
<Folder id="fid"> |
| 75 |
<name>f name</name> |
| 76 |
<description>f description</description> |
| 77 |
<Folder id="nested-fid"> |
| 78 |
<name>nested f name</name> |
| 79 |
<description>nested f description</description> |
| 80 |
</Folder> |
| 81 |
</Folder> |
| 82 |
<Folder id="id2"> |
| 83 |
<name>name2</name> |
| 84 |
<description>description2</description> |
| 85 |
<Placemark id="id"> |
| 86 |
<name>name</name> |
| 87 |
<description>description</description> |
| 88 |
<Polygon> |
| 89 |
<outerBoundaryIs> |
| 90 |
<LinearRing> |
| 91 |
<coordinates>0.000000,0.000000,0.000000 1.000000,1.000000,0.000000 1.000000,0.000000,1.000000 0.000000,0.000000,0.000000</coordinates> |
| 92 |
</LinearRing> |
| 93 |
</outerBoundaryIs> |
| 94 |
</Polygon> |
| 95 |
</Placemark> |
| 96 |
</Folder> |
| 97 |
</Document> |
| 98 |
</kml> |
| 99 |
<BLANKLINE> |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Read a KML File/String |
| 104 |
---------------------- |
| 105 |
|
| 106 |
You can create a KML object by reading a KML file from a string |
| 107 |
|
| 108 |
.. code-block:: pycon |
| 109 |
|
| 110 |
>>> doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 111 |
... <Document> |
| 112 |
... <name>Document.kml</name> |
| 113 |
... <open>1</open> |
| 114 |
... <Style id="exampleStyleDocument"> |
| 115 |
... <LabelStyle> |
| 116 |
... <color>ff0000cc</color> |
| 117 |
... </LabelStyle> |
| 118 |
... </Style> |
| 119 |
... <Placemark> |
| 120 |
... <name>Document Feature 1</name> |
| 121 |
... <styleUrl>#exampleStyleDocument</styleUrl> |
| 122 |
... <Point> |
| 123 |
... <coordinates>-122.371,37.816,0</coordinates> |
| 124 |
... </Point> |
| 125 |
... </Placemark> |
| 126 |
... <Placemark> |
| 127 |
... <name>Document Feature 2</name> |
| 128 |
... <styleUrl>#exampleStyleDocument</styleUrl> |
| 129 |
... <Point> |
| 130 |
... <coordinates>-122.370,37.817,0</coordinates> |
| 131 |
... </Point> |
| 132 |
... </Placemark> |
| 133 |
... </Document> |
| 134 |
... </kml>""" |
| 135 |
|
| 136 |
Read in the KML string |
| 137 |
|
| 138 |
.. code-block:: pycon |
| 139 |
|
| 140 |
>>> k = kml.KML.from_string(doc) |
| 141 |
|
| 142 |
|
| 143 |
.. note:: |
| 144 |
|
| 145 |
To read a KML file directly, you can use the parse method: |
| 146 |
|
| 147 |
.. code-block:: Python |
| 148 |
|
| 149 |
k = kml.KML.parse("path/to/file.kml") |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
Next we perform some simple sanity checks, such as checking the number of features. |
| 154 |
|
| 155 |
.. code-block:: pycon |
| 156 |
|
| 157 |
# This corresponds to the single ``Document`` |
| 158 |
>>> len(k.features) |
| 159 |
1 |
| 160 |
|
| 161 |
Check the number of Placemarks in the Document: |
| 162 |
|
| 163 |
.. code-block:: pycon |
| 164 |
|
| 165 |
# (The two Placemarks of the Document) |
| 166 |
>>> k.features[0].features # doctest: +ELLIPSIS |
| 167 |
[fastkml.features.Placemark... |
| 168 |
>>> len(k.features[0].features) |
| 169 |
2 |
| 170 |
|
| 171 |
Check the Placemarks in the Document: |
| 172 |
|
| 173 |
.. code-block:: pycon |
| 174 |
|
| 175 |
# Check specifics of the first Placemark in the Document |
| 176 |
>>> k.features[0].features[0] # doctest: +ELLIPSIS |
| 177 |
fastkml.features.Placemark(... |
| 178 |
>>> k.features[0].features[0].description |
| 179 |
>>> k.features[0].features[0].name |
| 180 |
'Document Feature 1' |
| 181 |
|
| 182 |
# Check specifics of the second Placemark in the Document |
| 183 |
>>> k.features[0].features[1].name |
| 184 |
'Document Feature 2' |
| 185 |
>>> k.features[0].features[1].name = "ANOTHER NAME" |
| 186 |
|
| 187 |
Finally, print out the KML object as a string: |
| 188 |
|
| 189 |
.. code-block:: pycon |
| 190 |
|
| 191 |
>>> print(k.to_string(prettyprint=True, precision=6)) |
| 192 |
<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 193 |
<Document> |
| 194 |
<name>Document.kml</name> |
| 195 |
<open>1</open> |
| 196 |
<Style id="exampleStyleDocument"> |
| 197 |
<LabelStyle> |
| 198 |
<color>ff0000cc</color> |
| 199 |
</LabelStyle> |
| 200 |
</Style> |
| 201 |
<Placemark> |
| 202 |
<name>Document Feature 1</name> |
| 203 |
<styleUrl>#exampleStyleDocument</styleUrl> |
| 204 |
<Point> |
| 205 |
<coordinates>-122.371000,37.816000,0.000000</coordinates> |
| 206 |
</Point> |
| 207 |
</Placemark> |
| 208 |
<Placemark> |
| 209 |
<name>ANOTHER NAME</name> |
| 210 |
<styleUrl>#exampleStyleDocument</styleUrl> |
| 211 |
<Point> |
| 212 |
<coordinates>-122.370000,37.817000,0.000000</coordinates> |
| 213 |
</Point> |
| 214 |
</Placemark> |
| 215 |
</Document> |
| 216 |
</kml> |
| 217 |
<BLANKLINE> |
| 218 |
|
| 219 |
.. note:: |
| 220 |
|
| 221 |
To save the KML object to a file, you can use the write method: |
| 222 |
|
| 223 |
.. code-block:: Python |
| 224 |
|
| 225 |
output_path = Path("path/to/file.kml") |
| 226 |
k.write(output_path) |