aboutsummaryrefslogtreecommitdiff
path: root/src/geojson2kml.py
diff options
context:
space:
mode:
authorgdamms <damguillotin@gmail.com>2026-07-24 13:25:21 +0200
committergdamms <damguillotin@gmail.com>2026-07-24 13:25:21 +0200
commit9daf8e0fa5db84988679606c9b4c8f26ef463db1 (patch)
tree0e2a0df8912f65c0b22f9efab017312df9218c76 /src/geojson2kml.py
parent77aed43a99a295f3202063edefcb7ce01e011958 (diff)
downloaddog-friendly-data-9daf8e0fa5db84988679606c9b4c8f26ef463db1.tar.gz
dog-friendly-data-9daf8e0fa5db84988679606c9b4c8f26ef463db1.zip
clean the repo
Diffstat (limited to 'src/geojson2kml.py')
-rw-r--r--src/geojson2kml.py56
1 files changed, 22 insertions, 34 deletions
diff --git a/src/geojson2kml.py b/src/geojson2kml.py
index b923d98..8c116f0 100644
--- a/src/geojson2kml.py
+++ b/src/geojson2kml.py
@@ -1,60 +1,48 @@
+import argparse
import json
import xml.etree.ElementTree as ET
+from kml_utils import add_placemark, add_polygon, new_document, write_kml
-KML_NS = "http://www.opengis.net/kml/2.2"
-ET.register_namespace("", KML_NS)
+def geojson_to_kml(geojson: dict) -> ET.Element:
+ """Convert a GeoJSON FeatureCollection into a KML tree.
-def geojson_to_kml(geojson: dict) -> ET.ElementTree[ET.Element]:
- root = ET.Element(f"kml", xmlns=KML_NS)
- document = ET.SubElement(root, f"Document")
+ Only MultiPolygon features are supported; other geometry types are skipped.
- ET.SubElement(document, f"name").text = geojson.get("name", "Unnamed")
+ Args:
+ geojson (dict): Parsed GeoJSON FeatureCollection.
+ Returns:
+ ET.Element: The root <kml> element.
+ """
+ root, document = new_document(geojson.get("name", "Unnamed"))
for feature in geojson.get("features", []):
- placemark = ET.SubElement(document, f"Placemark")
-
- ET.SubElement(placemark, f"name").text = ""
- ET.SubElement(placemark, f"description").text = ""
-
- # TODO: Handle other geometry types if needed
- if feature["geometry"]["type"] != "MultiPolygon":
- print(f"Skipping feature with unsupported geometry type: {feature['geometry']['type']}")
+ geometry_type = feature["geometry"]["type"]
+ if geometry_type != "MultiPolygon":
+ print(f"Skipping feature with unsupported geometry type: {geometry_type}")
continue
- for polygon in feature["geometry"]["coordinates"][0]:
- polygon_el = ET.SubElement(placemark, f"Polygon")
-
- outer = ET.SubElement(polygon_el, f"outerBoundaryIs")
- ring = ET.SubElement(outer, f"LinearRing")
+ placemark = add_placemark(document)
+ for ring in feature["geometry"]["coordinates"][0]:
+ add_polygon(placemark, ring)
- coords = ET.SubElement(ring, f"coordinates")
- coords.text = "\n".join(
- f"{lon},{lat}"
- for lon, lat in polygon
- )
-
- ET.indent(root)
-
- return ET.ElementTree(root)
+ return root
def main():
- import argparse
-
parser = argparse.ArgumentParser(description="Convert GeoJSON to KML")
parser.add_argument("input_file", help="Path to the input GeoJSON file")
- parser.add_argument("-o", "--output_file", help="Path to the output KML file")
+ parser.add_argument("-o", "--output", help="Path to the output KML file")
args = parser.parse_args()
with open(args.input_file, "r") as geojson_file:
geojson = json.load(geojson_file)
- output_file = args.output_file or args.input_file.rsplit(".", 1)[0] + ".kml"
+ output_file = args.output or args.input_file.rsplit(".", 1)[0] + ".kml"
- kml = geojson_to_kml(geojson)
- kml.write(output_file, encoding="utf-8", xml_declaration=True)
+ root = geojson_to_kml(geojson)
+ write_kml(root, output_file)
if __name__ == "__main__":