NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

I'm working on XML file (in particula .osm file) with libxml2 and C language.

This is my XML source (not all but just a piece):

...
        < way id="9484424" visible="true" version="8" changeset="11179686" timestamp="2012-04-04T18:01:31Z" user="Davlak" uid="217070">
      < nd ref="72843570"/>
      < nd ref="963203594"/>
      < nd ref="612588857"/>
      < nd ref="300164764"/>
      < nd ref="72843572"/>
      < tag k="highway" v="unclassified"/>
      < tag k="name" v="Via Giorgio Pitacco"/> 
      < tag k="odbl" v="clean"/>
      < tag k="oneway" v="yes"/>
     < /way>
     < way id="11378242" visible="true" version="29" changeset="22876351" timestamp="2014-06-11T18:00:17Z" user="Davlak" uid="217070">
      < nd ref="101181728"/>
      < nd ref="2911704721"/>
      < nd ref="2911704714"/>
      < nd ref="101181731"/>
      < tag k="foot" v="yes"/>
      < tag k="highway" v="trunk_link"/>
      < tag k="lanes" v="2"/>
      < tag k="lit" v="yes"/>
      < tag k="maxspeed" v="40"/>
      < tag k="mofa" v="no"/>
      < tag k="moped" v="no"/>
      < tag k="motorroad" v="yes"/>
      < tag k="name" v="Circonvallazione Tiburtina"/>
      < tag k="oneway" v="yes"/>
      < tag k="sidewalk" v="right"/>
     < /way>
     < way id="20507982" visible="true" version="11" changeset="17228559" timestamp="2013-08-05T14:00:29Z" user="Davlak" uid="217070">
      < nd ref="219557958"/>
      < nd ref="1920238894"/>
      < nd ref="1701671630"/>
      < nd ref="1920238892"/>
      < nd ref="219557991"/>
      < tag k="highway" v="residential"/>
      < tag k="lit" v="yes"/>
      < tag k="name" v="Via Prenestina"/>
     < /way>
     < way id="20507983" visible="true" version="14" changeset="26174383" timestamp="2014-10-18T17:12:55Z" user="Privits" uid="2009312">
      < nd ref="219558042"/>
      < nd ref="2276045993"/>
      < nd ref="2023496104"/>
      < nd ref="219558031"/>
      < nd ref="2276045996"/>
      < nd ref="922740441"/>
      < nd ref="922740415"/>
      < nd ref="3137149509"/>
      < nd ref="1620785409"/>
      < nd ref="219558021"/>
      < nd ref="301486927"/>
      < nd ref="298561544"/>
      < tag k="highway" v="residential"/>
      < tag k="lanes" v="1"/>
      < tag k="name" v="Via Prenestina"/>
      < tag k="oneway" v="yes"/>
      < /way>

 ...

My output, right now , is (so using the following code i'm printing):

way: 9484424
 node: 72843570
 node: 963203594
 node: 612588857
 node: 300164764
 node: 72843572

and so on for each way element...

I would like to print, for each way, ONLY the first and the last node, in the same line, and the way that these nodes connect. For example:

node: 72843570 node: 72843572 way: 9484424

This is my code actually:


#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>


int is_leaf(xmlNode * node) {

    xmlNode * child = node->children;

    while(child)  {

        if(child->type == XML_ELEMENT_NODE) return 0;

    child = child->next;
  }

  return 1;
}

void print_xml(xmlNode * node, int indent_len) {
        char *name= "way";
        char *nd = "nd";
    while(node)    {

        if(node->type == XML_ELEMENT_NODE)  {
            if(strcmp (node->name, name) ==0)  {

                printf("%*c%s: %s\n", indent_len*2, ' ', node->name, is_leaf(node)?xmlNodeGetContent(node):xmlGetProp(node, "id"));

            }

        }

        if(node->type == XML_ELEMENT_NODE)  {
            if(strcmp (node->name, nd) ==0)  {

                printf("%*c%s: %s\n", indent_len*2, ' ', "node", is_leaf(node)?xmlNodeGetContent(node):xmlGetProp(node, "ref"));

            }

        }
        print_xml(node->children, indent_len + 1);
        node = node->next;

    }
}

int main() {
  xmlDoc *doc = NULL;
  xmlNode *root_element = NULL;

  doc = xmlReadFile("Casal.osm", NULL, 0);

  if (doc == NULL) {
    printf("Could not parse the XML file");
  }

  root_element = xmlDocGetRootElement(doc);

  print_xml(root_element, 1);

  xmlFreeDoc(doc);

  xmlCleanupParser();
}

I'm not able to save or to store the first and last node and print every way during the scan. How can I modify my code? THank you everybody.

asked 05 Apr '18, 11:11

claudiodipilla87's gravatar image

claudiodipil...
11334
accept rate: 0%

1

You need some data structure to store ways and their nodes. For example a list of lists, or a hash map that contains a list. There are various possible solutions. None of this has anything to do with OSM. However there are various existing tools for processing OSM files, for example osmium (which is C++, not C). You don't have to reinvent the wheel.

(06 Apr '18, 08:10) scai ♦

Using the osm format is good only for small files.

You can use osmium lib or o5mreader the o5m file reader in C/C++.

I think that the shortest way is to use o5mreader. The demo file is a perfect template for the purpose. If you have not o5m data files, just pipe the output of osmconvert to your script, it is efficient enough to get a production chain. Osmconvert can also bypass nodes and relations

osmium, osmconvert and o5mreader are referenced in the Osm wiki.

permanent link

answered 11 May '18, 02:44

AntaC's gravatar image

AntaC
13115
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×19
×5
×3
×2

question asked: 05 Apr '18, 11:11

question was seen: 1,988 times

last updated: 11 May '18, 02:44

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum