Method: OGR::SpatialReference#initialize

Defined in:
lib/ogr/spatial_reference.rb

#initialize(spatial_reference_or_wkt = nil) ⇒ SpatialReference

Builds a spatial reference object using either the passed-in WKT string, OGR::SpatialReference object, or a pointer to an in-memory SpatialReference object. If nothing is passed in, an empty SpatialReference object is created, in which case you’ll need to populate relevant attributes.

If a OGR::SpatialReference is given, this clones that object so it can have it’s own object (relevant for cleaning up when garbage collecting).

Parameters:

Raises:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ogr/spatial_reference.rb', line 156

def initialize(spatial_reference_or_wkt = nil)
  pointer =
    case spatial_reference_or_wkt.class.name
    when "OGR::SpatialReference"
      # This is basically getting a reference to the SpatialReference that
      # was passed in, thus when this SpatialReference gets garbage-collected,
      # it shouldn't release anything.
      ptr = spatial_reference_or_wkt.c_pointer
      ptr.autorelease = false
    when "String", "NilClass"
      # FWIW, the docs say:
      # Note that newly created objects are given a reference count of one.
      #
      # ...which implies that we should use Release here instead of Destroy.
      ptr = FFI::OGR::SRSAPI.OSRNewSpatialReference(spatial_reference_or_wkt)
      ptr.autorelease = false

      # We're instantiating a new SR, so we can use .destroy.
      FFI::AutoPointer.new(ptr, SpatialReference.method(:release))
    when "FFI::AutoPointer", "FFI::Pointer", "FFI::MemoryPointer"
      # If we got a pointer, we don't know who owns the data, so don't
      # touch anything about autorelease/AutoPointer.
      spatial_reference_or_wkt
    else
      log "Dunno what to do with #{spatial_reference_or_wkt.inspect}"
    end

  raise OGR::CreateFailure, "Unable to create SpatialReference." if pointer.nil? || pointer.null?

  @c_pointer = pointer
end