Skip to content

Commit

Permalink
Merge pull request telus-agcg#100 from syngenta/feat/add-support-for-…
Browse files Browse the repository at this point in the history
…more-gdal-data-types

feat: add support for GDT_Int8, GDT_UInt64 and GDT_Int64
  • Loading branch information
tindron authored May 14, 2024
2 parents 750be15 + 8412bd1 commit 88c1636
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/ext/numeric_as_data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module NumericAsDataType
# @param data_type [FFI::GDAL::GDAL::DataType]
def to_data_type(data_type)
case data_type
when :GDT_Byte, :GDT_UInt16, :GDT_Int16, :GDT_UInt32, :GDT_Int32
when :GDT_Byte, :GDT_Int8, :GDT_UInt16, :GDT_Int16, :GDT_UInt32, :GDT_Int32, :GDT_UInt64, :GDT_Int64
to_i
when :GDT_Float32, :GDT_Float64
to_f
Expand Down
11 changes: 9 additions & 2 deletions lib/ffi/gdal/gdal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@

module FFI
module GDAL
# rubocop:disable Metrics/ModuleLength
module GDAL
extend ::FFI::Library
ffi_lib [::FFI::CURRENT_PROCESS, ::FFI::GDAL.gdal_library_path]

# ----------------------------------------------------------------
# Enums
# ----------------------------------------------------------------
DataType = enum :GDT_Unknown, 0,
# https://gdal.org/doxygen/gdal_8h_source.html
# NOTE: GDT_TypeCount is maximum type # + 1.
DataType = enum :GDT_Unknown, 0,
:GDT_Byte, 1,
:GDT_Int8, 14,
:GDT_UInt16, 2,
:GDT_Int16, 3,
:GDT_UInt32, 4,
:GDT_Int32, 5,
:GDT_UInt64, 12,
:GDT_Int64, 13,
:GDT_Float32, 6,
:GDT_Float64, 7,
:GDT_CInt16, 8,
:GDT_CInt32, 9,
:GDT_CFloat32, 10,
:GDT_CFloat64, 11,
:GDT_TypeCount, 12
:GDT_TypeCount, 15

AsyncStatusType = enum :GARIO_PENDING, 0,
:GARIO_UPDATE, 1,
Expand Down Expand Up @@ -679,5 +685,6 @@ module GDAL
%i[pointer int int pointer int int int int],
:void
end
# rubocop:enable Metrics/ModuleLength
end
end
3 changes: 3 additions & 0 deletions lib/gdal/internal_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ def _cpl_read_and_free_string
def _gdal_data_type_to_ffi(data_type)
case data_type
when :GDT_Byte then :uchar
when :GDT_Int8 then :int8
when :GDT_UInt16 then :uint16
when :GDT_Int16, :GDT_CInt16 then :int16
when :GDT_UInt32 then :uint32
when :GDT_Int32, :GDT_CInt32 then :int32
when :GDT_UInt64 then :uint64
when :GDT_Int64 then :int64
when :GDT_Float32, :GDT_CFloat32 then :float
when :GDT_Float64, :GDT_CFloat64 then :double
else
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/ext/numeric_as_data_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Numeric do
let(:integer_data_types) do
%i[GDT_Byte GDT_UInt16 GDT_Int16 GDT_UInt32 GDT_Int32]
%i[GDT_Byte GDT_Int8 GDT_UInt16 GDT_Int16 GDT_UInt32 GDT_Int32 GDT_UInt64 GDT_Int64]
end

let(:float_data_types) do
Expand Down
54 changes: 44 additions & 10 deletions spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
RSpec.describe "GDAL::RasterBand::IOExtensions" do
let(:driver) { GDAL::Driver.by_name("MEM") }
let(:dataset_byte) { driver.create_dataset("test", 15, 25, data_type: :GDT_Byte) }
let(:dataset_int16) { driver.create_dataset("test", 15, 25, data_type: :GDT_Int16) }
let(:dataset_int8) { driver.create_dataset("test", 15, 25, data_type: :GDT_Int8) }
let(:dataset_uint16) { driver.create_dataset("test", 15, 25, data_type: :GDT_UInt16) }
let(:dataset_int32) { driver.create_dataset("test", 15, 25, data_type: :GDT_Int32) }
let(:dataset_int16) { driver.create_dataset("test", 15, 25, data_type: :GDT_Int16) }
let(:dataset_uint32) { driver.create_dataset("test", 15, 25, data_type: :GDT_UInt32) }
let(:dataset_int32) { driver.create_dataset("test", 15, 25, data_type: :GDT_Int32) }
let(:dataset_uint64) { driver.create_dataset("test", 15, 25, data_type: :GDT_UInt64) }
let(:dataset_int64) { driver.create_dataset("test", 15, 25, data_type: :GDT_Int64) }
let(:dataset_float32) { driver.create_dataset("test", 15, 25, data_type: :GDT_Float32) }
let(:dataset_float64) { driver.create_dataset("test", 15, 25, data_type: :GDT_Float64) }

Expand Down Expand Up @@ -44,6 +47,24 @@
end
end

context "valid values, GDT_Int8" do
it "sets and gets the value successfully" do

Check warning on line 51 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.1 on ubuntu-22.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_Int8 sets and gets the value successfully Skipped: This spec only for GDAL 3.7+

Check warning on line 51 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.3 on ubuntu-22.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_Int8 sets and gets the value successfully Skipped: This spec only for GDAL 3.7+

Check warning on line 51 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 2.7 on ubuntu-20.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_Int8 sets and gets the value successfully Skipped: This spec only for GDAL 3.7+
skip "This spec only for GDAL 3.7+" if GDAL.version_num < "3070000"

subject.set_pixel_value(0, 0, -128)
expect(subject.pixel_value(0, 0)).to eq(-128)
end
end

context "valid values, GDT_UInt16" do
subject { dataset_uint16.raster_band(1) }

it "sets and gets the value successfully" do
subject.set_pixel_value(0, 0, 32_123)
expect(subject.pixel_value(0, 0)).to eq(32_123)
end
end

context "valid values, GDT_Int16" do
subject { dataset_int16.raster_band(1) }

Expand All @@ -53,12 +74,12 @@
end
end

context "valid values, GDT_UInt16" do
subject { dataset_uint16.raster_band(1) }
context "valid values, GDT_UInt32" do
subject { dataset_uint32.raster_band(1) }

it "sets and gets the value successfully" do
subject.set_pixel_value(0, 0, 32_123)
expect(subject.pixel_value(0, 0)).to eq(32_123)
subject.set_pixel_value(0, 0, 4_123_456_789)
expect(subject.pixel_value(0, 0)).to eq(4_123_456_789)
end
end

Expand All @@ -71,12 +92,25 @@
end
end

context "valid values, GDT_UInt32" do
subject { dataset_uint32.raster_band(1) }
context "valid values, GDT_UInt64" do
subject { dataset_uint64.raster_band(1) }

it "sets and gets the value successfully" do

Check warning on line 98 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.1 on ubuntu-22.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_UInt64 sets and gets the value successfully Skipped: This spec only for GDAL 3.5+

Check warning on line 98 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.3 on ubuntu-22.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_UInt64 sets and gets the value successfully Skipped: This spec only for GDAL 3.5+

Check warning on line 98 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 2.7 on ubuntu-20.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_UInt64 sets and gets the value successfully Skipped: This spec only for GDAL 3.5+
subject.set_pixel_value(0, 0, 4_123_456_789)
expect(subject.pixel_value(0, 0)).to eq(4_123_456_789)
skip "This spec only for GDAL 3.5+" if GDAL.version_num < "3050000"

subject.set_pixel_value(0, 0, 18_446_744_073_709_551_615)
expect(subject.pixel_value(0, 0)).to eq(18_446_744_073_709_551_615)
end
end

context "valid values, GDT_Int64" do
subject { dataset_int64.raster_band(1) }

it "sets and gets the value successfully" do

Check warning on line 109 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.1 on ubuntu-22.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_Int64 sets and gets the value successfully Skipped: This spec only for GDAL 3.5+

Check warning on line 109 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 3.3 on ubuntu-22.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_Int64 sets and gets the value successfully Skipped: This spec only for GDAL 3.5+

Check warning on line 109 in spec/unit/gdal/extensions/raster_band/io_extensions_spec.rb

View workflow job for this annotation

GitHub Actions / Ruby 2.7 on ubuntu-20.04

GDAL::RasterBand::IOExtensions #set_pixel_value/#pixel_value valid values, GDT_Int64 sets and gets the value successfully Skipped: This spec only for GDAL 3.5+
skip "This spec only for GDAL 3.5+" if GDAL.version_num < "3050000"

subject.set_pixel_value(0, 0, -9_223_372_036_854_775_808)
expect(subject.pixel_value(0, 0)).to eq(-9_223_372_036_854_775_808)
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/unit/gdal/internal_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ module Tester
it { is_expected.to eq :uchar }
end

context "data type is :GDT_Int8" do
subject { tester._gdal_data_type_to_ffi(:GDT_Int8) }
it { is_expected.to eq :int8 }
end

context "data type is :GDT_UInt16" do
subject { tester._gdal_data_type_to_ffi(:GDT_UInt16) }
it { is_expected.to eq :uint16 }
Expand All @@ -85,6 +90,16 @@ module Tester
it { is_expected.to eq :int32 }
end

context "data type is :GDT_UInt64" do
subject { tester._gdal_data_type_to_ffi(:GDT_UInt64) }
it { is_expected.to eq :uint64 }
end

context "data type is :GDT_Int64" do
subject { tester._gdal_data_type_to_ffi(:GDT_Int64) }
it { is_expected.to eq :int64 }
end

context "data type is :GDT_Float32" do
subject { tester._gdal_data_type_to_ffi(:GDT_Float32) }
it { is_expected.to eq :float }
Expand Down

0 comments on commit 88c1636

Please sign in to comment.