Class: Proj::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/proj/grid.rb

Overview

Grids define models that are used to perform dimension shifts.

Grid files can be quite large and may not be included with Proj depending on how it was packaged and any grid licensing requirements. Therefore, Proj has the ability to download grids on the fly if networking is enabled.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, context = Context.default, full_name: nil, package_name: nil, url: nil, downloadable: false, open_license: false, available: false) ⇒ Grid

Returns a new instance of Grid.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/proj/grid.rb', line 22

def initialize(name, context = Context.default, full_name: nil, package_name: nil, url: nil,
               downloadable: false, open_license: false, available: false)
  @name = name
  @context = context
  @full_name = full_name
  @package_name = package_name
  @url = url
  @downloadable = downloadable
  @open_license = open_license
  @available = available
end

Instance Attribute Details

#contextContext (readonly)

Returns The grid context.

Returns:



20
21
22
# File 'lib/proj/grid.rb', line 20

def context
  @context
end

#full_nameString (readonly)

Returns The grid’s full name.

Returns:

  • (String)

    The grid’s full name



20
# File 'lib/proj/grid.rb', line 20

attr_reader :context, :name, :full_name, :package_name, :url

#nameString (readonly)

Returns The grid’s name.

Returns:

  • (String)

    The grid’s name



20
# File 'lib/proj/grid.rb', line 20

attr_reader :context, :name, :full_name, :package_name, :url

#package_nameString (readonly)

Returns The grid’s package name.

Returns:

  • (String)

    The grid’s package name



20
# File 'lib/proj/grid.rb', line 20

attr_reader :context, :name, :full_name, :package_name, :url

#urlObject (readonly)

Returns the value of attribute url.



20
# File 'lib/proj/grid.rb', line 20

attr_reader :context, :name, :full_name, :package_name, :url

Instance Method Details

#available?Boolean

Returns whether the grid is available at runtime

Returns:

  • (Boolean)


51
52
53
# File 'lib/proj/grid.rb', line 51

def available?
  @available
end

#deleteObject

Deletes the grid if it has been downloaded



105
106
107
108
109
110
# File 'lib/proj/grid.rb', line 105

def delete
  if self.downloaded?
    path = File.join(self.context.user_directory, self.name)
    File.delete(path) if File.exist?(path)
  end
end

#download(ignore_ttl = false) {|percent| ... } ⇒ Boolean

Download a file in the PROJ user-writable directory if has not already been downlaoded. This function can only be used if networking is enabled

Parameters:

  • ignore_ttl (Boolean) (defaults to: false)

    If set to FALSE, PROJ will only check the recentness of an already downloaded file, if the delay between the last time it has been verified and the current time exceeds the TTL setting. This can save network accesses. If set to TRUE, PROJ will unconditionally check from the server the recentness of the file.

Yield Parameters:

  • percent (Float)

    The progress downloading the file in the range of 0 to 1

Returns:

  • (Boolean)

    True if the download was successful or unneeded. Otherwise false

See Also:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/proj/grid.rb', line 91

def download(ignore_ttl = false)
  callback = if block_given?
               Proc.new do |context, percent, user_data|
                 result = yield percent
                 # Return 1 to tell Proj to keep downloading the file
                 result ? 1 : 0
               end
             end

  result = Api.proj_download_file(self.context, self.url&.to_s || self.name, ignore_ttl ? 1 : 0, callback, nil)
  result == 1 ? true : false
end

#downloadable?Boolean

Returns whether the grid can be downloaded

Returns:

  • (Boolean)


37
38
39
# File 'lib/proj/grid.rb', line 37

def downloadable?
  @downloadable
end

#downloaded?(ignore_ttl = false) ⇒ Boolean

Returns if a grid is available in the PROJ user-writable directory. This method will only return true if Context#network_enabled? is true

Parameters:

  • ignore_ttl (Boolean) (defaults to: false)

    If set to FALSE, PROJ will only check the recentness of an already downloaded file, if the delay between the last time it has been verified and the current time exceeds the TTL setting. This can save network accesses. If set to TRUE, PROJ will unconditionally check from the server the recentness of the file.

Returns:

  • (Boolean)

See Also:



73
74
75
76
77
78
79
80
# File 'lib/proj/grid.rb', line 73

def downloaded?(ignore_ttl = false)
  if self.context.network_enabled?
    result = Api.proj_is_download_needed(self.context, self.url&.to_s || self.name, ignore_ttl ? 1 : 0)
    result == 1 ? false : true
  else
    false
  end
end

#infoGridInfo

Returns information about this grid

See proj.org/development/reference/functions.html#c.proj_grid_info proj_grid_info

Returns:



60
61
62
63
# File 'lib/proj/grid.rb', line 60

def info
  ptr = Api.proj_grid_info(self.name)
  GridInfo.new(ptr)
end

#open_license?Boolean

Returns whether the grid is released with an open license

Returns:

  • (Boolean)


44
45
46
# File 'lib/proj/grid.rb', line 44

def open_license?
  @open_license
end

#pathString

Returns the path to the grid if it has been downloaded

Returns:

  • (String)


115
116
117
118
119
# File 'lib/proj/grid.rb', line 115

def path
  if self.downloaded?
    File.join(self.context.user_directory, self.name)
  end
end