FortranGIS  Version2.5
Data Types | Functions/Subroutines | Variables
readosm Module Reference

Fortran 2003 interface to the readosm https://www.gaia-gis.it/fossil/readosm/index library. More...

Data Types

interface  append
 Quick method to append an element to the array. More...
 
type  arrayof_readosm_node_f
 Derived type defining a dynamically extensible array of TYPE(readosm_node_f) elements. More...
 
type  arrayof_readosm_relation_f
 Derived type defining a dynamically extensible array of TYPE(readosm_relation_f) elements. More...
 
type  arrayof_readosm_way_f
 Derived type defining a dynamically extensible array of TYPE(readosm_way_f) elements. More...
 
interface  delete
 Destructor for finalizing an array object. More...
 
interface  insert
 Method for inserting elements of the array at a desired position. More...
 
interface  packarray
 Method for packing the array object reducing at a minimum the memory occupation, without destroying its contents. More...
 
interface  readosm_close
 Close the .osm or .pbf file and release any allocated resource. More...
 
type  readosm_full_f
 Derived type for performing a prepackaged full parsing of an osm file. More...
 
type  readosm_member
 Object describing a RELATION-MEMBER structure. More...
 
type  readosm_member_f
 A more Fortran-friendly object describing a RELATION-MEMEBER structure. More...
 
type  readosm_node
 Object describing a NODE structure. More...
 
type  readosm_node_f
 A more Fortran-friendly object describing a NODE structure. More...
 
interface  readosm_open
 Open the .osm or .pbf file, preparing for future functions. More...
 
interface  readosm_parse
 Parse the corresponding file calling the selected callbacks for every entity encountered. More...
 
type  readosm_relation
 Object describing a RELATION structure. More...
 
type  readosm_relation_f
 A more Fortran-friendly object describing a RELATION structure. More...
 
type  readosm_tag
 Object describing a TAG structure. More...
 
type  readosm_tag_f
 A more Fortran-friendly object describing a TAG structure. More...
 
type  readosm_way
 Object describing a WAY structure. More...
 
type  readosm_way_f
 A more Fortran-friendly object describing a WAY structure. More...
 
interface  remove
 Method for removing elements of the array at a desired position. More...
 

Functions/Subroutines

integer(kind=c_int) function readosm_parse_f (osm_handle, user_data, node_fnct, way_fnct, relation_fnct)
 Parse the corresponding file calling the selected callbacks for every entity encountered. More...
 
integer function readosm_parse_full_f (osm_handle, fulldata)
 Simplified parsing method for quickly retrieving all the information from a osm file. More...
 
integer(kind=c_int) function readosm_full_node (user_data, node)
 Predefined callback for parsing nodes. More...
 
integer(kind=c_int) function readosm_full_way (user_data, way)
 Predefined callback for parsing ways. More...
 
integer(kind=c_int) function readosm_full_relation (user_data, relation)
 Predefined callback for parsing relations. More...
 

Variables

integer, parameter readosm_undefined = -1234567890
 information is not available More...
 
integer, parameter readosm_member_node = 7361
 MemberType: NODE. More...
 
integer, parameter readosm_member_way = 6731
 MemberType: WAY. More...
 
integer, parameter readosm_member_relation = 3671
 MemberType: RELATION. More...
 
integer, parameter readosm_ok = 0
 No error, success. More...
 
integer, parameter readosm_invalid_suffix = -1
 not .osm or .pbf suffix More...
 
integer, parameter readosm_file_not_found = -2
 .osm or .pbf file does not exist or is not accessible for reading More...
 
integer, parameter readosm_null_handle = -3
 Null OSM_handle argument. More...
 
integer, parameter readosm_invalid_handle = -4
 Invalid OSM_handle argument. More...
 
integer, parameter readosm_insufficient_memory = -5
 some kind of memory allocation failure More...
 
integer, parameter readosm_create_xml_parser_error = -6
 cannot create the XML Parser More...
 
integer, parameter readosm_read_error = -7
 read error More...
 
integer, parameter readosm_xml_error = -8
 XML parser error. More...
 
integer, parameter readosm_invalid_pbf_header = -9
 invalid PBF header More...
 
integer, parameter readosm_unzip_error = -10
 unZip error More...
 
integer, parameter readosm_abort = -11
 user-required parser abort More...
 

Detailed Description

Fortran 2003 interface to the readosm https://www.gaia-gis.it/fossil/readosm/index library.

ReadOSM is an open source library which is able to extract valid data from within an Open Street Map input file. It can read files in the .osm and .osm.pbf formats. ReadOSM is developed and maintained by Alessandro Furieri. This module defines an API which reflects the original readosm C API, plus some additional objects and methods to simplify the use from Fortran.

The reading of the file is callback-based, i.e. a user-defined function is called whenever a node, way or relation is encounterd in the file; the callback functions receive a copy of the entity read from the file in the form of a C-interoperable derived type which matches exactly the structures defined in the original readosm C library. The derived types can be converted on the fly to a more Fortran-friendly version, where pointers are replaced with Fortran arrays where possible. It is up to the callbacks to do something useful with the data received.

For an example of application of the readosm module, please refer to the following test program, which parses an osm file and dumps some information about it:

MODULE readosm_test
use,INTRINSIC :: iso_c_binding
IMPLICIT NONE
CONTAINS
! subroutine for parsing a file using user-supplied callback
SUBROUTINE readosm_test_setup()
INTEGER :: err
TYPE(c_ptr) :: handle ! readosm file object
CHARACTER(len=512) :: file
CALL getarg(1, file)
IF (len_trim(file) == 0) file = 'readosm_test.osm'
! open osm file and get a file object
print*,'Opening osm file ',trim(file)
err = readosm_open(fchartrimtostr(file), handle)
IF (err /= readosm_ok) THEN
print*,'Error opening osm file ',err
stop 1
ENDIF
! parse the file passing, as node_fnct argument, a user-defined
! function (see below) which will be called whenever a node
! (georeferenced point) is read from the osm file
print*,'Parsing osm file with user-defined callback'
err = readosm_parse(handle, c_null_ptr, &
node_fnct=node_callback)
IF (err /= readosm_ok) THEN
print*,'Error parsing osm file ',err
stop 1
ENDIF
print*,'Closing osm file'
err = readosm_close(handle)
IF (err /= readosm_ok) THEN
print*,'Error closing osm file ',err
stop 1
ENDIF
END SUBROUTINE readosm_test_setup
! definition of the node callback function
FUNCTION node_callback(user_data, node)
TYPE(c_ptr),VALUE :: user_data
TYPE(readosm_node) :: node
INTEGER(kind=c_int) :: node_callback
TYPE(readosm_node_f) :: node_f
INTEGER :: i
! display coordinates
print*,node%longitude,node%latitude
! convert to a more Fortran-friendly object
node_f = readosm_object_f(node)
! scan the attribute list if present
IF (ALLOCATED(node_f%tags)) THEN
DO i = 1, SIZE(node_f%tags)
! WRITE(*,'(A,'' = '',A)')TRIM(strtofchar(node_f%tags(i)%key)), &
! TRIM(strtofchar(node_f%tags(i)%value))
ENDDO
ENDIF
! set the return code to OK, otherwise parsing will stop
node_callback = readosm_ok
END FUNCTION node_callback
! subroutine for fully parsing a file using predefined callbacks
SUBROUTINE readosm_test_setup_full()
INTEGER :: err
TYPE(c_ptr) :: handle ! readosm file object
TYPE(readosm_full_f) :: fulldata
CHARACTER(len=512) :: file
INTEGER :: i, j
CHARACTER(len=1),ALLOCATABLE :: key(:), val(:)
CALL getarg(1, file)
IF (len_trim(file) == 0) file = 'readosm_test.osm'
! open osm file and get a file object
print*,'Opening osm file ',trim(file)
err = readosm_open(fchartrimtostr(file), handle)
IF (err /= readosm_ok) THEN
print*,'Error opening osm file ',err
stop 1
ENDIF
! parse the file with predefined callbacks
print*,'Parsing osm file with predefined callbacks'
err = readosm_parse_full_f(handle, fulldata)
IF (err /= readosm_ok) THEN
print*,'Error parsing osm file ',err
stop 1
ENDIF
! data has to be used before closing the file, otherwise allocated
! buffers may be lost
DO j = 1, fulldata%nodes%arraysize
print*,fulldata%nodes%array(j)%longitude,fulldata%nodes%array(j)%latitude
IF (ALLOCATED(fulldata%nodes%array(j)%tags)) THEN
DO i = 1, SIZE(fulldata%nodes%array(j)%tags)
key = fulldata%nodes%array(j)%tags(i)%key
val = fulldata%nodes%array(j)%tags(i)%value
! WRITE(*,'(A,''='',A)')strtofchar(key),strtofchar(val)
! WRITE(*,*)fulldata%nodes%array(j)%tags(i)%key, &
! fulldata%nodes%array(j)%tags(i)%value
ENDDO
ENDIF
ENDDO
print*,'Closing osm file'
err = readosm_close(handle)
IF (err /= readosm_ok) THEN
print*,'Error closing osm file ',err
stop 1
ENDIF
END SUBROUTINE readosm_test_setup_full
END MODULE readosm_test
PROGRAM readosm_test_main
USE readosm_test
IMPLICIT NONE
CALL readosm_test_setup()
CALL readosm_test_setup_full()
END PROGRAM readosm_test_main