forked from Gnonthgol/sosi2osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
101 lines (83 loc) · 2.59 KB
/
node.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
#include "sosi2osm.h"
#include <algorithm>
#include <proj_api.h>
projPJ origProj, osmProj;
void setProjection(const char* proj) {
if (!(origProj = pj_init_plus(proj)) )
exit(1);
if (!(osmProj = pj_init_plus("+proj=latlong +datum=WGS84")) )
exit(1);
}
void getCoords(long int* size, double** lat, double** lon) {
*size = getSOSICoordsSize();
double* x = (double*)malloc(sizeof(double) * *size);
double* y = (double*)malloc(sizeof(double) * *size);
*lat = y;
*lon = x;
int i;
for (i = 0; i < *size; i++) {
getSOSICoord(i, x+i, y+i);
}
pj_transform(origProj, osmProj, *size, 1, x, y, NULL );
for (i = 0; i < *size; i++) {
x[i] = x[i]*RAD_TO_DEG;
y[i] = y[i]*RAD_TO_DEG;
}
}
long nodeId = -1;
long createNode(double lat, double lon, short kp, FILE* output) {
if (kp == 0) {
fprintf(output, "<node id=\"%ld\" lat=\"%.7f\" lon=\"%.7f\" version=\"1\" visible=\"true\"/>\n", nodeId, lat, lon);
return nodeId--;
}
static int sizeM = 0;
static int lenM = 0;
static double* latM = NULL;
static double* lonM = NULL;
static short* kpM = NULL;
static long* idM = NULL;
for (int i = 0; i < lenM; i++) {
if (lat == latM[i] && lon == lonM[i] && kp == kpM[i]) {
return idM[i];
}
}
if (lenM >= sizeM) {
sizeM = std::max(1024, sizeM*2);
latM = (double*)realloc(latM, sizeof(double) * sizeM);
lonM = (double*)realloc(lonM, sizeof(double) * sizeM);
kpM = (short*)realloc(kpM, sizeof(short) * sizeM);
idM = (long*)realloc(idM, sizeof(long) * sizeM);
}
latM[lenM] = lat;
lonM[lenM] = lon;
kpM[lenM] = kp;
idM[lenM] = nodeId;
lenM++;
fprintf(output, "<node id=\"%ld\" lat=\"%.7f\" lon=\"%.7f\" version=\"1\" visible=\"true\"/>\n", nodeId, lat, lon);
return nodeId--;
}
long int createNodes(long int** ids, FILE* output) {
long int size;
double *lat, *lon;
getCoords(&size, &lat, &lon);
long int *nd = (long*)malloc(sizeof(long) * size);
*ids = nd;
for (int i = 0; i < size; i++) {
nd[i] = createNode(lat[i], lon[i], LC_GetKp(i+1), output);
}
free(lat);
free(lon);
return size;
}
void outputNode(FILE* output) {
long int size;
double *lat, *lon;
getCoords(&size, &lat, &lon);
for (int i = 0; i < size; i++) {
fprintf(output, "<node id=\"%ld\" lat=\"%.7f\" lon=\"%.7f\" version=\"1\" visible=\"true\">\n", nodeId--, lat[i], lon[i]);
outputTags(output);
fprintf(output, "</node>\n");
}
free(lat);
free(lon);
}