r.plane.py 3.7 KB

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