Advertisement
Guest User

Untitled

a guest
Mar 8th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. ############################################################################
  4. #
  5. # MODULE:   r.surf.lrm
  6. #
  7. # AUTHOR(S):    Eric Goddard, egoddard@memphis.edu
  8. #
  9. # PURPOSE:  Create a Local Relief Model from input DEM. Adapted from
  10. #               Rebecca Bennett's bash script.
  11. #
  12. # COPYRIGHT:    (C) 2013 Eric Goddard
  13. #
  14. # REFERENCES:
  15. #       2011  Bennett, Rebecca.  Archaeological Remote Sensing: Visualisation and analysis
  16. #                       of grass-dominated environments using airborne laser scanning and
  17. #                       digital spectra data. Unpublished PhD Thesis. Electronic Document,
  18. #                       http://eprints.bournemouth.ac.uk/20459/1/Bennett%2C_Rebecca_PhD_Thesis_2011.pdf,
  19. #                       Accessed 25 February 2013.
  20.  
  21. #
  22. #       2010  Hesse, Ralf. LiDAR-derived Local Relief Models - a new tool for archaeological
  23. #                        prospection. Archaeological Prospection 17:67-72.
  24. #
  25. #
  26. #############################################################################
  27.  
  28. #%module
  29. #%  description: Creates a Local Relief Model from input raster.
  30. #%  keywords: lrm
  31. #%  keywords: lidar
  32. #%  overwrite: yes
  33. #%end
  34. #%option G_OPT_R_INPUT
  35. #%  key:input_map
  36. #%  description: Select input raster
  37. #%  required: yes
  38. #%end
  39. #%option
  40. #%  key: output_prefix
  41. #%  description: Prefix for output LRM files
  42. #%  required: yes
  43. #%end
  44. #%option
  45. #%  key: kernel
  46. #%  type: integer
  47. #%  description: The kernel size to be used in the low pass filter.
  48. #%  required: no
  49. #%  answer: 25
  50. #%end
  51.  
  52. import sys
  53. import os
  54. import grass.script as grass
  55.  
  56. if not os.environ.has_key("GISBASE"):
  57.     print "You must be in GRASS GIS to run this program."
  58.     sys.exit(1)
  59.  
  60. def main():
  61.     input_map = options['input_map']
  62.     output_prefix = options['output_prefix']
  63.     kernel = options['kernel']
  64.    
  65.     #Processing files
  66.     lp_out = "%s_lpf" % output_prefix
  67.     lp_subtract = "%s_lpf_difference" % output_prefix
  68.     LP_contour = "%s_diff_contour" % output_prefix
  69.     LP_points = "%s_points" % LP_contour
  70.     LP_purged = "%s_purged" % output_prefix
  71.     lrm_output = "%s_lrm" % output_prefix
  72.    
  73.    
  74.     #Run the low pass filter on input map
  75.     grass.message("Running low pass filter on %s." % input_map)
  76.     grass.run_command("r.neighbors", input=input_map, output=lp_out, size=kernel, method="average")
  77.    
  78.     #Subtract the result of the low pass filter from the input map
  79.     grass.message("Subtracting low pass from DEM...")
  80.     grass.mapcalc("$lp_subtract = $input_map - $lp_out", lp_subtract = lp_subtract, input_map = input_map, lp_out = lp_out)
  81.    
  82.     #Extract the zero contours
  83.     grass.message("Extracting zero contours from low pass difference...")
  84.     grass.run_command("r.contour", input=lp_subtract, output=LP_contour, minlevel=0, maxlevel=0, step=10)
  85.     grass.run_command("v.to.points", input=LP_contour, llayer=1, type="line", output=LP_points, dmax=10)
  86.     grass.run_command("v.what.rast", map=LP_points, raster=input_map, layer=2, column="along")
  87.    
  88.     #Get mean distance between points to optimize spline interpolation
  89.     meanDist = grass.parse_command("v.surf.bspline", flags="e", input=LP_points, raster_output=LP_purged, layer="2", column="along", method="bilinear")
  90.    
  91.     #The -e flag on v.surf.bspline returns a length 1 dictionary key string
  92.     #containing the Estimated point density and mean distance between points,
  93.     #with the mean distance between points as the last value. Extact the
  94.     #dictionary key, split it into a list using space as a separator, and
  95.     #the last element in the list is the mean distance between points.
  96.     #Double to get length of spline step for interpolation.
  97.     splineStep = round(float(meanDist.keys()[0].split(" ")[-1])) * 2
  98.    
  99.     grass.message("Interpolating purged surface using a spline step value of %.0f..." % splineStep)
  100.     #Interpolate using v.surf.bspline with meanDist * 2 as sie and sin
  101.     grass.run_command("v.surf.bspline", input=LP_points, raster_output=LP_purged, layer="2", sie=splineStep, sin=splineStep, column="along", method="bilinear")
  102.    
  103.     #Subtract the purged surface from input surface to get the local relief and apply a grey color table.
  104.     grass.message("Creating Local Relief Model...")
  105.     grass.mapcalc("$lrm_output = $input_map - $LP_purged", lrm_output = lrm_output, input_map = input_map, LP_purged = LP_purged)
  106.     grass.run_command("r.colors", map=lrm_output, color="differences")
  107.    
  108.     grass.message("Done.")
  109.    
  110. if __name__ == "__main__":
  111.     options, flags = grass.parser()
  112.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement