diff --git a/plugins/contours/calc_contours.grass b/plugins/contours/calc_contours.grass deleted file mode 100755 index 870f94bd..00000000 --- a/plugins/contours/calc_contours.grass +++ /dev/null @@ -1,50 +0,0 @@ -# dem_file: GeoTIFF DEM containing the surface to calculate contours -# interval: Contours interval -# format: OGR output format -# simplify: Simplify value -# epsg: target EPSG code -# -# ------ -# output: If successful, prints the full path to the contours file. Otherwise it prints "error" - -ext="" -if [ "${format}" = "GeoJSON" ]; then - ext="json" -elif [ "${format}" = "GPKG" ]; then - ext="gpkg" -elif [ "${format}" = "DXF" ]; then - ext="dxf" -elif [ "${format}" = "ESRI Shapefile" ]; then - ext="shp" -fi - -#gdal_contour -a elevation -i ${interval} -f GPKG "${dem_file}" contours.gpkg > /dev/null -#ogr2ogr -dialect SQLite -where "ST_Length(geom) > 4" -simplify ${simplify} -t_srs EPSG:${epsg} -overwrite -f "${format}" output.$$ext contours.gpkg > /dev/null - -MIN_CONTOUR_LENGTH=5 - -r.external input="${dem_file}" output=dem --overwrite -g.region raster=dem -r.contour input=dem output=contours step=${interval} --overwrite -v.generalize --overwrite input=contours output=contours_smooth method=douglas threshold=${simplify} -v.generalize --overwrite input=contours_smooth output=contours_simplified method=chaiken threshold=1 -v.generalize --overwrite input=contours_simplified output=contours_final method=douglas threshold=${simplify} -v.edit map=contours_final tool=delete threshold=-1,0,-$$MIN_CONTOUR_LENGTH query=length -v.out.ogr input=contours_final output=temp.gpkg format="GPKG" - -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 diff --git a/plugins/contours/calc_contours.py b/plugins/contours/calc_contours.py new file mode 100755 index 00000000..39004838 --- /dev/null +++ b/plugins/contours/calc_contours.py @@ -0,0 +1,95 @@ +#%module +#% description: Calculate contours +#%end +#%option +#% key: dem_file +#% type: string +#% required: yes +#% multiple: no +#% description: GeoTIFF DEM containing the surface to calculate contours +#%end +#%option +#% key: interval +#% type: double +#% required: yes +#% multiple: no +#% description: Contours interval +#%end +#%option +#% key: format +#% type: string +#% required: yes +#% multiple: no +#% description: OGR output format +#%end +#%option +#% key: simplify +#% type: double +#% required: yes +#% multiple: no +#% description: OGR output format +#%end +#%option +#% key: epsg +#% type: string +#% required: yes +#% multiple: no +#% description: target EPSG code +#%end + +# output: If successful, prints the full path to the contours file. Otherwise it prints "error" + +import sys +from grass.pygrass.modules import Module +import grass.script as grass + +def main(): + ext = "" + if opts['format'] == "GeoJSON": + ext = "json" + elif opts['format'] == "GPKG": + ext = "gpkg" + elif opts['format'] == "DXF": + ext = "dxf" + elif opts['format'] == "ESRI Shapefile": + ext = "shp" + + MIN_CONTOUR_LENGTH = 5 + 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.out.ogr", input="contours_final", output="temp.gpkg", format="GPKG") + + 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 diff --git a/plugins/measure/calc_volume.py b/plugins/measure/calc_volume.py index c4e3f9db..8deb2673 100755 --- a/plugins/measure/calc_volume.py +++ b/plugins/measure/calc_volume.py @@ -60,30 +60,3 @@ def main(): if __name__ == "__main__": opts, _ = grass.parser() sys.exit(main()) - -#Import raster and vector -# v.import input=${area_file} output=polygon_area --overwrite -# v.import input=${points_file} output=polygon_points --overwrite -# v.buffer -s --overwrite input=polygon_area type=area output=region distance=1 minordistance=1 -# r.external input=${dsm_file} output=dsm --overwrite -# -# # Set Grass region to vector bbox -# g.region vector=region -# -# # Create a mask to speed up computation -# r.mask vect=region -# -# # Transfer dsm raster data to vector -# v.what.rast map=polygon_points raster=dsm column=height -# -# # Decimate DSM and generate interpolation of new terrain -# v.surf.rst --overwrite input=polygon_points zcolumn=height elevation=dsm_below_pile -# -# # Compute difference between dsm and new dsm -# r.mapcalc expression='pile_height_above_dsm=dsm-dsm_below_pile' --overwrite -# -# # Set region to polygon area to calculate volume -# g.region vect=polygon_area -# -# # Volume output from difference -# r.volume -f input=pile_height_above_dsm