Class: OGR::SpatialReference

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-ogr/spatial_reference.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ SpatialReference

Returns a new instance of SpatialReference.


5
6
7
8
# File 'lib/ffi-ogr/spatial_reference.rb', line 5

def initialize(ptr)
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:release))
  @ptr.autorelease = false
end

Instance Attribute Details

#ptrObject

Returns the value of attribute ptr


3
4
5
# File 'lib/ffi-ogr/spatial_reference.rb', line 3

def ptr
  @ptr
end

Class Method Details

.import(sr_import, format = 'epsg') ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ffi-ogr/spatial_reference.rb', line 28

def self.import(sr_import, format = 'epsg')
  sr = OGR::Tools.cast_spatial_reference(FFIOGR.OSRNewSpatialReference(nil))

  case format
  when 'epsg'
    sr.import_epsg sr_import
  when 'wkt'
    sr.import_wkt sr_import
  when 'proj4'
    sr.import_proj4 sr_import
  when 'esri'
    sr.import_esri sr_import
  else
    raise RuntimeError.new "Format: #{format} is not currently supported"
  end

  sr
end

.release(ptr) ⇒ Object


10
# File 'lib/ffi-ogr/spatial_reference.rb', line 10

def self.release(ptr);end

Instance Method Details

#==(other) ⇒ Object


94
95
96
# File 'lib/ffi-ogr/spatial_reference.rb', line 94

def ==(other)
  self.to_wkt == other.to_wkt
end

#check_int(sr_import, format = 'epsg') ⇒ Object


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

def check_int(sr_import, format = 'epsg')
  begin
    Integer(sr_import)
  rescue => ex
    raise RuntimeError.new "Format: #{format} requires an integer value"
  end
end

#check_string(sr_import, format) ⇒ Object


24
25
26
# File 'lib/ffi-ogr/spatial_reference.rb', line 24

def check_string(sr_import, format)
  raise RuntimeError.new "Format: #{format} requires a string value" unless sr_import.instance_of? String
end

#find_transformation(out_sr) ⇒ Object


98
99
100
# File 'lib/ffi-ogr/spatial_reference.rb', line 98

def find_transformation(out_sr)
  CoordinateTransformation.find_transformation self, out_sr
end

#freeObject


12
13
14
# File 'lib/ffi-ogr/spatial_reference.rb', line 12

def free
  FFIOGR.OSRDestroySpatialReference(@ptr)
end

#import_epsg(epsg_code) ⇒ Object


60
61
62
63
# File 'lib/ffi-ogr/spatial_reference.rb', line 60

def import_epsg(epsg_code)
  epsg_code = check_int epsg_code, 'epsg'
  FFIOGR.OSRImportFromEPSG @ptr, epsg_code
end

#import_esri(esri_string) ⇒ Object


65
66
67
68
69
70
71
72
# File 'lib/ffi-ogr/spatial_reference.rb', line 65

def import_esri(esri_string)
  check_string esri_string, 'esri'
  esri_ptr = FFI::MemoryPointer.from_string esri_string
  esri_ptr_ptr = FFI::MemoryPointer.new :pointer, 2
  esri_ptr_ptr[0].put_pointer 0, esri_ptr
  esri_ptr_ptr[1].put_pointer 0, nil
  FFIOGR.OSRImportFromESRI @ptr, esri_ptr_ptr
end

#import_proj4(proj4_string) ⇒ Object


55
56
57
58
# File 'lib/ffi-ogr/spatial_reference.rb', line 55

def import_proj4(proj4_string)
  check_string proj4_string, 'proj4'
  FFIOGR.OSRImportFromProj4 @ptr, proj4_string
end

#import_wkt(wkt) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/ffi-ogr/spatial_reference.rb', line 47

def import_wkt(wkt)
  check_string wkt, 'wkt'
  wkt_ptr = FFI::MemoryPointer.from_string wkt
  wkt_ptr_ptr = FFI::MemoryPointer.new :pointer
  wkt_ptr_ptr.put_pointer 0, wkt_ptr
  FFIOGR.OSRImportFromWkt @ptr, wkt_ptr_ptr
end

#to_proj4Object


87
88
89
90
91
92
# File 'lib/ffi-ogr/spatial_reference.rb', line 87

def to_proj4
  ptr = FFI::MemoryPointer.new :pointer
  FFIOGR.OSRExportToProj4(@ptr, ptr)
  str_ptr = ptr.read_pointer
  return str_ptr.null? ? nil: str_ptr.read_string
end

#to_wkt(pretty = false) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ffi-ogr/spatial_reference.rb', line 74

def to_wkt(pretty=false)
  ptr = FFI::MemoryPointer.new :pointer

  unless pretty
    FFIOGR.OSRExportToWkt(@ptr, ptr)
  else
    FFIOGR.OSRExportToPrettyWkt(@ptr, ptr, 4)
  end
  str_ptr = ptr.read_pointer

  return str_ptr.null? ? nil : str_ptr.read_string
end