Class: Georeference::GeoLocate

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

Constant Summary collapse

URI_HOST =

These are fine here

'www.museum.tulane.edu'
URI_PATH =
'/webservices/geolocatesvcv2/glcwrap.aspx?'

Constants inherited from Georeference

FACTORY

Instance Attribute Summary collapse

Attributes inherited from Georeference

#collecting_event_id, #error_depth, #error_geographic_item_id, #error_radius, #geographic_item_id, #position, #source_id, #type

Instance Method Summary collapse

Methods included from Housekeeping

#has_polymorphic_relationship?

Constructor Details

#initialize(params = {}) ⇒ GeoLocate

g = Georeference::GeoLocate.new g.locate('USA', 'Champaign', 'IL')

Things to build a request:

r = JSON.parse(Net::HTTP.get('www.museum.tulane.edu', '/webservices/geolocatesvcv2/glcwrap.aspx?Country=USA&Locality=champaign&state=illinois&dopoly=true'))

request => text from '?' to end of request
'?country=usa&locality=champaign&state=illinois&dopoly=true'

locality => name of a place 'CHAMPAIGN' (or building, i.e. 'Eiffel Tower')
country => name of a country 'USA', or Germany
state => 'IL', or 'illinois' (required in the United States)
county => supply as a parameter, returned as 'Adm='
hwyX
enableH2O
doUncert
doPoly
displacePoly
languageKey
fmt => JSON or GeoJSON


36
37
38
39
40
41
42
43
44
45
# File 'app/models/georeference/geo_locate.rb', line 36

def initialize(params = {})
  if params != {}
    @request = params[:request]
    params.delete(:request)
  end
  super
  @response = nil
  @request  ||= {}
  build if @request != {}
end

Instance Attribute Details

#requestObject

Returns the value of attribute request



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

def request
  @request
end

#responseObject

require 'json'



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

def response
  @response
end

Instance Method Details

#buildObject



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

def build
  if @request.keys.size > 0
    locate
    if @response['numResults'] > 0
      make_geographic_item
      make_error_geographic_item
    else
      errors.add(:api_request, 'requested parameters returned no results.')
    end
  else
    errors.add(:base, 'no request parameters provided.')
  end
end

#call_apiObject



93
94
95
96
97
# File 'app/models/georeference/geo_locate.rb', line 93

def call_api
  # TODO: remove 'part' after debugging
  part = Net::HTTP.get(URI_HOST, URI_PATH + self.api_request)
  part
end

#get_responseObject



87
88
89
90
91
# File 'app/models/georeference/geo_locate.rb', line 87

def get_response
  @response = JSON.parse(self.call_api)
  # TODO: remove the following line after debugging
  @response
end

#locateObject



51
52
53
54
55
# File 'app/models/georeference/geo_locate.rb', line 51

def locate
  if self.make_request
    get_response
  end
end

#make_error_geographic_itemObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/georeference/geo_locate.rb', line 105

def make_error_geographic_item

  # evaluate for error_radius only if called for (default)
  self.error_radius = @request[:doUncert] ? @response['resultSet']['features'][0]['properties']['uncertaintyRadiusMeters'] : 3.0

  #evaluate for error polygon only if called for (non-default)
  if @request[:doPoly]
    # Build the error geographic shape
    # isolate the array of points from the response, and build the polygon from a line_string
    # made out of the points
    p         = @response['resultSet']['features'][0]['properties']['uncertaintyPolygon']['coordinates'][0]
    # build an array of Georeference::FACTORY.points from p

    # TODO: could benchmark
    # poly = 'MULTIPOLYGON(((' + p.collect{|a,b| "#{a} #{b}"}.join(',') + ')))'
    # parsed_poly = Georeference::FACTORY.parse_wkt(poly)

    err_array = []
    p.each { |point| err_array.push(Georeference::FACTORY.point(point[0], point[1])) }
    self.error_geographic_item         = GeographicItem.new
    self.error_geographic_item.polygon = Georeference::FACTORY.polygon(Georeference::FACTORY.line_string(err_array))
  else
    # this may not be strictly necessary...
    self.error_geographic_item = nil
  end

end

#make_geographic_itemObject



99
100
101
102
103
# File 'app/models/georeference/geo_locate.rb', line 99

def make_geographic_item
  p                          = @response['resultSet']['features'][0]['geometry']['coordinates']
  self.geographic_item       = GeographicItem.new
  self.geographic_item.point = Georeference::FACTORY.point(p[0], p[1])
end

#make_requestObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/georeference/geo_locate.rb', line 57

def make_request
  # TODO: validation: if country is some form of 'USA', a state is required
  # TODO: options can be added: county, hwyX, etc.
  opts             = {
    country:      nil,
    state:        nil,
    county:       nil,
    locality:     nil,
    hwyX:         'false',
    enableH2O:    'false',
    doUncert:     'true',
    doPoly:       'false',
    displacePoly: 'false',
    languageKey:  '0',
    fmt:          'json'
  }.merge!(@request)

  # store the complete request to be evaluated to parse results later
  @request = opts

  # TODO: write actual validation
  # if valid == true
  self.api_request = opts.collect { |key, value| "#{key}=#{value}" }.join('&')
  #  true
  # else
  #  errors.add(:api_request, 'invalid request parameters')
  #  false
  # end
end

#request_hashObject



47
48
49
# File 'app/models/georeference/geo_locate.rb', line 47

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