FortranGIS Version 3.0
readosm_test.F90
1MODULE readosm_test
2USE readosm
3USE fortranc
4use,INTRINSIC :: iso_c_binding
5IMPLICIT NONE
6
7CONTAINS
8
9! subroutine for parsing a file using user-supplied callback
10SUBROUTINE readosm_test_setup()
11
12INTEGER :: err
13TYPE(c_ptr) :: handle ! readosm file object
14CHARACTER(len=512) :: file
15
16CALL get_command_argument(1, file)
17IF (len_trim(file) == 0) file = 'readosm_test.osm'
18
19! open osm file and get a file object
20print*,'Opening osm file ',trim(file)
21err = readosm_open(fchartrimtostr(file), handle)
22IF (err /= readosm_ok) THEN
23 print*,'Error opening osm file ',err
24 stop 1
25ENDIF
26
27! parse the file passing, as node_fnct argument, a user-defined
28! function (see below) which will be called whenever a node
29! (georeferenced point) is read from the osm file
30print*,'Parsing osm file with user-defined callback'
31err = readosm_parse(handle, c_null_ptr, &
32 node_fnct=node_callback)
33IF (err /= readosm_ok) THEN
34 print*,'Error parsing osm file ',err
35 stop 1
36ENDIF
37
38
39print*,'Closing osm file'
40err = readosm_close(handle)
41IF (err /= readosm_ok) THEN
42 print*,'Error closing osm file ',err
43 stop 1
44ENDIF
45
46END SUBROUTINE readosm_test_setup
47
48
49! definition of the node callback function
50FUNCTION node_callback(user_data, node)
51TYPE(c_ptr),VALUE :: user_data
52TYPE(readosm_node) :: node
53INTEGER(kind=c_int) :: node_callback
54
55TYPE(readosm_node_f) :: node_f
56INTEGER :: i
57
58! display coordinates
59print*,node%longitude,node%latitude
60
61! convert to a more Fortran-friendly object
62node_f = readosm_object_f(node)
63
64! scan the attribute list if present
65IF (ALLOCATED(node_f%tags)) THEN
66 DO i = 1, SIZE(node_f%tags)
67! WRITE(*,'(A,'' = '',A)')TRIM(strtofchar(node_f%tags(i)%key)), &
68! TRIM(strtofchar(node_f%tags(i)%value))
69 ENDDO
70ENDIF
71
72! set the return code to OK, otherwise parsing will stop
73node_callback = readosm_ok
74
75END FUNCTION node_callback
76
77
78! subroutine for fully parsing a file using predefined callbacks
79SUBROUTINE readosm_test_setup_full()
80
81INTEGER :: err
82TYPE(c_ptr) :: handle ! readosm file object
83TYPE(readosm_full_f) :: fulldata
84CHARACTER(len=512) :: file
85INTEGER :: i, j
86CHARACTER(len=1),ALLOCATABLE :: key(:), val(:)
87
88CALL getarg(1, file)
89IF (len_trim(file) == 0) file = 'readosm_test.osm'
90
91! open osm file and get a file object
92print*,'Opening osm file ',trim(file)
93err = readosm_open(fchartrimtostr(file), handle)
94IF (err /= readosm_ok) THEN
95 print*,'Error opening osm file ',err
96 stop 1
97ENDIF
98
99! parse the file with predefined callbacks
100print*,'Parsing osm file with predefined callbacks'
101err = readosm_parse_full_f(handle, fulldata)
102IF (err /= readosm_ok) THEN
103 print*,'Error parsing osm file ',err
104 stop 1
105ENDIF
106
107! data has to be used before closing the file, otherwise allocated
108! buffers may be lost
109
110DO j = 1, fulldata%nodes%arraysize
111 print*,fulldata%nodes%array(j)%longitude,fulldata%nodes%array(j)%latitude
112 IF (ALLOCATED(fulldata%nodes%array(j)%tags)) THEN
113 DO i = 1, SIZE(fulldata%nodes%array(j)%tags)
114 key = fulldata%nodes%array(j)%tags(i)%key
115 val = fulldata%nodes%array(j)%tags(i)%value
116! WRITE(*,'(A,''='',A)')strtofchar(key),strtofchar(val)
117! WRITE(*,*)fulldata%nodes%array(j)%tags(i)%key, &
118! fulldata%nodes%array(j)%tags(i)%value
119 ENDDO
120 ENDIF
121
122ENDDO
123
124print*,'Closing osm file'
125err = readosm_close(handle)
126IF (err /= readosm_ok) THEN
127 print*,'Error closing osm file ',err
128 stop 1
129ENDIF
130
131
132
133END SUBROUTINE readosm_test_setup_full
134
135
136END MODULE readosm_test
137
138
139PROGRAM readosm_test_main
140USE readosm_test
141IMPLICIT NONE
142
143CALL readosm_test_setup()
144CALL readosm_test_setup_full()
145
146END PROGRAM readosm_test_main
Close the .osm or .pbf file and release any allocated resource.
Definition readosm.F90:501
Open the .osm or .pbf file, preparing for future functions.
Definition readosm.F90:486
Parse the corresponding file calling the selected callbacks for every entity encountered.
Definition readosm.F90:513
Utility module for supporting Fortran 2003 C language interface module.
Definition fortranc.F90:103
Fortran 2003 interface to the readosm https://www.gaia-gis.it/fossil/readosm/index library.
Definition readosm.F90:55