Module: Geos::Tools

Extended by:
Tools
Includes:
GeomTypes
Included in:
BufferParams, GeoJSONReader, 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



95
96
97
98
99
100
101
102
103
104
# File 'lib/ffi-geos/tools.rb', line 95

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

#bool_to_int(bool) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/ffi-geos/tools.rb', line 106

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

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



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

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

  raise Geos::NullPointerError if geom_ptr.null?

  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
  end

  klass.new(geom_ptr, options).tap do |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
end

#check_enum_value(enum, value) ⇒ Object



114
115
116
117
# File 'lib/ffi-geos/tools.rb', line 114

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

#check_geometry(geom) ⇒ Object

Raises:

  • (TypeError)


66
67
68
# File 'lib/ffi-geos/tools.rb', line 66

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

#extract_options!(args) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/ffi-geos/tools.rb', line 127

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



85
86
87
88
89
90
91
92
93
# File 'lib/ffi-geos/tools.rb', line 85

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



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

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, "Unexpected policy value: #{policy}"
  end
end

#symbol_for_enum(enum, value) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/ffi-geos/tools.rb', line 119

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