Class: OGR::PostgisReader

Inherits:
Object
  • Object
show all
Includes:
FFIOGR
Defined in:
lib/ffi-ogr/postgis_reader.rb

Constant Summary collapse

TF_MAP =
{
  true => 1,
  false => 0,
  1 => true,
  0 => false
}

Constants included from FFIOGR

FFIOGR::OGR_FUNCTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FFIOGR

find_lib, gdal_library_path, search_paths

Constructor Details

#initialize(db_config_file) ⇒ PostgisReader


16
17
18
19
20
# File 'lib/ffi-ogr/postgis_reader.rb', line 16

def initialize(db_config_file)
  @db_config = parse_db_config db_config_file
  @driver = OGRGetDriverByName 'PostgreSQL'
  @type = 'PostgreSQL'
end

Instance Attribute Details

#db_configObject

Returns the value of attribute db_config


7
8
9
# File 'lib/ffi-ogr/postgis_reader.rb', line 7

def db_config
  @db_config
end

Instance Method Details

#execute_query(query, writeable = false) ⇒ Object


53
54
55
56
57
58
59
# File 'lib/ffi-ogr/postgis_reader.rb', line 53

def execute_query(query, writeable = false)
  pg_driver = OGR_Dr_Open @driver, @db_config, TF_MAP[writeable]
  result_set = OGR_DS_ExecuteSQL pg_driver, query, nil, nil
  OGR::Tools.cast_layer result_set
  # result_set is OGRLayer
  # must call OGR_DS_ReleaseResultSet(pg_driver, result_set) to clean up
end

#parse_db_config(db_config_file) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ffi-ogr/postgis_reader.rb', line 22

def parse_db_config(db_config_file)
  conf =
    if db_config_file.instance_of? String
      YAML.load_file(File.expand_path(db_config_file))
    elsif db_config_file.instance_of? Hash
      db_config_file
    end

  db_config = 'PG:'
  
  if conf['dbname']
    db_config = db_config + "dbname='#{conf['dbname']}'"
    if conf['host']
      db_config = db_config + " host='#{conf['host']}'"
      if conf['port']
        db_config = db_config + " port='#{conf['port']}'"
        db_config = db_config + " user='#{conf['user']}'" if conf['user']
        db_config = db_config + " password='#{conf['password']}'" if conf['password']
      else
        raise RuntimeError.new 'port must be specified'
      end
    else
      raise RuntimeError.new 'host must be specified'
    end
  else
    raise RuntimeError.new 'dbname must be specified'
  end

  db_config
end