nodes-csv-lat-lon-name.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*****************************************************************
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the license contained in the
  4. * COPYING file that comes with the expat distribution.
  5. *
  6. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  7. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  8. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  9. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  10. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  11. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  12. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. *
  14. * Must be used with Expat compiled for UTF-8 output.
  15. */
  16. #include <iomanip>
  17. #include <iostream>
  18. #include <list>
  19. #include <map>
  20. #include <sstream>
  21. #include <string>
  22. #include <math.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <time.h>
  26. #include "../expat_justparse_interface.h"
  27. using namespace std;
  28. struct NamedNode
  29. {
  30. public:
  31. NamedNode() : lat(100.0), lon(200.0), name("") {}
  32. double lat, lon;
  33. string name;
  34. };
  35. map< unsigned int, NamedNode > nodes;
  36. list< unsigned int > stops;
  37. NamedNode nnode;
  38. unsigned int id;
  39. int direction(0);
  40. void start(const char *el, const char **attr)
  41. {
  42. if (!strcmp(el, "tag"))
  43. {
  44. string key, value;
  45. for (unsigned int i(0); attr[i]; i += 2)
  46. {
  47. if (!strcmp(attr[i], "k"))
  48. key = attr[i+1];
  49. else if (!strcmp(attr[i], "v"))
  50. value = attr[i+1];
  51. }
  52. if (key == "name")
  53. nnode.name = value;
  54. }
  55. else if (!strcmp(el, "node"))
  56. {
  57. for (unsigned int i(0); attr[i]; i += 2)
  58. {
  59. if (!strcmp(attr[i], "id"))
  60. id = atol(attr[i+1]);
  61. else if (!strcmp(attr[i], "lat"))
  62. nnode.lat = atof(attr[i+1]);
  63. else if (!strcmp(attr[i], "lon"))
  64. nnode.lon = atof(attr[i+1]);
  65. }
  66. }
  67. else if (!strcmp(el, "member"))
  68. {
  69. unsigned int ref(0);
  70. string type, role;
  71. for (unsigned int i(0); attr[i]; i += 2)
  72. {
  73. if (!strcmp(attr[i], "ref"))
  74. ref = atol(attr[i+1]);
  75. else if (!strcmp(attr[i], "type"))
  76. type = attr[i+1];
  77. else if (!strcmp(attr[i], "role"))
  78. role = attr[i+1];
  79. }
  80. if (type == "node")
  81. {
  82. if (direction == 0)
  83. stops.push_back(ref);
  84. else if (direction == 1)
  85. {
  86. if ((role == "forward_stop") || (role == "forward_stop_0"))
  87. stops.push_back(ref);
  88. }
  89. else if (direction == -1)
  90. {
  91. if ((role == "backward_stop") || (role == "backward_stop_0"))
  92. stops.push_front(ref);
  93. }
  94. }
  95. }
  96. }
  97. void end(const char *el)
  98. {
  99. if (!strcmp(el, "node"))
  100. {
  101. nodes[id] = nnode;
  102. }
  103. }
  104. int main(int argc, char *argv[])
  105. {
  106. if (argc == 2)
  107. {
  108. if (!strcmp(argv[1], "--forward"))
  109. {
  110. direction = 1;
  111. }
  112. if (!strcmp(argv[1], "--backward"))
  113. {
  114. direction = -1;
  115. }
  116. }
  117. //reading the main document
  118. parse(stdin, start, end);
  119. for (list< unsigned int >::const_iterator it(stops.begin()); it != stops.end(); ++it)
  120. {
  121. NamedNode nnode(nodes[*it]);
  122. if (nnode.lat <= 90.0)
  123. {
  124. cout<<setprecision(14)<<nnode.lat<<"\t"<<nnode.lon<<"\t"<<nnode.name<<"\n";
  125. }
  126. }
  127. return 0;
  128. }