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_envelope
print " 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]
end
end
for i in [1, 3]
if (@global_extent[i].nil?) or @global_extent[i] < extent[i]
@global_extent[i] = extent[i]
end
end
if (geom.get_geometry_count > 0)
print "Geom Count = ", geom.get_geometry_count, "\n"
geom.get_geometry_count.times do |i|
dump_geom(geom.get_geometry_ref(i),v)
end
else
if(!v)
print "PointCount: ", geom.get_point_count,"\n"
geom.get_point_count.time do |i|
x = geom.get_x(i)
y = geom.get_y(i)
z = geom.get_x(i)
print " ", x, " ", y, " ", z, "\n";
end
end
end
end
def dump_feature_data(feature, layer)
feature_defn = layer.get_layer_defn
print "Feature ID: ", feature.get_fid, "\n"
feature_defn.get_field_count.times do |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_type
print " Field type: ", field_defn.get_field_type_name(type)," type\n"
field = feature.get_field_as_string(i);
print " Field value: ", field,"\n"
end
end
def dump_layer(layer)
layer.reset_reading
layer.get_feature_count.times do |i|
feature = layer.get_feature(i)
dump_feature_data(feature, layer)
geom = feature.get_geometry_ref()
do_not_show_vertices = 1
if not geom.nil?
dump_geom(geom, do_not_show_vertices)
elsif geom.nil?
print "*** expected geometry but got nill\n"
end
end
end
if ARGV.length < 1
print "Usage: #$0", "
\n"
exit
else
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.times do |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
Hi,
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.
Thanks for any response!
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.
For your immediate needs of manipulating a shapefile have a look at GeoRuby which has tools for reading/writing shapefiles, eg.:
http://georuby.rubyforge.org/svn/GeoRuby/trunk/tools/shp2sql.rb