forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1411 lines (1231 loc) · 41.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* MODULE: r.slope.aspect
* AUTHOR(S): Michael Shapiro and
* Olga Waupotitsch (original CERL contributors),
* Markus Neteler <neteler itc.it>,
* Bernhard Reiter <bernhard intevation.de>,
* Brad Douglas <rez touchofmadness.com>,
* Glynn Clements <glynn gclements.plus.com>,
* Hamish Bowman <hamish_b yahoo.com>,
* Jachym Cepicky <jachym les-ejk.cz>,
* Jan-Oliver Wagner <jan intevation.de>,
* Radim Blazek <radim.blazek gmail.com>
* PURPOSE: generates raster maps of slope, aspect, curvatures and
* first and second order partial derivatives from a raster map
* of true elevation values
* COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>
/* 10/99 from GMSL, updated to new GRASS 5 code style , changed default "prec" to float */
#define abs(x) ((x)<0?-(x):(x))
/**************************************************************************
* input is from command line.
* arguments are elevation file, slope file, aspect file, profile curvature
* file and tangential curvature file
* elevation filename required
* either slope filename or aspect filename or profile curvature filename
* or tangential curvature filename required
* usage: r.slope.aspect [-av] elevation=input slope=output1 aspect=output2
* pcurv=output3 tcurv=output4 format=name prec=name zfactor=value
* min_slope=value dx=output5 dy=output6 dxx=output7
* dyy=output8 dxy=output9
* -a don't align window
* -q quiet
**************************************************************************/
/* some changes made to code to retrieve correct distances when using
lat/lon projection. changes involve recalculating H and V. see
comments within code. */
/* added colortables for topographic parameters and fixed
* the sign bug for the second order derivatives (jh) */
/* convert aspect from CCW from East to CW from North
* aspect for flat areas is set to -9999 */
static double aspect_cw_n(double aspect)
{
/* aspect == 0: flat */
if (aspect == 0)
return -9999;
/* no modulus because of fp values */
aspect = (450 - aspect);
if (aspect >= 360)
aspect -= 360;
return aspect;
}
int main(int argc, char *argv[])
{
struct Categories cats;
int elevation_fd;
int aspect_fd;
int slope_fd;
int pcurv_fd;
int tcurv_fd;
int dx_fd;
int dy_fd;
int dxx_fd;
int dyy_fd;
int dxy_fd;
DCELL *elev_cell[3], *temp;
DCELL *pc1, *pc2, *pc3;
DCELL c1, c2, c3, c4, c5, c6, c7, c8, c9;
DCELL tmp1, tmp2;
FCELL dat1, dat2;
CELL cat;
void *asp_raster, *asp_ptr = NULL;
void *slp_raster, *slp_ptr = NULL;
void *pcurv_raster, *pcurv_ptr = NULL;
void *tcurv_raster, *tcurv_ptr = NULL;
void *dx_raster, *dx_ptr = NULL;
void *dy_raster, *dy_ptr = NULL;
void *dxx_raster, *dxx_ptr = NULL;
void *dyy_raster, *dyy_ptr = NULL;
void *dxy_raster, *dxy_ptr = NULL;
int i;
RASTER_MAP_TYPE out_type, data_type;
int Wrap; /* global wraparound */
struct Cell_head window, cellhd;
struct History hist;
struct Colors colors;
const char *elev_name;
const char *aspect_name;
const char *slope_name;
const char *pcurv_name;
const char *tcurv_name;
const char *dx_name;
const char *dy_name;
const char *dxx_name;
const char *dyy_name;
const char *dxy_name;
char buf[300];
int nrows, row;
int ncols, col;
double north, east, south, west, ns_med;
double radians_to_degrees;
double degrees_to_radians;
double H, V;
double dx; /* partial derivative in ew direction */
double dy; /* partial derivative in ns direction */
double dxx, dxy, dyy;
double s3, s4, s5, s6;
double pcurv, tcurv;
double scik1 = 100000.;
double zfactor;
double factor;
double aspect, min_asp = 360., max_asp = 0.;
double dnorm1, dx2, dy2, grad2, grad, dxy2;
double gradmin = 0.001;
double c1min = 0., c1max = 0., c2min = 0., c2max = 0.;
double answer[92];
double degrees;
double tan_ans;
double key;
double slp_in_perc, slp_in_deg;
double min_slp = 900., max_slp = 0., min_slope;
int low, hi, test = 0;
int deg = 0;
int perc = 0;
char *slope_fmt;
struct GModule *module;
struct
{
struct Option *elevation, *slope_fmt, *slope, *aspect, *pcurv, *tcurv,
*zfactor, *min_slope, *out_precision,
*dx, *dy, *dxx, *dyy, *dxy;
} parm;
struct
{
struct Flag *a, *n, *e;
} flag;
int compute_at_edges;
G_gisinit(argv[0]);
module = G_define_module();
G_add_keyword(_("raster"));
G_add_keyword(_("terrain"));
G_add_keyword(_("aspect"));
G_add_keyword(_("slope"));
G_add_keyword(_("curvature"));
module->label = _("Generates raster maps of slope, aspect, curvatures and "
"partial derivatives from an elevation raster map.");
module->description = _("Aspect is calculated counterclockwise from east.");
parm.elevation = G_define_standard_option(G_OPT_R_ELEV);
parm.slope = G_define_standard_option(G_OPT_R_OUTPUT);
parm.slope->key = "slope";
parm.slope->required = NO;
parm.slope->description = _("Name for output slope raster map");
parm.slope->guisection = _("Outputs");
parm.aspect = G_define_standard_option(G_OPT_R_OUTPUT);
parm.aspect->key = "aspect";
parm.aspect->required = NO;
parm.aspect->description = _("Name for output aspect raster map");
parm.aspect->guisection = _("Outputs");
parm.slope_fmt = G_define_option();
parm.slope_fmt->key = "format";
parm.slope_fmt->type = TYPE_STRING;
parm.slope_fmt->required = NO;
parm.slope_fmt->answer = "degrees";
parm.slope_fmt->options = "degrees,percent";
parm.slope_fmt->description = _("Format for reporting the slope");
parm.slope_fmt->guisection = _("Settings");
parm.out_precision = G_define_standard_option(G_OPT_R_TYPE);
parm.out_precision->key = "precision";
parm.out_precision->required = NO;
parm.out_precision->label =
_("Type of output aspect and slope maps");
parm.out_precision->answer = "FCELL";
parm.out_precision->guisection = _("Settings");
parm.pcurv = G_define_standard_option(G_OPT_R_OUTPUT);
parm.pcurv->key = "pcurvature";
parm.pcurv->required = NO;
parm.pcurv->description =
_("Name for output profile curvature raster map");
parm.pcurv->guisection = _("Outputs");
parm.tcurv = G_define_standard_option(G_OPT_R_OUTPUT);
parm.tcurv->key = "tcurvature";
parm.tcurv->required = NO;
parm.tcurv->description =
_("Name for output tangential curvature raster map");
parm.tcurv->guisection = _("Outputs");
parm.dx = G_define_standard_option(G_OPT_R_OUTPUT);
parm.dx->key = "dx";
parm.dx->required = NO;
parm.dx->description =
_("Name for output first order partial derivative dx (E-W slope) raster map");
parm.dx->guisection = _("Outputs");
parm.dy = G_define_standard_option(G_OPT_R_OUTPUT);
parm.dy->key = "dy";
parm.dy->required = NO;
parm.dy->description =
_("Name for output first order partial derivative dy (N-S slope) raster map");
parm.dy->guisection = _("Outputs");
parm.dxx = G_define_standard_option(G_OPT_R_OUTPUT);
parm.dxx->key = "dxx";
parm.dxx->required = NO;
parm.dxx->description =
_("Name for output second order partial derivative dxx raster map");
parm.dxx->guisection = _("Outputs");
parm.dyy = G_define_standard_option(G_OPT_R_OUTPUT);
parm.dyy->key = "dyy";
parm.dyy->required = NO;
parm.dyy->description =
_("Name for output second order partial derivative dyy raster map");
parm.dyy->guisection = _("Outputs");
parm.dxy = G_define_standard_option(G_OPT_R_OUTPUT);
parm.dxy->key = "dxy";
parm.dxy->required = NO;
parm.dxy->description =
_("Name for output second order partial derivative dxy raster map");
parm.dxy->guisection = _("Outputs");
parm.zfactor = G_define_option();
parm.zfactor->key = "zscale";
parm.zfactor->description =
_("Multiplicative factor to convert elevation units to horizontal units");
parm.zfactor->type = TYPE_DOUBLE;
parm.zfactor->required = NO;
parm.zfactor->answer = "1.0";
parm.zfactor->guisection = _("Settings");
parm.min_slope = G_define_option();
parm.min_slope->key = "min_slope";
parm.min_slope->description =
_("Minimum slope value (in percent) for which aspect is computed");
parm.min_slope->type = TYPE_DOUBLE;
parm.min_slope->required = NO;
parm.min_slope->answer = "0.0";
parm.min_slope->guisection = _("Settings");
flag.a = G_define_flag();
flag.a->key = 'a';
flag.a->description =
_("Do not align the current region to the raster elevation map");
flag.a->guisection = _("Settings");
flag.e = G_define_flag();
flag.e->key = 'e';
flag.e->description =
_("Compute output at edges and near NULL values");
flag.e->guisection = _("Settings");
flag.n = G_define_flag();
flag.n->key = 'n';
flag.n->label =
_("Create aspect as degrees clockwise from North (azimuth), with flat = -9999");
flag.n->description =
_("Default: degrees counter-clockwise from East, with flat = 0");
flag.n->guisection = _("Settings");
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
radians_to_degrees = 180.0 / M_PI;
degrees_to_radians = M_PI / 180.0;
compute_at_edges = flag.e->answer;
/* INC BY ONE
answer[0] = 0.0;
answer[91] = 15000.0;
for (i = 1; i < 91; i++)
{
degrees = i - .5;
tan_ans = tan ( degrees / radians_to_degrees );
answer[i] = tan_ans * tan_ans;
}
*/
answer[0] = 0.0;
answer[90] = 15000.0;
for (i = 0; i < 90; i++) {
degrees = i + .5;
tan_ans = tan(degrees / radians_to_degrees);
answer[i] = tan_ans * tan_ans;
}
elev_name = parm.elevation->answer;
slope_name = parm.slope->answer;
aspect_name = parm.aspect->answer;
pcurv_name = parm.pcurv->answer;
tcurv_name = parm.tcurv->answer;
dx_name = parm.dx->answer;
dy_name = parm.dy->answer;
dxx_name = parm.dxx->answer;
dyy_name = parm.dyy->answer;
dxy_name = parm.dxy->answer;
G_check_input_output_name(elev_name, slope_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, aspect_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, pcurv_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, tcurv_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, dx_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, dy_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, dxx_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, dyy_name, G_FATAL_EXIT);
G_check_input_output_name(elev_name, dxy_name, G_FATAL_EXIT);
if (sscanf(parm.zfactor->answer, "%lf", &zfactor) != 1 || zfactor <= 0.0) {
G_fatal_error(_("%s=%s - must be a positive number"),
parm.zfactor->key, parm.zfactor->answer);
}
if (sscanf(parm.min_slope->answer, "%lf", &min_slope) != 1 ||
min_slope < 0.0) {
G_fatal_error(_("%s=%s - must be a non-negative number"),
parm.min_slope->key,
parm.min_slope->answer);
}
slope_fmt = parm.slope_fmt->answer;
if (strcmp(slope_fmt, "percent") == 0)
perc = 1;
else if (strcmp(slope_fmt, "degrees") == 0)
deg = 1;
if (slope_name == NULL && aspect_name == NULL
&& pcurv_name == NULL && tcurv_name == NULL
&& dx_name == NULL && dy_name == NULL
&& dxx_name == NULL && dyy_name == NULL && dxy_name == NULL) {
G_fatal_error(_("You must specify at least one of the parameters: "
"<%s>, <%s>, <%s>, <%s>, <%s>, <%s>, <%s>, <%s> or <%s>"),
parm.slope->key, parm.aspect->key, parm.pcurv->key,
parm.tcurv->key, parm.dx->key, parm.dy->key,
parm.dxx->key, parm.dyy->key, parm.dxy->key);
}
G_get_window(&window);
/* set the window from the header for the elevation file */
if (!flag.a->answer) {
Rast_get_cellhd(elev_name, "", &cellhd);
Rast_align_window(&window, &cellhd);
Rast_set_window(&window);
/* probably not needed, just to make sure
* G_get_window() and Rast_get_window()
* return the same */
G_set_window(&window);
}
if (strcmp(parm.out_precision->answer, "DCELL") == 0)
out_type = DCELL_TYPE;
else if (strcmp(parm.out_precision->answer, "FCELL") == 0)
out_type = FCELL_TYPE;
else if (strcmp(parm.out_precision->answer, "CELL") == 0)
out_type = CELL_TYPE;
else
G_fatal_error(_("Wrong raster type: %s"), parm.out_precision->answer);
data_type = out_type;
/* data type is the type of data being processed,
out_type is type of map being created */
/* ? why not use Rast_map_type() then ? */
nrows = Rast_window_rows();
ncols = Rast_window_cols();
if (((window.west == (window.east - 360.))
|| (window.east == (window.west - 360.))) &&
(G_projection() == PROJECTION_LL)) {
Wrap = 1;
ncols += 2;
}
else
Wrap = 0;
/* H = window.ew_res * 4 * 2/ zfactor; *//* horizontal (east-west) run
times 4 for weighted difference */
/* V = window.ns_res * 4 * 2/ zfactor; *//* vertical (north-south) run
times 4 for weighted difference */
/* we don't assume vertical units to be meters any more */
factor = G_database_units_to_meters_factor();
if (factor != 1.0 && zfactor != 1.0)
G_warning(_("r.slope.aspect does not convert horizontal units "
"to meters in this version, see manual page."));
G_begin_distance_calculations();
north = Rast_row_to_northing(0.5, &window);
ns_med = Rast_row_to_northing(1.5, &window);
south = Rast_row_to_northing(2.5, &window);
east = Rast_col_to_easting(2.5, &window);
west = Rast_col_to_easting(0.5, &window);
V = G_distance(east, north, east, south) * 4 / (factor * zfactor);
H = G_distance(east, ns_med, west, ns_med) * 4 / (factor * zfactor);
/* ____________________________
|c1 |c2 |c3 |
| | | |
| | north | |
| | | |
|________|________|________|
|c4 |c5 |c6 |
| | | |
| west | ns_med | east |
| | | |
|________|________|________|
|c7 |c8 |c9 |
| | | |
| | south | |
| | | |
|________|________|________|
*/
/* open the elevation file for reading */
elevation_fd = Rast_open_old(elev_name, "");
elev_cell[0] = (DCELL *) G_calloc(ncols + 2, sizeof(DCELL));
Rast_set_d_null_value(elev_cell[0], ncols + 2);
elev_cell[1] = (DCELL *) G_calloc(ncols + 2, sizeof(DCELL));
Rast_set_d_null_value(elev_cell[1], ncols + 2);
elev_cell[2] = (DCELL *) G_calloc(ncols + 2, sizeof(DCELL));
Rast_set_d_null_value(elev_cell[2], ncols + 2);
if (slope_name != NULL) {
slope_fd = Rast_open_new(slope_name, out_type);
slp_raster = Rast_allocate_buf(data_type);
}
else {
slp_raster = NULL;
slope_fd = -1;
}
if (aspect_name != NULL) {
aspect_fd = Rast_open_new(aspect_name, out_type);
asp_raster = Rast_allocate_buf(data_type);
}
else {
asp_raster = NULL;
aspect_fd = -1;
}
if (pcurv_name != NULL) {
pcurv_fd = Rast_open_new(pcurv_name, out_type);
pcurv_raster = Rast_allocate_buf(data_type);
}
else {
pcurv_raster = NULL;
pcurv_fd = -1;
}
if (tcurv_name != NULL) {
tcurv_fd = Rast_open_new(tcurv_name, out_type);
tcurv_raster = Rast_allocate_buf(data_type);
}
else {
tcurv_raster = NULL;
tcurv_fd = -1;
}
if (dx_name != NULL) {
dx_fd = Rast_open_new(dx_name, out_type);
dx_raster = Rast_allocate_buf(data_type);
}
else {
dx_raster = NULL;
dx_fd = -1;
}
if (dy_name != NULL) {
dy_fd = Rast_open_new(dy_name, out_type);
dy_raster = Rast_allocate_buf(data_type);
}
else {
dy_raster = NULL;
dy_fd = -1;
}
if (dxx_name != NULL) {
dxx_fd = Rast_open_new(dxx_name, out_type);
dxx_raster = Rast_allocate_buf(data_type);
}
else {
dxx_raster = NULL;
dxx_fd = -1;
}
if (dyy_name != NULL) {
dyy_fd = Rast_open_new(dyy_name, out_type);
dyy_raster = Rast_allocate_buf(data_type);
}
else {
dyy_raster = NULL;
dyy_fd = -1;
}
if (dxy_name != NULL) {
dxy_fd = Rast_open_new(dxy_name, out_type);
dxy_raster = Rast_allocate_buf(data_type);
}
else {
dxy_raster = NULL;
dxy_fd = -1;
}
if (aspect_fd < 0 && slope_fd < 0 && pcurv_fd < 0 && tcurv_fd < 0
&& dx_fd < 0 && dy_fd < 0 && dxx_fd < 0 && dyy_fd < 0 && dxy_fd < 0)
exit(EXIT_FAILURE);
Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, 0);
if (Wrap) {
elev_cell[2][0] = elev_cell[1][Rast_window_cols()];
elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][1];
}
G_verbose_message(_("Percent complete..."));
for (row = 0; row < nrows; row++) {
/* if projection is Lat/Lon, recalculate V and H */
if (G_projection() == PROJECTION_LL) {
north = Rast_row_to_northing((row - 1 + 0.5), &window);
ns_med = Rast_row_to_northing((row + 0.5), &window);
south = Rast_row_to_northing((row + 1 + 0.5), &window);
east = Rast_col_to_easting(2.5, &window);
west = Rast_col_to_easting(0.5, &window);
V = G_distance(east, north, east, south) * 4 / (factor * zfactor);
H = G_distance(east, ns_med, west, ns_med) * 4 / (factor * zfactor);
/* ____________________________
|c1 |c2 |c3 |
| | | |
| | north | |
| | | |
|________|________|________|
|c4 |c5 |c6 |
| | | |
| west | ns_med | east |
| | | |
|________|________|________|
|c7 |c8 |c9 |
| | | |
| | south | |
| | | |
|________|________|________|
*/
}
G_percent(row, nrows, 2);
temp = elev_cell[0];
elev_cell[0] = elev_cell[1];
elev_cell[1] = elev_cell[2];
elev_cell[2] = temp;
if (row < nrows - 1)
Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, row + 1);
else
Rast_set_d_null_value(elev_cell[2], ncols + 2);
if (Wrap) {
elev_cell[2][0] = elev_cell[2][Rast_window_cols()];
elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][1];
}
pc1 = elev_cell[0];
pc2 = elev_cell[1];
pc3 = elev_cell[2];
if (aspect_fd >= 0) {
asp_ptr = asp_raster;
}
if (slope_fd >= 0) {
slp_ptr = slp_raster;
}
if (pcurv_fd >= 0) {
pcurv_ptr = pcurv_raster;
}
if (tcurv_fd >= 0) {
tcurv_ptr = tcurv_raster;
}
if (dx_fd >= 0) {
dx_ptr = dx_raster;
}
if (dy_fd >= 0) {
dy_ptr = dy_raster;
}
if (dxx_fd >= 0) {
dxx_ptr = dxx_raster;
}
if (dyy_fd >= 0) {
dyy_ptr = dyy_raster;
}
if (dxy_fd >= 0) {
dxy_ptr = dxy_raster;
}
for (col = ncols; col-- > 0; pc1++, pc2++, pc3++) {
c1 = *pc1;
c2 = *(pc1 + 1);
c3 = *(pc1 + 2);
c4 = *pc2;
c5 = *(pc2 + 1);
c6 = *(pc2 + 2);
c7 = *pc3;
c8 = *(pc3 + 1);
c9 = *(pc3 + 2);
/* DEBUG:
fprintf(stdout, "\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n",
c1, c2, c3, c4, c5, c6, c7, c8, c9);
*/
if (Rast_is_d_null_value(&c5) || (!compute_at_edges &&
(Rast_is_d_null_value(&c1) || Rast_is_d_null_value(&c2) ||
Rast_is_d_null_value(&c3) || Rast_is_d_null_value(&c4) ||
Rast_is_d_null_value(&c6) || Rast_is_d_null_value(&c7) ||
Rast_is_d_null_value(&c8) || Rast_is_d_null_value(&c9)))) {
if (slope_fd > 0) {
Rast_set_null_value(slp_ptr, 1, data_type);
slp_ptr =
G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
}
if (aspect_fd > 0) {
Rast_set_null_value(asp_ptr, 1, data_type);
asp_ptr =
G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
}
if (pcurv_fd > 0) {
Rast_set_null_value(pcurv_ptr, 1, data_type);
pcurv_ptr =
G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
}
if (tcurv_fd > 0) {
Rast_set_null_value(tcurv_ptr, 1, data_type);
tcurv_ptr =
G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
}
if (dx_fd > 0) {
Rast_set_null_value(dx_ptr, 1, data_type);
dx_ptr =
G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
}
if (dy_fd > 0) {
Rast_set_null_value(dy_ptr, 1, data_type);
dy_ptr =
G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
}
if (dxx_fd > 0) {
Rast_set_null_value(dxx_ptr, 1, data_type);
dxx_ptr =
G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
}
if (dyy_fd > 0) {
Rast_set_null_value(dyy_ptr, 1, data_type);
dyy_ptr =
G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
}
if (dxy_fd > 0) {
Rast_set_null_value(dxy_ptr, 1, data_type);
dxy_ptr =
G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
}
continue;
} /* no data */
if (compute_at_edges) {
/* same method like ComputeVal in gdaldem_lib.cpp */
if (Rast_is_d_null_value(&c1))
c1 = c5;
if (Rast_is_d_null_value(&c2))
c2 = c5;
if (Rast_is_d_null_value(&c3))
c3 = c5;
if (Rast_is_d_null_value(&c4))
c4 = c5;
if (Rast_is_d_null_value(&c6))
c6 = c5;
if (Rast_is_d_null_value(&c7))
c7 = c5;
if (Rast_is_d_null_value(&c8))
c8 = c5;
if (Rast_is_d_null_value(&c9))
c9 = c5;
}
dx = ((c1 + c4 + c4 + c7) - (c3 + c6 + c6 + c9)) / H;
dy = ((c7 + c8 + c8 + c9) - (c1 + c2 + c2 + c3)) / V;
/* compute topographic parameters */
key = dx * dx + dy * dy;
slp_in_perc = 100 * sqrt(key);
slp_in_deg = atan(sqrt(key)) * radians_to_degrees;
/* now update min and max */
if (deg) {
if (min_slp > slp_in_deg)
min_slp = slp_in_deg;
if (max_slp < slp_in_deg)
max_slp = slp_in_deg;
}
else {
if (min_slp > slp_in_perc)
min_slp = slp_in_perc;
if (max_slp < slp_in_perc)
max_slp = slp_in_perc;
}
if (slp_in_perc < min_slope)
slp_in_perc = 0.;
if (deg && out_type == CELL_TYPE) {
/* INC BY ONE
low = 1;
hi = 91;
*/
low = 0;
hi = 90;
test = 20;
while (hi >= low) {
if (key >= answer[test])
low = test + 1;
else if (key < answer[test - 1])
hi = test - 1;
else
break;
test = (low + hi) / 2;
}
}
else if (perc && out_type == CELL_TYPE)
/* INCR_BY_ONE */
/* test = slp_in_perc + 1.5; *//* All the slope categories are
incremented by 1 */
test = slp_in_perc + .5;
if (slope_fd > 0) {
if (data_type == CELL_TYPE)
*((CELL *) slp_ptr) = (CELL) test;
else {
if (deg)
Rast_set_d_value(slp_ptr,
(DCELL) slp_in_deg, data_type);
else
Rast_set_d_value(slp_ptr,
(DCELL) slp_in_perc, data_type);
}
slp_ptr = G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
} /* computing slope */
if (aspect_fd > 0) {
double aspect_flat = 0.;
if (slp_in_perc == 0.)
aspect = 0.;
else if (dx == 0) {
if (dy > 0)
aspect = 90.;
else
aspect = 270.;
}
else {
aspect = (atan2(dy, dx) / degrees_to_radians);
if (aspect <= 0.)
aspect = 360. + aspect;
}
if (flag.n->answer) {
aspect_flat = -9999;
aspect = aspect_cw_n(aspect);
}
if (out_type == CELL_TYPE) {
if (aspect > 0 && aspect < 0.5)
aspect = 360;
*((CELL *) asp_ptr) = (CELL) floor(aspect + .5);
}
else
Rast_set_d_value(asp_ptr,
(DCELL) aspect, data_type);
asp_ptr = G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
/* now update min and max */
if (aspect > aspect_flat && min_asp > aspect)
min_asp = aspect;
if (max_asp < aspect)
max_asp = aspect;
} /* computing aspect */
if (dx_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) dx_ptr) = (CELL) (scik1 * dx);
else
Rast_set_d_value(dx_ptr, (DCELL) dx, data_type);
dx_ptr = G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
}
if (dy_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) dy_ptr) = (CELL) (scik1 * dy);
else
Rast_set_d_value(dy_ptr, (DCELL) dy, data_type);
dy_ptr = G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
}
if (dxx_fd <= 0 && dxy_fd <= 0 && dyy_fd <= 0 &&
pcurv_fd <= 0 && tcurv_fd <= 0)
continue;
/* compute second order derivatives */
s4 = c1 + c3 + c7 + c9 - c5 * 8.;
s5 = c4 * 4. + c6 * 4. - c8 * 2. - c2 * 2.;
s6 = c8 * 4. + c2 * 4. - c4 * 2. - c6 * 2.;
s3 = c7 - c9 + c3 - c1;
dxx = -(s4 + s5) / ((3. / 32.) * H * H);
dyy = -(s4 + s6) / ((3. / 32.) * V * V);
dxy = -s3 / ((1. / 16.) * H * V);
if (dxx_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) dxx_ptr) = (CELL) (scik1 * dxx);
else
Rast_set_d_value(dxx_ptr, (DCELL) dxx, data_type);
dxx_ptr = G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
}
if (dyy_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) dyy_ptr) = (CELL) (scik1 * dyy);
else
Rast_set_d_value(dyy_ptr, (DCELL) dyy, data_type);
dyy_ptr = G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
}
if (dxy_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) dxy_ptr) = (CELL) (scik1 * dxy);
else
Rast_set_d_value(dxy_ptr, (DCELL) dxy, data_type);
dxy_ptr = G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
}
/* compute curvature */
if (pcurv_fd <= 0 && tcurv_fd <= 0)
continue;
grad2 = key; /*dx2 + dy2 */
grad = sqrt(grad2);
if (grad <= gradmin) {
pcurv = 0.;
tcurv = 0.;
}
else {
dnorm1 = sqrt(grad2 + 1.);
dxy2 = 2. * dxy * dx * dy;
dx2 = dx * dx;
dy2 = dy * dy;
pcurv = (dxx * dx2 + dxy2 + dyy * dy2) /
(grad2 * dnorm1 * dnorm1 * dnorm1);
tcurv = (dxx * dy2 - dxy2 + dyy * dx2) / (grad2 * dnorm1);
if (c1min > pcurv)
c1min = pcurv;
if (c1max < pcurv)
c1max = pcurv;
if (c2min > tcurv)
c2min = tcurv;
if (c2max < tcurv)
c2max = tcurv;
}
if (pcurv_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) pcurv_ptr) = (CELL) (scik1 * pcurv);
else
Rast_set_d_value(pcurv_ptr, (DCELL) pcurv, data_type);
pcurv_ptr =
G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
}
if (tcurv_fd > 0) {
if (out_type == CELL_TYPE)
*((CELL *) tcurv_ptr) = (CELL) (scik1 * tcurv);
else
Rast_set_d_value(tcurv_ptr, (DCELL) tcurv, data_type);
tcurv_ptr =
G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
}
} /* column for loop */
if (aspect_fd > 0)
Rast_put_row(aspect_fd, asp_raster, data_type);
if (slope_fd > 0)
Rast_put_row(slope_fd, slp_raster, data_type);
if (pcurv_fd > 0)
Rast_put_row(pcurv_fd, pcurv_raster, data_type);
if (tcurv_fd > 0)
Rast_put_row(tcurv_fd, tcurv_raster, data_type);
if (dx_fd > 0)
Rast_put_row(dx_fd, dx_raster, data_type);
if (dy_fd > 0)
Rast_put_row(dy_fd, dy_raster, data_type);
if (dxx_fd > 0)
Rast_put_row(dxx_fd, dxx_raster, data_type);
if (dyy_fd > 0)
Rast_put_row(dyy_fd, dyy_raster, data_type);
if (dxy_fd > 0)
Rast_put_row(dxy_fd, dxy_raster, data_type);
} /* row loop */
G_percent(row, nrows, 2);
Rast_close(elevation_fd);
G_debug(1, "Creating support files...");
G_verbose_message(_("Elevation products for mapset <%s> in <%s>"),
G_mapset(), G_location());
if (aspect_fd >= 0) {
DCELL min, max;
struct FPRange range;
Rast_close(aspect_fd);
if (out_type != CELL_TYPE)
Rast_quantize_fp_map_range(aspect_name, G_mapset(), 0., 360., 0,
360);
Rast_read_cats(aspect_name, G_mapset(), &cats);
if (flag.n->answer)
Rast_set_cats_title
("Aspect clockwise in degrees from north", &cats);
else
Rast_set_cats_title
("Aspect counterclockwise in degrees from east", &cats);
G_verbose_message(_("Min computed aspect %.4f, max computed aspect %.4f"),
min_asp, max_asp);
/* the categries quant intervals are 1.0 long, plus
we are using reverse order so that the label looked up
for i-.5 is not the one defined for i-.5, i+.5 interval, but
the one defile for i-1.5, i-.5 interval which is added later */
if (!flag.n->answer) {
for (i = ceil(max_asp); i >= 1; i--) {
if (i == 360)
sprintf(buf, "east");
else if (i == 45)
sprintf(buf, "north ccw of east");
else if (i == 90)
sprintf(buf, "north");
else if (i == 135)
sprintf(buf, "north ccw of west");