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, #day_georeferenced, #error_depth, #error_geographic_item_id, #error_radius, #geographic_item_id, #is_median_z, #is_public, #is_undefined_z, #month_georeferenced, #no_cached, #position, #project_id, #type, #year_georeferenced

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, #dwc_occurrences, #error_box, #error_radius_buffer_polygon, filter_by, #geographic_item_present_if_error_radius_provided, #heading, #latitude, #longitude, #method_name, point_type, #radius_from_error_shape, #round_error_radius, #set_cached, #set_cached_collecting_event, #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::Citations

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

Methods included from Shared::ProtocolRelationships

#protocolled?, #reject_protocols

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:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/georeference/geo_locate.rb', line 169

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

Returns parsing the four possible bits of a response into an array.

Parameters:

  • response_string (String)

    from Tulane

Returns:

  • (Array)

    parsing the four possible bits of a response into an array



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/georeference/geo_locate.rb', line 145

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



160
161
162
163
164
# File 'app/models/georeference/geo_locate.rb', line 160

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.')
  h[:georeferenceProtocol] = 'Generated via a query through the GEOLocate web interface' if h[:georeferenceProtocol].blank?
  h
end

#make_err_polygon(wkb) ⇒ GeographicItem::Polygon

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

Parameters:

  • wkb (String)

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/georeference/geo_locate.rb', line 70

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:)]
  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



131
132
133
134
135
136
137
138
139
140
# File 'app/models/georeference/geo_locate.rb', line 131

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.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/georeference/geo_locate.rb', line 89

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



64
65
66
# File 'app/models/georeference/geo_locate.rb', line 64

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