0

我正在使用gdal-ruby来解析ESRI ShapeFiles,就像在这个演示中一样。我想遍历所有功能,以便将字段值推送到数据库中。但是,我不知道如何检索我需要与数据库列匹配的每个字段的名称。到目前为止,我只能使用字段的字段索引,例如:

dataset = Gdal::Ogr.open(filename)
number_of_layers = dataset.get_layer_count
number_of_layers.times do |layer_index|
  layer = dataset.get_layer(layer_index)
  layer.get_feature_count.times do |feature_index|
    feature = layer.get_feature(feature_index)
    feature.get_field_count.times do |field_index|
      field_value = feature.get_field(field_index)
      # How can I find out the name of the field?
      puts "Value = #{field_value} for unknown field name"
    end
  end
end

我用irb检查了可用的方法并查看了API 文档。好像我在寻找错误的术语。

4

1 回答 1

1

查看 OGR API 本身,我认为您需要通过feature.GetDefnRef来获取功能定义,然后是 .GetFieldDefn获取相关字段,最后是 .GetNameRef ...?

...
feature.get_field_count.times do |field_index|
  defn_ref = feature.get_defn_ref
  field_defn = defn_ref.get_field_defn(field_index)
  field_name = field_defn.get_name
  field_value = feature.get_field(field_index)
  puts "Value = #{field_value} for field named #{field_name}"
end
...
于 2013-07-17T12:04:42.643 回答