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

Feature Request: pass optional dictionary of namespaces and optional doc type string #346

Open
wesley-murray opened this issue Aug 29, 2024 · 4 comments

Comments

@wesley-murray
Copy link

wesley-murray commented Aug 29, 2024

I extended the KML class so I could add a from_geojson function. Ideally this concept can be taken further to include other types such as shape files. The raw data formats dont have kml specific logic like xml doc type and namespaces. It would be very helpful if the to_string or constructor method had optional args that allow you to pass on the doctype and a dictionary of namespaces. The namespaces would also need to be passed along to the etree_element function. I had good results with this in my case.

In the below example I used inheritance to overwrite the parent. It assumes use of LXML.

def to_string(self, prettyprint=False, namespaces=None, doctype='<?xml version="1.0" encoding="UTF-8"?>') -> str:
        """Returns the KML object as a string."""
        return etree.tostring(
            self.etree_element(namespaces=namespaces),
            encoding="utf-8",
            pretty_print=prettyprint,
            doctype=doctype,
        ).decode("UTF-8")
@HazemAbdo
Copy link

@cleder I'd like to work on this feature under Hacktoberfest. Can you please assign it to me?

@cleder
Copy link
Owner

cleder commented Sep 28, 2024

Please have a look at the contributing guidelines.

@cleder
Copy link
Owner

cleder commented Sep 28, 2024

I think instead of adding it to the to_string method, it would be better to add a save method to fastkml.kml.KML like the parse method, taking the file or filename to write to. I don't think you will need to pass the namespaces as @wesley-murray suggested, they are already present on the object.

@cleder
Copy link
Owner

cleder commented Sep 30, 2024

In this context, I think it is worth a look if we want to register the namespaces on import time or when the object is serialized to XML -> https://github.com/cleder/fastkml/blob/develop/fastkml/config.py

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

No branches or pull requests

3 participants