File: osmparser.cpp

package info (click to toggle)
mapnik 2.0.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,496 kB
  • sloc: cpp: 91,793; python: 6,051; xml: 3,528; sh: 848; makefile: 70; lisp: 10
file content (157 lines) | stat: -rw-r--r-- 4,154 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
// much of this is based on osm2pgsql

#include "osmparser.h"
#include "osm.h"
#include <string>
#include <cassert>

using std::cerr;
using std::endl;


 osm_item* osmparser::cur_item=NULL;
 long osmparser::curID=0;
 bool osmparser::in_node=false, osmparser::in_way=false;
 osm_dataset* osmparser::components=NULL;
 std::string osmparser::error="";
 std::map<long,osm_node*> osmparser::tmp_node_store=std::map<long,osm_node*>();

void osmparser::processNode(xmlTextReaderPtr reader)
{
  xmlChar *name = xmlTextReaderName(reader);
  if(name==NULL)
    name=xmlStrdup(BAD_CAST "--");

  switch(xmlTextReaderNodeType(reader))
  {
    case XML_READER_TYPE_ELEMENT:
      startElement(reader,name);
      break;

    case XML_READER_TYPE_END_ELEMENT:
      endElement(name);
  }
  xmlFree(name);
}

void osmparser::startElement(xmlTextReaderPtr reader, const xmlChar *name)
{
  std::string tags; 
  xmlChar *xid, *xlat, *xlon, *xk, *xv;

    if(xmlStrEqual(name,BAD_CAST "node"))
    {
      curID = 0;
      in_node = true;
      osm_node *node=new osm_node;
      xlat=xmlTextReaderGetAttribute(reader,BAD_CAST "lat");
      xlon=xmlTextReaderGetAttribute(reader,BAD_CAST "lon");
      xid=xmlTextReaderGetAttribute(reader,BAD_CAST "id");
      assert(xlat);
      assert(xlon);
      assert(xid);
      node->lat=atof((char*)xlat);  
      node->lon=atof((char*)xlon);  
      node->id = atol((char*)xid);
      cur_item = node;  
      tmp_node_store[node->id] = node;
      xmlFree(xid);
      xmlFree(xlon);
      xmlFree(xlat);
    }
    else if (xmlStrEqual(name,BAD_CAST "way")) 
    {
      curID=0;
      in_way = true;
      osm_way *way=new osm_way;
      xid=xmlTextReaderGetAttribute(reader,BAD_CAST "id");
      assert(xid);
      way->id = atol((char*)xid); 
      cur_item  =  way; 
      // Prevent ways with no name being assigned a name of "0"
      cur_item->keyvals["name"] = ""; 

      // HACK: allows comparison with "" in the XML file. Otherwise it
      // doesn't work. Only do for the most crucial tags for Freemap's
      // purposes.  TODO investigate why this is
      cur_item->keyvals["width"] = "";  
      cur_item->keyvals["horse"] = "";  
      cur_item->keyvals["foot"] = "";  
      cur_item->keyvals["bicycle"] = "";  
      xmlFree(xid);
    }
    else if (xmlStrEqual(name,BAD_CAST "nd"))
    {
      xid=xmlTextReaderGetAttribute(reader,BAD_CAST "ref");
      assert(xid);
      long ndid = atol((char*)xid); 
      if(tmp_node_store.find(ndid)!=tmp_node_store.end())
      {
        (static_cast<osm_way*>(cur_item))->nodes.push_back
            (tmp_node_store[ndid]);
      }
      xmlFree(xid);
    }
    else if (xmlStrEqual(name,BAD_CAST "tag"))
    {
      std::string key="", value="";
      xk = xmlTextReaderGetAttribute(reader,BAD_CAST "k");
      xv = xmlTextReaderGetAttribute(reader,BAD_CAST "v");
      assert(xk);
      assert(xv);
      cur_item->keyvals[(char*)xk] = (char*)xv; 
      xmlFree(xk);
      xmlFree(xv);
    }
}

void osmparser::endElement(const xmlChar* name)
{
  if(xmlStrEqual(name,BAD_CAST "node"))
  {
    in_node = false;
    components->add_node(static_cast<osm_node*>(cur_item));
  }
  else if(xmlStrEqual(name,BAD_CAST "way"))
  {
    in_way = false;
    components->add_way(static_cast<osm_way*>(cur_item));
  }
}

bool osmparser::parse(osm_dataset *ds, const char* filename)
{
  components=ds;
  xmlTextReaderPtr reader = xmlNewTextReaderFilename(filename);
  int ret=do_parse(reader);
  xmlFreeTextReader(reader);
  return (ret==0) ?  true:false;
}

bool osmparser::parse(osm_dataset *ds,char* data, int nbytes)
{
  // from cocoasamurai.blogspot.com/2008/10/getting-some-xml-love-with-
  // libxml2.html, converted from Objective-C to straight C

  components=ds;
  xmlTextReaderPtr reader = xmlReaderForMemory(data,nbytes,NULL,NULL,0);
  int ret=do_parse(reader);
  xmlFreeTextReader(reader);
  return (ret==0) ? true:false;
}

  
int osmparser::do_parse(xmlTextReaderPtr reader)
{
  int ret=-1;
  if(reader!=NULL)
  {
    ret = xmlTextReaderRead(reader);
    while(ret==1)
    {
      processNode(reader);
      ret=xmlTextReaderRead(reader);
    }
  }
  return ret;
}