Python with GIS: Extracting Purok and Path Data from Google Earth Pro KMZ
Introduction
Geographic Information Systems (GIS) are used to collect, organize, manage, analyze, and visualize information connected to real-world locations. GIS is commonly used for mapping, government information systems, infrastructure planning, environmental monitoring, disaster management, and location-based applications.
In many GIS projects, geographic information is first prepared using a mapping application such as Google Earth Pro. Instead of manually transferring every geographic record into another system, Python can be used to automate the extraction and processing of the geographic information.
In this practical workflow, Google Earth Pro is used to prepare the geographic data. The completed map is saved as a KMZ file containing Purok information and Path features represented as LineString geometry. Python then reads and processes the KMZ data and generates a CSV file containing the extracted information.
Python GIS automation workflow: Google Earth Pro → KMZ → Python → CSV.
1. Understanding the GIS Workflow
The workflow in this project is different from a traditional GIS workflow where geographic points are created from latitude and longitude values. In this project, the geographic information has already been prepared using Google Earth Pro.
The main purpose of Python is to automate the extraction and transformation of the existing geographic information.
Google Earth Pro as the GIS Preparation Tool
Google Earth Pro is used to prepare the geographic information before Python processing begins. Geographic features such as Purok areas and Path objects can be organized inside the Google Earth project.
KMZ as the Input
The completed Google Earth project is saved as a KMZ file. KMZ is a compressed geographic file that can contain KML geographic data and related resources.
In this workflow, the KMZ file serves as the main input for the Python program.
Python as the Processing Layer
Python is responsible for reading the geographic input, extracting the required information, processing the records, and preparing the final CSV output.
CSV as the Output
After processing the geographic information, Python generates a CSV file. The CSV can then be used for reporting, database import, further data processing, or integration with another application.
Overall Workflow
Google Earth Pro
↓
Prepare Purok and Path
↓
Path / LineString
↓
Export as KMZ
↓
input/
↓
code.py
↓
Read KMZ
↓
Extract KML
↓
Extract Purok and Path
↓
Extract LineString Coordinates
↓
Generate CSV
↓
output/
2. GIS Data Fundamentals
Before working with the Python processing script, it is important to understand the geographic objects used in the project.
Purok
A Purok is used as a geographic reference or administrative grouping within a barangay. In this project, Purok information is represented within the Google Earth Pro geographic data.
Path
A Path is a geographic feature created in Google Earth Pro. It can represent a route, boundary, road, connection, or other geographic line depending on how the project is designed.
LineString
In geographic data processing, a LineString represents a sequence of connected geographic coordinates that form a line.
The Path features prepared in Google Earth Pro are treated as LineString geographic data during processing.
Geographic Coordinates
A LineString is composed of geographic coordinate points. These coordinates define the shape and location of the path on the map.
Python can extract these coordinates from the geographic data and transform them into structured records.
3. Tools and Technologies
This project combines Google Earth Pro with Python automation.
Google Earth Pro
Google Earth Pro is used to create and organize the geographic features before they are processed by Python.
KMZ
KMZ is used as the geographic input file. It contains the geographic information prepared in Google Earth Pro.
Python
Python performs the automated extraction and processing of the KMZ data.
CSV
CSV is used as the generated output because it provides a simple tabular representation of the processed information.
Python Libraries
- zipfile - handles the compressed KMZ package
- xml.etree.ElementTree - reads the KML XML structure
- pandas - creates and exports tabular data
- os - handles input and output directories
4. Project Structure
The project is intentionally kept simple. The geographic KMZ file is placed inside the input directory, while the generated CSV file is stored in the output directory.
Barangay_Municipality/
│
├── input/
│ └── geographic_data.kmz
│
├── output/
│ └── generated_data.csv
│
└── code.py
Input Folder
The input folder contains the KMZ file exported from Google Earth Pro. The KMZ already contains the geographic features prepared for the project, including Purok and Path information.
Output Folder
The output folder contains the CSV generated by the Python program.
code.py
The code.py file contains the Python automation responsible for reading the KMZ, extracting the required information, processing the records, and generating the CSV output.
5. Preparing Geographic Data in Google Earth Pro
The first part of the workflow happens inside Google Earth Pro. Instead of creating the geographic information using Python, the geographic features are prepared manually in Google Earth Pro.
Preparing Purok Information
The Google Earth project contains the Purok information that will later be processed by Python.
Creating Path Features
Path features are created using the drawing tools available in Google Earth Pro. These paths contain geographic coordinates that describe the shape of the line.
Path as LineString
For Python processing, a Path can be interpreted as a LineString because it contains a sequence of connected geographic coordinate points.
Path
↓
Coordinate 1
↓
Coordinate 2
↓
Coordinate 3
↓
Coordinate 4
↓
LineString
6. Exporting the Google Earth Project as KMZ
Once the geographic features have been prepared, the Google Earth project is saved as a KMZ file.
The KMZ becomes the bridge between Google Earth Pro and the Python processing system.
Google Earth Pro
↓
Purok
+
Path / LineString
↓
Save / Export
↓
KMZ
↓
input/
The important point is that Python does not need to recreate the geographic paths. The paths have already been prepared in Google Earth Pro. Python only needs to read and process the geographic information contained in the KMZ.
7. Understanding the KMZ File
A KMZ file is a compressed package containing KML geographic information and potentially other supporting resources.
Because of this structure, Python can first access the contents of the KMZ file and then process the KML information inside it.
KMZ Processing Concept
KMZ File
↓
Extract ZIP Contents
↓
Find KML File
↓
Read XML/KML
↓
Find Geographic Features
↓
Extract Names
↓
Extract Coordinates
↓
Process Records
↓
Generate CSV
8. Python Processing
After the KMZ has been prepared, Python becomes the processing layer of the workflow.
Importing Python Libraries
import os
import zipfile
import xml.etree.ElementTree as ET
import pandas as pd
These libraries provide the basic functionality required to work with the KMZ package, read the KML XML structure, and generate the final CSV file.
Defining the Input and Output Folders
INPUT_DIR = "input"
OUTPUT_DIR = "output"
os.makedirs(OUTPUT_DIR, exist_ok=True)
The input directory is used to locate the KMZ file, while the output directory is used to store the generated CSV.
Locating the KMZ File
kmz_files = [
file
for file in os.listdir(INPUT_DIR)
if file.lower().endswith(".kmz")
]
if not kmz_files:
raise FileNotFoundError(
"No KMZ file found in the input folder."
)
kmz_path = os.path.join(
INPUT_DIR,
kmz_files[0]
)
print("KMZ file:", kmz_path)
This allows the program to automatically locate a KMZ file from the input folder.
9. Reading the KMZ File
Since KMZ is a compressed geographic package, Python can first extract its contents before reading the KML data.
TEMP_DIR = "temp_kmz"
os.makedirs(TEMP_DIR, exist_ok=True)
with zipfile.ZipFile(kmz_path, "r") as kmz:
kmz.extractall(TEMP_DIR)
After extraction, the program can search for the KML file that contains the geographic information.
Finding the KML File
kml_path = None
for root_dir, directories, files in os.walk(TEMP_DIR):
for file in files:
if file.lower().endswith(".kml"):
kml_path = os.path.join(
root_dir,
file
)
break
if kml_path:
break
if not kml_path:
raise FileNotFoundError(
"No KML file was found inside the KMZ."
)
print("KML file:", kml_path)
Searching recursively is more reliable than assuming that the KML file will always be located directly at doc.kml.
10. Reading the KML Structure
KML is an XML-based geographic format. Python can use an XML parser to inspect the elements contained inside the KML file.
tree = ET.parse(kml_path)
root = tree.getroot()
print("KML loaded successfully.")
KML Namespace
namespace = {
"kml": "http://www.opengis.net/kml/2.2"
}
The namespace allows Python to correctly locate KML elements inside the XML document.
11. Extracting Geographic Features
The next step is to identify the geographic features contained in the KML document.
KML commonly stores geographic features inside Placemark elements. A Placemark may contain a name and geographic geometry such as a Point, LineString, or Polygon.
placemarks = root.findall(
".//kml:Placemark",
namespace
)
print(
"Total Placemark records:",
len(placemarks)
)
12. Extracting Path Coordinates
Since this project uses Path features, the Python script looks for LineString geometry inside each Placemark.
linestring = placemark.find(
".//kml:LineString",
namespace
)
Once the LineString element is found, its coordinate data can be extracted.
coordinates = linestring.find(
"kml:coordinates",
namespace
)
13. Converting Coordinates into Records
A LineString can contain multiple geographic coordinates. Python can separate these coordinates and transform them into structured records.
coordinate_text = coordinates.text.strip()
coordinate_list = []
for coordinate in coordinate_text.split():
values = coordinate.split(",")
longitude = values[0]
latitude = values[1]
coordinate_list.append({
"Longitude": longitude,
"Latitude": latitude
})
14. Combining Purok and Path Information
One important part of the workflow is preserving the identifying information associated with each geographic feature.
Depending on how the Google Earth project is organized, the name of the Placemark or its parent folder can be used to identify the Purok and Path information.
record = {
"Purok": purok_name,
"Path": path_name,
"Longitude": longitude,
"Latitude": latitude
}
The exact fields can be adjusted according to the structure of the KMZ and the requirements of the generated CSV.
15. Generating the CSV
After all geographic records have been extracted and processed, Python can convert them into a pandas DataFrame.
df = pd.DataFrame(records)
print(df.head())
Saving the CSV
output_path = os.path.join(
OUTPUT_DIR,
"generated_data.csv"
)
df.to_csv(
output_path,
index=False
)
print(
"CSV generated successfully:",
output_path
)
16. Complete Processing Concept
The entire Python operation can be understood as a sequence of extraction and transformation steps.
KMZ
↓
Extract
↓
KML
↓
Placemark
↓
Purok / Path
↓
LineString
↓
Coordinates
↓
Structured Records
↓
Pandas DataFrame
↓
CSV
This is the core concept behind the automation. Google Earth Pro is responsible for preparing the geographic information, while Python is responsible for transforming that information into structured data.
17. Project Architecture
The project can be summarized using the following architecture:
Barangay_Municipality/
│
├── input/
│ └── geographic_data.kmz
│
├── output/
│ └── generated_data.csv
│
└── code.py
The input folder contains the geographic source, the Python script performs the processing, and the output folder stores the generated tabular result.
18. Python GIS Automation Workflow
Google Earth Pro
↓
Prepare Purok
↓
Create Path
↓
Path / LineString
↓
Export KMZ
↓
input/
↓
code.py
↓
Extract KMZ
↓
Read KML
↓
Extract Placemark
↓
Identify Purok / Path
↓
Extract LineString
↓
Extract Coordinates
↓
Create DataFrame
↓
output/
↓
CSV
19. Why Use Python Automation?
Without automation, geographic information may need to be manually copied from the Google Earth project into a spreadsheet or database. This becomes inefficient when the number of geographic features increases.
Python allows the same processing logic to be executed repeatedly. Once the script has been properly configured, a new KMZ file can be processed using the same workflow.
Manual Approach
Google Earth Pro
↓
Inspect Geographic Data
↓
Manually Copy Data
↓
Create Spreadsheet
↓
Save CSV
Automated Approach
Google Earth Pro
↓
Export KMZ
↓
input/
↓
Run code.py
↓
Extract Geographic Data
↓
Generate CSV
↓
output/
20. Practical Use Case
This type of workflow can be useful when geographic information is maintained in Google Earth Pro but needs to be transferred into a structured dataset for another application.
For example, a barangay or municipality project may maintain Purok and Path information inside a geographic project. Python can extract that information and produce a CSV that can later be imported into a database, profiling system, GIS application, reporting system, or other software.
Possible Data Flow
Google Earth Pro
↓
KMZ
↓
Python Processing
↓
CSV
↓
Database
↓
Application
↓
GIS Map
21. Common Problems
KMZ File Not Found
The Python program must be able to locate the KMZ file inside the input directory. Check that the file exists and uses the .kmz extension.
KML File Not Found
A KMZ normally contains KML data, but the internal structure can vary. The script therefore searches the extracted directory for a KML file.
LineString Not Found
If the Python script cannot find a LineString, the corresponding geographic feature may not contain a Path or may use a different geometry type.
Missing Coordinates
A Path without usable coordinate information cannot be converted into geographic coordinate records.
Incorrect Feature Names
If the extraction process depends on names or folders inside Google Earth Pro, inconsistent naming can affect the resulting CSV.
22. Improving the Python Script
Once the basic extraction process works, the Python program can be improved with additional validation and automation.
- Automatically detect the KMZ file
- Automatically locate the KML file
- Validate geographic features
- Detect missing LineString data
- Validate coordinate values
- Generate processing reports
- Process multiple KMZ files
- Create standardized CSV output
- Log processing errors
- Automatically organize generated files
23. Batch Processing
The same architecture can eventually be expanded to process multiple geographic files.
input/
│
├── barangay_01.kmz
├── barangay_02.kmz
├── barangay_03.kmz
└── barangay_04.kmz
↓
Python Processing
↓
output/
│
├── barangay_01.csv
├── barangay_02.csv
├── barangay_03.csv
└── barangay_04.csv
24. From Python Script to GIS System
A simple KMZ-to-CSV script can eventually become part of a larger geographic information system.
Google Earth Pro
↓
KMZ
↓
Python GIS Processor
↓
CSV
↓
Database
↓
GIS Application
↓
Web Map / Dashboard
The Python processing layer can therefore act as a bridge between manually prepared geographic information and an automated information system.
25. Developer Lessons
This project demonstrates several important programming and GIS concepts beyond simply reading a geographic file.
- File processing
- GIS data extraction
- XML parsing
- KMZ processing
- Coordinate processing
- Data transformation
- CSV generation
- Automation
- Data validation
- Pipeline design
The main programming concept is to transform an existing geographic data source into a structured dataset that can be consumed by other systems.
26. Final GIS Automation Pipeline
Google Earth Pro
│
▼
Purok + Path
│
▼
Path / LineString
│
▼
KMZ
│
▼
input/
│
▼
code.py
│
├── Find KMZ
│
├── Extract KMZ
│
├── Find KML
│
├── Read KML
│
├── Find Placemark
│
├── Identify Purok
│
├── Identify Path
│
└── Extract Coordinates
│
▼
Pandas DataFrame
│
▼
output/
│
▼
CSV
Conclusion
Python can be used as an automation layer between Google Earth Pro and structured data processing. Instead of manually transferring geographic information from a Google Earth project into a spreadsheet, the geographic data can be exported as KMZ and processed programmatically.
In this workflow, Google Earth Pro is responsible for preparing the geographic information, including Purok and Path features. The Path information is represented as LineString geographic data inside the KML structure contained by the KMZ file.
Python then reads the KMZ, accesses the KML data, identifies the relevant geographic features, extracts the coordinates, organizes the information into structured records, and generates a CSV output.
The overall process can therefore be summarized as:
Google Earth Pro
↓
Purok + Path
↓
LineString
↓
KMZ
↓
input/
↓
Python
↓
GIS Data Extraction
↓
output/
↓
CSV
📖 2,543 Words