Class: Proj::Database
- Inherits:
-
Object
- Object
- Proj::Database
- Defined in:
- lib/proj/database.rb
Overview
This class provides access the Proj SQLite database called proj.db. The database stores transformation information that must be accessible for the library to work properly.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#authorities ⇒ Array<Strings>
Return a list of authorities used in the database.
-
#celestial_bodies(authority = nil) ⇒ Array<CelestialBody>
Returns a list of celestial bodies from the database.
-
#celestial_body_name(object) ⇒ String
Return the name of the celestial body of the specified object.
-
#codes(auth_name, pj_type, allow_deprecated = false) ⇒ Strings
Returns the set of authority codes of the given object type.
-
#crs_info(auth_name = nil, parameters = nil) ⇒ Array<CrsInfo>
Enumerate CRS infos from the database, taking into account various criteria.
-
#geoid_models(authority, code) ⇒ Strings
Returns a list of geoid models available.
-
#grid(name) ⇒ Grid
Returns information about a Grid from the database.
-
#initialize(context) ⇒ Database
constructor
Create a new database instance to query the Proj database.
-
#metadata(key) ⇒ String
Return a metadata from the database.
-
#path ⇒ Object
Returns the path the Proj database.
-
#path=(value) ⇒ Database
Sets the path to the Proj database.
-
#structure ⇒ Array<Strings>
Returns SQL statements to run to initiate a new valid auxiliary empty database.
-
#suggest_code_for(object, authority, numeric_code) ⇒ String
Suggests a database code for the specified object.
-
#unit(auth_name, code) ⇒ Unit
Returns information for a unit of measure from a database lookup.
-
#units(auth_name: nil, category: nil, allow_deprecated: false) ⇒ Array<Unit>
Returns a list of units from the database.
Constructor Details
#initialize(context) ⇒ Database
Create a new database instance to query the Proj database
18 19 20 21 |
# File 'lib/proj/database.rb', line 18 def initialize(context) Error.validate_context!(context) @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
11 12 13 |
# File 'lib/proj/database.rb', line 11 def context @context end |
Instance Method Details
#authorities ⇒ Array<Strings>
Return a list of authorities used in the database
104 105 106 107 |
# File 'lib/proj/database.rb', line 104 def ptr = Api.(self.context) Strings.new(ptr) end |
#celestial_bodies(authority = nil) ⇒ Array<CelestialBody>
Returns a list of celestial bodies from the database
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/proj/database.rb', line 197 def celestial_bodies( = nil) out_result_count = FFI::MemoryPointer.new(:int) ptr = Api.proj_get_celestial_body_list_from_database(self.context, , out_result_count) body_ptrs = ptr.read_array_of_pointer(out_result_count.read_int) result = body_ptrs.map do |body_ptr| # First read the pointer to a structure struct = Api::ProjCelestialBodyInfo.new(body_ptr) # Now map this to a Ruby Struct CelestialBody.new(struct[:auth_name].read_string_to_null, struct[:name].read_string_to_null) end Api.proj_celestial_body_list_destroy(ptr) result end |
#celestial_body_name(object) ⇒ String
Return the name of the celestial body of the specified object.
222 223 224 |
# File 'lib/proj/database.rb', line 222 def celestial_body_name(object) Api.proj_get_celestial_body_name(self.context, object) end |
#codes(auth_name, pj_type, allow_deprecated = false) ⇒ Strings
Returns the set of authority codes of the given object type.
94 95 96 97 |
# File 'lib/proj/database.rb', line 94 def codes(auth_name, pj_type, allow_deprecated = false) ptr = Api.proj_get_codes_from_database(self.context, auth_name, pj_type, allow_deprecated ? 1 : 0) Strings.new(ptr) end |
#crs_info(auth_name = nil, parameters = nil) ⇒ Array<CrsInfo>
Enumerate CRS infos from the database, taking into account various criteria.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/proj/database.rb', line 117 def crs_info(auth_name = nil, parameters = nil) out_result_count = FFI::MemoryPointer.new(:int) params = parameters.is_a?(Parameters) ? parameters.instance_variable_get(:@params) : parameters ptr = Api.proj_get_crs_info_list_from_database(self.context, auth_name, params, out_result_count) result = out_result_count.read_int.times.map do |index| index_ptr = ptr + (index * FFI::Pointer::SIZE) struct = Api::ProjCrsInfo.new(index_ptr.read_pointer) CrsInfo.from_proj_crs_info(struct) end Api.proj_crs_info_list_destroy(ptr) result end |
#geoid_models(authority, code) ⇒ Strings
Returns a list of geoid models available
185 186 187 188 |
# File 'lib/proj/database.rb', line 185 def geoid_models(, code) ptr = Api.proj_get_geoid_models_from_database(self.context, , code, nil) Strings.new(ptr) end |
#grid(name) ⇒ Grid
Returns information about a Grid from the database
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/proj/database.rb', line 139 def grid(name) out_full_name = FFI::MemoryPointer.new(:string) out_package_name = FFI::MemoryPointer.new(:string) out_url = FFI::MemoryPointer.new(:string) out_downloadable = FFI::MemoryPointer.new(:int) out_open_license = FFI::MemoryPointer.new(:int) out_available = FFI::MemoryPointer.new(:int) result = Api.proj_grid_get_info_from_database(self.context, name, out_full_name, out_package_name, out_url, out_downloadable, out_open_license, out_available) if result == 1 full_name_ptr = out_full_name.read_pointer package_name_ptr = out_package_name.read_pointer url_ptr = out_url.read_pointer downloadable_ptr = out_downloadable open_license_ptr = out_open_license available_ptr = out_available full_name = full_name_ptr.read_string_to_null package_name = package_name_ptr.read_string_to_null url = url_ptr.read_string_to_null downloadable = downloadable_ptr.read_int == 1 ? true : false open_license = open_license_ptr.read_int == 1 ? true : false available = available_ptr.read_int == 1 ? true : false Grid.new(name, self.context, full_name: full_name, package_name: package_name, url: url ? URI(url) : nil, downloadable: downloadable, open_license: open_license, available: available) else Error.check_context(self.context) end end |
#metadata(key) ⇒ String
Return a metadata from the database.
81 82 83 |
# File 'lib/proj/database.rb', line 81 def (key) Api.(self.context, key) end |
#path ⇒ Object
Returns the path the Proj database
return [String]
28 29 30 31 32 |
# File 'lib/proj/database.rb', line 28 def path if Api.method_defined?(:proj_context_get_database_path) Api.proj_context_get_database_path(self.context) end end |
#path=(value) ⇒ Database
Sets the path to the Proj database
41 42 43 44 45 46 47 |
# File 'lib/proj/database.rb', line 41 def path=(value) result = Api.proj_context_set_database_path(self.context, value, nil, nil) unless result == 1 Error.check_context(self.context) end self end |
#structure ⇒ Array<Strings>
Returns SQL statements to run to initiate a new valid auxiliary empty database.
54 55 56 57 |
# File 'lib/proj/database.rb', line 54 def structure ptr = Api.proj_context_get_database_structure(self.context, nil) Strings.new(ptr) end |
#suggest_code_for(object, authority, numeric_code) ⇒ String
Suggests a database code for the specified object.
235 236 237 238 239 240 241 |
# File 'lib/proj/database.rb', line 235 def suggest_code_for(object, , numeric_code) ptr = Api.proj_suggests_code_for(self.context, object, , numeric_code ? 1 : 0, nil) return nil if ptr.null? result = ptr.read_string_to_null Api.proj_string_destroy(ptr) result end |
#unit(auth_name, code) ⇒ Unit
Returns information for a unit of measure from a database lookup
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/proj/database.rb', line 287 def unit(auth_name, code) out_name = FFI::MemoryPointer.new(:string) out_conv_factor = FFI::MemoryPointer.new(:double) out_category = FFI::MemoryPointer.new(:string) result = Api.proj_uom_get_info_from_database(self.context, auth_name, code, out_name, out_conv_factor , out_category) if result == 1 name_ptr = out_name.read_pointer conv_factor_ptr = out_conv_factor category_ptr = out_category.read_pointer name = name_ptr.read_string_to_null conv_factor = conv_factor_ptr.read_double category = category_ptr.read_string_to_null Unit.new(auth_name, code, name, category, conv_factor, nil, false) else Error.check_context(self.context) end end |
#units(auth_name: nil, category: nil, allow_deprecated: false) ⇒ Array<Unit>
Returns a list of units from the database
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/proj/database.rb', line 252 def units(auth_name: nil, category: nil, allow_deprecated: false) # Create pointer to read the count output parameter out_result_count = FFI::MemoryPointer.new(:int) # Result is an array of pointers to structures pp_units = Api.proj_get_units_from_database(self.context, auth_name, category, allow_deprecated ? 1 : 0, out_result_count) count = out_result_count.read(:int) array_p_units = pp_units.read_array_of_pointer(count) result = Array.new(count) count.times do |i| unit_info = Api::ProjUnitInfo.new(array_p_units[i]) result[i] = Unit.new(unit_info[:auth_name].read_string_to_null, unit_info[:code].read_string_to_null, unit_info[:name].read_string_to_null, unit_info[:category].read_string_to_null, unit_info[:conv_factor], unit_info[:proj_short_name].null? ? nil : unit_info[:proj_short_name].read_string_to_null, unit_info[:deprecated]) end Api.proj_unit_list_destroy(pp_units) result end |