-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·72 lines (55 loc) · 2.93 KB
/
main.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
#!/usr/bin/env python3.7
from pydantic import BaseModel
from grass_engine import grass, GrassEngineException, cleanup_grass_context
import uvicorn
import os
import json
from typing import Optional
from fastapi import FastAPI
from tasks import add, execute_grass_script
from geojson import Feature, Point, FeatureCollection
app = FastAPI()
class Grass_Data(BaseModel):
polygon_geojson: dict
area: int
point_geojson: dict
@app.get("/")
async def read_root():
a = 'test_ping_successful'
return {"status": a}
@app.get("/add/{a}/{b}")
def adds(a: int, b: int):
return {'task_id': add.delay(a, b).task_id}
@app.post("/calcVolume/{project_name}")
def calc_grass_volume(project_name: str, payload: Grass_Data):
try:
# grass_data = json.loads(data.data.replace("\\", r"\\")).decode('utf-8')
area = payload.polygon_geojson
points = payload.point_geojson
dsm = os.path.abspath('./files/dsm-' + project_name + '.tif')
except Exception as e:
return {'error': str(e)}
try:
context = grass.create_context({'auto_cleanup': False})
context.add_file('area_file.geojson', json.dumps(area))
context.add_file('points_file.geojson', str(points))
context.add_param('dsm_file', dsm)
context.set_location(dsm)
celery_task_id = execute_grass_script.delay(os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"calc_volume.py"
), context.serialize()).task_id
# data = execute_grass_script(os.path.join(
# os.path.dirname(os.path.abspath(__file__)),
# "calc_volume.py"
# ), context.serialize())
return {'celery_task_id': celery_task_id}
except GrassEngineException as e:
return {'error': str(e)}
return {"project_name": project_name, 'points': points, 'area': area, 'dsm': dsm}
if __name__ == "__main__":
uvicorn.run("main:app", host="localhost", reload=True,
port=5000, log_level="info")
# {
# "data": "{"polygon_geojson":{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[77.56791878658811,13.111361044641356],[77.5679393868565,13.11131937483033],[77.56799960302692,13.111339438073003],[77.5679885105755,13.111382651206526],[77.56791878658811,13.111361044641356]]]}},"area":37.26398530864275,"point_geojson":{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[77.56791878658811,13.111361044641356]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[77.5679393868565,13.11131937483033]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[77.56799960302692,13.111339438073003]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[77.5679885105755,13.111382651206526]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[77.56791878658811,13.111361044641356]}}]}}"
# }