Class: Georeference::GeoLocate

Inherits:
Georeference show all
Defined in:
app/models/georeference/geo_locate.rb

Overview

A Georeference derived from a call to the Tulane GeoLocate API.

Defined Under Namespace

Classes: Request, RequestUI, Response

Constant Summary collapse

API_HOST =
'www.geo-locate.org'.freeze
API_PATH =
'/webservices/geolocatesvcv2/glcwrap.aspx?'.freeze
EMBED_PATH =
'/web/webgeoreflight.aspx?'.freeze
EMBED_HOST =
'www.geo-locate.org'.freeze

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary collapse

Attributes inherited from Georeference

#api_request, #collecting_event_id, #error_depth, #error_geographic_item_id, #error_radius, #geographic_item_id, #is_median_z, #is_public, #is_undefined_z, #no_cached, #position, #project_id, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Georeference

#add_err_geo_item_inside_err_radius, #add_error_depth, #add_error_geo_item_inside_area, #add_error_geo_item_intersects_area, #add_error_radius, #add_error_radius_inside_area, #add_obj_inside_area, #add_obj_inside_err_geo_item, #add_obj_inside_err_radius, batch_create_from_georeference_matcher, #check_err_geo_item_inside_err_radius, #check_error_geo_item_inside_area, #check_error_geo_item_intersects_area, #check_error_radius_inside_area, #check_obj_inside_area, #check_obj_inside_err_geo_item, #check_obj_inside_err_radius, #error_box, #error_radius_buffer_polygon, filter_by, #geographic_item_present_if_error_radius_provided, #heading, #latitude, #longitude, #method_name, point_type, #set_cached, #to_geo_json_feature, #to_simple_json_feature, with_geographic_area, with_locality, with_locality_as, with_locality_like, within_radius_of_item

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::HasRoles

#has_roles?

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#api_responseObject

Returns the value of attribute api_response.



4
5
6
# File 'app/models/georeference/geo_locate.rb', line 4

def api_response
  @api_response
end

#iframe_responseObject

Returns the value of attribute iframe_response.



4
5
6
# File 'app/models/georeference/geo_locate.rb', line 4

def iframe_response
  @iframe_response
end

Class Method Details

.build(request_params) ⇒ GeoLocate

Build a georeference starting with a set of request parameters.

Parameters:

  • request_params (ActionController::Parameters)

Returns:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/georeference/geo_locate.rb', line 159

def self.build(request_params)
  g = self.new

  # @todo write a Request.valid_params? method to use here
  # @todo #1: Just what will be the validation criteria for the request?
  # @todo #2: Why not judge validity from the response?
  if request_params.nil?
    g.errors.add(:base, 'invalid or no request parameters provided.')
    return g
  end

  request = Request.new(request_params)
  request.locate

  if request.succeeded?
    g.api_response = request.response
    g.api_request  = request.request_param_string
  else
    g.errors.add(:api_request, 'requested parameters did not succeed in returning a result')
  end
  g
end

.parse_iframe_result(response_string) ⇒ Array

parsing the four possible bits of a response into an array

Parameters:

  • response_string (String)

    from Tulane

Returns:

  • (Array)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/georeference/geo_locate.rb', line 135

def self.parse_iframe_result(response_string)
  s = unify_response_string(response_string)
  lat, long, error_radius, uncertainty_polygon = s.split('|')
  uncertainty_points = nil
  unless uncertainty_polygon.nil?
    if uncertainty_polygon =~ /unavailable/i # todo: there are many more possible error conditions
      uncertainty_points = nil
    else
      uncertainty_points = uncertainty_polygon.split(',').reverse.in_groups_of(2)
    end
  end
  [lat, long, error_radius, uncertainty_points]
end

.unify_response_string(response_string) ⇒ Object

TODO: move ti iframe getter/setter



150
151
152
153
154
# File 'app/models/georeference/geo_locate.rb', line 150

def self.unify_response_string(response_string)
  response_string.gsub!(/[\t]/, '|')
  response_string.gsub!(/\s+/, '|')
  response_string
end

Instance Method Details

#dwc_georeference_attributesObject



11
12
13
14
15
16
17
18
19
# File 'app/models/georeference/geo_locate.rb', line 11

def dwc_georeference_attributes
  h = {}
  super(h)
  h.merge!(
    georeferenceSources: "GEOLocate ",
    georeferenceRemarks: "Typically created by copy-pasting one or more values from a collecting event into a GEOLocate form.",
    georeferenceProtocol: 'Generated via a query through the GEOLocate web interface')
  h
end

#make_err_polygon(wkb) ⇒ GeographicItem::Polygon

Returns GeographicItem::Polygon, either found, or created.

Parameters:

  • wkb (String)

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/georeference/geo_locate.rb', line 61

def make_err_polygon(wkb)
  polygon  = Gis::FACTORY.parse_wkb(wkb)
  # ActiveRecord::Base.send(:sanitize_sql_array, ['polygon = ST_GeographyFromText(?)', polygon.to_s])
  test_grs = GeographicItem::Polygon.where(['polygon = ST_GeographyFromText(?)', polygon.to_s])
  if test_grs.empty?
    test_grs = [GeographicItem.new(polygon: polygon)]
  end
  if test_grs.first.new_record?
    test_grs.first.save
  else
    test_grs.first
  end
  test_grs.first
end

#make_error_geographic_item(uncertainty_polygon, uncertainty_radius) ⇒ Object

TODO:

get geoJson results and handle all this automatically?

Parameters:

  • uncertainty_polygon (RGeo::Polygon)
  • uncertainty_radius (Integer)

    in meters



122
123
124
125
126
127
128
129
130
# File 'app/models/georeference/geo_locate.rb', line 122

def make_error_geographic_item(uncertainty_polygon, uncertainty_radius)
  self.error_radius = uncertainty_radius if !uncertainty_radius.nil?
  unless uncertainty_polygon.nil?
    err_array = []
    uncertainty_polygon.each { |point| err_array.push(Gis::FACTORY.point(point[0], point[1])) }
    self.error_geographic_item =
      GeographicItem.new(polygon: Gis::FACTORY.polygon(Gis::FACTORY.line_string(err_array)))
  end
end

#make_geographic_point(x, y, z = '0.0') ⇒ Object

Returns GeographicItem::Point, either found or created.

Parameters:

  • x (String)

    longitude

  • y (String)

    latitude

  • z (String) (defaults to: '0.0')

    elevation, defaults to 0.0

Returns:

  • (Object)

    GeographicItem::Point, either found or created.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/georeference/geo_locate.rb', line 80

def make_geographic_point(x, y, z = '0.0')
  if x.blank? || y.blank?
    test_grs = []
  else
    test_grs = GeographicItem::Point
                 .where("point = ST_GeographyFromText('POINT(? ? ?)')", x.to_f, y.to_f, z.to_f)
                 # .where(['ST_Z(point::geometry) = ?', z.to_f])
  end
  if test_grs.empty? # put a new one in the array
    test_grs = [GeographicItem.new(point: Gis::FACTORY.point(x, y, z))]
  end
  test_grs.first
end

#request_hashHash

Returns of api request pieces.

Returns:

  • (Hash)

    of api request pieces



55
56
57
# File 'app/models/georeference/geo_locate.rb', line 55

def request_hash
  Hash[*self.api_request.split('&').collect { |a| a.split('=', 2) }.flatten]
end