File: node.cpp

package info (click to toggle)
sosi2osm 1.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 228 kB
  • sloc: cpp: 442; makefile: 24; sh: 11
file content (101 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download | duplicates (3)
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
#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) {
	if (kp == 0) {
    	printf("<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++;
    printf("<node id=\"%ld\" lat=\"%.7f\" lon=\"%.7f\" version=\"1\" visible=\"true\"/>\n", nodeId, lat, lon);
    return nodeId--;
}

long int createNodes(long int** ids) {
	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));
    }
    
    free(lat);
    free(lon);
    return size;
}

void outputNode() {
    long int size;
    double *lat, *lon;
    getCoords(&size, &lat, &lon);
    
    for (int i = 0; i < size; i++) {
        printf("<node id=\"%ld\" lat=\"%.7f\" lon=\"%.7f\" version=\"1\" visible=\"true\">\n", nodeId--, lat[i], lon[i]);
        outputTags();
        printf("</node>\n");
    }
    
    free(lat);
    free(lon);
}