Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiGLE Cellular .csv to .kml #6

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
D14b0l1c committed Apr 7, 2024
commit b477aa8cc283ed66bc798434d2bb0cb4d47a690e
39 changes: 39 additions & 0 deletions Cellular-csvtokml.py
Original file line number Diff line number Diff line change
@@ -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('<?xml version="1.0" encoding="UTF-8"?>\n')
kmlfile.write('<kml xmlns="http://www.opengis.net/kml/2.2">\n')
kmlfile.write('<Document>\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('<Placemark>\n')
kmlfile.write('<name>{}</name>\n'.format(network_id))
kmlfile.write('<description>ID: {}</description>\n'.format(network_id))
kmlfile.write('<Point>\n')
kmlfile.write('<coordinates>{},{},0</coordinates>\n'.format(lon, lat))
kmlfile.write('</Point>\n')
kmlfile.write('</Placemark>\n')
kmlfile.write('</Document>\n')
kmlfile.write('</kml>\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)