Class: OGR::Geocoder

Inherits:
Object
  • Object
show all
Defined in:
lib/ogr/geocoder.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Geocoder

Returns a new instance of Geocoder.

Parameters:

  • options (Hash)

Options Hash (**options):

  • cache_file (String)

    Name of the file to write the cache to.

  • read_cache (Boolean)

    Defaults to true.

  • write_cache (Boolean)

    Defaults to true.

  • service (String)

    “MAPQUEST_NOMINATIM”, “YAHOO”, “GEONAMES”, “BING”, or other value.

  • email (String)

    Required for the GEONAMES service.

  • key (String)

    Required for the BING service.

  • application (String)

    Sets the User-Agent request header. Defaults to the GDAL version string.

  • language (String)

    Sets the Accept-Language request header.

  • delay (Float)

    Minimum delay, in seconds, between consecutive requests. Defaults to 1.0.

  • query_template (String)

    URL template for GET requests. Must contain one and only one occurrence of %s. If not specified, the URL template is hard-coded.

  • reverse_query_template (String)

    Template to use for reverse geocoding.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ogr/geocoder.rb', line 38

def initialize(**options)
  converted_options = options.each_with_object({}) do |(k, v), obj|
    key = "OGR_GEOCODE_#{k.to_s.upcase}"
    obj[key] = v
  end

  options_ptr = GDAL::Options.pointer(converted_options)

  pointer = FFI::GDAL::GDAL.OGRGeocodeCreateSession(options_ptr)
  @c_pointer = FFI::AutoPointer.new(pointer, Geocoder.method(:release))
end

Instance Attribute Details

#c_pointerFFI::Pointer (readonly)

Returns C pointer to the C geocoding session.

Returns:

  • (FFI::Pointer)

    C pointer to the C geocoding session.



16
17
18
# File 'lib/ogr/geocoder.rb', line 16

def c_pointer
  @c_pointer
end

Class Method Details

.release(pointer) ⇒ Object

Parameters:

  • pointer (FFI::Pointer)


9
10
11
12
13
# File 'lib/ogr/geocoder.rb', line 9

def self.release(pointer)
  return unless pointer && !pointer.null?

  FFI::GDAL::GDAL.OGRGeocodeDestroySession(pointer)
end

Instance Method Details

#destroy!Object



50
51
52
53
54
# File 'lib/ogr/geocoder.rb', line 50

def destroy!
  Geocoder.release(@c_pointer)

  @c_pointer = nil
end

#geocode(query, **options) ⇒ OGR::Layer?

Parameters:

  • query (String)
  • options (Hash)
  • extra_query_parameters (Hash)

    a customizable set of options

Options Hash (**options):

  • addressdetails (Boolean)

    true to include a breakdown of the address into elements. Only works with some geocoding services.

  • countrycodes (Array<String, Symbol>)

    Limit the search to a specific country or countries. Only works with some geocoding services.

  • limit (Integer)

    Limit the number of records returned. Only works with some geocoding services.

  • raw_feature ("YES")

    Adds a raw feature to the returned feature that includes that raw XML response body.

Returns:



68
69
70
71
72
73
74
75
# File 'lib/ogr/geocoder.rb', line 68

def geocode(query, **options)
  options_ptr = GDAL::Options.pointer(options)
  layer_ptr =
    FFI::GDAL::GDAL.OGRGeocode(@c_pointer, query, nil, options_ptr)
  return nil if layer_ptr.null?

  OGR::Layer.new(layer_ptr)
end

#reverse_geocode(lon, lat, **options) ⇒ OGR::Layer

Parameters:

  • lon (Float)
  • lat (Float)
  • options (Hash)
  • extra_query_parameters (Hash)

    a customizable set of options

Options Hash (**options):

  • zoom (Float)

    Only used by the Nominatim service.

  • raw_feature ("YES")

    Adds a raw feature to the returned feature that includes that raw XML response body.

Returns:



85
86
87
88
89
90
91
92
# File 'lib/ogr/geocoder.rb', line 85

def reverse_geocode(lon, lat, **options)
  options_ptr = GDAL::Options.pointer(options)
  layer_ptr =
    FFI::GDAL::GDAL.OGRGeocodeReverse(@c_pointer, lon, lat, options_ptr)
  return nil if layer_ptr.null?

  OGR::Layer.new(layer_ptr)
end