r.plane.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. #
  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. # i18N
  68. import os
  69. import gettext
  70. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  71. def main():
  72. name = options['output']
  73. type = options['type']
  74. dip = float(options['dip'])
  75. az = float(options['azimuth'])
  76. ea = float(options['easting'])
  77. no = float(options['northing'])
  78. el = float(options['elevation'])
  79. # reg = gscript.region()
  80. ### test input values ###
  81. if abs(dip) >= 90:
  82. gscript.fatal(_("dip must be between -90 and 90."))
  83. if az < 0 or az >= 360:
  84. gscript.fatal(_("azimuth must be between 0 and 360"))
  85. # now the actual algorithm
  86. az_r = math.radians(-az)
  87. sinaz = math.sin(az_r)
  88. cosaz = math.cos(az_r)
  89. dip_r = math.radians(-dip)
  90. tandip = math.tan(dip_r)
  91. kx = sinaz * tandip
  92. ky = cosaz * tandip
  93. kz = el - ea * sinaz * tandip - no * cosaz * tandip
  94. if type == "CELL":
  95. round = "round"
  96. dtype = "int"
  97. elif type == "FCELL":
  98. round = ""
  99. dtype = "float"
  100. else:
  101. round = ""
  102. dtype = "double"
  103. gscript.mapcalc("$name = $type($round(x() * $kx + y() * $ky + $kz))",
  104. name=name, type=dtype, round=round, kx=kx, ky=ky, kz=kz)
  105. gscript.run_command('r.support', map=name, history='')
  106. gscript.raster_history(name)
  107. gscript.message(_("Done."))
  108. t = string.Template("Raster map <$name> generated by r.plane "
  109. "at point $ea E, $no N, elevation $el with dip = $dip"
  110. " degrees and aspect = $az degrees ccw from north.")
  111. gscript.message(t.substitute(name=name, ea=ea, no=no, el=el, dip=dip,
  112. az=az))
  113. if __name__ == "__main__":
  114. options, flags = gscript.parser()
  115. main()