-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
85 lines (76 loc) · 3 KB
/
app.py
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
from flask import Flask, request, jsonify, render_template
import geopandas as gpd
from shapely.geometry import Polygon, Point
import pandas as pd
import numpy as np
app = Flask(__name__)
def generate_rectilinear_path(polygon, spacing=10, altitude=50, speed=5, camera_action='none'):
minx, miny, maxx, maxy = polygon.bounds
x_coords = np.arange(minx, maxx, spacing)
y_coords = np.arange(miny, maxy, spacing)
waypoints = []
for y in y_coords:
line = [Point(x, y) for x in x_coords]
for point in line:
if polygon.contains(point):
waypoints.append((point.x, point.y, altitude, speed, camera_action))
return waypoints
def generate_kml(waypoints):
kml_template = """
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:wpml="http://www.dji.com/wpmz/1.0.2">
<Document>
<wpml:missionConfig>
<wpml:flyToWaylineMode>safely</wpml:flyToWaylineMode>
<wpml:finishAction>goHome</wpml:finishAction>
<wpml:exitOnRCLost>executeLostAction</wpml:exitOnRCLost>
<wpml:executeRCLostAction>goBack</wpml:executeRCLostAction>
<wpml:globalTransitionalSpeed>2.5</wpml:globalTransitionalSpeed>
<wpml:droneInfo>
<wpml:droneEnumValue>68</wpml:droneEnumValue>
<wpml:droneSubEnumValue>0</wpml:droneSubEnumValue>
</wpml:droneInfo>
</wpml:missionConfig>
<Folder>
<wpml:templateId>0</wpml:templateId>
<wpml:executeHeightMode>relativeToStartPoint</wpml:executeHeightMode>
<wpml:waylineId>0</wpml:waylineId>
<wpml:distance>0</wpml:distance>
<wpml:duration>0</wpml:duration>
<wpml:autoFlightSpeed>2.5</wpml:autoFlightSpeed>
{}
</Folder>
</Document>
</kml>
"""
placemarks = ""
for i, waypoint in enumerate(waypoints):
placemarks += f"""
<Placemark>
<Point>
<coordinates>{waypoint[0]},{waypoint[1]}</coordinates>
</Point>
<wpml:index>{i}</wpml:index>
<wpml:executeHeight>{waypoint[2]}</wpml:executeHeight>
<wpml:waypointSpeed>{waypoint[3]}</wpml:waypointSpeed>
<wpml:cameraAction>{waypoint[4]}</wpml:cameraAction>
</Placemark>
"""
return kml_template.format(placemarks)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate', methods=['POST'])
def generate():
data = request.get_json()
geojson = data['geojson']
altitude = data['altitude']
speed = data['speed']
camera_action = data['cameraAction']
gdf = gpd.GeoDataFrame.from_features(geojson['features'])
polygon = gdf.iloc[0].geometry
waypoints = generate_rectilinear_path(polygon, spacing=0.0001, altitude=altitude, speed=speed, camera_action=camera_action) # Adjust spacing as needed
kml_content = generate_kml(waypoints)
return jsonify({'kml': kml_content})
if __name__ == '__main__':
app.run(debug=True)