| 1 |
Working with KML Files |
| 2 |
====================== |
| 3 |
|
| 4 |
Import the necessary modules: |
| 5 |
|
| 6 |
.. code-block:: pycon |
| 7 |
|
| 8 |
>>> from fastkml.utils import find, find_all |
| 9 |
>>> from fastkml import KML |
| 10 |
>>> from fastkml import Placemark, Point, StyleUrl, Style |
| 11 |
>>> from typing import Dict, Any, Optional, Iterable |
| 12 |
|
| 13 |
|
| 14 |
Open a KML file: |
| 15 |
|
| 16 |
.. code-block:: pycon |
| 17 |
|
| 18 |
>>> k = KML.parse("docs/Document-clean.kml") |
| 19 |
|
| 20 |
Extract all placemarks and print their geometries. |
| 21 |
The function ``find_all`` recursively searches a KML document for elements of a specific |
| 22 |
type and returns an iterator of all matching elements found in the document tree. |
| 23 |
|
| 24 |
.. code-block:: pycon |
| 25 |
|
| 26 |
>>> placemarks = list(find_all(k, of_type=Placemark)) |
| 27 |
>>> for p in placemarks: |
| 28 |
... print(p.geometry) # doctest: +ELLIPSIS |
| 29 |
... |
| 30 |
POINT Z (-123.93563168 49.16716103 5.0) |
| 31 |
POLYGON Z ((-123.940449937288 49.16927524669021 17.0, ... |
| 32 |
|
| 33 |
We could also search for all Points, which will also return the Points inside the |
| 34 |
``MultiGeometries``. |
| 35 |
|
| 36 |
.. code-block:: pycon |
| 37 |
|
| 38 |
>>> pts = list(find_all(k, of_type=Point)) |
| 39 |
>>> for point in pts: |
| 40 |
... print(point.geometry) |
| 41 |
... |
| 42 |
POINT Z (-123.93563168 49.16716103 5.0) |
| 43 |
POINT Z (-123.1097 49.2774 0.0) |
| 44 |
POINT Z (-123.028369 49.26107900000001 0.0) |
| 45 |
POINT Z (-123.3215766 49.2760338 0.0) |
| 46 |
POINT Z (-123.2643704 49.3301853 0.0) |
| 47 |
POINT Z (-123.2477084 49.2890857 0.0) |
| 48 |
POINT Z (8.23 46.707 0.0) |
| 49 |
|
| 50 |
|
| 51 |
``find_all`` can also search for arbitrary elements by their attributes, by passing the |
| 52 |
attribute name and value as keyword arguments. |
| 53 |
|
| 54 |
.. note:: |
| 55 |
|
| 56 |
``find`` is a shortcut for ``find_all`` that returns the first element found, |
| 57 |
which is useful when we know there is only one element that matches the search |
| 58 |
criteria. |
| 59 |
|
| 60 |
.. code-block:: pycon |
| 61 |
|
| 62 |
>>> pm = find(k, name="Vancouver Film Studios") |
| 63 |
>>> pm.name |
| 64 |
'Vancouver Film Studios' |
| 65 |
>>> pm.get_tag_name() |
| 66 |
'Placemark' |
| 67 |
>>> find(k, href="http://www.vancouverfilmstudios.com/") # doctest: +ELLIPSIS |
| 68 |
fastkml.atom.Link(ns=... |
| 69 |
|
| 70 |
We can also use ``find`` to get the parent of the element we found: |
| 71 |
|
| 72 |
.. code-block:: pycon |
| 73 |
|
| 74 |
>>> a_link = find(k, href="http://www.vancouverfilmstudios.com/") |
| 75 |
>>> find(k, atom_link=a_link) # doctest: +ELLIPSIS |
| 76 |
fastkml.features.Placemark(ns=... |
| 77 |
|
| 78 |
For more targeted searches, we can combine multiple search criteria: |
| 79 |
|
| 80 |
.. code-block:: pycon |
| 81 |
|
| 82 |
>>> style_url = StyleUrl(url="#khStyle712") |
| 83 |
>>> pm = find(k, of_type=Placemark, name="HBC Bastion", style_url=style_url) |
| 84 |
>>> pm.geometry |
| 85 |
Point(-123.93563168, 49.16716103, 5.0) |
| 86 |
>>> pm.style_url.url |
| 87 |
'#khStyle712' |
| 88 |
>>> pm.name |
| 89 |
'HBC Bastion' |
| 90 |
|
| 91 |
|
| 92 |
Extending FastKML |
| 93 |
----------------- |
| 94 |
|
| 95 |
FastKML is designed to be easily extended. For example, we can add a new object to KML |
| 96 |
by subclassing ``fastkml.base.__XMLObject`` or ``fastkml.kml_base._BaseObject`` and |
| 97 |
defining the new element's tag name and attributes. |
| 98 |
The ``<gx:CascadingStyle>`` is an undocumented element that is created in |
| 99 |
Google Earth Web that is unsupported by Google Earth Pro, we want to transform it into |
| 100 |
a supported element. |
| 101 |
|
| 102 |
.. code-block:: pycon |
| 103 |
|
| 104 |
>>> from fastkml.kml_base import _BaseObject |
| 105 |
>>> from fastkml import config |
| 106 |
>>> class CascadingStyle(_BaseObject): |
| 107 |
... _default_nsid = config.GX |
| 108 |
... def __init__( |
| 109 |
... self, |
| 110 |
... ns: Optional[str] = None, |
| 111 |
... name_spaces: Optional[Dict[str, str]] = None, |
| 112 |
... id: Optional[str] = None, |
| 113 |
... target_id: Optional[str] = None, |
| 114 |
... style: Optional[Style] = None, |
| 115 |
... **kwargs: Any, |
| 116 |
... ) -> None: |
| 117 |
... self.style = style |
| 118 |
... super().__init__(ns, name_spaces, id, target_id, **kwargs) |
| 119 |
... |
| 120 |
|
| 121 |
We need to register the attributes of the KML object to be able to parse it: |
| 122 |
|
| 123 |
.. code-block:: pycon |
| 124 |
|
| 125 |
>>> from fastkml.registry import RegistryItem, registry |
| 126 |
>>> from fastkml.helpers import xml_subelement, xml_subelement_kwarg |
| 127 |
>>> registry.register( |
| 128 |
... CascadingStyle, |
| 129 |
... RegistryItem( |
| 130 |
... ns_ids=("kml", ""), |
| 131 |
... attr_name="style", |
| 132 |
... node_name="Style", |
| 133 |
... classes=(Style,), |
| 134 |
... get_kwarg=xml_subelement_kwarg, |
| 135 |
... set_element=xml_subelement, |
| 136 |
... ), |
| 137 |
... ) |
| 138 |
|
| 139 |
And register the new element with the KML Document object: |
| 140 |
|
| 141 |
.. code-block:: pycon |
| 142 |
|
| 143 |
>>> from fastkml import Document |
| 144 |
>>> from fastkml.helpers import xml_subelement_list, xml_subelement_list_kwarg |
| 145 |
>>> registry.register( |
| 146 |
... Document, |
| 147 |
... RegistryItem( |
| 148 |
... ns_ids=("gx",), |
| 149 |
... attr_name="gx_cascading_style", |
| 150 |
... node_name="CascadingStyle", |
| 151 |
... classes=(CascadingStyle,), |
| 152 |
... get_kwarg=xml_subelement_list_kwarg, |
| 153 |
... set_element=xml_subelement_list, |
| 154 |
... ), |
| 155 |
... ) |
| 156 |
|
| 157 |
The CascadingStyle object is now part of the KML document and can be accessed like any |
| 158 |
other element. |
| 159 |
|
| 160 |
.. note:: |
| 161 |
When parsing the document we have to skip the validation by passing ``validate=False`` |
| 162 |
to ``KML.parse`` as the ``gx:CascadingStyle`` is not in the XSD Schema. |
| 163 |
|
| 164 |
Create a new KML object and confirm that the new element is parsed correctly: |
| 165 |
|
| 166 |
.. code-block:: pycon |
| 167 |
|
| 168 |
>>> cs_kml = KML.parse("examples/gx_cascading_style.kml", validate=False) |
| 169 |
>>> cs = find(cs_kml, of_type=CascadingStyle) |
| 170 |
>>> cs.style # doctest: +ELLIPSIS |
| 171 |
fastkml.styles.Style(... |
| 172 |
|
| 173 |
|
| 174 |
To be able to open the KML file in Google Earth Pro, we need to transform the |
| 175 |
CascadingStyle element into a supported Style element. |
| 176 |
To achieve this we copy the styles into the document styles and adjust their id |
| 177 |
to match the id of the CascadingStyle. |
| 178 |
|
| 179 |
.. code-block:: pycon |
| 180 |
|
| 181 |
>>> document = find(cs_kml, of_type=Document) |
| 182 |
>>> for cascading_style in document.gx_cascading_style: |
| 183 |
... kml_style = cascading_style.style |
| 184 |
... kml_style.id = cascading_style.id |
| 185 |
... document.styles.append(kml_style) |
| 186 |
... |
| 187 |
|
| 188 |
Now we can remove the CascadingStyle from the document and have a look at the result. |
| 189 |
|
| 190 |
.. code-block:: pycon |
| 191 |
|
| 192 |
>>> document.gx_cascading_style = [] |
| 193 |
>>> print(document) |
| 194 |
<kml:Document xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 195 |
<kml:name>Test2</kml:name> |
| 196 |
<kml:StyleMap id="__managed_style_0D301BCC0014827EFCCB"> |
| 197 |
<kml:Pair> |
| 198 |
<kml:key>normal</kml:key> |
| 199 |
<kml:styleUrl>#__managed_style_14CDD4276C14827EFCCB</kml:styleUrl> |
| 200 |
</kml:Pair> |
| 201 |
<kml:Pair> |
| 202 |
<kml:key>highlight</kml:key> |
| 203 |
<kml:styleUrl>#__managed_style_25EBAAC82614827EFCCB</kml:styleUrl> |
| 204 |
</kml:Pair> |
| 205 |
</kml:StyleMap> |
| 206 |
<kml:Style id="__managed_style_25EBAAC82614827EFCCB"> |
| 207 |
<kml:IconStyle> |
| 208 |
<kml:scale>1.2</kml:scale> |
| 209 |
<kml:Icon> |
| 210 |
<kml:href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&id=2000&scale=4</kml:href> |
| 211 |
</kml:Icon> |
| 212 |
<kml:hotSpot x="64.0" y="128.0" xunits="pixels" yunits="insetPixels"/> |
| 213 |
</kml:IconStyle> |
| 214 |
<kml:LineStyle> |
| 215 |
<kml:width>24.0</kml:width> |
| 216 |
</kml:LineStyle> |
| 217 |
<kml:PolyStyle> |
| 218 |
<kml:color>80000000</kml:color> |
| 219 |
</kml:PolyStyle> |
| 220 |
<kml:BalloonStyle> |
| 221 |
<kml:displayMode>hide</kml:displayMode> |
| 222 |
</kml:BalloonStyle> |
| 223 |
</kml:Style> |
| 224 |
<kml:Style id="__managed_style_14CDD4276C14827EFCCB"> |
| 225 |
<kml:IconStyle> |
| 226 |
<kml:Icon> |
| 227 |
<kml:href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&id=2000&scale=4</kml:href> |
| 228 |
</kml:Icon> |
| 229 |
<kml:hotSpot x="64.0" y="128.0" xunits="pixels" yunits="insetPixels"/> |
| 230 |
</kml:IconStyle> |
| 231 |
<kml:LineStyle> |
| 232 |
<kml:width>16.0</kml:width> |
| 233 |
</kml:LineStyle> |
| 234 |
<kml:PolyStyle> |
| 235 |
<kml:color>80000000</kml:color> |
| 236 |
</kml:PolyStyle> |
| 237 |
<kml:BalloonStyle> |
| 238 |
<kml:displayMode>hide</kml:displayMode> |
| 239 |
</kml:BalloonStyle> |
| 240 |
</kml:Style> |
| 241 |
<kml:Placemark id="04AFE6060F147CE66FBD"> |
| 242 |
<kml:name>Ort1</kml:name> |
| 243 |
<kml:LookAt> |
| 244 |
<kml:longitude>10.06256752902339</kml:longitude> |
| 245 |
<kml:latitude>53.57036326842834</kml:latitude> |
| 246 |
<kml:altitude>13.96486261382906</kml:altitude> |
| 247 |
<kml:heading>0.0</kml:heading> |
| 248 |
<kml:tilt>0.0</kml:tilt> |
| 249 |
<kml:range>632.584179697442</kml:range> |
| 250 |
<kml:altitudeMode>absolute</kml:altitudeMode> |
| 251 |
</kml:LookAt> |
| 252 |
<kml:styleUrl>#__managed_style_0D301BCC0014827EFCCB</kml:styleUrl> |
| 253 |
<kml:Polygon> |
| 254 |
<kml:outerBoundaryIs> |
| 255 |
<kml:LinearRing> |
| 256 |
<kml:coordinates>10.05998904317019,53.57172202479447,10.32521244530025 10.06072970043745,53.57050957507556,13.60797686155092 10.06170365480513,53.57072597737833,13.60026817081542 10.06094034058923,53.57192922042453,10.47620396741323 10.05998904317019,53.57172202479447,10.32521244530025</kml:coordinates> |
| 257 |
</kml:LinearRing> |
| 258 |
</kml:outerBoundaryIs> |
| 259 |
</kml:Polygon> |
| 260 |
</kml:Placemark> |
| 261 |
</kml:Document> |
| 262 |
<BLANKLINE> |