-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3
105 lines (90 loc) · 3.82 KB
/
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import simplekml
import math
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut, GeocoderServiceError
def geocode_address(address):
geolocator = Nominatim(user_agent="my_kml_generator")
try:
location = geolocator.geocode(address)
if location:
return location.latitude, location.longitude
else:
print("Address not found. Please try again.")
return None
except (GeocoderTimedOut, GeocoderServiceError):
print("Geocoding service timed out or error occurred. Please try again.")
return None
def generate_circle_coords(center, radius, num_points=36):
lat, lon = center
coords = []
for i in range(num_points):
angle = math.radians(float(i) / num_points * 360.0)
dx = radius * math.cos(angle)
dy = radius * math.sin(angle)
point_lat = lat + (dy / 111111) # 1 degree of latitude = 111111 meters
point_lon = lon + (dx / (111111 * math.cos(math.radians(lat))))
coords.append((point_lon, point_lat)) # Note the order: lon, lat
coords.append(coords[0]) # Close the polygon
return coords
def generate_square_coords(center, side_length):
lat, lon = center
half_side = side_length / 2
d_lat = half_side / 111111
d_lon = half_side / (111111 * math.cos(math.radians(lat)))
return [
(lon - d_lon, lat + d_lat),
(lon + d_lon, lat + d_lat),
(lon + d_lon, lat - d_lat),
(lon - d_lon, lat - d_lat),
(lon - d_lon, lat + d_lat) # Close the polygon
]
def generate_triangle_coords(center, side_length):
lat, lon = center
height = side_length * (math.sqrt(3) / 2)
d_lat = height / (3 * 111111)
d_lon = side_length / (2 * 111111 * math.cos(math.radians(lat)))
return [
(lon, lat + 2*d_lat),
(lon - d_lon, lat - d_lat),
(lon + d_lon, lat - d_lat),
(lon, lat + 2*d_lat) # Close the polygon
]
def create_polygon_kml(polygon_type, center, size, output_file):
kml = simplekml.Kml()
pol = kml.newpolygon(name=f"My {polygon_type.capitalize()}")
if polygon_type == "circle":
coords = generate_circle_coords(center, size)
elif polygon_type == "square":
coords = generate_square_coords(center, size)
elif polygon_type == "triangle":
coords = generate_triangle_coords(center, size)
else:
raise ValueError("Invalid polygon type. Choose 'circle', 'square', or 'triangle'.")
pol.outerboundaryis = coords
pol.style.linestyle.color = simplekml.Color.blue
pol.style.linestyle.width = 2
pol.style.polystyle.color = simplekml.Color.changealphaint(100, simplekml.Color.green)
# Add a LookAt view to ensure Google Earth focuses on the polygon
lookat = simplekml.LookAt()
lookat.longitude, lookat.latitude = center[1], center[0] # Note the order: lon, lat
lookat.range = size * 4 # Adjust the view to show an area 4 times the size of the polygon
lookat.heading = 0
lookat.tilt = 0
pol.lookat = lookat
kml.save(output_file)
print(f"KML file '{output_file}' has been created.")
def main():
address = input("Enter a street address: ")
center = geocode_address(address)
if center:
print(f"Coordinates: {center}")
while True:
polygon_type = input("Enter polygon type (circle, square, or triangle): ").lower()
if polygon_type in ["circle", "square", "triangle"]:
break
print("Invalid polygon type. Please choose circle, square, or triangle.")
size = float(input("Enter size in meters (radius for circle, side length for square/triangle): "))
output_file = f"{polygon_type}_at_{address.replace(' ', '_')}.kml"
create_polygon_kml(polygon_type, center, size, output_file)
if __name__ == "__main__":
main()