|
To remove non-intersecting circles and display a warning, we need to calculate whether each circle intersects with any other circle. Here's an updated version of the code that includes this functionality: |
|
|
|
Updated Code |
|
|
|
import tkinter as tk |
|
from tkinter import filedialog, messagebox |
|
import simplekml |
|
from math import sin, cos, radians, sqrt |
|
|
|
def add_coordinate(): |
|
if len(coordinates) < 10: |
|
lat = lat_entry.get() |
|
lon = lon_entry.get() |
|
diameter = diameter_entry.get() |
|
|
|
if not lat or not lon or not diameter: |
|
messagebox.showerror("Error", "Please fill in all fields.") |
|
return |
|
|
|
try: |
|
lat = float(lat) |
|
lon = float(lon) |
|
diameter = float(diameter) |
|
except ValueError: |
|
messagebox.showerror("Error", "Latitude, Longitude, and Diameter must be numeric.") |
|
return |
|
|
|
coordinates.append((lat, lon, diameter)) |
|
listbox.insert(tk.END, f"Lat: {lat}, Lon: {lon}, Diameter: {diameter}m") |
|
lat_entry.delete(0, tk.END) |
|
lon_entry.delete(0, tk.END) |
|
diameter_entry.delete(0, tk.END) |
|
else: |
|
messagebox.showerror("Error", "You can add up to 10 coordinates.") |
|
|
|
def remove_coordinate(): |
|
selected = listbox.curselection() |
|
if selected: |
|
listbox.delete(selected) |
|
del coordinates[selected[0]] |
|
else: |
|
messagebox.showerror("Error", "Please select a coordinate to remove.") |
|
|
|
def export_kml(): |
|
if not coordinates: |
|
messagebox.showerror("Error", "No coordinates to export.") |
|
return |
|
|
|
non_intersecting = remove_non_intersecting_circles() |
|
|
|
if not coordinates: |
|
messagebox.showerror("Error", "No intersecting circles remain.") |
|
return |
|
|
|
if non_intersecting: |
|
messagebox.showwarning( |
|
"Warning", |
|
f"{len(non_intersecting)} non-intersecting circles were removed." |
|
) |
|
|
|
file_path = filedialog.asksaveasfilename( |
|
defaultextension=".kml", |
|
filetypes=[("KML files", "*.kml")] |
|
) |
|
if not file_path: |
|
return |
|
|
|
kml = simplekml.Kml() |
|
|
|
for lat, lon, diameter in coordinates: |
|
circle = kml.newpolygon(name=f"Circle at {lat},{lon}") |
|
radius = diameter / 2 / 1000 # Convert diameter to radius in km |
|
circle.outerboundaryis = generate_circle(lat, lon, radius) |
|
circle.innerboundaryis = [] |
|
circle.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.red) |
|
circle.style.polystyle.fill = 1 |
|
|
|
kml.save(file_path) |
|
messagebox.showinfo("Success", f"KML file saved to {file_path}.") |
|
|
|
def remove_non_intersecting_circles(): |
|
non_intersecting = [] |
|
for i, (lat1, lon1, diameter1) in enumerate(coordinates): |
|
radius1 = diameter1 / 2 / 1000 # Radius in km |
|
intersecting = False |
|
for j, (lat2, lon2, diameter2) in enumerate(coordinates): |
|
if i != j: |
|
radius2 = diameter2 / 2 / 1000 # Radius in km |
|
if circles_intersect(lat1, lon1, radius1, lat2, lon2, radius2): |
|
intersecting = True |
|
break |
|
if not intersecting: |
|
non_intersecting.append((lat1, lon1, diameter1)) |
|
|
|
for circle in non_intersecting: |
|
coordinates.remove(circle) |
|
listbox.delete(0, tk.END) |
|
for coord in coordinates: |
|
listbox.insert(tk.END, f"Lat: {coord[0]}, Lon: {coord[1]}, Diameter: {coord[2]}m") |
|
|
|
return non_intersecting |
|
|
|
def circles_intersect(lat1, lon1, radius1, lat2, lon2, radius2): |
|
distance = haversine(lat1, lon1, lat2, lon2) |
|
return distance <= (radius1 + radius2) |
|
|
|
def haversine(lat1, lon1, lat2, lon2): |
|
R = 6371 # Earth radius in km |
|
dlat = radians(lat2 - lat1) |
|
dlon = radians(lon2 - lon1) |
|
a = sin(dlat / 2) ** 2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon / 2) ** 2 |
|
return 2 * R * atan2(sqrt(a), sqrt(1 - a)) |
|
|
|
def generate_circle(lat, lon, radius, segments=36): |
|
coords = [] |
|
for i in range(segments + 1): |
|
angle = radians(i * (360 / segments)) |
|
dlat = radius * cos(angle) / 111 # Approximation for 1 degree latitude |
|
dlon = radius * sin(angle) / (111 * cos(radians(lat))) |
|
coords.append((lon + dlon, lat + dlat)) |
|
return coords |
|
|
|
# GUI setup |
|
root = tk.Tk() |
|
root.title("Google Earth Circle Drawer") |
|
coordinates = [] |
|
|
|
frame = tk.Frame(root) |
|
frame.pack(padx=10, pady=10) |
|
|
|
tk.Label(frame, text="Latitude:").grid(row=0, column=0, padx=5, pady=5) |
|
lat_entry = tk.Entry(frame) |
|
lat_entry.grid(row=0, column=1, padx=5, pady=5) |
|
|
|
tk.Label(frame, text="Longitude:").grid(row=1, column=0, padx=5, pady=5) |
|
lon_entry = tk.Entry(frame) |
|
lon_entry.grid(row=1, column=1, padx=5, pady=5) |
|
|
|
tk.Label(frame, text="Diameter (m):").grid(row=2, column=0, padx=5, pady=5) |
|
diameter_entry = tk.Entry(frame) |
|
diameter_entry.grid(row=2, column=1, padx=5, pady=5 |
|
|