Sha256: 3db2112742b5d88d283cd7bd566971ff7eff3d6e6b7818ba284f9c3954330cf4

Contents?: true

Size: 1.52 KB

Versions: 9

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module OGR
  class Point
    include OGR::Geometry

    # @param [FFI::Pointer] geometry_ptr
    def initialize(geometry_ptr = nil, spatial_reference: nil)
      geometry_ptr ||= OGR::Geometry.create(:wkbPoint)
      initialize_from_pointer(geometry_ptr)
      self.spatial_reference = spatial_reference if spatial_reference
    end

    # @return [Float]
    def x
      return nil if empty?

      FFI::OGR::API.OGR_G_GetX(@c_pointer, 0)
    end

    # @return [Float]
    def y
      return nil if empty?

      FFI::OGR::API.OGR_G_GetY(@c_pointer, 0)
    end

    # @return [<Array<Float, Float>] [x, y].
    def point
      return [] if empty?

      x_ptr = FFI::MemoryPointer.new(:double)
      y_ptr = FFI::MemoryPointer.new(:double)
      z_ptr = FFI::MemoryPointer.new(:double)
      FFI::OGR::API.OGR_G_GetPoint(@c_pointer, 0, x_ptr, y_ptr, z_ptr)

      [x_ptr.read_double, y_ptr.read_double]
    end
    alias point_value point

    # Wrapper around {#point_value} to provide API parity with other geometries
    # that can have multiple points.
    #
    # @return [Array<Array<Float, Float>>]
    # TODO: move to an extension
    def point_values
      [point]
    end

    # @param x [Number]
    # @param y [Number]
    def set_point(x, y)
      FFI::OGR::API.OGR_G_SetPoint_2D(@c_pointer, 0, x, y)
    end

    # Adds a point to a LineString or Point geometry.
    #
    # @param x [Float]
    # @param y [Float]
    def add_point(x, y)
      FFI::OGR::API.OGR_G_AddPoint_2D(@c_pointer, x, y)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ffi-gdal-1.0.0.beta16 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta15 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta14 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta13 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta12 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta11 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta10 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta9 lib/ogr/geometries/point.rb
ffi-gdal-1.0.0.beta8 lib/ogr/geometries/point.rb