Class: OGR::Geometry

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Geometry

Returns a new instance of Geometry.



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

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

Instance Attribute Details

#ptrObject

Returns the value of attribute ptr.



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

def ptr
  @ptr
end

Class Method Details

.create_empty(geometry_type) ⇒ Object



34
35
36
# File 'lib/ffi-ogr/geometry.rb', line 34

def self.create_empty(geometry_type)
  OGR::Tools.cast_geometry(FFIOGR.OGR_G_CreateGeometry(geometry_type))
end

.from_geojson(geojson) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ffi-ogr/geometry.rb', line 17

def self.from_geojson(geojson)
  if geojson.instance_of? String
    geojson = MultiJson.load(geojson)
  end

  coords = geojson['coordinates']

  case geojson['type']
  when 'Point'
    OGR::Point.create coords
  when 'LineString'
    OGR::LineString.create coords
  when 'Polygon'
    OGR::Polygon.create coords
  end
end

.release(ptr) ⇒ Object



11
# File 'lib/ffi-ogr/geometry.rb', line 11

def self.release(ptr);end

Instance Method Details

#add_geometry(geometry) ⇒ Object



38
39
40
# File 'lib/ffi-ogr/geometry.rb', line 38

def add_geometry(geometry)
  FFIOGR.OGR_G_AddGeometry(@ptr, geometry.ptr)
end

#add_geometry_directly(geometry) ⇒ Object



42
43
44
# File 'lib/ffi-ogr/geometry.rb', line 42

def add_geometry_directly(geometry)
  FFIOGR.OGR_G_AddGeometryDirectly(@ptr, geometry.ptr)
end

#add_point(coords) ⇒ Object

Raises:

  • (RuntimeError)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ffi-ogr/geometry.rb', line 46

def add_point(coords)
  raise RuntimeError.new("Invalid coordinate(s) specified") unless coords.size >= 2
  x = Float(coords[0])
  y = Float(coords[1])
  z = Float(coords[2]) if coords.size >= 3

  unless z
    FFIOGR.OGR_G_AddPoint_2D(@ptr, x, y)
  else
    FFIOGR.OGR_G_AddPoint(@ptr, x, y, z)
  end
end

#flattenObject



76
77
78
# File 'lib/ffi-ogr/geometry.rb', line 76

def flatten
  FFIOGR.OGR_G_FlattenTo2D(@ptr)
end

#freeObject



13
14
15
# File 'lib/ffi-ogr/geometry.rb', line 13

def free
  FFIOGR.OGR_G_DestroyGeometry(@ptr)
end

#get_areaObject Also known as: area



99
100
101
# File 'lib/ffi-ogr/geometry.rb', line 99

def get_area
  FFIOGR.OGR_G_Area(@ptr)
end

#get_boundaryObject Also known as: boundary



104
105
106
# File 'lib/ffi-ogr/geometry.rb', line 104

def get_boundary
  FFIOGR.OGR_G_Boundary(@ptr)
end

#get_envelopeObject Also known as: envelope



109
110
111
112
113
# File 'lib/ffi-ogr/geometry.rb', line 109

def get_envelope
  envelope = FFI::MemoryPointer.new :pointer, 4
  FFIOGR.OGR_G_GetEnvelope(@ptr, envelope)
  OGR::Envelope.new(envelope.read_array_of_double(4))
end

#get_geometry_typeObject Also known as: geometry_type



80
81
82
# File 'lib/ffi-ogr/geometry.rb', line 80

def get_geometry_type
  FFIOGR.OGR_G_GetGeometryType(@ptr)
end

#get_lengthObject Also known as: length



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

def get_length
  FFIOGR.OGR_G_Length(@ptr)
end

#get_spatial_refObject Also known as: spatial_ref



85
86
87
# File 'lib/ffi-ogr/geometry.rb', line 85

def get_spatial_ref
  OGR::Tools.cast_spatial_reference(FFIOGR.OGR_G_GetSpatialReference(@ptr))
end

#is_3d?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/ffi-ogr/geometry.rb', line 72

def is_3d?
  !!(geometry_type.to_s =~ /_25d/)
end

#set_point(coords, idx) ⇒ Object

Raises:

  • (RuntimeError)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ffi-ogr/geometry.rb', line 59

def set_point(coords, idx)
  raise RuntimeError.new("Invalid coordinate(s) specified") unless coords.size >= 2
  x = Float(coords[0])
  y = Float(coords[1])
  z = Float(coords[2]) if coords.size >= 3

  unless z
    FFIOGR.OGR_G_SetPoint_2D(@ptr, idx, x, y)
  else
    FFIOGR.OGR_G_SetPoint(@ptr, idx, x, y, z)
  end
end

#to_geojsonObject



116
117
118
# File 'lib/ffi-ogr/geometry.rb', line 116

def to_geojson
  MultiJson.load(FFIOGR.OGR_G_ExportToJson(@ptr))
end

#to_gmlObject



125
126
127
# File 'lib/ffi-ogr/geometry.rb', line 125

def to_gml
  FFIOGR.OGR_G_ExportToGML(@ptr)
end

#to_kml(elevation = nil) ⇒ Object



120
121
122
123
# File 'lib/ffi-ogr/geometry.rb', line 120

def to_kml(elevation=nil)
  elevation = String(elevation) unless elevation.nil?
  FFIOGR.OGR_G_ExportToKML(@ptr, elevation)
end

#transform(ct) ⇒ Object



90
91
92
# File 'lib/ffi-ogr/geometry.rb', line 90

def transform(ct)
  FFIOGR.OGR_G_Transform(@ptr, ct)
end