Skip to content

Commit df08ac8

Browse files
feat: replace narray with numo-narray
1 parent 1687619 commit df08ac8

File tree

14 files changed

+86
-121
lines changed

14 files changed

+86
-121
lines changed

ffi-gdal.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.add_dependency "ffi"
2626
spec.add_dependency "log_switch", "~> 1.0.0"
2727
spec.add_dependency "multi_xml"
28-
spec.add_dependency "narray", "~> 0.6.0"
2928
spec.add_dependency "numo-narray"
3029
end

lib/ext/narray_ext.rb

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/ffi-gdal.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require "ffi"
44
require_relative "ext/ffi_library_function_checks"
5-
require_relative "ext/narray_ext"
65
require_relative "ext/numeric_as_data_type"
76
require_relative "ext/float_ext"
87

lib/gdal/extensions/dataset/extensions.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "narray"
3+
require "numo/narray"
44
require "ffi-gdal"
55
require "gdal/dataset"
66
require "gdal/raster_band"
@@ -193,11 +193,9 @@ def contains_geometry?(wkt_geometry_string, wkt_srid = 4326)
193193
#
194194
# # This array would look like:
195195
# [[0, 10, 99, 2], [0, 10, 99, 150], [0, 10, 99, 250]]
196-
# @return NArray
196+
# @return Numo::NArray
197197
def to_na(to_data_type = nil)
198-
na = NMatrix.to_na(raster_bands.map { |r| r.to_na(to_data_type) })
199-
200-
NArray[*na.transpose]
198+
Numo::NArray[*raster_bands.map { |r| r.to_na(to_data_type) }.transpose]
201199
end
202200
end
203201
end

lib/gdal/extensions/gridder.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "narray"
43
require "gdal"
54
require "gdal/options"
65
require "ogr"

lib/gdal/extensions/raster_band/extensions.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,35 @@ def to_a
2626
read_lines_by_block.to_a
2727
end
2828

29-
# Iterates through all lines and builds an NArray of pixels.
29+
# Iterates through all lines and builds a Numo::NArray of pixels.
3030
#
31-
# @return [NArray]
31+
# @return [Numo::NArray]
3232
def to_na(to_data_type = nil)
33-
narray = NArray.to_na(to_a)
34-
35-
return narray unless to_data_type
33+
data_array = to_a
34+
target_type = to_data_type || data_type
35+
numo_type = GDAL._gdal_data_type_to_numo_narray_type_constant(target_type)
3636

37-
narray_type = GDAL._gdal_data_type_to_narray_type_constant(to_data_type)
38-
39-
narray.to_type(narray_type)
37+
# Create a typed array from the nested array structure
38+
# to_a returns [y_size][x_size] which matches Numo's row-major order [y_size, x_size]
39+
numo_type.cast(data_array)
4040
end
4141

42-
# Iterates through all lines and builds an NArray of pixels.
42+
# Iterates through all lines and builds a Numo::NArray of pixels.
4343
#
4444
# @return [Numo::NArray]
4545
def to_nna
46-
Numo::NArray[*to_a]
46+
to_na
4747
end
4848

4949
# Each pixel of the raster projected using the dataset's geo_transform.
5050
# The output NArray is a 3D array where the inner-most array is a the
5151
# lat an lon, those are contained in an array per pixel line, and finally
5252
# the outer array contains each of the pixel lines.
5353
#
54-
# @return [NArray]
54+
# @return [Numo::NArray]
5555
def projected_points
56-
narray = GDAL._narray_from_data_type(data_type, 2, x_size, y_size)
56+
numo_type = GDAL._gdal_data_type_to_numo_narray_type_constant(data_type)
57+
narray = numo_type.zeros(2, x_size, y_size)
5758
geo_transform = dataset.geo_transform
5859

5960
y_size.times do |y_point|

lib/gdal/grid.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require "forwardable"
4-
require "narray"
4+
require "numo/narray"
55
require_relative "../gdal"
66
require_relative "grid_algorithms"
77

@@ -24,7 +24,7 @@ def initialize(algorithm_type, data_type: :GDT_Float32)
2424
@data_type = data_type
2525
end
2626

27-
# @param points [Array,NArray] An Array containing all x, y, and z points.
27+
# @param points [Array,Numo::NArray] An Array containing all x, y, and z points.
2828
# @param extents [Hash{x_min: Integer, y_min: Integer, x_max: Integer, y_max: Integer}]
2929
# @param data_pointer [FFI::Pointer] Pointer that will contain the gridded
3030
# data (after this method is done).
@@ -35,7 +35,7 @@ def initialize(algorithm_type, data_type: :GDT_Float32)
3535
# @return [FFI::MemoryPointer] Pointer to the grid data.
3636
def create(points, extents, data_pointer, output_size = { x: 256, y: 256 },
3737
progress_block = nil, progress_arg = nil)
38-
points = points.to_a if points.is_a? NArray
38+
points = points.to_a if points.is_a? Numo::NArray
3939
point_count = points.length
4040
log "Number of points: #{point_count}"
4141
raise GDAL::NoValuesToGrid, "No points to grid" if point_count.zero?

lib/gdal/internal_helpers.rb

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "numo/narray"
4+
35
module GDAL
46
# @private
57
module InternalHelpers
@@ -62,14 +64,15 @@ def _buffer_from_data_type(data_type, size = nil)
6264
end
6365
end
6466

67+
# DEPRECATED: Use Numo::NArray directly instead.
6568
# @param data_type [FFI::GDAL::GDAL::DataType]
6669
# @param narray_args Args to pass to the NArray initializer.
67-
# @return [NArray]
70+
# @return [Numo::NArray]
6871
def _narray_from_data_type(data_type, *narray_args)
69-
init_meth = _gdal_data_type_to_narray(data_type)
70-
narray_args = 0 if narray_args.empty?
72+
numo_type = _gdal_data_type_to_numo_narray_type_constant(data_type)
73+
narray_args = [1] if narray_args.empty?
7174

72-
NArray.send(init_meth, *narray_args)
75+
numo_type.zeros(*narray_args)
7376
end
7477

7578
# Takes an array of strings (or things that should be converted to
@@ -155,42 +158,24 @@ def _gdal_data_type_to_ffi(data_type)
155158
end
156159
end
157160

158-
# Maps GDAL DataTypes to NArray types.
161+
# DEPRECATED: These methods are deprecated. Use _gdal_data_type_to_numo_narray_type_constant instead.
162+
# Maps GDAL DataTypes to NArray types (deprecated).
159163
#
160164
# @param data_type [FFI::GDAL::GDAL::DataType]
161165
# @return [Symbol]
162166
def _gdal_data_type_to_narray(data_type)
163-
case data_type
164-
when :GDT_Byte then :byte
165-
when :GDT_Int16 then :sint
166-
when :GDT_UInt16, :GDT_Int32, :GDT_UInt32 then :int
167-
when :GDT_Float32 then :float
168-
when :GDT_Float64 then :dfloat
169-
when :GDT_CInt16, :GDT_CInt32 then :scomplex
170-
when :GDT_CFloat32 then :complex
171-
when :GDT_CFloat64 then :dcomplex
172-
else
173-
raise GDAL::InvalidDataType, "Unknown data type: #{data_type}"
174-
end
167+
# Delegate to Numo::NArray method
168+
_gdal_data_type_to_numo_narray_type_constant(data_type)
175169
end
176170

177-
# Maps GDAL DataTypes to NArray type constants.
171+
# DEPRECATED: Use _gdal_data_type_to_numo_narray_type_constant instead.
172+
# Maps GDAL DataTypes to NArray type constants (deprecated).
178173
#
179174
# @param data_type [FFI::GDAL::GDAL::DataType]
180-
# @return [Symbol]
175+
# @return [Class]
181176
def _gdal_data_type_to_narray_type_constant(data_type)
182-
case data_type
183-
when :GDT_Byte then NArray::BYTE
184-
when :GDT_Int16 then NArray::SINT
185-
when :GDT_UInt16, :GDT_Int32, :GDT_UInt32 then NArray::INT
186-
when :GDT_Float32 then NArray::FLOAT
187-
when :GDT_Float64 then NArray::DFLOAT
188-
when :GDT_CInt16, :GDT_CInt32 then NArray::SCOMPLEX
189-
when :GDT_CFloat32 then NArray::COMPLEX
190-
when :GDT_CFloat64 then NArray::DCOMPLEX
191-
else
192-
raise GDAL::InvalidDataType, "Unknown data type: #{data_type}"
193-
end
177+
# Delegate to Numo::NArray method
178+
_gdal_data_type_to_numo_narray_type_constant(data_type)
194179
end
195180

196181
# Maps GDAL DataTypes to [Numo::NArray] type constants.
@@ -199,12 +184,15 @@ def _gdal_data_type_to_narray_type_constant(data_type)
199184
# @return [Class]
200185
def _gdal_data_type_to_numo_narray_type_constant(data_type)
201186
case data_type
202-
when :GDT_Byte then Numo::UInt8
203-
when :GDT_Int16 then Numo::Int16
204-
when :GDT_UInt16, :GDT_Int32, :GDT_UInt32 then Numo::Int32
205-
when :GDT_Float32 then Numo::SFloat
206-
when :GDT_Float64 then Numo::DFloat
207-
when :GDT_CInt16, :GDT_CInt32, :GDT_CFloat32, :GDT_CFloat64 then Numo::SComplex
187+
when :GDT_Byte then Numo::UInt8
188+
when :GDT_Int16 then Numo::Int16
189+
when :GDT_UInt16 then Numo::UInt16
190+
when :GDT_Int32 then Numo::Int32
191+
when :GDT_UInt32 then Numo::UInt32
192+
when :GDT_Float32 then Numo::SFloat
193+
when :GDT_Float64 then Numo::DFloat
194+
when :GDT_CInt16, :GDT_CInt32, :GDT_CFloat32 then Numo::SComplex
195+
when :GDT_CFloat64 then Numo::DComplex
208196
else
209197
raise GDAL::InvalidDataType, "Unknown data type: #{data_type}"
210198
end

lib/gdal/raster_band.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require "bigdecimal"
44
require "bigdecimal/util"
5-
require "narray"
65
require_relative "../gdal"
76
require_relative "raster_band_mixins/algorithm_methods"
87
require_relative "major_object"

lib/ogr/coordinate_transformation.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "narray"
43
require_relative "../ogr"
54
require_relative "../gdal"
65

0 commit comments

Comments
 (0)