From b477aa8cc283ed66bc798434d2bb0cb4d47a690e Mon Sep 17 00:00:00 2001 From: D14b0l1c <66981132+D14b0l1c@users.noreply.github.com> Date: Sat, 6 Apr 2024 18:23:47 -0700 Subject: [PATCH] WiGLE Cellular .csv to .kml converter Description: This Python script converts CSV files to KML (Keyhole Markup Language) files, which are commonly used for geographic visualization, particularly with mapping applications like Google Maps. It extracts specific columns from the CSV file (trilat, trilong, and id) and creates Placemarks in the KML file based on this information. The resulting KML file can be imported into mapping software for visualization and analysis. Libraries used: - `os`: Provides functions for interacting with the operating system, used here to list files in a directory. - `csv`: Allows reading and writing CSV files, utilized to parse the input CSV file. How to use: 1. Specify the directory containing your CSV files in the `input_directory` variable. 2. Run the script, which will iterate through all CSV files in the specified directory. 3. For each CSV file, it will create a corresponding KML file with Placemarks based on the specified columns. Note: - Make sure to adjust the `input_directory` variable to point to the correct directory containing your CSV files before running the script. --- Cellular-csvtokml.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Cellular-csvtokml.py diff --git a/Cellular-csvtokml.py b/Cellular-csvtokml.py new file mode 100644 index 0000000..d16fb1a --- /dev/null +++ b/Cellular-csvtokml.py @@ -0,0 +1,39 @@ +import os +import csv + +def csv_to_kml(csv_file, kml_file): + # Open CSV file for reading + with open(csv_file, 'r') as csvfile: + csvreader = csv.DictReader(csvfile) + # Open KML file for writing + with open(kml_file, 'w') as kmlfile: + kmlfile.write('\n') + kmlfile.write('\n') + kmlfile.write('\n') + for row in csvreader: + # Extract relevant columns + lat, lon = row['trilat'], row['trilong'] + network_id = row['id'] + # Write Placemark with relevant information + kmlfile.write('\n') + kmlfile.write('{}\n'.format(network_id)) + kmlfile.write('ID: {}\n'.format(network_id)) + kmlfile.write('\n') + kmlfile.write('{},{},0\n'.format(lon, lat)) + kmlfile.write('\n') + kmlfile.write('\n') + kmlfile.write('\n') + kmlfile.write('\n') + +def convert_csv_files_to_kml(input_dir): + # Iterate over files in the input directory + for file_name in os.listdir(input_dir): + if file_name.endswith('.csv'): + csv_file = os.path.join(input_dir, file_name) + kml_file = os.path.join(input_dir, os.path.splitext(file_name)[0] + '.kml') + csv_to_kml(csv_file, kml_file) + +# Specify the directory containing your CSV files +input_directory = r'YOUR DIRECTORY' + +convert_csv_files_to_kml(input_directory) \ No newline at end of file