Your database columns are called CORDS
but in the hash you pass to Location.new
, you pass the key :coords
. You also fail to pass a :zip
.
Does the Location
model define a different mapping?
Вопрос
I have a process on my ruby site that should pull geographical data and insert it into my locations table.
I did not program this. It was with the site when our company took over. I dont know alot about ruby or JSON/XML
def self.check_unknown(cs)
report = Report.first( :conditions => { :metric => "unknown_locations", :name => cs } )
#i.e. if it's been tried 25 times, add it to the locations table.
if report.value > 5
city, state = cs.split(",")
state = state.strip
google_api_results = HTTParty.get("http://maps.googleapis.com/maps/api/geocode/json?address=#{cs.gsub(/ /, "%20")}&sensor=true")
geo_data = google_api_results.parsed_response
lat = geo_data["results"][0]["geometry"]["location"]["lat"]
lng = geo_data["results"][0]["geometry"]["location"]["lng"]
l = Location.new({
:city => city,
:state => state,
:coords => "ST_GeographyFromText('POINT( #{lat} #{lng} )')"
})
l.save
Report.delete(report.id)
return l
else
return false
end
end
This is using the following table for information:
ID NAME METRIC VALUE CREATED_AT UPDATED_AT
19 | "Jenkinstownship, PA" | "unknown_locations" | 89 | "2013-01-17 05:22:25.603078" | "2013-01-27 16:49:38.642298"
23 | "Evergreen, SC" | "unknown_locations" | 154 | "2013-01-17 05:22:52.280582" | "2013-03-25 18:46:21.642839"
24 | "East Scio, OH" | "unknown_locations" | 29 | "2013-01-17 05:23:01.952421" | "2013-01-18 00:58:39.457106"
28 | "Ft Collins, CO" | "unknown_locations" | 134 | "2013-01-17 05:23:34.109053" | "2013-04-02 02:45:16.37779"
Expose to insert data into locations table like:
ID CORDS CITY STATE ZIP CREATED UPDATED CS ALT
69640 | "0101000020E6100000B5A679C729A255C0787AA52C430E4140" | "blountsville" | "al" | "35031" | "2012-05-31 11:00:34.269803" | "2012-05-31 11:00:34.269803" | "blountsville, al" | ""
69641 | "0101000020E61000009D4B7155D9C155C09E0AB8E7F9F94040" | "bremen" | "al" | "35033" | "2012-05-31 11:00:34.278116" | "2012-05-31 11:00:34.278116" | "bremen, al" | ""
69642 | "0101000020E610000020240B98C0CD55C0A6F10BAF24754040" | "brent" | "al" | "35034" | "2012-05-31 11:00:34.28644" | "2012-05-31 11:00:34.28644" | "brent, al" | ""
69643 | "0101000020E6100000EDBB22F8DFBC55C083DA6FED44854040" | "brierfield" | "al" | "35035" | "2012-05-31 11:00:34.294715" | "2012-05-31 11:00:34.294715" | "brierfield, al" | ""
69644 | "0101000020E61000006DAD2F12DABA55C0A1F831E6AED14040" | "brookside" | "al" | "35036" | "2012-05-31 11:00:34.303135" | "2012-05-31 11:00:34.303135" | "brookside, al" | ""
69645 | "0101000020E6100000B9533A58FFAF55C08FFB56EBC48D4040" | "calera" | "al" | "35040" | "2012-05-31 11:00:34.311167" | "2012-05-31 11:00:34.311167" | "calera, al" | ""
69646 | "0101000020E6100000EDBB22F8DFC855C0B2135E82537B4040" | "centreville" | "al" | "35042" | "2012-05-31 11:00:34.319782" | "2012-05-31 11:00:34.319782" | "centreville, al" | ""
69647 | "0101000020E61000008638D6C56DAA55C03065E08096A84040" | "chelsea" | "al" | "35043" | "2012-05-31 11:00:34.328041" | "2012-05-31 11:00:34.328041" | "chelsea, al" | ""
But it is entering like this:
ID CORDS CITY STATE ZIP CREATED UPDATED CS ALT
8806 | "" | "URBANCREST" | "OH" | "" | "2014-04-29 12:58:39.209943" | "2014-04-29 12:58:39.209943" | "" | ""
8807 | "" | "north valmey" | "NV" | "" | "2014-04-29 13:19:23.996848" | "2014-04-29 13:19:23.996848" | "" | ""
8808 | "" | "SAN SIMON" | "AZ" | "" | "2014-04-29 13:19:30.744791" | "2014-04-29 13:19:30.744791" | "" | ""
8809 | "" | "COLUMBIA FALLS"| "ID" | "" | "2014-04-29 13:19:30.789425" | "2014-04-29 13:19:30.789425" | "" | ""
WHOLE Locations model
class Location < ActiveRecord::Base
attr_accessible :city, :coords, :state, :zip
set_rgeo_factory_for_column(:coords, RGeo::Geographic.spherical_factory(:srid => 4326))
def self.validate_cs_and_return_proper(location)
return "ANYWHERE" if location.upcase == "ANYWHERE" || location.nil? || location == ""
state = location[-2,2]
city = location.gsub(%r(, [a-zA-Z]+), '').strip
l = first(:conditions => {:city => city.downcase, :state => state.downcase})
if l.nil?
new_l = where("(levenshtein(city, '#{city}') <= 4) AND state = '#{state}'").order("levenshtein(city, '#{city}') ASC").first
return "#{new_l.city}, #{new_l.state}"
else
return location
end
end
def self.city_state_exists?(location)
return true if location.upcase == "ANYWHERE" || location.nil? || location == ""
state = location[-2,2]
city = location.gsub(%r(, [a-zA-Z]+), '').strip
l = first(:conditions => {:city => city.downcase, :state => state.downcase})
if l.nil?
new_l = where("(levenshtein(city, '#{city}') <= 4) AND state = '#{state}'").order("levenshtein(city, '#{city}') ASC").first
return true unless new_l.blank?
return false
else
return true
end
end
def self.to_point(location)
return location unless location.class == String
return nil if location.upcase=='ANYWHERE'
state = location[-2,2]
city = location.gsub(%r(, [a-zA-Z]+), '').strip
l = first(:conditions => {:city => city.downcase, :state => state.downcase})
l.coords
end
def self.to_cs(point)
return "ANYWHERE" if point.nil? || (point.x == 0.0 && point.y == 0.0)
l = first(:conditions => {:coords => point})
l[:state] = l[:state].upcase
c_parts = l[:city].split(" ")
c_parts.each {|part| part.capitalize! }
l[:city] = c_parts.join(" ")
return "#{l[:city]}, #{l[:state].upcase}"
end
def self.city_state(point)
return "ANYWHERE" if point.nil? || (point.x == 0.0 && point.y == 0.0)
city_state = {}
l = first(:conditions => {:coords => point})
city_state[:city] = l[:city]
city_state[:state] = l[:state]
city_state
end
def self.distance_between(locale_one, locale_two)
#locale_one and locale_two must be points
select = "ST_Distance(gg1, gg2)"
from = "(SELECT ST_GeographyFromText('#{locale_one}') AS gg1, ST_GeographyFromText('#{locale_two}') AS gg2) AS foo"
distance = Location.select(select).from(from)
#1609.344 is how many meters are in a mile
"%.2f" % (distance[0].st_distance.to_f/1609.344)
end
def self.check_unknown(cs)
report = Report.first( :conditions => { :metric => "unknown_locations", :name => cs } )
#i.e. if it's been tried 25 times, add it to the locations table.
if report.value > 5
city, state = cs.split(",")
state = state.strip
google_api_results = HTTParty.get("http://maps.googleapis.com/maps/api/geocode/json?address=#{cs.gsub(/ /, "%20")}&sensor=true")
geo_data = google_api_results.parsed_response
lat = geo_data["results"][0]["geometry"]["location"]["lat"]
lng = geo_data["results"][0]["geometry"]["location"]["lng"]
l = Location.new({
:city => city,
:state => state,
:coords => "ST_GeographyFromText('POINT( #{lat} #{lng} )')",
:cs => "#{city}, #{state}"
})
l.save
Report.delete(report.id)
return l
else
return false
end
end
end
Решение
Your database columns are called CORDS
but in the hash you pass to Location.new
, you pass the key :coords
. You also fail to pass a :zip
.
Does the Location
model define a different mapping?