Module: Geos::Tools

Extended by:
Tools
Includes:
GeomTypes
Included in:
BufferParams, Geometry, PreparedGeometry, STRtree, Tools, Utils, WkbReader, WkbWriter, WktReader
Defined in:
lib/ffi-geos/tools.rb

Constant Summary

Constants included from GeomTypes

GeomTypes::GEOS_GEOMETRYCOLLECTION, GeomTypes::GEOS_LINEARRING, GeomTypes::GEOS_LINESTRING, GeomTypes::GEOS_MULTILINESTRING, GeomTypes::GEOS_MULTIPOINT, GeomTypes::GEOS_MULTIPOLYGON, GeomTypes::GEOS_POINT, GeomTypes::GEOS_POLYGON

Instance Method Summary collapse

Instance Method Details

#bool_result(result) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/ffi-geos/tools.rb', line 98

def bool_result(result)
  case result
  when 1
    true
  when 0
    false
  else
    raise Geos::UnexpectedBooleanResultError.new(result)
  end
end

#bool_to_int(bool) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/ffi-geos/tools.rb', line 109

def bool_to_int(bool)
  if bool
    1
  else
    0
  end
end

#cast_geometry_ptr(geom_ptr, options = {}) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ffi-geos/tools.rb', line 26

def cast_geometry_ptr(geom_ptr, options = {})
  options = {
    :auto_free => true
  }.merge(options)

  if geom_ptr.null?
    raise Geos::NullPointerError.new
  end

  klass = case FFIGeos.GEOSGeomTypeId_r(Geos.current_handle_pointer, geom_ptr)
    when GEOS_POINT
      Point
    when GEOS_LINESTRING
      LineString
    when GEOS_LINEARRING
      LinearRing
    when GEOS_POLYGON
      Polygon
    when GEOS_MULTIPOINT
      MultiPoint
    when GEOS_MULTILINESTRING
      MultiLineString
    when GEOS_MULTIPOLYGON
      MultiPolygon
    when GEOS_GEOMETRYCOLLECTION
      GeometryCollection
    else
      raise Geos::InvalidGeometryTypeError.new
  end

  klass.new(geom_ptr, options).tap { |ret|
    if options[:srid]
      ret.srid = options[:srid] || 0
    elsif options[:srid_copy]
      ret.srid = if Geos.srid_copy_policy == :zero
        0
      else
        options[:srid_copy] || 0
      end
    end
  }
end

#check_enum_value(enum, value) ⇒ Object



117
118
119
120
# File 'lib/ffi-geos/tools.rb', line 117

def check_enum_value(enum, value)
  enum[value] or
    raise TypeError.new("Couldn't find valid #{enum.tag} value: #{value}")
end

#check_geometry(geom) ⇒ Object

Raises:

  • (TypeError)


69
70
71
# File 'lib/ffi-geos/tools.rb', line 69

def check_geometry(geom)
  raise TypeError.new("Expected Geos::Geometry") unless geom.is_a?(Geos::Geometry)
end

#extract_options!(args) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/ffi-geos/tools.rb', line 130

def extract_options!(args)
  if args.last.is_a?(Hash)
    args.pop
  else
    {}
  end
end

#pick_srid_according_to_policy(srid, policy = Geos.srid_copy_policy) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/ffi-geos/tools.rb', line 88

def pick_srid_according_to_policy(srid, policy = Geos.srid_copy_policy)
  policy = Geos.srid_copy_policy_default if policy == :default

  if srid != 0 && policy != :zero
    srid
  else
    0
  end
end

#pick_srid_from_geoms(srid_a, srid_b, policy = Geos.srid_copy_policy) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ffi-geos/tools.rb', line 73

def pick_srid_from_geoms(srid_a, srid_b, policy = Geos.srid_copy_policy)
  policy = Geos.srid_copy_policy_default if policy == :default

  case policy
    when :zero
      0
    when :lenient
      srid_a
    when :strict
      raise Geos::MixedSRIDsError.new(srid_a, srid_b)
    else
      raise ArgumentError.new("Unexpected policy value: #{policy}")
  end
end

#symbol_for_enum(enum, value) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/ffi-geos/tools.rb', line 122

def symbol_for_enum(enum, value)
  if value.is_a?(Symbol)
    value
  else
    enum[value]
  end
end