r.plane.py 3.4 KB

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