First, ensure you have the necessary libraries installed:
pip install geopandas
Now, use the following function:
import zipfile
import os
def unzip_file(file_path, output_dir):
"""
Unzips KMZ and shapefiles to a specified directory.
Parameters:
- file_path (str): Path to the KMZ or shapefile ZIP.
- output_dir (str): Directory where the unzipped files will be saved.
Returns:
- list of unzipped file paths
"""
# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Open the KMZ/ZIP file
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(output_dir)
# Return a list of the unzipped file paths
return [os.path.join(output_dir, name) for name in zip_ref.namelist()]
# Example Usage:
file_path = "path_to_your_file.kmz_or_zip"
output_directory = "path_where_you_want_to_unzip"
unzipped_files = unzip_file(file_path, output_directory)
print(unzipped_files)
For KMZ files, after unzipping, you'll typically get a KML file (along with possibly some other assets). For zipped shapefiles, you'll usually get multiple files with extensions like .shp, .shx, .dbf, and possibly others.
This function will unzip both KMZ and ZIP files containing shapefiles and return a list of the paths to the unzipped files. Adjust as necessary to fit your specific requirements!
First, ensure you have the necessary libraries installed:
Now, use the following function:
For KMZ files, after unzipping, you'll typically get a KML file (along with possibly some other assets). For zipped shapefiles, you'll usually get multiple files with extensions like
.shp
,.shx
,.dbf
, and possibly others.This function will unzip both KMZ and ZIP files containing shapefiles and return a list of the paths to the unzipped files. Adjust as necessary to fit your specific requirements!