r.plane.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: r.plane for GRASS 5.7; based on r.plane for GRASS 5
  6. # AUTHOR(S): 1994, Stefan Jäger, University of Heidelberg/USGS
  7. # updated to GRASS 5.7 by Michael Barton
  8. # Dec 2004: Alessandro Frigeri & Ivan Marchesini
  9. # Modified to produce floating and double values maps
  10. # Converted to Python by Glynn Clements
  11. # PURPOSE: Creates a raster plane map from user specified inclination and azimuth
  12. # COPYRIGHT: (C) 2004-2012 by the GRASS Development Team
  13. #
  14. # This program is free software under the GNU General Public
  15. # License (>=v2). Read the file COPYING that comes with GRASS
  16. # for details.
  17. #
  18. #############################################################################
  19. #%module
  20. #% description: Creates raster plane map given dip (inclination), aspect (azimuth) and one point.
  21. #% keyword: raster
  22. #% keyword: elevation
  23. #%end
  24. #%option G_OPT_R_OUTPUT
  25. #%end
  26. #%option
  27. #% key: dip
  28. #% type: double
  29. #% gisprompt: -90-90
  30. #% answer: 0.0
  31. #% description: Dip of plane in degrees
  32. #% required : yes
  33. #%end
  34. #%option
  35. #% key: azimuth
  36. #% type: double
  37. #% gisprompt: 0-360
  38. #% answer: 0.0
  39. #% description: Azimuth of the plane in degrees
  40. #% required : yes
  41. #%end
  42. #%option
  43. #% key: easting
  44. #% type: double
  45. #% description: Easting coordinate of a point on the plane
  46. #% required : yes
  47. #%end
  48. #%option
  49. #% key: northing
  50. #% type: double
  51. #% description: Northing coordinate of a point on the plane
  52. #% required : yes
  53. #%end
  54. #%option
  55. #% key: elevation
  56. #% type: double
  57. #% description: Elevation coordinate of a point on the plane
  58. #% required : yes
  59. #%end
  60. #%option G_OPT_R_TYPE
  61. #% answer: FCELL
  62. #% required: no
  63. #%end
  64. import math
  65. import string
  66. import grass.script as gscript
  67. def main():
  68. name = options['output']
  69. type = options['type']
  70. dip = float(options['dip'])
  71. az = float(options['azimuth'])
  72. try:
  73. ea = float(options['easting'])
  74. no = float(options['northing'])
  75. except ValueError:
  76. try:
  77. ea = float(gscript.utils.float_or_dms(options['easting']))
  78. no = float(gscript.utils.float_or_dms(options['northing']))
  79. except:
  80. gscript.fatal(_("Input coordinates seems to be invalid"))
  81. el = float(options['elevation'])
  82. # reg = gscript.region()
  83. ### test input values ###
  84. if abs(dip) >= 90:
  85. gscript.fatal(_("dip must be between -90 and 90."))
  86. if az < 0 or az >= 360:
  87. gscript.fatal(_("azimuth must be between 0 and 360"))
  88. # now the actual algorithm
  89. az_r = math.radians(-az)
  90. sinaz = math.sin(az_r)
  91. cosaz = math.cos(az_r)
  92. dip_r = math.radians(-dip)
  93. tandip = math.tan(dip_r)
  94. kx = sinaz * tandip
  95. ky = cosaz * tandip
  96. kz = el - ea * sinaz * tandip - no * cosaz * tandip
  97. if type == "CELL":
  98. round = "round"
  99. dtype = "int"
  100. elif type == "FCELL":
  101. round = ""
  102. dtype = "float"
  103. else:
  104. round = ""
  105. dtype = "double"
  106. gscript.mapcalc("$name = $type($round(x() * $kx + y() * $ky + $kz))",
  107. name=name, type=dtype, round=round, kx=kx, ky=ky, kz=kz)
  108. gscript.run_command('r.support', map=name, history='')
  109. gscript.raster_history(name)
  110. gscript.message(_("Done."))
  111. t = string.Template("Raster map <$name> generated by r.plane "
  112. "at point $ea E, $no N, elevation $el with dip = $dip"
  113. " degrees and aspect = $az degrees ccw from north.")
  114. gscript.message(t.substitute(name=name, ea=ea, no=no, el=el, dip=dip,
  115. az=az))
  116. if __name__ == "__main__":
  117. options, flags = gscript.parser()
  118. main()