import argparse import json import xml.etree.ElementTree as ET from kml_utils import add_placemark, add_polygon, new_document, write_kml def geojson_to_kml(geojson: dict) -> ET.Element: """Convert a GeoJSON FeatureCollection into a KML tree. Only MultiPolygon features are supported; other geometry types are skipped. Args: geojson (dict): Parsed GeoJSON FeatureCollection. Returns: ET.Element: The root element. """ root, document = new_document(geojson.get("name", "Unnamed")) for feature in geojson.get("features", []): geometry_type = feature["geometry"]["type"] if geometry_type != "MultiPolygon": print(f"Skipping feature with unsupported geometry type: {geometry_type}") continue placemark = add_placemark(document) for ring in feature["geometry"]["coordinates"][0]: add_polygon(placemark, ring) return root def main(): 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", 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 or args.input_file.rsplit(".", 1)[0] + ".kml" root = geojson_to_kml(geojson) write_kml(root, output_file) if __name__ == "__main__": main()