zhm / gdal-ruby

GDAL/OGR bindings for ruby
BSD 3-Clause "New" or "Revised" License
34 stars 10 forks source link

Strange bug with get_driver_by_name #2

Closed oleksii-leonov closed 9 years ago

oleksii-leonov commented 11 years ago

Hi!

If I use only 'gdal-ruby/gdal' or 'gdal-ruby/ogr' — everything works fine. But when I require both — strange thing happen: Gdal::Gdal.get_driver_by_name return Gdal:: Ogr ::Driver object, not Gdal:: Gdal ::Driver.

Example:

If I require only 'gdal-ruby/gdal' everything is Ok, Gdal::Gdal.get_driver_by_name('GTiff') return #<Gdal::Gdal::Driver:0x000000064b5b30>. But, if I after that require 'gdal-ruby/ogr' then Gdal::Gdal.get_driver_by_name('GTiff') return #<Gdal::Ogr::Driver:0x0000000509a3d0>. But Gdal::Ogr.get_driver_by_name('GeoJson') return nil.

irb(main):001:0> require 'gdal-ruby/gdal'
=> true
irb(main):002:0> driver = Gdal::Gdal.get_driver_by_name('GTiff')
=> #<Gdal::Gdal::Driver:0x000000064b5b30>
irb(main):003:0> require 'gdal-ruby/ogr'
=> true
irb(main):004:0> driver = Gdal::Gdal.get_driver_by_name('GTiff') # again
=> #<Gdal::Ogr::Driver:0x0000000509a3d0>
irb(main):005:0> driver = Gdal::Ogr.get_driver_by_name('GeoJson')
=> nil

And this bug is only on Ubuntu Server 12.04 x64 (ruby 1.9.3-p194, GDAL 1.9.1). On Mac OS X (10.8, ruby 1.9.3-p194, GDAL 1.9.2) everything works fine and I can require both 'gdal-ruby/gdal' and 'gdal-ruby/ogr'.

Also tested this case with Python on same machine (Ubuntu Server 12.04 x64, ruby 1.9.3-p194, GDAL 1.9.1), but everything is fine — I can import both gdal and ogr and then GetDriverByName works correct.

Do you have any idea why this might happens?

zhm commented 11 years ago

Thanks for reporting this. I found this same exact issue when I put this in production, and I ended up splitting out the requires and only using ogr (that's all I really needed). I'm pretty sure this is a bug in the GDAL ruby bindings. This gem is basically just a copy of the SWIG code from GDAL with an extconf build script. I'd really like to get this fixed, but I haven't had time to dig into the SWIG code and actually see what's going on. Maybe it's overwriting a method or something?

oleksii-leonov commented 11 years ago

Thanks! I will try to dig GDAL bindings and fix this bug (on this or next week).

oleksii-leonov commented 11 years ago

Spend many hours today and make simple and working solution :) I rename OGR GetDriverByName and GetDriver (in ogr.i from gdal source gdal/swig/include) to OGR_GetDriverByName and OGR_GetDriver. After that rebuild wrappers with SWIG, and get functions ogr_get_driver_by_name and ogr_get_driver for Ruby.

So, now I write in Ruby Gdal::Ogr.ogr_get_driver_by_name instead of Gdal::Ogr.get_driver_by_name — and everything works fine, without any name conflicts.

But still interesting why problem with name conflicts was only on Linux. I suggest, that Linux shared objects (.so) and Mac OS bundles (.bundle) have other namespaces mechanisms ("Two-level Namespaces" in Mac OS resolves name conflicts of this type), but not sure.

Anyway, I make fork of gdal-ruby with this fix (https://github.com/aleksejleonov/gdal-ruby). Also will write to osgeo/gdal bug tracker with this issue — it will be better solution if they update gdal bindings.

zhm commented 11 years ago

This is great. Thanks for the info. In your opinion, is this fix good for a 0.0.6 on rubygems? If you think it's ready to go, you can create a pull request and I'll merge it in and get the fix up on rubygems. I'll leave that up to you, since you spent the time to look into the issue and know more about it :)

Also, have you looked at GDAL 1.9.2 that was recently released to see if there are any changes to the SWIG bindings? If there are any changes, we can get those in also.