-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.cpp
144 lines (129 loc) · 3.84 KB
/
context.cpp
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
#include <sys/queue.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <math.h>
#include "dataset.h"
static int endsWith(const char *str, const char *suffix);
static void joinPath(char *dst, size_t n, const char *dir, const char *file);
static int isDir(const char *path);
static int exist(const char *path);
static void contextAddDataset(struct context *ctx, const char *filepath, const char *srs);
struct dataset_item {
struct dataset *dataset;
LIST_ENTRY(dataset_item) entry;
};
LIST_HEAD(dataset_list, dataset_item);
struct context {
struct dataset_list datasets;
size_t num_datasets;
char *auth;
};
struct context *ContextCreate(const char *path, const char *srs, const char *auth) {
struct context *ctx = (context *) calloc(1, sizeof(struct context));
LIST_INIT(&ctx->datasets);
DIR *d;
struct dirent *ent;
char filepath[1024];
if (exist(path)) {
if (isDir(path)) {
d = opendir(path);
if (d) {
while ((ent = readdir(d)) != NULL) {
if (endsWith(ent->d_name, ".tif") || endsWith(ent->d_name, ".hgt")) {
joinPath(filepath, sizeof(filepath), path, ent->d_name);
contextAddDataset(ctx, filepath, srs);
}
}
closedir(d);
}
} else {
contextAddDataset(ctx, path, srs);
}
} else {
printf("%s: %s\n", strerror(ENOENT), path);
}
size_t n = strlen(auth);
if (n > 0) {
ctx->auth = (char *)malloc(n+1);
sprintf(ctx->auth, "%s", auth);
}
return ctx;
}
void ContextFree(struct context *ctx) {
if (!ctx) {
return;
}
if (ctx->auth) {
free(ctx->auth);
}
struct dataset_item *item;
LIST_FOREACH(item, &ctx->datasets, entry) {
if (item->dataset) {
DatasetFree(item->dataset);
}
free(item);
}
free(ctx);
}
const char *ContextAuth(struct context *ctx) {
return ctx->auth;
}
int ContextEmpty(struct context *ctx) {
return LIST_EMPTY(&ctx->datasets);
}
double ContextGetAltitude(struct context *ctx, double x, double y) {
struct dataset_item *item;
double alt;
LIST_FOREACH(item, &ctx->datasets, entry) {
alt = DatasetGetAltitude(item->dataset, x, y);
if (!isnan(alt)) {
return alt;
}
}
return NAN;
}
void contextAddDataset(struct context *ctx, const char *filepath, const char *srs) {
struct dataset *dataset = DatasetCreate(filepath, srs);
if (dataset) {
double top, left, bottom, right;
DatasetGetBounds(dataset, &top, &left, &bottom, &right);
printf("Dataset loaded: %s => (%f,%f,%f,%f)\n", filepath, top, left, bottom, right);
struct dataset_item *item = (struct dataset_item *) calloc(1, sizeof(struct dataset_item));
item->dataset = dataset;
LIST_INSERT_HEAD(&ctx->datasets, item, entry);
ctx->num_datasets++;
} else {
printf("Failed to load dataset: %s => %s\n", filepath, strerror(errno));
return;
}
}
int endsWith(const char *str, const char *suffix) {
if (!str || !suffix)
return 0;
size_t lenstr = strlen(str);
size_t lensuffix = strlen(suffix);
if (lensuffix > lenstr)
return 0;
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
}
void joinPath(char *dst, size_t n, const char *dir, const char *file) {
if (endsWith(dir, "/")) {
snprintf(dst, n, "%s%s", dir, file);
} else {
snprintf(dst, n, "%s/%s", dir, file);
}
}
int exist(const char *path) {
struct stat st;
return stat(path, &st) == 0;
}
int isDir(const char *path) {
struct stat st;
stat(path, &st);
return S_ISDIR(st.st_mode);
}