Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gx:CascadingStyle #311

Closed
OxygenCobalt opened this issue Mar 22, 2024 · 5 comments
Closed

Support gx:CascadingStyle #311

OxygenCobalt opened this issue Mar 22, 2024 · 5 comments

Comments

@OxygenCobalt
Copy link

OxygenCobalt commented Mar 22, 2024

Hello,

We've ran into an issue on our end where we cannot extract Polygon colors from the Google Earth KML files we exported on our end, as they are contained within a gx:CascadingStyle extension element that is not read. At minimum it would be useful for fastkml to read this element when it is referenced by styleUrl and then apply the relevant styles to the object being read.

Thanks.

@cleder
Copy link
Owner

cleder commented Mar 24, 2024

I cannot find documentation about gx:CascadingStyle

@OxygenCobalt
Copy link
Author

OxygenCobalt commented Mar 24, 2024

From what I see it's basically like any other Style element but presumably cascades some inherited style like CSS. I'm not too well-versed on KML though. All I want is for fastkml to recognize it as a Style and use it, the actual functionality isn't needed.

Here's a CascadingStyle from our KML file:

<gx:CascadingStyle kml:id="__managed_style_881999257A2F4EDCAD33">
	<styleUrl>https://earth.google.com/balloon_components/base/1.0.26.0/card_template.kml#main</styleUrl>
	<Style>
		<IconStyle>
			<scale>1.2</scale>
			<Icon>
				<href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&amp;id=2000&amp;scale=4</href>
			</Icon>
			<hotSpot x="64" y="128" xunits="pixels" yunits="insetPixels"/>
		</IconStyle>
		<LabelStyle>
		</LabelStyle>
		<LineStyle>
			<color>ff2846cc</color>
			<width>3</width>
		</LineStyle>
		<PolyStyle>
			<color>40ffffff</color>
		</PolyStyle>
		<BalloonStyle>
		</BalloonStyle>
	</Style>
</gx:CascadingStyle>

Hope this helps @cleder

@cleder
Copy link
Owner

cleder commented Apr 7, 2024

The architecture has changed a lot from fastkml 0.x to 1.x.

One of the advantages of these changes is that you can define and add new behaviour at runtime through the registry.
What is still missing to be able to add arbitrary XML structures is that **kwargs should be added to the base._XMLObject and propagated through the inheritance chain. This way, the integration of things like gx:CascadingStyle can be implemented by the end user without patching the underlying fastkml library.

Ping me if you are interested in helping to implement this.

@cleder cleder added this to FastKML Sep 15, 2024
@cleder
Copy link
Owner

cleder commented Oct 27, 2024

This is now documented, and an example was added:

#!/usr/bin/env python
from typing import Any
from typing import Dict
from typing import Optional

from fastkml import KML
from fastkml import Document
from fastkml import Style
from fastkml import config
from fastkml.helpers import xml_subelement
from fastkml.helpers import xml_subelement_kwarg
from fastkml.helpers import xml_subelement_list
from fastkml.helpers import xml_subelement_list_kwarg
from fastkml.kml_base import _BaseObject
from fastkml.registry import RegistryItem
from fastkml.registry import registry
from fastkml.utils import find


class CascadingStyle(_BaseObject):
    """
    CascadingStyle.

    The ``<gx:CascadingStyle>`` is an undocumented element that is created in
    Google Earth Web that is unsupported by Google Earth Pro
    """
    _default_nsid = config.GX

    def __init__(
        self,
        ns: Optional[str] = None,
        name_spaces: Optional[Dict[str, str]] = None,
        id: Optional[str] = None,
        target_id: Optional[str] = None,
        style: Optional[Style] = None,
        **kwargs: Any,
    ) -> None:
        """Initialize the CascadingStyle object."""
        self.style = style
        super().__init__(ns, name_spaces, id, target_id, **kwargs)


registry.register(
    CascadingStyle,
    RegistryItem(
        ns_ids=("kml",),
        attr_name="style",
        node_name="Style",
        classes=(Style,),
        get_kwarg=xml_subelement_kwarg,
        set_element=xml_subelement,
    ),
)


registry.register(
    Document,
    RegistryItem(
        ns_ids=("gx",),
        attr_name="gx_cascading_style",
        node_name="CascadingStyle",
        classes=(CascadingStyle,),
        get_kwarg=xml_subelement_list_kwarg,
        set_element=xml_subelement_list,
    ),
)

cs_kml = KML.parse("examples/gx_cascading_style.kml")
document = find(cs_kml, of_type=Document)
for cascading_style in document.gx_cascading_style:
    kml_style = cascading_style.style
    kml_style.id = cascading_style.id
    document.styles.append(kml_style)

document.gx_cascading_style = []
print(cs_kml.to_string(prettyprint=True))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

2 participants