Class: JSONToMap
- Inherits:
-
Object
- Object
- JSONToMap
- Defined in:
- lib/jsontomap.rb
Instance Method Summary collapse
-
#genmap ⇒ Object
Generates the JSON for the map.
-
#genpopup(item) ⇒ Object
Generates formatted popup text.
-
#geocode(item) ⇒ Object
Gets the coordinates for any location.
-
#geocodestart(location) ⇒ Object
Get coordinates for start location.
-
#initialize(input, geocodefield, picturefield, namefield) ⇒ JSONToMap
constructor
A new instance of JSONToMap.
Constructor Details
#initialize(input, geocodefield, picturefield, namefield) ⇒ JSONToMap
Returns a new instance of JSONToMap.
5 6 7 8 9 10 11 |
# File 'lib/jsontomap.rb', line 5 def initialize(input, geocodefield, picturefield, namefield) @input = JSON.parse(input) @geocodefield = geocodefield @picturefield = picturefield @namefield = namefield @geotrack = 0 end |
Instance Method Details
#genmap ⇒ Object
Generates the JSON for the map
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jsontomap.rb', line 14 def genmap outarray = Array.new @input.each do |i| if i[@geocodefield] temphash = Hash.new temphash["type"] = "Feature" temphash["properties"] = genpopup(i) # Geocode and chceck if it succeeded if i[@geocodefield].is_a? Array i[@geocodefield].each do |g| geohash = Hash.new geohash["type"] = "Point" begin cleaned = g.strip # Geocode if @geotrack < 10 geocoded = Geocoder.coordinates(cleaned) @geotrack += 1 else sleep(1) geocoded = Geocoder.coordinates(cleaned) @geotrack = 1 end geohash["coordinates"] = [geocoded[1], geocoded[0]] temphash["geometry"] = geohash outarray.push(temphash) rescue end end else temphash["geometry"] = geocode(i) if temphash["geometry"] == "failed" else outarray.push(temphash) end end end end return JSON.pretty_generate(outarray) end |
#genpopup(item) ⇒ Object
Generates formatted popup text
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jsontomap.rb', line 85 def genpopup(item) popuphash = Hash.new popupstring = "" if item[@picturefield] if (item[@picturefield].include? ".jpg") || (item[@picturefield].include? ".png") popupstring = '<img src="'+item[@picturefield]+'" /><br />' end end popupstring = popupstring + "<b>" + item[@namefield] + "</b><br /><br />" item.each do |k,v| popupstring = popupstring + "<b>" + k + "</b>: " + v.to_s + "<br />" end popuphash["popupContent"] = popupstring return popuphash end |
#geocode(item) ⇒ Object
Gets the coordinates for any location
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jsontomap.rb', line 63 def geocode(item) geohash = Hash.new geohash["type"] = "Point" if @geotrack < 10 geocoded = Geocoder.coordinates(item[@geocodefield]) @geotrack += 1 else sleep(1) geocoded = Geocoder.coordinates(item[@geocodefield]) @geotrack = 1 end begin geohash["coordinates"] = [geocoded[1], geocoded[0]] return geohash rescue return "failed" end end |
#geocodestart(location) ⇒ Object
Get coordinates for start location
105 106 107 108 109 |
# File 'lib/jsontomap.rb', line 105 def geocodestart(location) out = Geocoder.coordinates(location) arr = [out[0], out[1]] return arr end |