1 | #include "BatchUploader.h" |
---|
2 | #include "Segment.h" |
---|
3 | |
---|
4 | namespace OpenStreetMap |
---|
5 | { |
---|
6 | |
---|
7 | // This method uploads a selected section of the GPX track as OSM nodes and |
---|
8 | // segments. |
---|
9 | // Loop through all trackpoints, make a node and upload each one |
---|
10 | // After loading nodes, create and upload the segments between them. |
---|
11 | void BatchUploader::batchUpload(int tp1, int tp2) |
---|
12 | { |
---|
13 | this->tp1 = tp1; |
---|
14 | this->tp2 = tp2; |
---|
15 | count = tp1; |
---|
16 | |
---|
17 | TrackPoint *tp = components->getTrackPoint(tp1); |
---|
18 | |
---|
19 | // Make a node from the current trackpoint, add to components |
---|
20 | nodes[count] = components->addNewNode |
---|
21 | (tp->getLat(), tp->getLon(), "", "node"); |
---|
22 | |
---|
23 | QObject::connect(osmhttp,SIGNAL(httpErrorOccurred(int,const QString&)), |
---|
24 | this,SLOT(handleHttpError(int,const QString&))); |
---|
25 | QObject::connect(osmhttp,SIGNAL(errorOccurred(const QString&)), |
---|
26 | this,SLOT(handleError(const QString&))); |
---|
27 | |
---|
28 | osmhttp->scheduleCommand("PUT","/api/0.3/node/0",nodes[count]->toOSM(), |
---|
29 | this, |
---|
30 | SLOT(nodeAdded(const QByteArray&,void*)), |
---|
31 | nodes[count],SLOT(handleError(const QString&))); |
---|
32 | } |
---|
33 | |
---|
34 | // nodeAdded() |
---|
35 | void BatchUploader::nodeAdded(const QByteArray& resp, void *node) |
---|
36 | { |
---|
37 | cerr << "BatchUploader::nodeAdded()" << endl; |
---|
38 | Node *n = (Node*)node; |
---|
39 | QString str = resp; |
---|
40 | QStringList ids; |
---|
41 | |
---|
42 | if(!str.isNull())cerr<<"STR=" << str << endl; |
---|
43 | ids = QStringList::split("\n", str); |
---|
44 | |
---|
45 | // Set the node ID to the ID returned |
---|
46 | if(n) |
---|
47 | { |
---|
48 | cerr << "node ID: " << ids[0] << endl; |
---|
49 | n->setOSMID(atoi(ids[0].ascii())); |
---|
50 | |
---|
51 | cerr << "count=" << count << endl; |
---|
52 | |
---|
53 | if(count != tp1) |
---|
54 | { |
---|
55 | cerr << "NOT FIRST NODE - SO UPLOADING A SEGMENT" << endl; |
---|
56 | if(nodes[count-1] && nodes[count]) |
---|
57 | { |
---|
58 | Segment *segx = |
---|
59 | components->addNewSegment(nodes[count-1],nodes[count]); |
---|
60 | |
---|
61 | osmhttp->scheduleCommand |
---|
62 | ("PUT","/api/0.3/segment/0",segx->toOSM(), |
---|
63 | this, SLOT(segmentAdded(const QByteArray&,void*)), |
---|
64 | segx, SLOT(handleError(const QString&))); |
---|
65 | } |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | cerr << "FIRST NODE - SO UPLOADING ANOTHER NODE" << endl; |
---|
70 | TrackPoint *tp = components->getTrackPoint(++count); |
---|
71 | |
---|
72 | // Make a node from the current trackpoint, add to components |
---|
73 | nodes[count] = components->addNewNode |
---|
74 | (tp->getLat(), tp->getLon(), "", "node"); |
---|
75 | |
---|
76 | osmhttp->scheduleCommand |
---|
77 | ("PUT","/api/0.3/node/0",nodes[count]->toOSM(), this, |
---|
78 | SLOT(nodeAdded(const QByteArray&,void*)), |
---|
79 | nodes[count],SLOT(handleError(const QString&))); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void BatchUploader::segmentAdded(const QByteArray& resp, void *segment) |
---|
86 | { |
---|
87 | cerr << "BatchUploader::segmentAdded()" << endl; |
---|
88 | |
---|
89 | Segment *s = (Segment*)segment; |
---|
90 | QString str = resp; |
---|
91 | QStringList ids; |
---|
92 | |
---|
93 | ids = QStringList::split("\n", str); |
---|
94 | |
---|
95 | // Set the node ID to the ID returned |
---|
96 | if(s) |
---|
97 | { |
---|
98 | s->setOSMID(atoi(ids[0].ascii())); |
---|
99 | cerr << "semgnet ID: " << ids[0] << endl; |
---|
100 | if(count<tp2) |
---|
101 | { |
---|
102 | TrackPoint *tp = components->getTrackPoint(++count); |
---|
103 | |
---|
104 | |
---|
105 | nodes[count] = components->addNewNode |
---|
106 | (tp->getLat(), tp->getLon(), "", "node"); |
---|
107 | osmhttp->scheduleCommand |
---|
108 | ("PUT","/api/0.3/node/0",nodes[count]->toOSM(), this, |
---|
109 | SLOT(nodeAdded(const QByteArray&,void*)), |
---|
110 | nodes[count],SLOT(handleError(const QString&))); |
---|
111 | } |
---|
112 | else |
---|
113 | emit done(); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | void BatchUploader::handleHttpError(int i,const QString& e) |
---|
118 | { |
---|
119 | QString eout; |
---|
120 | cerr << "HTTP error: " << i << " " << e << endl; |
---|
121 | eout.sprintf("HTTP error %d %s", i, e.ascii()); |
---|
122 | emit error(eout); |
---|
123 | } |
---|
124 | |
---|
125 | void BatchUploader::handleError(const QString& e) |
---|
126 | { |
---|
127 | cerr << "Error: " << e << endl; |
---|
128 | emit error(e); |
---|
129 | } |
---|
130 | |
---|
131 | } |
---|