-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
247 lines (182 loc) · 7.98 KB
/
Copy pathapp.py
File metadata and controls
247 lines (182 loc) · 7.98 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""Flask entry point exposing the plan generation endpoints.
Each plan endpoint accepts a JSON payload (see ``models.plan.PlanProps``),
generates the drawing, and responds with the URL of the uploaded DXF/DWG/PDF
bundle.
``/cad/inspect`` is the odd one out: it takes an uploaded legacy DWG/DXF and
reports what survey data can be recovered from it (Task 11).
"""
import json
import logging
import os
import tempfile
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
from dotenv import load_dotenv
load_dotenv()
from flask import Flask, jsonify, request
from pydantic import ValidationError
from werkzeug.utils import secure_filename
from cad_import import CadImportError, inspect_drawing
from point_stream import PointStreamError, read_plan_stream
from progress import JobProgress
from plans import CadastralPlan, LayoutPlan, RoutePlan, TopographicPlan
logging.basicConfig(level=logging.INFO)
# ezdxf logs an INFO line for every entity on a hidden layer (e.g. the TIN
# mesh when show_mesh is off) — hundreds of lines per render. Warnings only.
logging.getLogger("ezdxf").setLevel(logging.WARNING)
app = Flask(__name__)
#: Upload ceiling for legacy drawings. A survey DWG is normally well under a
#: megabyte; this leaves room for a drawing carrying scanned raster images
#: without letting the service be used as a file dump.
MAX_UPLOAD_BYTES = 32 * 1024 * 1024
app.config["MAX_CONTENT_LENGTH"] = MAX_UPLOAD_BYTES
ALLOWED_CAD_EXTENSIONS = (".dwg", ".dxf")
#: Content type used to stream a plan and its points together. See
#: ``point_stream`` for why a large survey cannot arrive as one JSON body.
NDJSON_CONTENT_TYPE = "application/x-ndjson"
#: Points for a background job are fetched from object storage; a large export
#: over a slow link should not be mistaken for a hang.
POINTS_FETCH_TIMEOUT = 300
def read_plan_request():
"""The plan payload for this request, however it was sent.
Three shapes, smallest first:
* ordinary JSON -- a plan whose points fit comfortably in a request body;
* NDJSON -- the plan on the first line then one point per line, read
incrementally so a million-point survey costs the same memory as a
thousand-point one;
* a JSON envelope carrying ``points_url`` -- the background-job path,
where the API has written the same NDJSON to object storage and the
engine streams it back. Passing by reference is what lets a queued job
outlive the worker that prepared it.
Returns ``(plan, progress)``.
"""
content_type = (request.content_type or "").split(";")[0].strip().lower()
if content_type == NDJSON_CONTENT_TYPE:
plan, stats = read_plan_stream(request.stream)
app.logger.info("streamed plan: %s", stats)
return plan, JobProgress(None)
data = request.get_json(silent=True)
if not isinstance(data, dict):
raise PointStreamError("Request body must be a JSON object")
points_url = data.get("points_url")
if not points_url:
return data, JobProgress(data.get("job_id"))
progress = JobProgress(data.get("job_id"))
progress.stage("reading survey points", fraction=0.0)
try:
with urlopen(points_url, timeout=POINTS_FETCH_TIMEOUT) as response:
plan, stats = read_plan_stream(
response, on_progress=progress.counter("reading survey points"),
)
except (URLError, HTTPError) as exc:
raise PointStreamError(f"Could not fetch the survey points: {exc}") from exc
app.logger.info("fetched plan points: %s", stats)
progress.stage("reading survey points", fraction=0.15,
processed=stats["coordinates_received"],
total=stats["coordinates_received"])
return plan, progress
def generate_plan(plan_cls, plan_label: str):
"""Validate the request payload, generate the plan, and upload it."""
try:
data, progress = read_plan_request()
except PointStreamError as e:
return jsonify({"error": str(e)}), 400
try:
plan = plan_cls(**data)
except ValidationError as e:
return jsonify({
"error": "Invalid plan data",
"details": json.loads(e.json(include_url=False)),
}), 400
except ValueError as e:
return jsonify({"error": str(e)}), 400
progress.stage("drawing the plan", fraction=0.25)
plan.draw()
progress.stage("exporting DXF, DWG and PDF", fraction=0.75)
url = plan.save()
return jsonify({
"message": f"{plan_label} plan generated",
"filename": plan.name,
"url": url,
}), 200
@app.get("/")
def home():
return jsonify({"service": "survey-plan-generator", "status": "ok"})
@app.get("/health")
def health():
return jsonify({"status": "ok"})
@app.post("/cadastral/plan")
def generate_cadastral_plan():
return generate_plan(CadastralPlan, "Cadastral")
@app.post("/topographic/plan")
def generate_topographic_plan():
return generate_plan(TopographicPlan, "Topographic")
@app.post("/layout/plan")
def generate_layout_plan():
return generate_plan(LayoutPlan, "Layout")
@app.post("/route/plan")
def generate_route_plan():
return generate_plan(RoutePlan, "Route")
@app.post("/cad/inspect")
def inspect_cad_upload():
"""Report the survey data recoverable from an uploaded DWG/DXF.
Returns every closed shape found, with its layer, area and vertices, plus
the point features, labels, detected units and coordinate extent, so the
caller can present the choice rather than the service guessing which
shape is the boundary.
Optional form fields:
``units`` ``$INSUNITS`` code overriding the drawing's own units.
``ring_id`` when given, the response also carries ``coordinates``: that
ring turned into a plan coordinate register, ready to feed
the existing pipeline.
"""
upload = request.files.get("file")
if upload is None or not upload.filename:
return jsonify({"error": "No file was uploaded"}), 400
filename = secure_filename(upload.filename)
extension = os.path.splitext(filename)[1].lower()
if extension not in ALLOWED_CAD_EXTENSIONS:
return jsonify({
"error": "Unsupported file type. Upload a DWG or DXF drawing."
}), 400
units_override = None
raw_units = (request.form.get("units") or "").strip()
if raw_units:
try:
units_override = int(raw_units)
except ValueError:
return jsonify({"error": "units must be a DXF $INSUNITS code"}), 400
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, filename)
upload.save(path)
try:
inspection = inspect_drawing(path, file_name=filename,
units_override=units_override)
except CadImportError as exc:
return jsonify({"error": str(exc)}), 400
payload = inspection.model_dump(mode="json")
ring_id = (request.form.get("ring_id") or "").strip()
if ring_id:
ring = next((r for r in inspection.rings if r.id == ring_id), None)
if ring is None:
return jsonify({"error": f"No shape with id '{ring_id}' in this drawing"}), 400
payload["coordinates"] = [s.model_dump(mode="json") for s in ring.coordinates]
payload["selected_ring_id"] = ring_id
return jsonify(payload), 200
@app.errorhandler(413)
def payload_too_large(e):
limit_mb = MAX_UPLOAD_BYTES // (1024 * 1024)
return jsonify({"error": f"The file is too large; the limit is {limit_mb} MB"}), 413
@app.errorhandler(404)
def not_found(e):
return jsonify({"error": "Resource not found"}), 404
@app.errorhandler(500)
def internal_error(e):
return jsonify({"error": "Something went wrong on our side"}), 500
@app.errorhandler(Exception)
def handle_exception(e):
app.logger.error("Unhandled exception: %s", e, exc_info=True)
return jsonify({"error": "An unexpected error occurred"}), 500
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
app.run(host="0.0.0.0", port=port)