| 1 |
|
| 2 |
import pathlib |
| 3 |
from typing import Any |
| 4 |
from typing import Optional |
| 5 |
|
| 6 |
from fastkml import KML |
| 7 |
from fastkml import Document |
| 8 |
from fastkml import Style |
| 9 |
from fastkml import config |
| 10 |
from fastkml.helpers import xml_subelement |
| 11 |
from fastkml.helpers import xml_subelement_kwarg |
| 12 |
from fastkml.helpers import xml_subelement_list |
| 13 |
from fastkml.helpers import xml_subelement_list_kwarg |
| 14 |
from fastkml.kml_base import _BaseObject |
| 15 |
from fastkml.registry import RegistryItem |
| 16 |
from fastkml.registry import registry |
| 17 |
from fastkml.utils import find |
| 18 |
|
| 19 |
examples_dir = pathlib.Path(__file__).parent |
| 20 |
|
| 21 |
|
| 22 |
class CascadingStyle(_BaseObject): |
| 23 |
""" |
| 24 |
CascadingStyle. |
| 25 |
|
| 26 |
The ``<gx:CascadingStyle>`` is an undocumented element that is created in |
| 27 |
Google Earth Web that is unsupported by Google Earth Pro |
| 28 |
""" |
| 29 |
|
| 30 |
_default_nsid = config.GX |
| 31 |
|
| 32 |
def __init__( |
| 33 |
self, |
| 34 |
ns: Optional[str] = None, |
| 35 |
name_spaces: Optional[dict[str, str]] = None, |
| 36 |
id: Optional[str] = None, |
| 37 |
target_id: Optional[str] = None, |
| 38 |
style: Optional[Style] = None, |
| 39 |
**kwargs: Any, |
| 40 |
) -> None: |
| 41 |
"""Initialize the CascadingStyle object.""" |
| 42 |
self.style = style |
| 43 |
super().__init__(ns, name_spaces, id, target_id, **kwargs) |
| 44 |
|
| 45 |
|
| 46 |
registry.register( |
| 47 |
CascadingStyle, |
| 48 |
RegistryItem( |
| 49 |
ns_ids=("kml",), |
| 50 |
attr_name="style", |
| 51 |
node_name="Style", |
| 52 |
classes=(Style,), |
| 53 |
get_kwarg=xml_subelement_kwarg, |
| 54 |
set_element=xml_subelement, |
| 55 |
), |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
registry.register( |
| 60 |
Document, |
| 61 |
RegistryItem( |
| 62 |
ns_ids=("gx",), |
| 63 |
attr_name="gx_cascading_style", |
| 64 |
node_name="CascadingStyle", |
| 65 |
classes=(CascadingStyle,), |
| 66 |
get_kwarg=xml_subelement_list_kwarg, |
| 67 |
set_element=xml_subelement_list, |
| 68 |
), |
| 69 |
) |
| 70 |
|
| 71 |
cs_kml = KML.parse(examples_dir / "gx_cascading_style.kml", validate=False) |
| 72 |
document = find(cs_kml, of_type=Document) |
| 73 |
for cascading_style in document.gx_cascading_style: |
| 74 |
kml_style = cascading_style.style |
| 75 |
kml_style.id = cascading_style.id |
| 76 |
document.styles.append(kml_style) |
| 77 |
|
| 78 |
document.gx_cascading_style = [] |
| 79 |
print(cs_kml.to_string(prettyprint=True)) |