Code, Explained

Reusable Python Library for KML/KMZ Route Parsing and Google Maps URL Generation

Working with GPS routes often means dealing with KML and KMZ files exported from Google Earth, Garmin devices, route planners, or GIS applications. While these formats are widely used, extracting route information and converting it into something practical—such as a Google Maps directions link—usually requires manual effort.

To simplify this process, I built a Python solution that consists of two parts:

  1. A reusable library class that can be imported into any Python application.
  2. A command-line utility that uses the library for interactive and automated workflows.

The result is a flexible tool that can parse KML and KMZ files, generate Google Maps URLs, and export location data to CSV.

Github: https://github.com/razonklnbd/kml-to-google-map-url

Why I Built This Project

This project started from a real family travel planning problem.

I was planning a long road trip for my family using Google My Maps. My goal was to create a custom route with multiple stops and then print step-by-step driving directions for my daughter so she could easily follow the journey and understand the route ahead of time.

Google My Maps is excellent for planning and visualizing custom routes, but I quickly discovered a limitation: it does not provide the same printable turn-by-turn directions that are available in the standard Google Maps interface.

One option was to manually recreate the route in Google Maps by entering every stop one by one. While this works for a route with only a few locations, it becomes tedious and time-consuming when dealing with dozens of waypoints spread across a multi-day trip.

Since Google My Maps allows routes to be exported as KML files, I started looking for a way to automatically convert that route data into a Google Maps Directions URL that could be opened directly in the standard Google Maps interface.

As a Python developer, I realized this problem was straightforward to automate:

  1. Export the route from Google My Maps as a KML or KMZ file.
  2. Extract all placemark locations.
  3. Generate a Google Maps Directions URL using those locations.
  4. Open the generated URL in Google Maps.
  5. Print the step-by-step directions from Google Maps.

What began as a small utility script for a family vacation quickly evolved into a reusable Python library and command-line tool that can parse KML/KMZ files, generate Google Maps route URLs, and export route information to CSV.

The result is a tool that saves time, eliminates manual data entry, and makes it easy to move route data from Google My Maps into standard Google Maps for navigation and printing.


Project Goals

The design goals were:

  • Support both KML and KMZ formats
  • Generate Google Maps directions URLs
  • Export route points to CSV
  • Provide both GUI and command-line workflows
  • Separate business logic from user interface code
  • Allow other developers to reuse the parsing functionality in their own projects

Architecture Overview

The project is divided into two files.

1. Library Layer

kml_route_parser.py

Contains:

class KmlRouteParser

Responsibilities:

  • Read KML files
  • Read KMZ files
  • Extract placemark points
  • Generate Google Maps URLs
  • Export CSV files
  • Provide route data programmatically

The library contains no user interface code.


2. Application Layer

main.py

Responsibilities:

  • Command-line argument processing
  • File selection dialogs
  • Save dialogs
  • User prompts
  • Displaying results

This separation follows a clean architecture approach where the reusable logic is independent of the user interface.


Supported File Formats

KML

KML (Keyhole Markup Language) is an XML-based format used to store geographic data.

Example:

<Placemark>
    <name>Home</name>
    <Point>
        <coordinates>
            72.5432,23.0345,0
        </coordinates>
    </Point>
</Placemark>

KMZ

KMZ is a compressed ZIP archive containing one or more KML files.

The library automatically:

  • Detects KMZ files
  • Extracts the embedded KML
  • Parses the route data
  • Cleans up temporary files

The user does not need to manually extract anything.


Extracting Route Points

Each placemark is converted into a structured Python dictionary.

Example:

{
    "name": "Home",
    "description": "Starting Point",
    "latitude": 23.0345,
    "longitude": 72.5432
}

The parser returns a list of points that can be consumed by any Python application.


Google Maps URL Generation

One of the primary features of the library is automatic generation of Google Maps Directions URLs.

The library supports two modes.


Name-Based URLs

Example:

https://www.google.com/maps/dir/Home/Office/Warehouse

Usage:

url = parser.get_google_maps_url(
    use_names=True
)

This produces human-readable URLs.


Coordinate-Based URLs

Example:

https://www.google.com/maps/dir/23.0345,72.5432/23.0654,72.6021

Usage:

url = parser.get_google_maps_url(
    use_names=False
)

Coordinates are often more reliable because they eliminate ambiguity caused by duplicate place names.


CSV Export

The library can export extracted points into a CSV file.

Example:

Name,Latitude,Longitude,Description
Home,23.0345,72.5432,Starting Point
Office,23.0654,72.6021,Destination

Usage:

parser.save_csv(
    "points.csv"
)

This allows route data to be easily opened in:

  • Excel
  • Google Sheets
  • Power BI
  • GIS software
  • Data analysis tools

Using the Library in Your Own Projects

The biggest advantage of the refactoring is that developers can now use the parser without the command-line application.

Example:

from kml_route_parser import KmlRouteParser

parser = KmlRouteParser(
    "route.kmz"
)

parser.load()

url = parser.get_google_maps_url(
    use_names=False
)

print(url)

Accessing Route Points Programmatically

You can access all extracted locations directly.

Example:

points = parser.get_points()

for point in points:

    print(
        point["name"],
        point["latitude"],
        point["longitude"]
    )

This makes the library suitable for:

  • GIS applications
  • Fleet management systems
  • Route optimization tools
  • Data processing pipelines
  • Custom map applications

Command-Line Utility

The project also includes a command-line application built on top of the library.

File:

main.py

Interactive File Selection

When no input file is supplied:

python main.py

a file picker dialog appears.

Supported file types:

  • KML
  • KMZ

This makes the tool accessible to non-technical users.


Command-Line File Input

Files can also be provided directly.

Example:

python main.py route.kml

or

python main.py route.kmz

This mode is useful for automation.


URL Mode Selection

If URL mode is not supplied through the command line, the application asks the user:

Build URL using:

1. Place Names
2. Coordinates

The selected mode is passed to the library.


Automated URL Mode

URL mode can be specified directly.

Use names:

python main.py route.kmz --url-mode name

Use coordinates:

python main.py route.kmz --url-mode coord

No prompt is displayed.


CSV Export Options

The application supports multiple CSV export workflows.


Save Dialog

If no CSV file is supplied, a Save As dialog appears.

The user can:

  • Choose location
  • Change filename
  • Cancel export

If cancelled:

CSV export cancelled.

No file is created.


Specify CSV File

Example:

python main.py route.kmz --csv points.csv

or

python main.py route.kmz --csv "C:\Routes\points.csv"

CSV is written directly without prompting.


Disable CSV Export

Example:

python main.py route.kmz --no-csv

The application skips CSV generation entirely.


Fully Automated Example

This example runs without any dialogs or prompts.

python main.py route.kmz \
    --url-mode coord \
    --csv points.csv

Workflow:

  1. Load KMZ file
  2. Extract route points
  3. Generate coordinate-based Google Maps URL
  4. Save CSV file

Perfect for automation scripts and scheduled jobs.


Fully Interactive Example

python main.py

Workflow:

  1. Select KML/KMZ file
  2. Choose URL generation mode
  3. Select CSV save location
  4. Receive Google Maps URL

Ideal for occasional users.


Why Split the Project into a Library and CLI?

The original version mixed:

  • Parsing logic
  • URL generation
  • User interaction
  • File dialogs

inside a single script.

By separating the code:

Benefits for Developers

  • Easier testing
  • Better maintainability
  • Cleaner architecture
  • Reusable functionality
  • Simpler future enhancements

Benefits for Users

  • Interactive desktop experience
  • Command-line automation
  • Flexible workflows

Future Enhancements

Potential improvements include:

  • GPX file support
  • Route statistics
  • Distance calculations
  • Elevation analysis
  • OpenStreetMap URL generation
  • GeoJSON export
  • Interactive map visualization
  • Batch processing of multiple files

Conclusion

By separating the project into a reusable KmlRouteParser library and a lightweight command-line application, the solution becomes useful for both developers and end users.

Developers can import the library directly into their applications, while non-technical users can continue to use graphical file dialogs and interactive prompts.

The result is a flexible tool capable of parsing KML/KMZ files, generating Google Maps route URLs, exporting CSV data, and fitting seamlessly into both manual and automated workflows.

Posted in PythonTagged , , ,