kopia lustrzana https://github.com/OpenDroneMap/WebODM
Contours script rewrite
rodzic
2fce053f24
commit
b542baca16
|
@ -42,7 +42,7 @@ class TaskContoursGenerate(TaskView):
|
|||
|
||||
celery_task_id = execute_grass_script.delay(os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"calc_contours.grass"
|
||||
"calc_contours.py"
|
||||
), context.serialize(), 'file').task_id
|
||||
|
||||
return Response({'celery_task_id': celery_task_id}, status=status.HTTP_200_OK)
|
||||
|
|
|
@ -40,8 +40,12 @@
|
|||
# output: If successful, prints the full path to the contours file. Otherwise it prints "error"
|
||||
|
||||
import sys
|
||||
import glob
|
||||
import os
|
||||
import shutil
|
||||
from grass.pygrass.modules import Module
|
||||
import grass.script as grass
|
||||
import subprocess
|
||||
|
||||
def main():
|
||||
ext = ""
|
||||
|
@ -58,38 +62,32 @@ def main():
|
|||
Module("r.external", input=opts['dem_file'], output="dem", overwrite=True)
|
||||
Module("g.region", raster="dem")
|
||||
Module("r.contour", input="dem", output="contours", step=opts["interval"], overwrite=True)
|
||||
Module("r.generalize", input="contours", output="contours_smooth", method="douglas", threshold=opts["simplify"], overwrite=True)
|
||||
Module("r.generalize", input="contours_smooth", output="contours_simplified", method="chaiken", threshold=1, overwrite=True)
|
||||
Module("r.generalize", input="contours_simplified", output="contours_final", method="douglas", threshold=opts["simplify"], overwrite=True)
|
||||
Module("v.edit", input="contours_final", tool="delete", threshold="-1,0,-%s" % MIN_CONTOUR_LENGTH, query="length")
|
||||
Module("v.generalize", input="contours", output="contours_smooth", method="douglas", threshold=opts["simplify"], overwrite=True)
|
||||
Module("v.generalize", input="contours_smooth", output="contours_simplified", method="chaiken", threshold=1, overwrite=True)
|
||||
Module("v.generalize", input="contours_simplified", output="contours_final", method="douglas", threshold=opts["simplify"], overwrite=True)
|
||||
Module("v.edit", map="contours_final", tool="delete", threshold=[-1,0,-MIN_CONTOUR_LENGTH], query="length")
|
||||
Module("v.out.ogr", input="contours_final", output="temp.gpkg", format="GPKG")
|
||||
|
||||
subprocess.check_call(["ogr2ogr", "-t_srs", "EPSG:%s" % opts['epsg'],
|
||||
'-overwrite', '-f', opts["format"], "output.%s" % ext, "temp.gpkg"], stdout=subprocess.DEVNULL)
|
||||
|
||||
if os.path.isfile("output.%s" % ext):
|
||||
if opts["format"] == "ESRI Shapefile":
|
||||
ext="zip"
|
||||
os.makedirs("contours")
|
||||
contour_files = glob.glob("output.*")
|
||||
for cf in contour_files:
|
||||
shutil.move(cf, os.path.join("contours", os.path.basename(cf)))
|
||||
|
||||
shutil.make_archive('output', 'zip', 'contours/')
|
||||
|
||||
print(os.path.join(os.getcwd(), "output.%s" % ext))
|
||||
else:
|
||||
print("error")
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
opts, _ = grass.parser()
|
||||
sys.exit(main())
|
||||
|
||||
|
||||
# TODO
|
||||
# Running external commands from Python
|
||||
# For information on running external commands from Python, see: http://docs.python.org/lib/module-subprocess.html
|
||||
|
||||
# Avoid using the older os.* functions. Section 17.1.3 lists equivalents using the Popen() interface, which is more robust (particularly on Windows).
|
||||
#
|
||||
# ogr2ogr -t_srs EPSG:${epsg} -overwrite -f "${format}" output.$$ext temp.gpkg > /dev/null
|
||||
#
|
||||
# if [ -e "output.$$ext" ]; then
|
||||
# # ESRI ShapeFile extra steps to compress into a zip archive
|
||||
# # we leverage Python's shutil in this case
|
||||
# if [ "${format}" = "ESRI Shapefile" ]; then
|
||||
# ext="zip"
|
||||
# mkdir contours/
|
||||
# mv output* contours/
|
||||
# echo "import shutil;shutil.make_archive('output', 'zip', 'contours/')" | python
|
||||
# fi
|
||||
#
|
||||
# echo "$$(pwd)/output.$$ext"
|
||||
# else
|
||||
# echo "error"
|
||||
# fi
|
||||
|
|
Ładowanie…
Reference in New Issue