[gdal-dev] Reading an OSM file

Gökçen Eraslan gokcen.eraslan at gmail.com
Sun Jun 21 05:53:02 PDT 2015


Hi Even,

On 2015-06-21 14:51, Even Rouault wrote:
> 
> Le dimanche 21 juin 2015 14:35:13, vous avez écrit :
>> Hi Even,
>>
>> On 2015-06-21 14:25, Even Rouault wrote:
>>> Le dimanche 21 juin 2015 14:07:28, Gökçen Eraslan a écrit :
>>>> Hi everyone,
>>>>
>>>> I am trying to read an OSM file which I downloaded from geofabrik.
>>>> Although I can get the OSM driver by name (readonly), I get nothing when
>>>> I try to open the data source in which several .osm and osm.bz2 files
>>>
>>>> present:
>>> Gökçen,
>>
>> Thanks for your very fast reply.
>>
>>> For .osm files, you must build GDAL against the Expat library to have XML
>>> support.
>>
>> I am using my distro package. I think my gdal is already built with the
>> expat support,
> 
> Ah yes, it should.
> 
>> otherwise it wouldn't show OSM in the "ogrinfo --formats"
>> output, would it?
> 
> It would appear, but with reduced functionnality (only .pbf reading)
> 
>>
>> In addition, when I try:
>>
>>     OGRSFDriverRegistrar *reg = OGRSFDriverRegistrar::GetRegistrar();
>>     OGRSFDriver *osmdrv = reg->GetDriverByName("OSM");
>>     OGRDataSource *myosm = osmdrv->Open("myfile.osm");
>>
>> last line returns successfully, which is good, but I still don't get why
>> OGRSFDriverRegistrar::Open() returns NULL always.
> 
> I didn't look closely at your initial message but in 'poDS = 
> OGRSFDriverRegistrar::Open( ".", FALSE );' you need to give a .osm filename, 
> not "."
> 

Aaaaaah, OK. I thought it works similarly to the shapefile driver, in
that, if I supply the name of the directory, gdal iterates over the
files in it and detects the ones with .osm extension.

>>
>>> For .osm.bz2, you have to uncompress them priorly to ingesting into GDAL.
>>> You can potentially do it on-the-fly, with something like "cat
>>> your.osm.bz2 | bunzip2 | your_program /vsistdin/"
>>
>> OK, but I am already using a plain XML .osm file. I also have .osm.pbf
>> files in the same directory but OGRSFDriverRegistrar::Open() always
>> returns NULL. My gdal library has sqlite support built-in.
>>
>>> You must also be aware that proper reading of "big" osm files require to
>>> turn on "interleaved reading" mode, as explained in
>>> http://gdal.org/drv_osm.html
>>
>> That I didn't know. Thanks for pointing out. My .osm file is 60M and
>> .osm.pbf is just 3M. Compared to the planet.osm file, they really tiny :)
> 
> You'll see. There's a warning emitted if you need to use interleave reading.
> 

Thank you so much.

Gokcen.

>>
>>> Even
>>
>> Gokcen.
>>
>>>> ----
>>>> #include "ogrsf_frmts.h"
>>>>
>>>> int main()
>>>>
>>>> {
>>>>
>>>>     OGRRegisterAll();
>>>>     
>>>>     OGRDataSource       *poDS;
>>>>     OGRSFDriverRegistrar *reg = OGRSFDriverRegistrar::GetRegistrar();
>>>>     OGRSFDriver *osmdrv = reg->GetDriverByName("OSM");
>>>>     
>>>>     if( osmdrv == NULL )
>>>>     {
>>>>     
>>>>         printf( "OSM driver open failed.\n" );
>>>>         exit( 1 );
>>>>     
>>>>     }
>>>>     
>>>>     poDS = OGRSFDriverRegistrar::Open( ".", FALSE );
>>>>     if( poDS == NULL )
>>>>     {
>>>>     
>>>>         printf( "Open failed.\n" );
>>>>         exit( 1 );
>>>>     
>>>>     }
>>>>     return 0;
>>>>
>>>> }
>>>> ----
>>>>
>>>> Program only prints the message "Open failed". I can also see OSM
>>>> (readonly) in "ogrinfo --formats" output. I would appreciate if someone
>>>> could help me out.
>>>>
>>>> PS: I am using gdal 1.11.2. Actually, what I would like to do is to read
>>>> the file from within R through rgdal but it seems I cannot even read the
>>>> file at C level via libgdal.
>>>>
>>>> Thanks in advance.
>>>>
>>>> Best regards.
>>>> Gokcen.
>>>> _______________________________________________
>>>> gdal-dev mailing list
>>>> gdal-dev at lists.osgeo.org
>>>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
> 

-- 
Gökçen Eraslan


More information about the gdal-dev mailing list