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(r) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/ffi-geos/tools.rb', line 79

def bool_result(r)
  case r
  when 1
    true
  when 0
    false
  else
    raise RuntimeError.new("Unexpected boolean result: #{r}")
  end
end

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/ffi-geos/tools.rb', line 7

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

  if geom_ptr.null?
    raise RuntimeError.new("Tried to create a Geometry from a NULL pointer!")
  end

  klass = case FFIGeos.GEOSGeomTypeId_r(Geos.current_handle, 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 RuntimeError.new("Invalid geometry type")
  end

  klass.new(geom_ptr, options[:auto_free]).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



90
91
92
93
# File 'lib/ffi-geos/tools.rb', line 90

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)


50
51
52
# File 'lib/ffi-geos/tools.rb', line 50

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

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



69
70
71
72
73
74
75
76
77
# File 'lib/ffi-geos/tools.rb', line 69

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ffi-geos/tools.rb', line 54

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



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

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