jsontomap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jsontomap.rb +109 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7c60accd58b94457d11c2e0fa7ede6729ce2ced
4
+ data.tar.gz: 9c8c469fd90cf86e3eca84e66ce55bf37b383e46
5
+ SHA512:
6
+ metadata.gz: 37f2ba2f8ec4efd9cbfec44c5d43b7b0d164ef78bc898a6640af41843855c180744f8f60f317db90953ef5472ed92ca3a7896e4fabd262a1237e21c3956ee18b
7
+ data.tar.gz: 035853cb05762f101b2ec7f7848004fab6635a00425205c25dc4a2ab1a83730781e04f6d3151e93da0f615aa85e741ada1293ab58cde1ec37ac60fec982b0615
@@ -0,0 +1,109 @@
1
+ require 'geocoder'
2
+ require 'json'
3
+
4
+ class JSONToMap
5
+ def initialize(input, geocodefield, picturefield, namefield)
6
+ @input = JSON.parse(input)
7
+ @geocodefield = geocodefield
8
+ @picturefield = picturefield
9
+ @namefield = namefield
10
+ @geotrack = 0
11
+ end
12
+
13
+ # Generates the JSON for the map
14
+ def genmap
15
+ outarray = Array.new
16
+
17
+ @input.each do |i|
18
+ if i[@geocodefield]
19
+ temphash = Hash.new
20
+ temphash["type"] = "Feature"
21
+ temphash["properties"] = genpopup(i)
22
+
23
+ # Geocode and chceck if it succeeded
24
+ if i[@geocodefield].is_a? Array
25
+ i[@geocodefield].each do |g|
26
+ geohash = Hash.new
27
+ geohash["type"] = "Point"
28
+ cleaned = g.strip
29
+
30
+ # Geocode
31
+ if @geotrack < 10
32
+ geocoded = Geocoder.coordinates(cleaned)
33
+ @geotrack += 1
34
+ else
35
+ sleep(1)
36
+ geocoded = Geocoder.coordinates(cleaned)
37
+ @geotrack = 1
38
+ end
39
+
40
+ begin
41
+ geohash["coordinates"] = [geocoded[1], geocoded[0]]
42
+ temphash["geometry"] = geohash
43
+ outarray.push(temphash)
44
+ rescue
45
+ end
46
+ end
47
+
48
+ else
49
+ temphash["geometry"] = geocode(i)
50
+ if temphash["geometry"] == "failed"
51
+ else
52
+ outarray.push(temphash)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ return JSON.pretty_generate(outarray)
59
+ end
60
+
61
+ # Gets the coordinates for any location
62
+ def geocode(item)
63
+ geohash = Hash.new
64
+ geohash["type"] = "Point"
65
+
66
+ if @geotrack < 10
67
+ geocoded = Geocoder.coordinates(item[@geocodefield])
68
+ @geotrack += 1
69
+ else
70
+ sleep(1)
71
+ geocoded = Geocoder.coordinates(item[@geocodefield])
72
+ @geotrack = 1
73
+ end
74
+
75
+ begin
76
+ geohash["coordinates"] = [geocoded[1], geocoded[0]]
77
+ return geohash
78
+ rescue
79
+ return "failed"
80
+ end
81
+ end
82
+
83
+ # Generates formatted popup text
84
+ def genpopup(item)
85
+ popuphash = Hash.new
86
+ popupstring = ""
87
+
88
+ if item[@picturefield]
89
+ if (item[@picturefield].include? ".jpg") || (item[@picturefield].include? ".png")
90
+ popupstring = '<img src="'+item[@picturefield]+'" /><br />'
91
+ end
92
+ end
93
+
94
+ popupstring = popupstring + "<b>" + item[@namefield] + "</b><br /><br />"
95
+
96
+ item.each do |k,v|
97
+ popupstring = popupstring + "<b>" + k + "</b>: " + v.to_s + "<br />"
98
+ end
99
+ popuphash["popupContent"] = popupstring
100
+ return popuphash
101
+ end
102
+
103
+ # Get coordinates for start location
104
+ def geocodestart(location)
105
+ out = Geocoder.coordinates(location)
106
+ switched = [out[1], out[0]]
107
+ return switched
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsontomap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - M. C. McGrath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Converts a JSON into a GeoJSON.
14
+ email: shidash@shidash.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/jsontomap.rb
20
+ homepage: https://github.com/TransparencyToolkit/JSONToMap
21
+ licenses:
22
+ - GPL
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Turns a JSON into a GeoJSON.
44
+ test_files: []
45
+ has_rdoc: