Class: Proj::Context

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/proj/context.rb

Overview

Proj 4.8 introduced the concept of a thread context object to support multi-threaded programs. The bindings automatically create one context per thread (its stored in local thread storage).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



32
33
34
35
36
37
# File 'lib/proj/context.rb', line 32

def initialize
  @pointer = Api.proj_context_create
  ObjectSpace.define_finalizer(self, self.class.finalize(@pointer))

  @database = Database.new(self)
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/proj/context.rb', line 5

def database
  @database
end

Class Method Details

.currentContext

The context for the current thread. If a context does not exist a new one is created

Returns:

  • (Context)

    The context for the current thread



22
23
24
# File 'lib/proj/context.rb', line 22

def self.current
  Thread.current[:proj_context] ||= Context.new
end

.defaultContext

Returns the default Proj context. This context must only be used in the main thread In general it is better to create new contexts

Returns:

  • (Context)

    The default context



11
12
13
14
15
16
# File 'lib/proj/context.rb', line 11

def self.default
  result = Context.allocate
  # The default Proj Context is represented by a null pointer
  result.instance_variable_set(:@pointer, FFI::Pointer::NULL)
  result
end

.finalize(pointer) ⇒ Object



26
27
28
29
30
# File 'lib/proj/context.rb', line 26

def self.finalize(pointer)
  proc do
    Api.proj_context_destroy(pointer)
  end
end

Instance Method Details

#ca_bundle_path=(path) ⇒ nil

Sets the CA Bundle path which will be used by PROJ when curl and PROJ_NETWORK are enabled.

Parameters:

  • path (String)

    Path to CA bundle.

Returns:

  • (nil)

See Also:



128
129
130
# File 'lib/proj/context.rb', line 128

def ca_bundle_path=(path)
  Api.proj_context_set_ca_bundle_path(self, path.encode(:utf8))
end

#cacheGridCache

Returns the cache used to store grid files locally

Returns:



135
136
137
# File 'lib/proj/context.rb', line 135

def cache
  GridCache.new(self)
end

#database_pathObject

— Deprecated ——-



236
237
238
# File 'lib/proj/context.rb', line 236

def database_path
  self.database.path
end

#database_path=(value) ⇒ Object

Sets the path to the Proj database



241
242
243
# File 'lib/proj/context.rb', line 241

def database_path=(value)
  self.database.path = value
end

#errnoObject

Returns the current error-state of the context. An non-zero error codes indicates an error.

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

return [Integer]



59
60
61
# File 'lib/proj/context.rb', line 59

def errno
  Api.proj_context_errno(self)
end

#initialize_copy(original) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/proj/context.rb', line 39

def initialize_copy(original)
  ObjectSpace.undefine_finalizer(self)

  super

  @pointer = Api.proj_context_clone(original)
  @database = Database.new(self)

  ObjectSpace.define_finalizer(self, self.class.finalize(@pointer))
end

#log_levelPJ_LOG_LEVEL

Gets the current log level

Returns:

  • (PJ_LOG_LEVEL)


81
82
83
# File 'lib/proj/context.rb', line 81

def log_level
  Api.proj_log_level(self, :PJ_LOG_TELL)
end

#log_level=(value) ⇒ nil

Sets the current log level

Parameters:

  • value (PJ_LOG_LEVEL)

Returns:

  • (nil)


89
90
91
# File 'lib/proj/context.rb', line 89

def log_level=(value)
  Api.proj_log_level(self, value)
end

#network_enabled=(value) ⇒ Object

Enable or disable network access for downloading grid files

Parameters:

  • value (Boolean)

    Specifies if network access should be enabled or disabled

See Also:



154
155
156
# File 'lib/proj/context.rb', line 154

def network_enabled=(value)
  Api.proj_context_set_enable_network(self, value ? 1 : 0)
end

#network_enabled?Boolean

Returns if network access is enabled allowing Grid files to be downloaded

Returns:

  • (Boolean)

    True if network access is enabled, otherwise false

See Also:



144
145
146
147
# File 'lib/proj/context.rb', line 144

def network_enabled?
  result = Api.proj_context_is_network_enabled(self)
  result == 1 ? true : false
end

#search_paths=(paths) ⇒ Object

Sets the paths that Proj will search when opening one of its resource files such as the proj.db database, grids, etc.

If set on the default context, they will be inherited by contexts created later.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/proj/context.rb', line 193

def search_paths=(paths)
  # Convert paths to C chars
  paths_ptr = paths.map do |path|
    FFI::MemoryPointer.from_string(path)
  end

  pointer = FFI::MemoryPointer.new(:pointer, paths.size)
  pointer.write_array_of_pointer(paths_ptr)

  if Api.method_defined?(:proj_context_set_search_paths)
    Api.proj_context_set_search_paths(self, paths.size, pointer)
  elsif Api.method_defined?(:pj_set_searchpath)
    Api.pj_set_searchpath(paths.size, pointer)
  end
end

#set_file_api(file_api_klass) ⇒ Object

Installs a new FileApiImpl



212
213
214
215
216
217
218
219
220
# File 'lib/proj/context.rb', line 212

def set_file_api(file_api_klass)
  unless file_api_klass.kind_of?(Class)
    raise("#{file_api_klass} must be a class whose initializer has single argument which is a context")
  end

  # There is no API to "uninstall" a FileApi. Thus it needs to stay alive
  # until the context is GCed
  @file_api = file_api_klass.new(self)
end

#set_log_function(pointer = nil, &proc) ⇒ nil

Sets a custom log function

Examples:

context.set_log_function(data) do |pointer, int, message|
  ... do stuff...
end

Parameters:

  • pointer (FFI::MemoryPointer) (defaults to: nil)

    Optional pointer to custom data

  • proc (Proc)

    Custom logging procedure

Returns:

  • (nil)


74
75
76
# File 'lib/proj/context.rb', line 74

def set_log_function(pointer = nil, &proc)
  Api.proj_log_func(self, pointer, proc)
end

#set_network_api(network_api_klass) ⇒ Object

Installs a new NetworkApiImpl



225
226
227
228
229
230
231
232
233
# File 'lib/proj/context.rb', line 225

def set_network_api(network_api_klass)
  unless network_api_klass.kind_of?(Class)
    raise("#{network_api_klass} must be a class whose initializer has single argument which is a context")
  end

  # There is no API to "uninstall" a FileApi. Thus it needs to stay alive
  # until the context is GCed
  @network_api = network_api_klass.new(self)
end

#to_ptrObject



50
51
52
# File 'lib/proj/context.rb', line 50

def to_ptr
  @pointer
end

#urlString

Returns the URL endpoint to query for remote grids



163
164
165
# File 'lib/proj/context.rb', line 163

def url
  Api.proj_context_get_url_endpoint(self)
end

#url=(value) ⇒ Object

Sets the URL endpoint to query for remote grids. This overrides the default endpoint in the PROJ configuration file or with the PROJ_NETWORK_ENDPOINT environment variable.



172
173
174
# File 'lib/proj/context.rb', line 172

def url=(value)
  Api.proj_context_set_url_endpoint(self, value)
end

#use_proj4_init_rulesBoolean

Gets if proj4 init rules are being used (i.e., support +init parameters)

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/proj/context.rb', line 96

def use_proj4_init_rules
  result = Api.proj_context_get_use_proj4_init_rules(self, 0)
  result == 1 ? true : false
end

#use_proj4_init_rules=(value) ⇒ nil

Sets if proj4 init rules should be used

Parameters:

  • value (Boolean)

Returns:

  • (nil)


106
107
108
# File 'lib/proj/context.rb', line 106

def use_proj4_init_rules=(value)
  Api.proj_context_use_proj4_init_rules(self, value ? 1 : 0)
end

#user_directory(create = false) ⇒ String

Returns the user directory used to save grid files.

Parameters:

  • If (Boolean)

    set to TRUE, create the directory if it does not exist already. Defaults to false

Returns:

  • (String)

    Directory

See Also:



183
184
185
# File 'lib/proj/context.rb', line 183

def user_directory(create = false)
  Api.proj_context_get_user_writable_directory(self, create ? 1 : 0)
end

#wkt_dialect(wkt) ⇒ PJ_GUESSED_WKT_DIALECT

Guess the “dialect” of the specified WKT string

Parameters:

  • wkt (String)

    A WKT string

Returns:

  • (PJ_GUESSED_WKT_DIALECT)

See Also:



117
118
119
# File 'lib/proj/context.rb', line 117

def wkt_dialect(wkt)
  Api.proj_context_guess_wkt_dialect(self, wkt)
end