-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspatialite_dem.c
3689 lines (3678 loc) · 116 KB
/
spatialite_dem.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
/*
/ spatialite_dem
/
/ a tool for setting Z coordinates from XYZ files
/
/ version 1.0, 2017-09-14
/
/ Author: Mark Johnson, Berlin Germany [email protected]
/
/ Copyright (C) 2017 Alessandro Furieri
/
/ This program is free software: you can redistribute it and/or modify
/ it under the terms of the GNU General Public License as published by
/ the Free Software Foundation, either version 3 of the License, or
/ (at your option) any later version.
/
/ This program is distributed in the hope that it will be useful,
/ but WITHOUT ANY WARRANTY; without even the implied warranty of
/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/ GNU General Public License for more details.
/
/ You should have received a copy of the GNU General Public License
/ along with this program. If not, see <http://www.gnu.source_geom/licenses/>.
/
*/
// -- -- ---------------------------------- --
// astyle spatialite_dem.c
// valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all --log-file=valgrind.spatialite_dem.utm_fetchz.txt ./spatialite_dem -fetchz_xy 24747.115253 20725.147344
// -- -- ---------------------------------- --
#if defined(_WIN32) && !defined(__MINGW32__)
/* MSVC strictly requires this include [off_t] */
#include <sys/types.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#if defined(_WIN32) && !defined(__MINGW32__)
#include "config-msvc.h"
#else
#include "config.h"
#endif
#if defined(_WIN32)
#include <io.h>
#include <direct.h>
#else
#include "config.h"
#include <dirent.h>
#endif
#ifdef SPATIALITE_AMALGAMATION
#include <spatialite/sqlite3.h>
#else
#include <sqlite3.h>
#endif
#include <spatialite/gaiaaux.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#endif /* not WIN32 */
// -- -- ---------------------------------- --
#define ARG_NONE 0
#define ARG_DB_PATH 1
#define ARG_TABLE 2
#define ARG_COL 3
#define ARG_DEM_PATH 4
#define ARG_TABLE_DEM 5
#define ARG_COL_DEM 6
#define ARG_RESOLUTION_DEM 7
#define ARG_COPY_M 8
#define ARG_COMMAND_TYPE 9
#define ARG_FETCHZ_X 10
#define ARG_FETCHZ_Y 11
#define ARG_FETCHZ_XY 12
#define ARG_DEFAULT_SRID 13
// -- -- ---------------------------------- --
#define CMD_DEM_SNIFF 100
#define CMD_DEM_FETCHZ 101
#define CMD_DEM_CREATE 102
#define CMD_DEM_IMPORT_XYZ 103
#define CMD_DEM_UPDATEZ 104
// -- -- ---------------------------------- --
#define CONF_TYPE_DEM 1
#define CONF_TYPE_SOURCE 2
// -- -- ---------------------------------- --
// Definitions used for dem-conf
// -- -- ---------------------------------- --
#define MAXBUF 1024
#define DELIM "="
#ifdef _WIN32
#define LENGTHNL 2
#else
#define LENGTHNL 1
#endif /* not WIN32 */
// -- -- ---------------------------------- --
// Output of time elapse
// min = (int)(time_diff.tv_sec/60);
// secs = (int)(time_diff.tv_sec-(min*60));
// msecs = (int)(time_diff.tv_usec/1000);
// printf("%d mins %02d.%03d secs",min,sec,msecs);
// -- -- ---------------------------------- --
int timeval_subtract (struct timeval *time_diff, struct timeval *time_end, struct timeval *time_start, char **time_message)
{
int diff=0;
int days=0;
int hours=0;
int mins=0;
int secs=0;
int msecs=0;
// Perform the carry for the later subtraction by updating time_start.
if (time_end->tv_usec < time_start->tv_usec)
{
int nsec = (time_start->tv_usec - time_end->tv_usec) / 1000000 + 1;
time_start->tv_usec -= 1000000 * nsec;
time_start->tv_sec += nsec;
}
if (time_end->tv_usec - time_start->tv_usec > 1000000)
{
int nsec = (time_end->tv_usec - time_start->tv_usec) / 1000000;
time_start->tv_usec += 1000000 * nsec;
time_start->tv_sec -= nsec;
}
// Compute the time remaining to wait. tv_usec is certainly positive.
time_diff->tv_sec = time_end->tv_sec - time_start->tv_sec;
time_diff->tv_usec = time_end->tv_usec - time_start->tv_usec;
// -- -- ---------------------------------- --
diff = (int)time_diff->tv_sec;
if (diff > 86400 )
{// sec per day
days = diff / 86400;
diff = diff-(days*86400);
}
if (diff > 3660 )
{// sec per hour
hours = diff / 3660;
diff = diff -(hours*3660);
}
if (diff > 60 )
{
mins = diff / 60;
}
secs = diff - (mins * 60);
msecs = (int)(time_diff->tv_usec/1000);
// -- -- ---------------------------------- --
if (*time_message)
{
sqlite3_free(*time_message);
*time_message = NULL;
}
if ( days > 0)
{
*time_message = sqlite3_mprintf(">> time needed: %2 days %02d hours %02d mins %02d secs %02d milli-secs", days, hours, mins, secs,msecs);
} else if ( hours > 0)
{
*time_message = sqlite3_mprintf(">> time needed: %02d hours %02d mins %02d secs %02d milli-secs", hours, mins, secs,msecs);
} else if ( mins > 0)
{
*time_message = sqlite3_mprintf(">> time needed: %02d mins %02d secs %02d milli-secs", mins, secs,msecs);
}
else if (secs > 0 )
{
*time_message = sqlite3_mprintf(">> time needed: %02d secs %02d milli-secs", secs,msecs);
}
else
{
*time_message = sqlite3_mprintf(">> time needed: %02d milli-secs",msecs);
}
// -- -- ---------------------------------- --
// Return 1 if time_diff is negative.
return time_end->tv_sec < time_start->tv_sec;
// -- -- ---------------------------------- --
}
// -- -- ---------------------------------- --
// dem-conf structure
// -- -- ---------------------------------- --
struct config_dem
{
char dem_path[MAXBUF];
char dem_table[MAXBUF];
char dem_geometry[MAXBUF];
double dem_extent_minx;
double dem_extent_miny;
double dem_extent_maxx;
double dem_extent_maxy;
double dem_resolution;
int dem_srid;
unsigned int dem_rows_count;
int default_srid;
// Not to be used for conf_file [internal use only]
char *schema;
double fetchz_x;
double fetchz_y;
double dem_z;
double dem_m;
int has_z;
int has_m;
int has_spatial_index;
int is_spatial_table;
int config_type; // dem=0 ; source=1;
unsigned int id_rowid; // For debugging
unsigned int count_points; // For debugging
unsigned int count_points_nr; // For debugging
};
// -- -- ---------------------------------- --
// Reading dem-conf
// Environment var 'SPATIALITE_DEM'
// - with path to dem-conf
// -> must be created by
// (or using same syntax as) write_demconfig
// -- -- ---------------------------------- --
// if not found, the tool will look for a
// - 'spatialite_dem.conf' file in the active directory
// if not found, default values will be used
// -- -- ---------------------------------- --
// - any line not starting
// -> with a '#' [comment]
// -> not containing a '=' will be ignored
// - any other line will look for specific
// keywords before the '='
// -> unknown keywords will be ignored
// -- -- ---------------------------------- --
struct config_dem get_demconfig(char *conf_filename, int verbose)
{
struct config_dem config_struct;
// -- -- ---------------------------------- --
// Setting default values
// -- -- ---------------------------------- --
strcpy(config_struct.dem_path,"");
strcpy(config_struct.dem_table,"");
strcpy(config_struct.dem_geometry,"");
config_struct.dem_extent_minx=0.0;
config_struct.dem_extent_miny=0.0;
config_struct.dem_extent_maxx=0.0;
config_struct.dem_extent_maxy=0.0;
config_struct.dem_resolution=0.0;
config_struct.dem_srid=-2; // invalid
config_struct.default_srid=-2; // invalid
config_struct.dem_rows_count=0;
// -- -- ---------------------------------- --
// Not to be used for conf_file [internal use only]
// -- -- ---------------------------------- --
config_struct.fetchz_x=0.0;
config_struct.fetchz_y=0.0;
config_struct.dem_z=0.0;
config_struct.dem_m=0.0;
config_struct.has_z=0;
config_struct.has_m=0;
config_struct.has_spatial_index=0;
config_struct.is_spatial_table=0;
config_struct.schema=NULL;
config_struct.config_type=-1;
config_struct.id_rowid=0; // For debugging
config_struct.count_points=0; // For debugging
config_struct.count_points_nr=0; // For debugging
// -- -- ---------------------------------- --
if ((conf_filename) && (strlen(conf_filename) > 0) )
{
FILE *conf_file = fopen(conf_filename, "r");
if (conf_file != NULL)
{
char line[MAXBUF];
while(fgets(line, sizeof(line), conf_file) != NULL)
{
char *conf_parm=NULL;
char *conf_value=NULL;
conf_parm=(char *)line;
conf_value = strstr((char *)line,DELIM);
// Skip any comments (#) lines that may also contain a '='
if ( (conf_parm[0] != '#') && (conf_value) )
{
conf_parm[(int)(conf_value-conf_parm)]=0;
conf_value = conf_value + strlen(DELIM);
conf_value[strcspn(conf_value, "\r\n")] = 0;
// printf("parm[%s] value[%s]\n",conf_parm,conf_value);
if (strcmp(conf_parm,"dem_path") == 0)
{
strcpy(config_struct.dem_path,conf_value);
// printf("%s[%s]\n",conf_parm,config_struct.dem_path);
} else if (strcmp(conf_parm,"dem_table") == 0)
{
strcpy(config_struct.dem_table,conf_value);
// printf("%s[%s]\n",conf_parm,config_struct.dem_table);
} else if (strcmp(conf_parm,"dem_geometry") == 0)
{
strcpy(config_struct.dem_geometry,conf_value);
// printf("%s[%s]\n",conf_parm,config_struct.dem_geometry);
} else if (strcmp(conf_parm,"dem_extent_minx") == 0)
{
config_struct.dem_extent_minx=atof(conf_value);
//printf("%s[%2.7f]\n",conf_parm,config_struct.dem_extent_minx);
} else if (strcmp(conf_parm,"dem_extent_miny") == 0)
{
config_struct.dem_extent_miny=atof(conf_value);
//printf("%s[%2.7f]\n",conf_parm,config_struct.dem_extent_miny);
} else if (strcmp(conf_parm,"dem_extent_maxx") == 0)
{
config_struct.dem_extent_maxx=atof(conf_value);
//printf("%s[%2.7f]\n",conf_parm,config_struct.dem_extent_maxx);
} else if (strcmp(conf_parm,"dem_extent_maxy") == 0)
{
config_struct.dem_extent_maxy=atof(conf_value);
//printf("%s[%2.7f]\n",conf_parm,config_struct.dem_extent_maxy);
} else if (strcmp(conf_parm,"dem_resolution") == 0)
{
config_struct.dem_resolution=atof(conf_value);
//printf("%s[%2.7f]\n",conf_parm,config_struct.dem_resolution);
} else if (strcmp(conf_parm,"dem_srid") == 0)
{
config_struct.dem_rows_count=atol(conf_value);
// printf("%s[%d]\n",conf_parm,config_struct.dem_rows_count);
} else if (strcmp(conf_parm,"dem_srid") == 0)
{
config_struct.dem_srid=atoi(conf_value);
// printf("%s[%d]\n",conf_parm,config_struct.dem_srid);
} else if (strcmp(conf_parm,"default_srid") == 0)
{
config_struct.default_srid=atoi(conf_value);
// printf("%s[%d]\n",conf_parm,config_struct.default_srid);
}
}
} // End while
fclose(conf_file);
} // End if file
else
{
if (strcmp(conf_filename,"spatialite_dem.conf") != 0)
{
if (verbose)
{
fprintf(stderr, "-E-> spatialite_dem: not found: conf_filename[%s]\n",conf_filename);
}
}
}
}
return config_struct;
}
// -- -- ---------------------------------- --
// writing dem-conf
// - any line not starting
// -> with a '#' [comment]
// -> not containing a '=' will be ignored
// - any other line will look for specific
// keywords before the '='
// -> unknown keywords will be ignored
// -- -- ---------------------------------- --
// Saving dem-conf '-save_conf'
// - will save to files set in 'SPATIALITE_DEM'
// - if empty to the default 'spatialite_dem.conf'
// -- -- ---------------------------------- --
int write_demconfig(char *conf_filename, struct config_dem config_struct)
{
int rc=0;
FILE *conf_file = fopen(conf_filename, "w");
if (conf_file != NULL)
{
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fprintf(conf_file, "# For use with spatialite_dem\n");
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fprintf(conf_file, "# export SPATIALITE_DEM=%s\n",conf_filename);
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fprintf(conf_file, "# Full path to Spatialite-Database containing a Dem-POINTZ (or POINTZM) Geometry\n");
fprintf(conf_file, "dem_path=%s\n", config_struct.dem_path);
fprintf(conf_file, "# Table-Name containing a Dem-POINTZ (or POINTZM) Geometry\n");
fprintf(conf_file, "dem_table=%s\n", config_struct.dem_table);
fprintf(conf_file, "# Geometry-Column containing a Dem-POINTZ (or POINTZM) Geometry\n");
fprintf(conf_file, "dem_geometry=%s\n", config_struct.dem_geometry);
fprintf(conf_file, "# Srid of the Dem-Geometry\n");
fprintf(conf_file, "dem_srid=%d\n", config_struct.dem_srid);
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fprintf(conf_file, "# Area around given point to Query Dem-Geometry in units of Dem-Srid\n");
fprintf(conf_file, "# -> Rule: a Dem with 1m resolution: min=0.50\n");
fprintf(conf_file, "dem_resolution=%2.7f\n", config_struct.dem_resolution);
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fprintf(conf_file, "# Default Srid to use for queries against Dem-Geometry [-fetchz_xy, -updatez]\n");
fprintf(conf_file, "default_srid=%d\n", config_struct.default_srid);
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fprintf(conf_file, "# Count of rows in Dem-Geometry\n");
fprintf(conf_file, "dem_rows_count=%u\n", config_struct.dem_rows_count);
fprintf(conf_file, "# Min X of Dem-Geometry\n");
fprintf(conf_file, "dem_extent_minx=%2.7f\n", config_struct.dem_extent_minx);
fprintf(conf_file, "# Max X of Dem-Geometry\n");
fprintf(conf_file, "dem_extent_maxx=%2.7f\n", config_struct.dem_extent_maxx);
fprintf(conf_file, "# Min Y of Dem-Geometry\n");
fprintf(conf_file, "dem_extent_miny=%2.7f\n", config_struct.dem_extent_miny);
fprintf(conf_file, "# Max Y of Dem-Geometry\n");
fprintf(conf_file, "dem_extent_maxy=%2.7f\n", config_struct.dem_extent_maxy);
fprintf(conf_file, "# Width of Dem-Area in Srid-Units\n");
fprintf(conf_file, "dem_extent_width=%2.7f\n", (config_struct.dem_extent_maxx-config_struct.dem_extent_minx));
fprintf(conf_file, "# Height of Dem-Area in Srid-Units\n");
fprintf(conf_file, "dem_extent_height=%2.7f\n", (config_struct.dem_extent_maxy-config_struct.dem_extent_miny));
fprintf(conf_file, "# -- -- ---------------------------------- --\n");
fclose(conf_file);
rc=1;
}
return rc;
}
// -- -- ---------------------------------- --
// From a given point, build area around it by 'resolution_dem'
// - utm 0.999 meters, Solder Berlin 1.0375644 meters
// (resolution_dem/2) could also be done
// - but to insure that at least 1 point is returned, left as is
// The nearest point will always be retrieved, or none at all.
// -- -- ---------------------------------- --
static int
insert_dem_points(sqlite3 *db_handle, struct config_dem *dem_config, double *xx_source, double *yy_source, double *zz_source, int verbose)
{
/* checking for 3D geometries - version 4 */
int ret=0;
int ret_insert=SQLITE_OK;
int i=0;
char *sql_statement = NULL;
sqlite3_stmt *stmt = NULL;
char *sql_err = NULL;
if (zz_source)
{
sql_statement = sqlite3_mprintf("INSERT INTO \"%s\" (point_x,point_y, point_z,\"%s\") "
"VALUES(?,?,?,MakePointZ(?,?,?,%d)) ",dem_config->dem_table,dem_config->dem_geometry,dem_config->dem_srid);
ret = sqlite3_prepare_v2( db_handle, sql_statement, -1, &stmt, NULL );
if ( ret == SQLITE_OK )
{
sqlite3_free(sql_statement);
if (sqlite3_exec(db_handle, "BEGIN", NULL, NULL, &sql_err) == SQLITE_OK)
{
for (i=0; i<(int)(dem_config->count_points); i++)
{
if (ret_insert != SQLITE_ABORT )
{
// Note: sqlite3_bind_* index is 1-based, os apposed to sqlite3_column_* that is 0-based.
sqlite3_bind_double(stmt, 1, xx_source[i]);
sqlite3_bind_double(stmt, 2, yy_source[i]);
sqlite3_bind_double(stmt, 3, zz_source[i]);
sqlite3_bind_double(stmt, 4, xx_source[i]);
sqlite3_bind_double(stmt, 5, yy_source[i]);
sqlite3_bind_double(stmt, 6, zz_source[i]);
dem_config->count_points_nr=i;
ret_insert = sqlite3_step( stmt );
if ( ret_insert == SQLITE_DONE || ret_insert == SQLITE_ROW )
{
ret_insert=SQLITE_OK;
}
else
{
ret_insert=SQLITE_ABORT;
}
}
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
xx_source[i]=0.0;
yy_source[i]=0.0;
zz_source[i]=0.0;
}
ret=0;
if (ret_insert == SQLITE_ABORT )
{
if (sqlite3_exec(db_handle, "ROLLBACK", NULL, NULL, &sql_err) == SQLITE_OK)
{
ret=0;
}
}
else
{
if (sqlite3_exec(db_handle, "COMMIT", NULL, NULL, &sql_err) == SQLITE_OK)
{
ret = 1;
dem_config->dem_rows_count+=dem_config->count_points;
dem_config->count_points=0;
dem_config->count_points_nr=0;
}
}
}
sqlite3_finalize( stmt );
if (sql_err)
{
sqlite3_free(sql_err);
}
}
else
{
if (verbose)
{
fprintf(stderr, "-W-> insert_dem_points: rc=%d sql[%s]\n",ret,sql_statement);
}
sqlite3_free(sql_statement);
}
}
return ret;
}
// -- -- ---------------------------------- --
// From a given point, build area around it by 'resolution_dem'
// - utm 0.999 meters, Solder Berlin 1.0375644 meters
// (resolution_dem/2) could also be done
// - but to insure that at least 1 point is returned, left as is
// The nearest point will always be retrieved, or none at all.
// -- -- ---------------------------------- --
static int
retrieve_dem_points(sqlite3 *db_handle, struct config_dem *dem_config, int count_points, double *xx_source, double *yy_source, double *zz, double *mm, int *count_z, int *count_m, int verbose)
{
/* checking for 3D geometries - version 4 */
int ret=0;
int i=0;
char *sql_statement = NULL;
sqlite3_stmt *stmt = NULL;
int has_m = 0;
double x_source=0.0;
double y_source=0.0;
double z_source=0.0;
double m_source=0.0;
*count_z=0;
*count_m=0;
if (mm)
{
has_m = 1;
}
if (zz)
{
for (i=0; i<count_points; i++)
{
dem_config->count_points_nr=i;
x_source=xx_source[i];
y_source=yy_source[i];
/* checking the GEOMETRY_COLUMNS table */
if (has_m)
{
sql_statement = sqlite3_mprintf("SELECT ST_Z(\"%s\"), ST_M(\"%s\") "
"FROM '%s'.'%s' WHERE (ROWID IN ( "
"SELECT ROWID FROM SpatialIndex WHERE ( "
"(f_table_name = 'DB=%s.%s') AND "
"(f_geometry_column = '%s') AND "
"(search_frame = ST_Buffer(MakePoint(%2.7f,%2.7f,%d),%2.7f)))) AND "
"(ST_ClosestPoint(%s, MakePoint(%2.7f,%2.7f,%d)) IS NOT Null) ) "
"ORDER BY ST_Distance(%s,MakePoint(%2.7f,%2.7f,%d)) ASC LIMIT 1"
,dem_config->dem_geometry,dem_config->dem_geometry,dem_config->schema,dem_config->dem_table,dem_config->schema,dem_config->dem_table
,dem_config->dem_geometry,x_source,y_source,dem_config->dem_srid,dem_config->dem_resolution
,dem_config->dem_geometry,x_source,y_source,dem_config->dem_srid,dem_config->dem_geometry,x_source,y_source,dem_config->dem_srid);
}
else
{
sql_statement = sqlite3_mprintf("SELECT ST_Z(\"%s\") "
"FROM '%s'.'%s' WHERE (ROWID IN ( "
"SELECT ROWID FROM SpatialIndex WHERE ("
"(f_table_name = 'DB=%s.%s') AND "
"(f_geometry_column = '%s') AND "
"(search_frame = ST_Buffer(MakePoint(%2.7f,%2.7f,%d),%2.7f)))) AND "
"(ST_ClosestPoint(%s, MakePoint(%2.7f,%2.7f,%d)) IS NOT Null) ) "
"ORDER BY ST_Distance(%s,MakePoint(%2.7f,%2.7f,%d)) ASC LIMIT 1"
,dem_config->dem_geometry,dem_config->schema,dem_config->dem_table,dem_config->schema,dem_config->dem_table
,dem_config->dem_geometry,x_source,y_source,dem_config->dem_srid,dem_config->dem_resolution
,dem_config->dem_geometry,x_source,y_source,dem_config->dem_srid,dem_config->dem_geometry,x_source,y_source,dem_config->dem_srid);
}
ret = sqlite3_prepare_v2( db_handle, sql_statement, -1, &stmt, NULL );
#if 0
if ((dem_config->id_rowid == 354) && (dem_config->count_points == 207))
{
fprintf(stderr, "-III-> [EXTERIOR RING] -1a- cnt[%d,%d,%d] sql[%s] id_rowid[%d]\n",dem_config->count_points,dem_config->count_points_nr,ret,sql_statement,dem_config->id_rowid);
}
#endif
if ( ret == SQLITE_OK )
{
sqlite3_free(sql_statement);
while ( sqlite3_step( stmt ) == SQLITE_ROW )
{
if ( sqlite3_column_type( stmt, 0 ) != SQLITE_NULL )
{
z_source = sqlite3_column_double (stmt, 0);
if ( (z_source != 0.0 ) && (zz[i] != z_source ) )
{// Do not force an update if everything is 0 or has not otherwise changed
zz[i] = z_source;
*count_z += 1;
}
}
if ( sqlite3_column_type( stmt, 1 ) != SQLITE_NULL )
{
m_source = sqlite3_column_double (stmt, 1);
if ( (m_source != 0.0 ) && (mm[i] != m_source ) )
{// Do not force an update if everything is 0 or has not otherwise changed
mm[i] = m_source;
*count_m += 1;
}
mm[i] = sqlite3_column_double (stmt, 1);
}
}
sqlite3_finalize( stmt );
}
else
{
if (verbose)
{
fprintf(stderr, "-W-> retrieve_dem_points: rc=%d sql[%s]\n",ret,sql_statement);
}
sqlite3_free(sql_statement);
}
}
}
// printf("-I-> retrieve_dem_points: total[%d] not 0.0: z[%d] m[%d]\n",count_points,*count_z,*count_m);
if (*count_z > 0)
return 1;
return 0;
}
// -- -- ---------------------------------- --
//
// -- -- ---------------------------------- --
static int
callFetchZ(sqlite3 *db_handle, struct config_dem *dem_config, int verbose)
{
int ret=0;
char *sql_statement = NULL;
sqlite3_stmt *stmt = NULL;
int i_count_z=0;
int i_count_m=0;
double *xx_use = NULL;
double *yy_use = NULL;
double *mm_use = NULL;
double *zz = NULL;
// -- -- ---------------------------------- --
dem_config->dem_z=0;;
dem_config->dem_m=0;
// -- -- ---------------------------------- --
if (dem_config->dem_srid != dem_config->default_srid )
{
sql_statement = sqlite3_mprintf("SELECT ST_X(ST_Transform(MakePoint(%2.7f,%2.7f,%d),%d)), "
"ST_Y(ST_Transform(MakePoint(%2.7f,%2.7f,%d),%d))"
,dem_config->fetchz_x, dem_config->fetchz_y, dem_config->default_srid,dem_config->dem_srid
,dem_config->fetchz_x, dem_config->fetchz_y, dem_config->default_srid,dem_config->dem_srid);
ret = sqlite3_prepare_v2( db_handle, sql_statement, -1, &stmt, NULL );
if ( ret == SQLITE_OK )
{
sqlite3_free(sql_statement);
while ( sqlite3_step( stmt ) == SQLITE_ROW )
{
if (( sqlite3_column_type( stmt, 0 ) != SQLITE_NULL ) &&
( sqlite3_column_type( stmt, 1 ) != SQLITE_NULL ) )
{
dem_config->fetchz_x = sqlite3_column_double(stmt, 0);
dem_config->fetchz_y = sqlite3_column_double(stmt, 1);
}
}
sqlite3_finalize( stmt );
}
else
{
if (verbose)
{
fprintf(stderr, "-W-> callFetchZ : rc=%d sql[%s]\n",ret,sql_statement);
}
sqlite3_free(sql_statement);
}
}
// -- -- ---------------------------------- --
ret=0;
if ((dem_config->fetchz_x >= dem_config->dem_extent_minx) && (dem_config->fetchz_x <= dem_config->dem_extent_maxx) &&
(dem_config->fetchz_y >= dem_config->dem_extent_miny) && (dem_config->fetchz_y <= dem_config->dem_extent_maxy ) )
{
xx_use = malloc(sizeof (double) * 1);
yy_use = malloc(sizeof (double) * 1);
zz = malloc(sizeof (double) * 1);
mm_use = malloc(sizeof (double) * 1);
xx_use[0] = dem_config->fetchz_x;
yy_use[0] = dem_config->fetchz_y;
zz[0] = dem_config->dem_z;
mm_use[0] = dem_config->dem_m;
dem_config->count_points=1;
if (retrieve_dem_points(db_handle, dem_config, 1, xx_use, yy_use,zz,mm_use,&i_count_z, &i_count_m,verbose))
{
ret=1;
dem_config->dem_z=zz[0];
dem_config->dem_m=mm_use[0];
}
free(xx_use);
free(yy_use);
free(zz);
free(mm_use);
xx_use = NULL;
yy_use = NULL;
mm_use = NULL;
zz = NULL;
}
// -- -- ---------------------------------- --
return ret;
}
// -- -- ---------------------------------- --
// GNU libc (Linux, and FreeBSD)
// - sys/param.h
// -- -- ---------------------------------- --
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
// -- -- ---------------------------------- --
// Based on gg_transform.c gaiaTransformCommon
// if the source geometry is out of range of the dem area, NULL is returned
// if the if the z or m values have not changed, NULL is returned
// - in both cases no update should be done and is not an error
// if the Dem-Database Geometry-Column uses a different Srid
// - a second Geometry will be sent with the transfored values
// --> those x,y values will be sent to retrieve_dem_points
// to retrieve the nearst point
// The retrieved z,m values will be copied WITHOUT any changes
// -- -- ---------------------------------- --
static gaiaGeomCollPtr
getDemCollect(sqlite3 *db_handle, gaiaGeomCollPtr source_geom, gaiaGeomCollPtr dem_geom, struct config_dem *dem_config,
int *count_points_total, int *count_z_total, int *count_m_total, int verbose)
{
// creates a new GEOMETRY replacing found z-points from the original one
int ib=0;
int cnt=0;
int i=0;
double *xx = NULL;
double *yy = NULL;
double *xx_dem = NULL;
double *yy_dem = NULL;
double *zz = NULL;
double *mm = NULL;
double *xx_use = NULL;
double *yy_use = NULL;
double *mm_use = NULL;
int count_z=0;
int count_m=0;
int i_count_z=0;
int i_count_m=0;
double x = 0.0;
double y = 0.0;
double z = 0.0;
double m = 0.0;
double extent_minx=0.0;
double extent_miny=0.0;
double extent_maxx=0.0;
double extent_maxy=0.0;
int error = 0;
int isExtentWithin=0;
gaiaPointPtr pt = NULL;
gaiaPointPtr pt_dem = NULL;
gaiaLinestringPtr ln = NULL;
gaiaLinestringPtr ln_dem = NULL;
gaiaLinestringPtr dst_ln = NULL;
gaiaPolygonPtr pg = NULL;
gaiaPolygonPtr pg_dem = NULL;
gaiaPolygonPtr dst_pg = NULL;
gaiaRingPtr rng = NULL;
gaiaRingPtr rng_dem = NULL;
gaiaRingPtr dst_rng = NULL;
gaiaGeomCollPtr dst = NULL;
if (source_geom->DimensionModel == GAIA_XY_Z)
dst = gaiaAllocGeomCollXYZ();
else if (source_geom->DimensionModel == GAIA_XY_M)
dst = gaiaAllocGeomCollXYM();
else if (source_geom->DimensionModel == GAIA_XY_Z_M)
dst = gaiaAllocGeomCollXYZM();
else
dst = gaiaAllocGeomColl ();
cnt = 0;
dst->Srid = source_geom->Srid;
pt = source_geom->FirstPoint;
extent_minx=source_geom->MinX;
extent_miny=source_geom->MinY;
extent_maxx=source_geom->MaxX;
extent_maxy=source_geom->MaxY;
if (dem_geom)
{
extent_minx=dem_geom->MinX;
extent_miny=dem_geom->MinY;
extent_maxx=dem_geom->MaxX;
extent_maxy=dem_geom->MaxY;
}
// -- -- ---------------------------------- --
// Touches (partially within)
// -- -- ---------------------------------- --
int left_x = MAX(extent_minx, dem_config->dem_extent_minx);
int right_x = MIN(extent_maxx, dem_config->dem_extent_maxx);
int bottom_y = MAX(extent_miny, dem_config->dem_extent_miny);
int top_y = MIN(extent_maxy, dem_config->dem_extent_maxy);
if ((right_x > left_x) && (top_y > bottom_y))
{
isExtentWithin=1;
}
else if ((right_x == left_x) && (top_y == bottom_y))
{
// This is a point
isExtentWithin=1;
}
// -- -- ---------------------------------- --
#if 0
if (verbose)
{
fprintf(stderr, "-I-> getDemCollect: isExtentWithin[%d] left_x[%d] right_x[%d] bottom_y[%d] top_y[%d]\n",isExtentWithin,left_x,right_x,bottom_y,top_y);
}
#endif
if (!isExtentWithin)
{
error=1;
goto stop;
}
// -- -- ---------------------------------- --
// Call only if geometry is partially within the Dem extent
// -- -- ---------------------------------- --
// Points
// -- -- ---------------------------------- --
while (pt)
{
// counting POINTs
cnt++;
pt = pt->Next;
}
if (cnt)
{
// reprojecting POINTs
xx = malloc(sizeof (double) * cnt);
yy = malloc(sizeof (double) * cnt);
if (dem_geom)
{
xx_dem = malloc(sizeof (double) * cnt);
yy_dem = malloc(sizeof (double) * cnt);
xx_use = xx_dem;
yy_use = yy_dem;
}
else
{
xx_use = xx;
yy_use = yy;
}
zz = malloc(sizeof (double) * cnt);
if (source_geom->DimensionModel == GAIA_XY_M || source_geom->DimensionModel == GAIA_XY_Z_M)
mm = malloc(sizeof (double) * cnt);
i = 0;
pt = source_geom->FirstPoint;
if (dem_geom)
{
pt_dem = dem_geom->FirstPoint;
}
while (pt)
{
// inserting points to be converted in temporary arrays
xx[i] = pt->X;
yy[i] = pt->Y;
if (source_geom->DimensionModel == GAIA_XY_Z || source_geom->DimensionModel == GAIA_XY_Z_M)
zz[i] = pt->Z;
else
zz[i] = 0.0;
if (source_geom->DimensionModel == GAIA_XY_M || source_geom->DimensionModel == GAIA_XY_Z_M)
mm[i] = pt->M;
if (pt_dem)
{
xx_dem[i] = pt_dem->X;
yy_dem[i] = pt_dem->Y;
}
i++;
// -- -- ---------------------------------- --
// MultiPoints, next
// -- -- ---------------------------------- --
pt = pt->Next;
if (dem_geom)
{
pt_dem =pt_dem->Next;
}
}
if ((dem_config->has_m) && (mm) )
{
mm_use = mm;
}
// searching for nearest point
*count_points_total+=cnt;
i_count_z=0;
i_count_m=0;
dem_config->count_points=cnt;
if (retrieve_dem_points(db_handle, dem_config, cnt, xx_use, yy_use,zz,mm_use,&i_count_z, &i_count_m,verbose))
{
*count_z_total+=i_count_z;
*count_m_total+=i_count_m;
count_z+=i_count_z;
count_m+=i_count_m;
}
xx_use = NULL;
yy_use = NULL;
mm_use = NULL;
// inserting the reprojected POINTs in the new GEOMETRY
for (i = 0; i < cnt; i++)
{
x = xx[i];
y = yy[i];
if (source_geom->DimensionModel == GAIA_XY_Z || source_geom->DimensionModel == GAIA_XY_Z_M)
z = zz[i];
else
z = 0.0;
if (source_geom->DimensionModel == GAIA_XY_M || source_geom->DimensionModel == GAIA_XY_Z_M)
m = mm[i];
else
m = 0.0;
if (dst->DimensionModel == GAIA_XY_Z)
gaiaAddPointToGeomCollXYZ(dst, x, y, z);
else if (dst->DimensionModel == GAIA_XY_M)
gaiaAddPointToGeomCollXYM(dst, x, y, m);
else if (dst->DimensionModel == GAIA_XY_Z_M)
gaiaAddPointToGeomCollXYZM(dst, x, y, z, m);
else
gaiaAddPointToGeomColl(dst, x, y);
}
free(xx);
free(yy);
free(zz);
xx = NULL;
yy = NULL;
zz = NULL;
if (xx_dem)
{
free(xx_dem);
xx_dem = NULL;
free(yy_dem);
yy_dem = NULL;
}
if (source_geom->DimensionModel == GAIA_XY_M || source_geom->DimensionModel == GAIA_XY_Z_M)
{
free(mm);
mm = NULL;
}
}
if (error)
goto stop;
// -- -- ---------------------------------- --
// Linestrings
// -- -- ---------------------------------- --
ln = source_geom->FirstLinestring;
if (dem_geom)
{
ln_dem = dem_geom->FirstLinestring;
}
// Call only if geometry is inside the Dem extent
while (ln)
{
// reprojecting LINESTRINGs
cnt = ln->Points;
xx = malloc(sizeof (double) * cnt);
yy = malloc(sizeof (double) * cnt);
if (dem_geom)
{
xx_dem = malloc(sizeof (double) * cnt);
yy_dem = malloc(sizeof (double) * cnt);
xx_use = xx_dem;
yy_use = yy_dem;
}
else
{
xx_use = xx;
yy_use = yy;
}
zz = malloc(sizeof (double) * cnt);
if (ln->DimensionModel == GAIA_XY_M || ln->DimensionModel == GAIA_XY_Z_M)
mm = malloc(sizeof (double) * cnt);
for (i = 0; i < cnt; i++)
{
// inserting points to be converted in temporary arrays
if (ln->DimensionModel == GAIA_XY_Z)
{
gaiaGetPointXYZ(ln->Coords, i, &x, &y, &z);
}
else if (ln->DimensionModel == GAIA_XY_M)
{
gaiaGetPointXYM(ln->Coords, i, &x, &y, &m);
}
else if (ln->DimensionModel == GAIA_XY_Z_M)
{
gaiaGetPointXYZM(ln->Coords, i, &x, &y, &z, &m);
}
else
{
gaiaGetPoint(ln->Coords, i, &x, &y);
}
xx[i] = x;
yy[i] = y;
if (ln_dem)
{
if (ln_dem->DimensionModel == GAIA_XY_Z)
{
gaiaGetPointXYZ(ln_dem->Coords, i, &x, &y, &z);
}
else if (ln_dem->DimensionModel == GAIA_XY_M)
{
gaiaGetPointXYM(ln->Coords, i, &x, &y, &m);
}
else if (ln_dem->DimensionModel == GAIA_XY_Z_M)
{
gaiaGetPointXYZM(ln_dem->Coords, i, &x, &y, &z, &m);
}
else
{
gaiaGetPoint(ln_dem->Coords, i, &x, &y);
}
xx_dem[i] = x;
yy_dem[i] = y;
}
if (ln->DimensionModel == GAIA_XY_Z || ln->DimensionModel == GAIA_XY_Z_M)
zz[i] = z;
else
zz[i] = 0.0;
if (ln->DimensionModel == GAIA_XY_M || ln->DimensionModel == GAIA_XY_Z_M)
mm[i] = m;