Codebase list readosm / upstream/1.0.0e examples / test_osm1.c
upstream/1.0.0e

Tree @upstream/1.0.0e (Download .tar.gz)

test_osm1.c @upstream/1.0.0eraw · history · blame

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* 
/ test_osm1.c
/
/ libreadosm Sample code
/
/ Author: Sandro Furieri a.furieri@lqt.it
/
/ ------------------------------------------------------------------------------
/ 
/ Version: MPL 1.1/GPL 2.0/LGPL 2.1
/ 
/ The contents of this file are subject to the Mozilla Public License Version
/ 1.1 (the "License"); you may not use this file except in compliance with
/ the License. You may obtain a copy of the License at
/ http://www.mozilla.org/MPL/
/ 
/ Software distributed under the License is distributed on an "AS IS" basis,
/ WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
/ for the specific language governing rights and limitations under the
/ License.
/
/ The Original Code is the ReadOSM library
/
/ The Initial Developer of the Original Code is Alessandro Furieri
/ 
/ Portions created by the Initial Developer are Copyright (C) 2012
/ the Initial Developer. All Rights Reserved.
/ 
/ Contributor(s):
/ 
/ Alternatively, the contents of this file may be used under the terms of
/ either the GNU General Public License Version 2 or later (the "GPL"), or
/ the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
/ in which case the provisions of the GPL or the LGPL are applicable instead
/ of those above. If you wish to allow use of your version of this file only
/ under the terms of either the GPL or the LGPL, and not to allow others to
/ use your version of this file under the terms of the MPL, indicate your
/ decision by deleting the provisions above and replace them with the notice
/ and other provisions required by the GPL or the LGPL. If you do not delete
/ the provisions above, a recipient may use your version of this file under
/ the terms of any one of the MPL, the GPL or the LGPL.
/ 
*/

#include <stdio.h>

#include "readosm.h"

static int
print_node (const void *user_data, const readosm_node * node)
{
/* 
* printing an OSM Node (callback function) 
*
* this function is called by the OSM parser for each 
* NODE object found
*
* please note well: the passed pointer corresponds to
* a READ-ONLY object; you can can query any node-related
* value, but you cannot alter them.
*
************************************************
*
* this didactic sample will simply print the node object
* on the standard output adopting the appropriate OSM XML
* notation
*/
    char buf[128];
    int i;
    const readosm_tag *tag;

#if defined(_WIN32) || defined(__MINGW32__)
    /* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
    sprintf (buf, "%I64d", node->id);
#else
    sprintf (buf, "%lld", node->id);
#endif
    printf ("\t<node id=\"%s\"", buf);

/*
* some individual values may be set, or may be not
* unset values are identified by the READOSM_UNDEFINED
* conventional value, and must be consequently ignored
*/
    if (node->latitude != READOSM_UNDEFINED)
	printf (" lat=\"%1.7f\"", node->latitude);
    if (node->longitude != READOSM_UNDEFINED)
	printf (" lon=\"%1.7f\"", node->longitude);
    if (node->version != READOSM_UNDEFINED)
	printf (" version=\"%d\"", node->version);
    if (node->changeset != READOSM_UNDEFINED)
      {

#if defined(_WIN32) || defined(__MINGW32__)
	  /* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
	  sprintf (buf, "%I64d", node->changeset);
#else
	  sprintf (buf, "%lld", node->changeset);
#endif
	  printf (" changeset=\"%s\"", buf);
      }

/*
* unset string values are identified by a NULL pointer
* and must be consequently ignored
*/
    if (node->user != NULL)
	printf (" user=\"%s\"", node->user);
    if (node->uid != READOSM_UNDEFINED)
	printf (" uid=\"%d\"", node->uid);
    if (node->timestamp != NULL)
	printf (" timestamp=\"%s\"", node->timestamp);

/*
* the Node object may have its own tag list
* please note: this one is a variable-length list,
* and may be empty: in this case tag_count will be ZERO
*/
    if (node->tag_count == 0)
	printf (" />\n");
    else
      {
	  printf (">\n");
	  for (i = 0; i < node->tag_count; i++)
	    {
		/* we'll now print each <tag> for this node */
		tag = node->tags + i;
		printf ("\t\t<tag k=\"%s\" v=\"%s\" />\n", tag->key,
			tag->value);
	    }
	  printf ("\t</node>\n");
      }
    return READOSM_OK;
}

static int
print_way (const void *user_data, const readosm_way * way)
{
/* 
* printing an OSM Way (callback function) 
*
* this function is called by the OSM parser for each 
* WAY object found
*
* please note well: the passed pointer corresponds to
* a READ-ONLY object; you can can query any way-related
* value, but you cannot alter them.
*
************************************************
*
* this didactic sample will simply print the way object
* on the standard output adopting the appropriate OSM XML
* notation
*/
    char buf[128];
    int i;
    const readosm_tag *tag;

#if defined(_WIN32) || defined(__MINGW32__)
    /* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
    sprintf (buf, "%I64d", way->id);
#else
    sprintf (buf, "%lld", way->id);
#endif
    printf ("\t<way id=\"%s\"", buf);

/*
* some individual values may be set, or may be not
* unset values are identified by the READOSM_UNDEFINED
* conventional value, and must be consequently ignored
*/
    if (way->version != READOSM_UNDEFINED)
	printf (" version=\"%d\"", way->version);
    if (way->changeset != READOSM_UNDEFINED)
      {
#if defined(_WIN32) || defined(__MINGW32__)
	  /* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
	  sprintf (buf, "%I64d", way->changeset);
#else
	  sprintf (buf, "%lld", way->changeset);
#endif
	  printf (" changeset=\"%s\"", buf);
      }

/*
* unset string values are identified by a NULL pointer
* and must be consequently ignored
*/
    if (way->user != NULL)
	printf (" user=\"%s\"", way->user);
    if (way->uid != READOSM_UNDEFINED)
	printf (" uid=\"%d\"", way->uid);
    if (way->timestamp != NULL)
	printf (" timestamp=\"%s\"", way->timestamp);

/*
* the Way object may have a noderefs-list and a tag-list
* please note: these are variable-length lists, and may 
* be empty: in this case the corresponding item count 
* will be ZERO
*/
    if (way->tag_count == 0 && way->node_ref_count == 0)
	printf (" />\n");
    else
      {
	  printf (">\n");
	  for (i = 0; i < way->node_ref_count; i++)
	    {
		/* we'll now print each <nd ref> for this way */
#if defined(_WIN32) || defined(__MINGW32__)
		/* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
		sprintf (buf, "%I64d", *(way->node_refs + i));
#else
		sprintf (buf, "%lld", *(way->node_refs + i));
#endif
		printf ("\t\t<nd ref=\"%s\" />\n", buf);
	    }
	  for (i = 0; i < way->tag_count; i++)
	    {
		/* we'll now print each <tag> for this way */
		tag = way->tags + i;
		printf ("\t\t<tag k=\"%s\" v=\"%s\" />\n", tag->key,
			tag->value);
	    }
	  printf ("\t</way>\n");
      }
    return READOSM_OK;
}

static int
print_relation (const void *user_data, const readosm_relation * relation)
{
/* 
* printing an OSM Relation (callback function) 
*
* this function is called by the OSM parser for each 
* RELATION object found
*
* please note well: the passed pointer corresponds to
* a READ-ONLY object; you can can query any relation-related
* value, but you cannot alter them.
*
************************************************
*
* this didactic sample will simply print the relation object
* on the standard output adopting the appropriate OSM XML
* notation
*/
    char buf[128];
    int i;
    const readosm_member *member;
    const readosm_tag *tag;

#if defined(_WIN32) || defined(__MINGW32__)
    /* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
    sprintf (buf, "%I64d", relation->id);
#else
    sprintf (buf, "%lld", relation->id);
#endif
    printf ("\t<relation id=\"%s\"", buf);

/*
* some individual values may be set, or may be not
* unset values are identified by the READOSM_UNDEFINED
* conventional value, and must be consequently ignored
*/
    if (relation->version != READOSM_UNDEFINED)
	printf (" version=\"%d\"", relation->version);
    if (relation->changeset != READOSM_UNDEFINED)
      {
#if defined(_WIN32) || defined(__MINGW32__)
	  /* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
	  sprintf (buf, "%I64d", relation->changeset);
#else
	  sprintf (buf, "%lld", relation->changeset);
#endif
	  printf (" changeset=\"%s\"", buf);
      }

/*
* unset string values are identified by a NULL pointer
* and must be consequently ignored
*/
    if (relation->user != NULL)
	printf (" user=\"%s\"", relation->user);
    if (relation->uid != READOSM_UNDEFINED)
	printf (" uid=\"%d\"", relation->uid);
    if (relation->timestamp != NULL)
	printf (" timestamp=\"%s\"", relation->timestamp);

/*
* the Relation object may have a member-list and a tag-list
* please note: these are variable-length lists, and may 
* be empty: in this case the corresponding item count 
* will be ZERO
*/
    if (relation->tag_count == 0 && relation->member_count == 0)
	printf (" />\n");
    else
      {
	  printf (">\n");
	  for (i = 0; i < relation->member_count; i++)
	    {
		/* we'll now print each <member> for this way */
		member = relation->members + i;
#if defined(_WIN32) || defined(__MINGW32__)
		/* CAVEAT - M$ runtime doesn't supports %lld for 64 bits */
		sprintf (buf, "%I64d", member->id);
#else
		sprintf (buf, "%lld", member->id);
#endif
		/* any <member> may be of "node", "way" or "relation" type */
		switch (member->member_type)
		  {
		  case READOSM_MEMBER_NODE:
		      printf ("\t\t<member type=\"node\" ref=\"%s\"", buf);
		      break;
		  case READOSM_MEMBER_WAY:
		      printf ("\t\t<member type=\"way\" ref=\"%s\"", buf);
		      break;
		  case READOSM_MEMBER_RELATION:
		      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");
	    }
	  for (i = 0; i < relation->tag_count; i++)
	    {
		/* we'll now print each <tag> for this way */
		tag = relation->tags + i;
		printf ("\t\t<tag k=\"%s\" v=\"%s\" />\n", tag->key,
			tag->value);
	    }
	  printf ("\t</relation>\n");
      }
    return READOSM_OK;
}

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;
      }

/*
* STEP #1: opening the OSM file
* this can indifferently be an OSM XML encoded file (.osm)
* or an OSM Protocol Buffer encoded file (.pbf)
* the library will transparently perform any required
* action in both cases.
*/
    ret = readosm_open (argv[1], &osm_handle);
    if (ret != READOSM_OK)
      {
	  fprintf (stderr, "OPEN error: %d\n", ret);
	  goto stop;
      }

/*
* STEP #2: parsing the OSM file
* this task is unbelievably simple
*
* you are simply required to pass the appropriate
* pointers for callback funtions respectively intended
* to process Node-objects, Way-objects and Relation-objects
*
* the library will then parse the whole input file, calling
* the appropriate callback handling function for each OSM object
* found: please see the callback functions implementing code
* to better understand how it works
*
* important notice: in this first example we'll not use at
* all the USER_DATA pointer. so the second arg will simply
* be (const void *)0 [i.e. NULL]
*/
    ret =
	readosm_parse (osm_handle, (const void *) 0, print_node, print_way,
		       print_relation);
    if (ret != READOSM_OK)
      {
	  fprintf (stderr, "PARSE error: %d\n", ret);
	  goto stop;
      }

    fprintf (stderr, "Ok, OSM input file successfully parsed\n");

  stop:
/*
* STEP #3: closing the OSM file
* this will release any internal memory allocation
*/
    readosm_close (osm_handle);
    return 0;
}