main.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*******************************************************************************
  2. r.horizon: This module does one of two things:
  3. 1) Using a map of the terrain elevation it calculates for a set of points
  4. the angle height of the horizon for each point, using an angle interval given
  5. by the user.
  6. 2) For a given minimum angle it calculates one or more raster map giving the mazimum
  7. distance to a point on the horizon.
  8. This program was written in 2006 by Thomas Huld and Tomas Cebecauer,
  9. Joint Research Centre of the European Commission, based on bits of the r.sun module by Jaro Hofierka
  10. *******************************************************************************/
  11. /*
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the
  24. * Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #include <grass/gis.h>
  32. #include <grass/raster.h>
  33. #include <grass/gprojects.h>
  34. #include <grass/glocale.h>
  35. #define WHOLE_RASTER 1
  36. #define SINGLE_POINT 0
  37. #define RAD (180. / M_PI)
  38. #define DEG ((M_PI)/180.)
  39. #define EARTHRADIUS 6371000.
  40. #define UNDEF 0. /* undefined value for terrain aspect */
  41. #define UNDEFZ -9999. /* undefined value for elevation */
  42. #define BIG 1.e20
  43. #define SMALL 1.e-20
  44. #define EPS 1.e-4
  45. #define DIST "1.0"
  46. #define DEGREEINMETERS 111120. /* 1852m/nm * 60nm/degree = 111120 m/deg */
  47. #define TANMINANGLE 0.008727 /* tan of minimum horizon angle (0.5 deg) */
  48. #define AMAX1(arg1, arg2) ((arg1) >= (arg2) ? (arg1) : (arg2))
  49. #define DISTANCE1(x1, x2, y1, y2) (sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)))
  50. FILE *fp;
  51. const double pihalf = M_PI * 0.5;
  52. const double twopi = M_PI * 2.;
  53. const double invEarth = 1. / EARTHRADIUS;
  54. const double deg2rad = M_PI / 180.;
  55. const double rad2deg = 180. / M_PI;
  56. const double minAngle = DEG;
  57. const char *elevin;
  58. const char *horizon = NULL;
  59. const char *mapset = NULL;
  60. const char *per;
  61. char *shad_filename;
  62. char *outfile;
  63. struct Cell_head cellhd;
  64. struct Key_value *in_proj_info, *in_unit_info;
  65. struct pj_info iproj;
  66. struct pj_info oproj;
  67. struct Cell_head new_cellhd;
  68. double bufferZone = 0., ebufferZone = 0., wbufferZone = 0.,
  69. nbufferZone = 0., sbufferZone = 0.;
  70. int INPUT(void);
  71. int OUTGR(int numrows, int numcols);
  72. double amax1(double, double);
  73. double amin1(double, double);
  74. int min(int, int);
  75. int max(int, int);
  76. void com_par(double angle);
  77. int is_shadow(void);
  78. double horizon_height(void);
  79. void calculate_shadow();
  80. double calculate_shadow_onedirection(double shadow_angle);
  81. int new_point();
  82. double searching();
  83. int test_low_res();
  84. /*void where_is_point();
  85. void cube(int, int);
  86. */
  87. void calculate(double xcoord, double ycoord, int buffer_e, int buffer_w,
  88. int buffer_s, int buffer_n);
  89. int ip, jp, ip100, jp100;
  90. int n, m, m100, n100;
  91. int degreeOutput, compassOutput = FALSE;
  92. float **z, **z100, **horizon_raster;
  93. double stepx, stepy, stepxhalf, stepyhalf, stepxy, xp, yp, op, dp, xg0, xx0,
  94. yg0, yy0, deltx, delty;
  95. double invstepx, invstepy, distxy;
  96. double offsetx, offsety;
  97. double single_direction;
  98. /*int arrayNumInt; */
  99. double xmin, xmax, ymin, ymax, zmax = 0.;
  100. int d, day, tien = 0;
  101. double length, maxlength = BIG, zmult = 1.0, dist;
  102. double fixedMaxLength = BIG, step = 0.0, start = 0.0, end = 0.0;
  103. char *tt, *lt;
  104. double z_orig, zp;
  105. double h0, tanh0, angle;
  106. double stepsinangle, stepcosangle, sinangle, cosangle, distsinangle,
  107. distcosangle;
  108. double TOLER;
  109. const char *str_step;
  110. int mode;
  111. int isMode()
  112. {
  113. return mode;
  114. }
  115. void setMode(int val)
  116. {
  117. mode = val;
  118. }
  119. int ll_correction = FALSE;
  120. double coslatsq;
  121. /* why not use G_distance() here which switches to geodesic/great
  122. circle distance as needed? */
  123. double distance(double x1, double x2, double y1, double y2)
  124. {
  125. if (ll_correction) {
  126. return DEGREEINMETERS * sqrt(coslatsq * (x1 - x2) * (x1 - x2)
  127. + (y1 - y2) * (y1 - y2));
  128. }
  129. else {
  130. return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  131. }
  132. }
  133. int main(int argc, char *argv[])
  134. {
  135. double xcoord, ycoord;
  136. struct GModule *module;
  137. struct
  138. {
  139. struct Option *elevin, *dist, *coord, *direction, *horizon,
  140. *step, *start, *end, *bufferzone, *e_buff, *w_buff,
  141. *n_buff, *s_buff, *maxdistance, *output;
  142. } parm;
  143. struct
  144. {
  145. struct Flag *degreeOutput, *compassOutput;
  146. }
  147. flag;
  148. G_gisinit(argv[0]);
  149. module = G_define_module();
  150. G_add_keyword(_("raster"));
  151. G_add_keyword(_("solar"));
  152. G_add_keyword(_("sun position"));
  153. module->label =
  154. _("Computes horizon angle height from a digital elevation model.");
  155. module->description =
  156. _("The module has two "
  157. "different modes of operation: "
  158. "1. Computes the entire horizon around a single point whose coordinates are "
  159. "given with the 'coord' option. The horizon height (in radians). "
  160. "2. Computes one or more raster maps of the horizon height in a single direction. "
  161. "The input for this is the angle (in degrees), which is measured "
  162. "counterclockwise with east=0, north=90 etc. The output is the horizon height in radians.");
  163. parm.elevin = G_define_standard_option(G_OPT_R_ELEV);
  164. parm.elevin->guisection = _("Input");
  165. parm.direction = G_define_option();
  166. parm.direction->key = "direction";
  167. parm.direction->type = TYPE_DOUBLE;
  168. parm.direction->required = NO;
  169. parm.direction->description =
  170. _("Direction in which you want to know the horizon height");
  171. parm.direction->guisection = _("Input");
  172. parm.step = G_define_option();
  173. parm.step->key = "step";
  174. parm.step->type = TYPE_DOUBLE;
  175. parm.step->required = NO;
  176. parm.step->description =
  177. _("Angle step size for multidirectional horizon [degrees]");
  178. parm.step->guisection = _("Input");
  179. parm.start = G_define_option();
  180. parm.start->key = "start";
  181. parm.start->type = TYPE_DOUBLE;
  182. parm.start->answer = "0.0";
  183. parm.start->required = NO;
  184. parm.start->description =
  185. _("Start angle for multidirectional horizon [degrees]");
  186. parm.start->guisection = _("Raster mode");
  187. parm.end = G_define_option();
  188. parm.end->key = "end";
  189. parm.end->type = TYPE_DOUBLE;
  190. parm.end->answer = "360.0";
  191. parm.end->required = NO;
  192. parm.end->description =
  193. _("End angle for multidirectional horizon [degrees]");
  194. parm.end->guisection = _("Raster mode");
  195. parm.bufferzone = G_define_option();
  196. parm.bufferzone->key = "bufferzone";
  197. parm.bufferzone->type = TYPE_DOUBLE;
  198. parm.bufferzone->required = NO;
  199. parm.bufferzone->description =
  200. _("For horizon rasters, read from the DEM an extra buffer around the present region");
  201. parm.bufferzone->guisection = _("Raster mode");
  202. parm.e_buff = G_define_option();
  203. parm.e_buff->key = "e_buff";
  204. parm.e_buff->type = TYPE_DOUBLE;
  205. parm.e_buff->required = NO;
  206. parm.e_buff->description =
  207. _("For horizon rasters, read from the DEM an extra buffer eastward the present region");
  208. parm.e_buff->guisection = _("Raster mode");
  209. parm.w_buff = G_define_option();
  210. parm.w_buff->key = "w_buff";
  211. parm.w_buff->type = TYPE_DOUBLE;
  212. parm.w_buff->required = NO;
  213. parm.w_buff->description =
  214. _("For horizon rasters, read from the DEM an extra buffer westward the present region");
  215. parm.w_buff->guisection = _("Raster mode");
  216. parm.n_buff = G_define_option();
  217. parm.n_buff->key = "n_buff";
  218. parm.n_buff->type = TYPE_DOUBLE;
  219. parm.n_buff->required = NO;
  220. parm.n_buff->description =
  221. _("For horizon rasters, read from the DEM an extra buffer northward the present region");
  222. parm.n_buff->guisection = _("Raster mode");
  223. parm.s_buff = G_define_option();
  224. parm.s_buff->key = "s_buff";
  225. parm.s_buff->type = TYPE_DOUBLE;
  226. parm.s_buff->required = NO;
  227. parm.s_buff->description =
  228. _("For horizon rasters, read from the DEM an extra buffer southward the present region");
  229. parm.s_buff->guisection = _("Raster mode");
  230. parm.maxdistance = G_define_option();
  231. parm.maxdistance->key = "maxdistance";
  232. parm.maxdistance->type = TYPE_DOUBLE;
  233. parm.maxdistance->required = NO;
  234. parm.maxdistance->description =
  235. _("The maximum distance to consider when finding the horizon height");
  236. parm.maxdistance->guisection = _("Optional");
  237. parm.horizon = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
  238. parm.horizon->required = NO;
  239. parm.horizon->guisection = _("Raster mode");
  240. parm.coord = G_define_standard_option(G_OPT_M_COORDS);
  241. parm.coord->description =
  242. _("Coordinate for which you want to calculate the horizon");
  243. parm.coord->guisection = _("Point mode");
  244. parm.dist = G_define_option();
  245. parm.dist->key = "distance";
  246. parm.dist->type = TYPE_DOUBLE;
  247. parm.dist->answer = DIST;
  248. parm.dist->required = NO;
  249. parm.dist->description = _("Sampling distance step coefficient (0.5-1.5)");
  250. parm.dist->guisection = _("Optional");
  251. parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
  252. parm.output->key = "file";
  253. parm.output->required = NO;
  254. parm.output->answer = "-";
  255. parm.output->description =
  256. _("Name of file for output (use output=- for stdout)");
  257. parm.output->guisection = _("Point mode");
  258. flag.degreeOutput = G_define_flag();
  259. flag.degreeOutput->key = 'd';
  260. flag.degreeOutput->description =
  261. _("Write output in degrees (default is radians)");
  262. flag.compassOutput = G_define_flag();
  263. flag.compassOutput->key = 'c';
  264. flag.compassOutput->description =
  265. _("Write output in compass orientation (default is CCW, East=0)");
  266. if (G_parser(argc, argv))
  267. exit(EXIT_FAILURE);
  268. G_get_set_window(&cellhd);
  269. stepx = cellhd.ew_res;
  270. stepy = cellhd.ns_res;
  271. stepxhalf = stepx / 2.;
  272. stepyhalf = stepy / 2.;
  273. invstepx = 1. / stepx;
  274. invstepy = 1. / stepy;
  275. /*
  276. offsetx = 2. * invstepx;
  277. offsety = 2. * invstepy;
  278. offsetx = 0.5*stepx;
  279. offsety = 0.5*stepy;
  280. */
  281. offsetx = 0.5;
  282. offsety = 0.5;
  283. n /*n_cols */ = cellhd.cols;
  284. m /*n_rows */ = cellhd.rows;
  285. n100 = ceil(n / 100.);
  286. m100 = ceil(m / 100.);
  287. xmin = cellhd.west;
  288. ymin = cellhd.south;
  289. xmax = cellhd.east;
  290. ymax = cellhd.north;
  291. deltx = fabs(cellhd.east - cellhd.west);
  292. delty = fabs(cellhd.north - cellhd.south);
  293. degreeOutput = flag.degreeOutput->answer;
  294. compassOutput = flag.compassOutput->answer;
  295. if (G_projection() == PROJECTION_LL)
  296. G_important_message(_("Note: In latitude-longitude coordinate system specify buffers in degree unit"));
  297. elevin = parm.elevin->answer;
  298. if (parm.coord->answer == NULL) {
  299. G_debug(1, "Setting mode: WHOLE_RASTER");
  300. setMode(WHOLE_RASTER);
  301. }
  302. else {
  303. G_debug(1, "Setting mode: SINGLE_POINT");
  304. setMode(SINGLE_POINT);
  305. if (sscanf(parm.coord->answer, "%lf,%lf", &xcoord, &ycoord) != 2) {
  306. G_fatal_error(
  307. _("Can't read the coordinates from the \"coordinate\" option."));
  308. }
  309. /* Transform the coordinates to row/column */
  310. /*
  311. xcoord = (int) ((xcoord-xmin)/stepx);
  312. ycoord = (int) ((ycoord-ymin)/stepy);
  313. */
  314. /* Open ASCII file for output or stdout */
  315. outfile = parm.output->answer;
  316. if ((strcmp("-", outfile)) == 0) {
  317. fp = stdout;
  318. }
  319. else if (NULL == (fp = fopen(outfile, "w")))
  320. G_fatal_error(_("Unable to open file <%s>"), outfile);
  321. }
  322. if (parm.direction->answer != NULL)
  323. sscanf(parm.direction->answer, "%lf", &single_direction);
  324. if (isMode(WHOLE_RASTER)) {
  325. if ((parm.direction->answer == NULL) && (parm.step->answer == NULL)) {
  326. G_fatal_error(
  327. _("You didn't specify a direction value or step size. Aborting."));
  328. }
  329. if (parm.horizon->answer == NULL) {
  330. G_fatal_error(
  331. _("You didn't specify a horizon raster name. Aborting."));
  332. }
  333. horizon = parm.horizon->answer;
  334. if (parm.step->answer != NULL) {
  335. str_step = parm.step->answer;
  336. sscanf(parm.step->answer, "%lf", &step);
  337. }
  338. else {
  339. step = 0;
  340. str_step = "0";
  341. }
  342. sscanf(parm.start->answer, "%lf", &start);
  343. sscanf(parm.end->answer, "%lf", &end);
  344. if (start < 0.0) {
  345. G_fatal_error(
  346. _("Negative values of start angle are not allowed. Aborting."));
  347. }
  348. if (end < 0.0 || end > 360.0) {
  349. G_fatal_error(
  350. _("End angle is not between 0 and 360. Aborting."));
  351. }
  352. if (start >= end) {
  353. G_fatal_error(
  354. _("You specify a start grater than the end angle. Aborting."));
  355. }
  356. G_debug(1, "Angle step: %g, start: %g, end: %g", step, start, end);
  357. }
  358. else {
  359. if (parm.step->answer == NULL) {
  360. G_fatal_error(
  361. _("You didn't specify an angle step size. Aborting."));
  362. }
  363. sscanf(parm.step->answer, "%lf", &step);
  364. }
  365. if (step == 0.0) {
  366. step = 360.;
  367. }
  368. if (parm.bufferzone->answer != NULL) {
  369. if (sscanf(parm.bufferzone->answer, "%lf", &bufferZone) != 1)
  370. G_fatal_error(_("Could not read bufferzone size. Aborting."));
  371. }
  372. if (parm.e_buff->answer != NULL) {
  373. if (sscanf(parm.e_buff->answer, "%lf", &ebufferZone) != 1)
  374. G_fatal_error(_("Could not read %s bufferzone size. Aborting."),
  375. _("east"));
  376. }
  377. if (parm.w_buff->answer != NULL) {
  378. if (sscanf(parm.w_buff->answer, "%lf", &wbufferZone) != 1)
  379. G_fatal_error(_("Could not read %s bufferzone size. Aborting."),
  380. _("west"));
  381. }
  382. if (parm.s_buff->answer != NULL) {
  383. if (sscanf(parm.s_buff->answer, "%lf", &sbufferZone) != 1)
  384. G_fatal_error(
  385. _("Could not read %s bufferzone size. Aborting."),
  386. _("south"));
  387. }
  388. if (parm.n_buff->answer != NULL) {
  389. if (sscanf(parm.n_buff->answer, "%lf", &nbufferZone) != 1)
  390. G_fatal_error(
  391. _("Could not read %s bufferzone size. Aborting."),
  392. _("north"));
  393. }
  394. if (parm.maxdistance->answer != NULL) {
  395. if (sscanf(parm.maxdistance->answer, "%lf", &fixedMaxLength) != 1)
  396. G_fatal_error(_("Could not read maximum distance. Aborting."));
  397. }
  398. G_debug(1,"Using maxdistance %f", fixedMaxLength); /* predefined as BIG */
  399. /* TODO: fixing BIG, there is a bug with distant mountains not being seen: attempt to contrain to current region
  400. fixedMaxLength = (fixedMaxLength < AMAX1(deltx, delty)) ? fixedMaxLength : AMAX1(deltx, delty);
  401. G_debug(1,"Using maxdistance %f", fixedMaxLength);
  402. */
  403. sscanf(parm.dist->answer, "%lf", &dist);
  404. if (dist < 0.5 || dist > 1.5 ) G_fatal_error(_("The distance value must be 0.5-1.5. Aborting."));
  405. stepxy = dist * 0.5 * (stepx + stepy);
  406. distxy = dist;
  407. TOLER = stepxy * EPS;
  408. if (bufferZone > 0. || ebufferZone > 0. || wbufferZone > 0. ||
  409. sbufferZone > 0. || nbufferZone > 0.) {
  410. new_cellhd = cellhd;
  411. if (ebufferZone == 0.)
  412. ebufferZone = bufferZone;
  413. if (wbufferZone == 0.)
  414. wbufferZone = bufferZone;
  415. if (sbufferZone == 0.)
  416. sbufferZone = bufferZone;
  417. if (nbufferZone == 0.)
  418. nbufferZone = bufferZone;
  419. new_cellhd.rows += (int)((nbufferZone + sbufferZone) / stepy);
  420. new_cellhd.cols += (int)((ebufferZone + wbufferZone) / stepx);
  421. new_cellhd.north += nbufferZone;
  422. new_cellhd.south -= sbufferZone;
  423. new_cellhd.east += ebufferZone;
  424. new_cellhd.west -= wbufferZone;
  425. xmin = new_cellhd.west;
  426. ymin = new_cellhd.south;
  427. xmax = new_cellhd.east;
  428. ymax = new_cellhd.north;
  429. deltx = fabs(new_cellhd.east - new_cellhd.west);
  430. delty = fabs(new_cellhd.north - new_cellhd.south);
  431. n /* n_cols */ = new_cellhd.cols;
  432. m /* n_rows */ = new_cellhd.rows;
  433. G_debug(1,"%lf %lf %lf %lf \n",ymax, ymin, xmin,xmax);
  434. n100 = ceil(n / 100.);
  435. m100 = ceil(m / 100.);
  436. Rast_set_window(&new_cellhd);
  437. }
  438. struct Key_Value *in_proj_info, *in_unit_info;
  439. if ((in_proj_info = G_get_projinfo()) == NULL)
  440. G_fatal_error(
  441. _("Can't get projection info of current location"));
  442. if ((in_unit_info = G_get_projunits()) == NULL)
  443. G_fatal_error(_("Can't get projection units of current location"));
  444. if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
  445. G_fatal_error(
  446. _("Can't get projection key values of current location"));
  447. G_free_key_value(in_proj_info);
  448. G_free_key_value(in_unit_info);
  449. /* Set output projection to latlong w/ same ellipsoid */
  450. oproj.zone = 0;
  451. oproj.meters = 1.;
  452. sprintf(oproj.proj, "ll");
  453. if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
  454. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  455. /**********end of parser - ******************************/
  456. INPUT();
  457. G_debug(1, "calculate() starts...");
  458. calculate(xcoord, ycoord, (int)(ebufferZone / stepx),
  459. (int)(wbufferZone / stepx), (int)(sbufferZone / stepy),
  460. (int)(nbufferZone / stepy));
  461. exit(EXIT_SUCCESS);
  462. }
  463. /**********************end of main.c*****************/
  464. int INPUT(void)
  465. {
  466. FCELL *cell1;
  467. int fd1, row, row_rev;
  468. int l, i, j, k;
  469. int lmax, kmax;
  470. cell1 = Rast_allocate_f_buf();
  471. z = (float **)G_malloc(sizeof(float *) * (m));
  472. z100 = (float **)G_malloc(sizeof(float *) * (m100));
  473. for (l = 0; l < m; l++) {
  474. z[l] = (float *)G_malloc(sizeof(float) * (n));
  475. }
  476. for (l = 0; l < m100; l++) {
  477. z100[l] = (float *)G_malloc(sizeof(float) * (n100));
  478. }
  479. /*read Z raster */
  480. fd1 = Rast_open_old(elevin, "");
  481. for (row = 0; row < m; row++) {
  482. Rast_get_f_row(fd1, cell1, row);
  483. for (j = 0; j < n; j++) {
  484. row_rev = m - row - 1;
  485. if (!Rast_is_f_null_value(cell1 + j))
  486. z[row_rev][j] = (float)cell1[j];
  487. else
  488. z[row_rev][j] = UNDEFZ;
  489. }
  490. }
  491. Rast_close(fd1);
  492. /* create low resolution array 100 */
  493. for (i = 0; i < m100; i++) {
  494. lmax = (i + 1) * 100;
  495. if (lmax > m)
  496. lmax = m;
  497. for (j = 0; j < n100; j++) {
  498. zmax = SMALL;
  499. kmax = (j + 1) * 100;
  500. if (kmax > n)
  501. kmax = n;
  502. for (l = (i * 100); l < lmax; l++) {
  503. for (k = (j * 100); k < kmax; k++) {
  504. zmax = amax1(zmax, z[l][k]);
  505. }
  506. }
  507. z100[i][j] = zmax;
  508. G_debug(3,"%d %d %lf\n", i, j, z100[i][j]);
  509. }
  510. }
  511. /* find max Z */
  512. for (i = 0; i < m; i++) {
  513. for (j = 0; j < n; j++) {
  514. zmax = amax1(zmax, z[i][j]);
  515. }
  516. }
  517. return 1;
  518. }
  519. int OUTGR(int numrows, int numcols)
  520. {
  521. FCELL *cell1 = NULL;
  522. int fd1 = 0;
  523. int i, iarc, j;
  524. Rast_set_window(&cellhd);
  525. if (horizon != NULL) {
  526. cell1 = Rast_allocate_f_buf();
  527. fd1 = Rast_open_fp_new(shad_filename);
  528. }
  529. if (numrows != Rast_window_rows())
  530. G_fatal_error(_("OOPS: rows changed from %d to %d"), numrows,
  531. Rast_window_rows());
  532. if (numcols != Rast_window_cols())
  533. G_fatal_error(_("OOPS: cols changed from %d to %d"), numcols,
  534. Rast_window_cols());
  535. for (iarc = 0; iarc < numrows; iarc++) {
  536. i = numrows - iarc - 1;
  537. if (horizon != NULL) {
  538. for (j = 0; j < numcols; j++) {
  539. if (horizon_raster[i][j] == UNDEFZ)
  540. Rast_set_f_null_value(cell1 + j, 1);
  541. else
  542. cell1[j] = (FCELL) horizon_raster[i][j];
  543. }
  544. Rast_put_f_row(fd1, cell1);
  545. }
  546. } /* End loop over rows. */
  547. Rast_close(fd1);
  548. return 1;
  549. }
  550. double amax1(arg1, arg2)
  551. double arg1;
  552. double arg2;
  553. {
  554. double res;
  555. if (arg1 >= arg2) {
  556. res = arg1;
  557. }
  558. else {
  559. res = arg2;
  560. }
  561. return res;
  562. }
  563. double amin1(arg1, arg2)
  564. double arg1;
  565. double arg2;
  566. {
  567. double res;
  568. if (arg1 <= arg2) {
  569. res = arg1;
  570. }
  571. else {
  572. res = arg2;
  573. }
  574. return res;
  575. }
  576. int min(arg1, arg2)
  577. int arg1;
  578. int arg2;
  579. {
  580. int res;
  581. if (arg1 <= arg2) {
  582. res = arg1;
  583. }
  584. else {
  585. res = arg2;
  586. }
  587. return res;
  588. }
  589. int max(arg1, arg2)
  590. int arg1;
  591. int arg2;
  592. {
  593. int res;
  594. if (arg1 >= arg2) {
  595. res = arg1;
  596. }
  597. else {
  598. res = arg2;
  599. }
  600. return res;
  601. }
  602. /**********************************************************/
  603. void com_par(double angle)
  604. {
  605. sinangle = sin(angle);
  606. if (fabs(sinangle) < 0.0000001) {
  607. sinangle = 0.;
  608. }
  609. cosangle = cos(angle);
  610. if (fabs(cosangle) < 0.0000001) {
  611. cosangle = 0.;
  612. }
  613. distsinangle = 32000;
  614. distcosangle = 32000;
  615. if (sinangle != 0.) {
  616. distsinangle = 100. / (distxy * sinangle);
  617. }
  618. if (cosangle != 0.) {
  619. distcosangle = 100. / (distxy * cosangle);
  620. }
  621. stepsinangle = stepxy * sinangle;
  622. stepcosangle = stepxy * cosangle;
  623. }
  624. double horizon_height(void)
  625. {
  626. double height;
  627. tanh0 = 0.;
  628. length = 0;
  629. height = searching();
  630. xx0 = xg0;
  631. yy0 = yg0;
  632. return height;
  633. }
  634. double calculate_shadow_onedirection(double shadow_angle)
  635. {
  636. shadow_angle = horizon_height();
  637. return shadow_angle;
  638. }
  639. void calculate_shadow()
  640. {
  641. double dfr_rad;
  642. int i;
  643. int printCount;
  644. double shadow_angle;
  645. double printangle;
  646. double sx, sy;
  647. double xp, yp;
  648. double latitude, longitude;
  649. double delt_lat, delt_lon;
  650. double delt_east, delt_nor;
  651. double delt_dist;
  652. double angle;
  653. printCount = 360. / fabs(step);
  654. if (printCount < 1)
  655. printCount = 1;
  656. dfr_rad = step * deg2rad;
  657. xp = xmin + xx0;
  658. yp = ymin + yy0;
  659. angle = (single_direction * deg2rad) + pihalf;
  660. maxlength = fixedMaxLength;
  661. fprintf(fp, "azimuth,horizon_height\n");
  662. for (i = 0; i < printCount; i++) {
  663. ip = jp = 0;
  664. sx = xx0 * invstepx;
  665. sy = yy0 * invstepy;
  666. ip100 = floor(sx / 100.);
  667. jp100 = floor(sy / 100.);
  668. if ((G_projection() != PROJECTION_LL)) {
  669. longitude = xp;
  670. latitude = yp;
  671. if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0) {
  672. G_fatal_error(_("Error in pj_do_proj"));
  673. }
  674. }
  675. else { /* ll projection */
  676. latitude = yp;
  677. longitude = xp;
  678. }
  679. latitude *= deg2rad;
  680. longitude *= deg2rad;
  681. delt_lat = -0.0001 * cos(angle);
  682. delt_lon = 0.0001 * sin(angle) / cos(latitude);
  683. latitude = (latitude + delt_lat) * rad2deg;
  684. longitude = (longitude + delt_lon) * rad2deg;
  685. if (pj_do_proj(&longitude, &latitude, &oproj, &iproj) < 0) {
  686. G_fatal_error(_("Error in pj_do_proj"));
  687. }
  688. delt_east = longitude - xp;
  689. delt_nor = latitude - yp;
  690. delt_dist = sqrt(delt_east * delt_east + delt_nor * delt_nor);
  691. stepsinangle = stepxy * delt_nor / delt_dist;
  692. stepcosangle = stepxy * delt_east / delt_dist;
  693. shadow_angle = horizon_height();
  694. if (degreeOutput) {
  695. shadow_angle *= rad2deg;
  696. }
  697. printangle = angle * rad2deg - 90.;
  698. if (printangle < 0.)
  699. printangle += 360;
  700. else if (printangle >= 360.)
  701. printangle -= 360;
  702. if(compassOutput ){
  703. double tmpangle;
  704. tmpangle = 360. - printangle + 90.;
  705. if (tmpangle >= 360.)
  706. tmpangle = tmpangle - 360.;
  707. fprintf(fp, "%lf,%lf\n", tmpangle, shadow_angle);
  708. } else {
  709. fprintf(fp, "%lf,%lf\n", printangle, shadow_angle);
  710. }
  711. angle += dfr_rad;
  712. if (angle < 0.)
  713. angle += twopi;
  714. else if (angle > twopi)
  715. angle -= twopi;
  716. } /* end of for loop over angles */
  717. }
  718. /*////////////////////////////////////////////////////////////////////// */
  719. int new_point()
  720. {
  721. int iold, jold;
  722. int succes = 1, succes2 = 1;
  723. double sx, sy;
  724. double dx, dy;
  725. iold = ip;
  726. jold = jp;
  727. while (succes) {
  728. yy0 += stepsinangle;
  729. xx0 += stepcosangle;
  730. /* offset 0.5 cell size to get the right cell i, j */
  731. sx = xx0 * invstepx + offsetx;
  732. sy = yy0 * invstepy + offsety;
  733. ip = (int)sx;
  734. jp = (int)sy;
  735. /* test outside of raster */
  736. if ((ip < 0) || (ip >= n) || (jp < 0) || (jp >= m))
  737. return (3);
  738. if ((ip != iold) || (jp != jold)) {
  739. dx = (double)ip *stepx;
  740. dy = (double)jp *stepy;
  741. length = distance(xg0, dx, yg0, dy); /* dist from orig. grid point to the current grid point */
  742. succes2 = test_low_res();
  743. if (succes2 == 1) {
  744. zp = z[jp][ip];
  745. return (1);
  746. }
  747. }
  748. }
  749. return -1;
  750. }
  751. int test_low_res()
  752. {
  753. int iold100, jold100;
  754. double sx, sy;
  755. int delx, dely, mindel;
  756. double zp100, z2, curvature_diff;
  757. iold100 = ip100;
  758. jold100 = jp100;
  759. ip100 = floor(ip / 100.);
  760. jp100 = floor(jp / 100.);
  761. /*test the new position with low resolution */
  762. if ((ip100 != iold100) || (jp100 != jold100)) {
  763. G_debug(2,"ip:%d jp:%d iold100:%d jold100:%d\n",ip,jp, iold100,jold100);
  764. /* replace with approximate version
  765. curvature_diff = EARTHRADIUS*(1.-cos(length/EARTHRADIUS));
  766. */
  767. curvature_diff = 0.5 * length * length * invEarth;
  768. z2 = z_orig + curvature_diff + length * tanh0;
  769. zp100 = z100[jp100][ip100];
  770. G_debug(2,"ip:%d jp:%d z2:%lf zp100:%lf \n",ip,jp,z2,zp100);
  771. if (zp100 <= z2)
  772. /*skip to the next lowres cell */
  773. {
  774. delx = 32000;
  775. dely = 32000;
  776. if (cosangle > 0.) {
  777. sx = xx0 * invstepx + offsetx;
  778. delx =
  779. floor(fabs
  780. ((ceil(sx / 100.) - (sx / 100.)) * distcosangle));
  781. }
  782. if (cosangle < 0.) {
  783. sx = xx0 * invstepx + offsetx;
  784. delx =
  785. floor(fabs
  786. ((floor(sx / 100.) - (sx / 100.)) * distcosangle));
  787. }
  788. if (sinangle > 0.) {
  789. sy = yy0 * invstepy + offsety;
  790. dely =
  791. floor(fabs
  792. ((ceil(sy / 100.) - (sy / 100.)) * distsinangle));
  793. }
  794. else if (sinangle < 0.) {
  795. sy = yy0 * invstepy + offsety;
  796. dely =
  797. floor(fabs
  798. ((floor(jp / 100.) - (sy / 100.)) * distsinangle));
  799. }
  800. mindel = min(delx, dely);
  801. G_debug(2,"%d %d %d %lf %lf\n",ip, jp, mindel,xg0, yg0);
  802. yy0 = yy0 + (mindel * stepsinangle);
  803. xx0 = xx0 + (mindel * stepcosangle);
  804. G_debug(2," %lf %lf\n",xx0,yy0);
  805. return (3);
  806. }
  807. else {
  808. return (1); /* change of low res array - new cell is reaching limit for high resolution processing */
  809. }
  810. }
  811. else {
  812. return (1); /* no change of low res array */
  813. }
  814. }
  815. double searching()
  816. {
  817. double z2;
  818. double curvature_diff;
  819. int succes = 1;
  820. if (zp == UNDEFZ)
  821. return 0;
  822. while (1) {
  823. succes = new_point();
  824. if (succes != 1) {
  825. break;
  826. }
  827. /* curvature_diff = EARTHRADIUS*(1.-cos(length/EARTHRADIUS)); */
  828. curvature_diff = 0.5 * length * length * invEarth;
  829. z2 = z_orig + curvature_diff + length * tanh0;
  830. if (z2 < zp) {
  831. tanh0 = (zp - z_orig - curvature_diff) / length;
  832. }
  833. if (z2 >= zmax) {
  834. break;
  835. }
  836. if (length >= maxlength) {
  837. break;
  838. }
  839. }
  840. return atan(tanh0);
  841. }
  842. /*////////////////////////////////////////////////////////////////////// */
  843. void calculate(double xcoord, double ycoord, int buffer_e, int buffer_w,
  844. int buffer_s, int buffer_n)
  845. {
  846. int i, j, l, k = 0;
  847. size_t decimals;
  848. int xindex, yindex;
  849. double shadow_angle;
  850. double coslat;
  851. double latitude, longitude;
  852. double xp, yp;
  853. double inputAngle;
  854. double delt_lat, delt_lon;
  855. double delt_east, delt_nor;
  856. double delt_dist;
  857. char msg_buff[256];
  858. int hor_row_start = buffer_s;
  859. int hor_row_end = m - buffer_n;
  860. int hor_col_start = buffer_w;
  861. int hor_col_end = n - buffer_e;
  862. int hor_numrows = m - (buffer_s + buffer_n);
  863. int hor_numcols = n - (buffer_e + buffer_w);
  864. int arrayNumInt;
  865. double dfr_rad, angle_deg;
  866. xindex = (int)((xcoord - xmin) / stepx);
  867. yindex = (int)((ycoord - ymin) / stepy);
  868. if ((G_projection() == PROJECTION_LL)) {
  869. ll_correction = TRUE;
  870. }
  871. if (isMode() == SINGLE_POINT) {
  872. /* Calculate the horizon for one single point */
  873. /*
  874. xg0 = xx0 = (double)xcoord * stepx;
  875. yg0 = yy0 = (double)ycoord * stepy;
  876. xg0 = xx0 = xcoord -0.5*stepx -xmin;
  877. yg0 = yy0 = ycoord -0.5*stepy-ymin;
  878. xg0 = xx0 = xindex*stepx -0.5*stepx;
  879. yg0 = yy0 = yindex*stepy -0.5*stepy;
  880. */
  881. xg0 = xx0 = xindex * stepx;
  882. yg0 = yy0 = yindex * stepy;
  883. if (ll_correction) {
  884. coslat = cos(deg2rad * (ymin + yy0));
  885. coslatsq = coslat * coslat;
  886. }
  887. z_orig = zp = z[yindex][xindex];
  888. G_debug(1, "yindex: %d, xindex %d, z_orig %.2f", yindex, xindex, z_orig);
  889. calculate_shadow();
  890. fclose(fp);
  891. }
  892. else {
  893. /****************************************************************/
  894. /* The loop over raster points starts here! */
  895. /****************************************************************/
  896. if (horizon != NULL) {
  897. horizon_raster = (float **)G_malloc(sizeof(float *) * (hor_numrows));
  898. for (l = 0; l < hor_numrows; l++) {
  899. horizon_raster[l] =
  900. (float *)G_malloc(sizeof(float) * (hor_numcols));
  901. }
  902. for (j = 0; j < hor_numrows; j++) {
  903. for (i = 0; i < hor_numcols; i++)
  904. horizon_raster[j][i] = 0.;
  905. }
  906. }
  907. /* definition of horizon angle in loop */
  908. if (step == 0.0) {
  909. dfr_rad = 0;
  910. arrayNumInt = 1;
  911. sprintf(shad_filename, "%s", horizon);
  912. }
  913. else {
  914. dfr_rad = step * deg2rad;
  915. arrayNumInt = (int)((end - start) / fabs(step));
  916. }
  917. decimals = G_get_num_decimals(str_step);
  918. for (k = 0; k < arrayNumInt; k++) {
  919. struct History history;
  920. angle = (start + single_direction) * deg2rad + (dfr_rad * k);
  921. angle_deg = angle * rad2deg + 0.0001;
  922. if (step != 0.0)
  923. shad_filename = G_generate_basename(horizon, angle_deg, 3, decimals);
  924. /*
  925. com_par(angle);
  926. */
  927. G_message(_("Calculating map %01d of %01d (angle %.2f, raster map <%s>)"),
  928. (k + 1), arrayNumInt, angle_deg, shad_filename);
  929. for (j = hor_row_start; j < hor_row_end; j++) {
  930. G_percent(j - hor_row_start, hor_numrows - 1, 2);
  931. shadow_angle = 15 * deg2rad;
  932. for (i = hor_col_start; i < hor_col_end; i++) {
  933. ip100 = floor(i / 100.);
  934. jp100 = floor(j / 100.);
  935. ip = jp = 0;
  936. xg0 = xx0 = (double)i *stepx;
  937. xp = xmin + xx0;
  938. yg0 = yy0 = (double)j *stepy;
  939. yp = ymin + yy0;
  940. length = 0;
  941. if (ll_correction) {
  942. coslat = cos(deg2rad * yp);
  943. coslatsq = coslat * coslat;
  944. }
  945. longitude = xp;
  946. latitude = yp;
  947. if ((G_projection() != PROJECTION_LL)) {
  948. if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0)
  949. G_fatal_error("Error in pj_do_proj");
  950. }
  951. latitude *= deg2rad;
  952. longitude *= deg2rad;
  953. inputAngle = angle + pihalf;
  954. inputAngle =
  955. (inputAngle >=
  956. twopi) ? inputAngle - twopi : inputAngle;
  957. delt_lat = -0.0001 * cos(inputAngle); /* Arbitrary small distance in latitude */
  958. delt_lon = 0.0001 * sin(inputAngle) / cos(latitude);
  959. latitude = (latitude + delt_lat) * rad2deg;
  960. longitude = (longitude + delt_lon) * rad2deg;
  961. if ((G_projection() != PROJECTION_LL)) {
  962. if (pj_do_proj(&longitude, &latitude, &oproj, &iproj) < 0)
  963. G_fatal_error("Error in pj_do_proj");
  964. }
  965. delt_east = longitude - xp;
  966. delt_nor = latitude - yp;
  967. delt_dist =
  968. sqrt(delt_east * delt_east + delt_nor * delt_nor);
  969. sinangle = delt_nor / delt_dist;
  970. if (fabs(sinangle) < 0.0000001) {
  971. sinangle = 0.;
  972. }
  973. cosangle = delt_east / delt_dist;
  974. if (fabs(cosangle) < 0.0000001) {
  975. cosangle = 0.;
  976. }
  977. distsinangle = 32000;
  978. distcosangle = 32000;
  979. if (sinangle != 0.) {
  980. distsinangle = 100. / (distxy * sinangle);
  981. }
  982. if (cosangle != 0.) {
  983. distcosangle = 100. / (distxy * cosangle);
  984. }
  985. stepsinangle = stepxy * sinangle;
  986. stepcosangle = stepxy * cosangle;
  987. z_orig = zp = z[j][i];
  988. maxlength = (zmax - z_orig) / TANMINANGLE;
  989. maxlength =
  990. (maxlength < fixedMaxLength) ? maxlength : fixedMaxLength;
  991. if (z_orig != UNDEFZ) {
  992. G_debug(4,"**************new line %d %d\n", i, j);
  993. shadow_angle = horizon_height();
  994. if (degreeOutput) {
  995. shadow_angle *= rad2deg;
  996. }
  997. /*
  998. if((j==1400)&&(i==1400))
  999. {
  1000. G_debug(1, "coordinates=%f,%f, raster no. %d, horizon=%f\n",
  1001. xp, yp, k, shadow_angle);
  1002. }
  1003. */
  1004. horizon_raster[j - buffer_s][i - buffer_w] =
  1005. shadow_angle;
  1006. } /* undefs */
  1007. }
  1008. }
  1009. G_debug(1, "OUTGR() starts...");
  1010. OUTGR(cellhd.rows, cellhd.cols);
  1011. /* empty array */
  1012. for (j = 0; j < hor_numrows; j++) {
  1013. for (i = 0; i < hor_numcols; i++)
  1014. horizon_raster[j][i] = 0.;
  1015. }
  1016. /* return back the buffered region */
  1017. if (bufferZone > 0.)
  1018. Rast_set_window(&new_cellhd);
  1019. /* write metadata */
  1020. Rast_short_history(shad_filename, "raster", &history);
  1021. sprintf(msg_buff,
  1022. "Angular height of terrain horizon, map %01d of %01d",
  1023. (k + 1), arrayNumInt);
  1024. Rast_put_cell_title(shad_filename, msg_buff);
  1025. if (degreeOutput)
  1026. Rast_write_units(shad_filename, "degrees");
  1027. else
  1028. Rast_write_units(shad_filename, "radians");
  1029. Rast_command_history(&history);
  1030. /* insert a blank line */
  1031. Rast_append_history(&history, "");
  1032. Rast_append_format_history(
  1033. &history,
  1034. "Horizon view from azimuth angle %.2f degrees CCW from East",
  1035. angle * rad2deg);
  1036. Rast_write_history(shad_filename, &history);
  1037. G_free(shad_filename);
  1038. }
  1039. }
  1040. }