test_osm1.c is a simple demonstration tool for OSM file formats.This sample code provides an example of:
Please note: the output produced by test_osm1 is usually verbose, so redirecting the standard output to a disk file is strongly recommended.
#include <stdio.h>
static int
print_node (
const void *user_data,
const readosm_node * node)
{
char buf[128];
int i;
if (user_data != NULL)
user_data = NULL;
#if defined(_WIN32) || defined(__MINGW32__)
sprintf (buf,
"%I64d", node->
id);
#else
sprintf (buf,
"%lld", node->
id);
#endif
printf ("\t<node id=\"%s\"", buf);
printf (
" lat=\"%1.7f\"", node->
latitude);
printf (
" version=\"%d\"", node->
version);
{
#if defined(_WIN32) || defined(__MINGW32__)
#else
#endif
printf (" changeset=\"%s\"", buf);
}
printf (
" user=\"%s\"", node->
user);
printf (
" uid=\"%d\"", node->
uid);
printf (
" timestamp=\"%s\"", node->
timestamp);
printf (" />\n");
else
{
printf (">\n");
{
printf (
"\t\t<tag k=\"%s\" v=\"%s\" />\n", tag->
key,
}
printf ("\t</node>\n");
}
}
static int
print_way (
const void *user_data,
const readosm_way * way)
{
char buf[128];
int i;
if (user_data != NULL)
user_data = NULL;
#if defined(_WIN32) || defined(__MINGW32__)
sprintf (buf,
"%I64d", way->
id);
#else
sprintf (buf,
"%lld", way->
id);
#endif
printf ("\t<way id=\"%s\"", buf);
printf (
" version=\"%d\"", way->
version);
{
#if defined(_WIN32) || defined(__MINGW32__)
#else
#endif
printf (" changeset=\"%s\"", buf);
}
printf (
" user=\"%s\"", way->
user);
printf (
" uid=\"%d\"", way->
uid);
printf (
" timestamp=\"%s\"", way->
timestamp);
printf (" />\n");
else
{
printf (">\n");
{
#if defined(_WIN32) || defined(__MINGW32__)
sprintf (buf,
"%I64d", *(way->
node_refs + i));
#else
sprintf (buf,
"%lld", *(way->
node_refs + i));
#endif
printf ("\t\t<nd ref=\"%s\" />\n", buf);
}
{
printf (
"\t\t<tag k=\"%s\" v=\"%s\" />\n", tag->
key,
}
printf ("\t</way>\n");
}
}
static int
{
char buf[128];
int i;
if (user_data != NULL)
user_data = NULL;
#if defined(_WIN32) || defined(__MINGW32__)
sprintf (buf,
"%I64d", relation->
id);
#else
sprintf (buf,
"%lld", relation->
id);
#endif
printf ("\t<relation id=\"%s\"", buf);
printf (
" version=\"%d\"", relation->
version);
{
#if defined(_WIN32) || defined(__MINGW32__)
#else
#endif
printf (" changeset=\"%s\"", buf);
}
if (relation->
user != NULL)
printf (
" user=\"%s\"", relation->
user);
printf (
" uid=\"%d\"", relation->
uid);
printf (
" timestamp=\"%s\"", relation->
timestamp);
printf (" />\n");
else
{
printf (">\n");
{
#if defined(_WIN32) || defined(__MINGW32__)
sprintf (buf,
"%I64d", member->
id);
#else
sprintf (buf,
"%lld", member->
id);
#endif
{
printf ("\t\t<member type=\"node\" ref=\"%s\"", buf);
break;
printf ("\t\t<member type=\"way\" ref=\"%s\"", buf);
break;
printf ("\t\t<member type=\"relation\" ref=\"%s\"", buf);
break;
default:
printf ("\t\t<member ref=\"%s\"", buf);
break;
};
if (member->
role != NULL)
printf (
" role=\"%s\" />\n", member->
role);
else
printf (" />\n");
}
{
tag = relation->
tags + i;
printf (
"\t\t<tag k=\"%s\" v=\"%s\" />\n", tag->
key,
}
printf ("\t</relation>\n");
}
}
int
main (int argc, char *argv[])
{
const void *osm_handle;
int ret;
if (argc != 2)
{
fprintf (stderr, "usage: test_osm1 path-to-OSM-file\n");
return -1;
}
{
fprintf (stderr, "OPEN error: %d\n", ret);
goto stop;
}
ret =
readosm_parse (osm_handle, (
const void *) 0, print_node, print_way,
print_relation);
{
fprintf (stderr, "PARSE error: %d\n", ret);
goto stop;
}
fprintf (stderr, "Ok, OSM input file successfully parsed\n");
stop:
return 0;
}