Created
June 3, 2026 10:51
-
-
Save aspose-com-gists/7cb2772a3c26744f04dde0aa0ece00fd to your computer and use it in GitHub Desktop.
Convert Shapefile to KML in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Convert Shapefile to KML in Python | |
| # Read the full guide here: https://blog.aspose.com/gis/convert-shapefile-to-kml-in-python/ | |
| # Complete working example: Convert Shapefile to KML using Aspose.GIS for Python via .NET | |
| import os | |
| from aspose.gis import VectorLayer, KmlWriter, KmlOptions | |
| def convert_shapefile_to_kml(input_shp: str, output_kml: str): | |
| # Validate input file | |
| if not os.path.isfile(input_shp): | |
| raise FileNotFoundError(f"Input Shapefile not found: {input_shp}") | |
| # Load the Shapefile (geometry + attribute table) | |
| layer = VectorLayer.open(input_shp) | |
| # Prepare KML export options | |
| options = KmlOptions() | |
| options.export_attributes = True # Preserve attribute data | |
| options.use_streaming = True # Optimize memory usage for large files | |
| options.pretty_print = True # Human‑readable KML output | |
| # Write to KML | |
| KmlWriter.write(layer, output_kml, options) | |
| print(f"KML file created at: {output_kml}") | |
| if __name__ == "__main__": | |
| INPUT_SHAPEFILE = "data/sample.shp" | |
| OUTPUT_KML = "output/sample.kml" | |
| convert_shapefile_to_kml(INPUT_SHAPEFILE, OUTPUT_KML) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment