main.c 30 KB

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