Since i have volunteered to write the gdal ruby-binding test cases i thought it would be a good time to try out some of the ogr ruby-bindings. Charlie Savage has done a lot of work on the gdal SWIG ruby biniding and already written the test suite for OGR. Thanks to his excellent instructions on how to get started with writing the gdal-ruby test suit i was able to to easily convert Ari Jolma’s dumpall.pl to a ruby script to dump the contents of ogr (Vector) datastore.
require'gdal/ogr'@global_extent = Array.new#(4, 0.0)def dump_geom(geom, v)print" Geometry type: ", geom.get_geometry_type, "\n"print" Geometry name: ", geom.get_geometry_name, "\n"
extent = geom.get_envelopeprint" BBox: ", extent[0], " ", extent[2], " ", extent[1], " ", extent[3], "\n"for i in[0, 2]if(@global_extent[i].nil?)or@global_extent[i]> extent[i]@global_extent[i] = extent[i]endendfor i in[1, 3]if(@global_extent[i].nil?)or@global_extent[i]< extent[i]@global_extent[i] = extent[i]endendif(geom.get_geometry_count>0)print"Geom Count = ", geom.get_geometry_count, "\n"
geom.get_geometry_count.timesdo|i|
dump_geom(geom.get_geometry_ref(i),v)endelseif(!v)print"PointCount: ", geom.get_point_count,"\n"
geom.get_point_count.timedo|i|
x = geom.get_x(i)
y = geom.get_y(i)
z = geom.get_x(i)print" ", x, " ", y, " ", z, "\n";
endendendenddef dump_feature_data(feature, layer)
feature_defn = layer.get_layer_defnprint"Feature ID: ", feature.get_fid, "\n"
feature_defn.get_field_count.timesdo|i|print"Field ", i+1, "\n"
field_defn = feature_defn.get_field_defn(i)print" Field name: ",field_defn.get_name_ref,"\n"
type = field_defn.get_typeprint" Field type: ", field_defn.get_field_type_name(type)," type\n"
field = feature.get_field_as_string(i);
print" Field value: ", field,"\n"endenddef dump_layer(layer)
layer.reset_reading
layer.get_feature_count.timesdo|i|
feature = layer.get_feature(i)
dump_feature_data(feature, layer)
geom = feature.get_geometry_ref()
do_not_show_vertices = 1ifnot geom.nil?
dump_geom(geom, do_not_show_vertices)elsif geom.nil?
print"*** expected geometry but got nill\n"endendendif ARGV.length<1print"Usage: #$0", " <input_file>\n"exitelse
filename = ARGV[0]end# driver = Gdal::Ogr.get_driver_by_name('ESRI Shapefile')# open file as a OGR dataset
dataset = Gdal::Ogr.open(filename)print"Looking at ", filename +" ...\n"# get the number of layers
layer_num = dataset.get_layer_count
layer_num.timesdo|i|
layer = dataset.get_layer(i)print" looking at layer num: ", i+1 , "\n",
" Name: "+layer.get_name+", Features: "+ layer.get_feature_count.to_s+"\n"
dump_layer(layer)end
Sadly there is no documentation for the ruby bindings and infact they are not being actively maintained. When I work with them it usually involves diving into the C/C++ sources. However I would like to start documenting the API for the gdal/ogr bindings.
I am trying to use this library on one of my project. I would love to locate the sources and at least generate documentation for them. Using this API without any sense of what which method expects as parameters is rather no funny.
taking GDAL/OGR ruby bindings for a spin
Since i have volunteered to write the gdal ruby-binding test cases i thought it would be a good time to try out some of the ogr ruby-bindings. Charlie Savage has done a lot of work on the gdal SWIG ruby biniding and already written the test suite for OGR. Thanks to his excellent instructions on how to get started with writing the gdal-ruby test suit i was able to to easily convert Ari Jolma’s dumpall.pl to a ruby script to dump the contents of ogr (Vector) datastore.