cartodb-rb-client 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/cartodb-rb-client.gemspec +2 -2
- data/lib/cartodb-rb-client/cartodb/model/geo.rb +39 -12
- data/lib/cartodb-rb-client/cartodb/model/getters.rb +4 -5
- data/lib/cartodb-rb-client/cartodb/model/schema.rb +4 -5
- data/lib/cartodb-rb-client/cartodb/model/setters.rb +0 -1
- data/lib/cartodb-rb-client/version.rb +1 -1
- data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_save_polygons_in_different_formats.yml +1264 -0
- data/spec/model/data_spec.rb +28 -1
- metadata +7 -5
data/cartodb-rb-client.gemspec
CHANGED
@@ -27,8 +27,8 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.add_dependency 'mime-types', '>= 1.16'
|
28
28
|
s.add_dependency 'activesupport', '>= 3.0.0'
|
29
29
|
s.add_dependency 'i18n', '>= 0.5.0', '<= 0.6.0'
|
30
|
-
s.add_dependency 'rgeo', '0.3.2'
|
31
|
-
s.add_dependency 'rgeo-geojson', '0.2.1'
|
30
|
+
s.add_dependency 'rgeo', '>= 0.3.2'
|
31
|
+
s.add_dependency 'rgeo-geojson', '>= 0.2.1'
|
32
32
|
s.add_dependency 'pg', '0.11.0' if postgresql_installed?
|
33
33
|
s.add_dependency 'json', '>= 1.5.3'
|
34
34
|
end
|
@@ -15,17 +15,25 @@ module CartoDB
|
|
15
15
|
|
16
16
|
geometry_name = geometry_column[:name].to_sym
|
17
17
|
|
18
|
-
self.send :define_method, :the_geom do
|
19
|
-
self.attributes[geometry_name]
|
20
|
-
end
|
21
|
-
|
22
|
-
self.send :define_method, :the_geom= do |the_geom|
|
23
|
-
self.attributes[geometry_name] = the_geom
|
24
|
-
end
|
25
|
-
|
26
18
|
case geometry_column[:geometry_type].upcase
|
27
19
|
when 'POINT'
|
20
|
+
self.send :define_method, :the_geom do
|
21
|
+
self.attributes[geometry_name]
|
22
|
+
end
|
23
|
+
|
24
|
+
self.send :define_method, :the_geom= do |the_geom|
|
25
|
+
self.attributes[geometry_name] = the_geom
|
26
|
+
end
|
27
|
+
|
28
28
|
setup_point_geometry
|
29
|
+
when 'MULTIPOLYGON'
|
30
|
+
self.send :define_method, :the_geom do
|
31
|
+
self.attributes[geometry_name]
|
32
|
+
end
|
33
|
+
|
34
|
+
self.send :define_method, :the_geom= do |the_geom|
|
35
|
+
self.attributes[geometry_name] = convert_to_polygon(the_geom)
|
36
|
+
end
|
29
37
|
end
|
30
38
|
end
|
31
39
|
private :setup_geometry_column
|
@@ -59,16 +67,35 @@ module CartoDB
|
|
59
67
|
|
60
68
|
def prepare_geo_attributes(attributes)
|
61
69
|
return if attributes.nil?
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
70
|
+
|
71
|
+
case self.class.geometry_type
|
72
|
+
when 'point'
|
73
|
+
longitude = attributes.delete(:longitude)
|
74
|
+
latitude = attributes.delete(:latitude)
|
75
|
+
|
76
|
+
attributes[:the_geom] = convert_to_point(latitude, longitude) if latitude && longitude
|
77
|
+
when /polygon/
|
78
|
+
attributes[:the_geom] = convert_to_polygon(attributes[:the_geom])
|
66
79
|
end
|
67
80
|
|
68
81
|
attributes
|
69
82
|
end
|
70
83
|
private :prepare_geo_attributes
|
71
84
|
|
85
|
+
def convert_to_point(latitude, longitude)
|
86
|
+
RGEO_FACTORY.point(longitude, latitude)
|
87
|
+
end
|
88
|
+
|
89
|
+
def convert_to_polygon(the_geom)
|
90
|
+
case the_geom
|
91
|
+
when String
|
92
|
+
RGeo::GeoJSON.decode(the_geom, :json_parser => :json, :geo_factory => RGeo::Geographic.spherical_factory(:srid => 4326))
|
93
|
+
when Hash
|
94
|
+
RGeo::GeoJSON.decode(::JSON.generate(the_geom), :json_parser => :json, :geo_factory => RGeo::Geographic.spherical_factory(:srid => 4326))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
private :convert_to_polygon
|
98
|
+
|
72
99
|
end
|
73
100
|
end
|
74
101
|
end
|
@@ -34,16 +34,15 @@ module CartoDB
|
|
34
34
|
columns.reject{|c| %w(cartodb_id created_at updated_at).include?(c[:name])}.compact
|
35
35
|
end
|
36
36
|
|
37
|
+
def geometry_type
|
38
|
+
@geometry_type
|
39
|
+
end
|
40
|
+
|
37
41
|
def model_columns
|
38
42
|
@model_columns || []
|
39
43
|
end
|
40
44
|
private :model_columns
|
41
45
|
|
42
|
-
def geometry_type
|
43
|
-
@geometry_type
|
44
|
-
end
|
45
|
-
private :geometry_type
|
46
|
-
|
47
46
|
end
|
48
47
|
|
49
48
|
def connection
|
@@ -90,19 +90,18 @@ module CartoDB
|
|
90
90
|
@columns.each do |c|
|
91
91
|
column_name = c[:name]
|
92
92
|
column_type = c[:type]
|
93
|
-
setup_geometry_column(c) and next if column_name.eql?(GEOMETRY_COLUMN) || column_type.eql?('geometry')
|
94
93
|
|
95
|
-
|
94
|
+
if column_name.eql?(GEOMETRY_COLUMN) || column_type.eql?('geometry')
|
95
|
+
setup_geometry_column(c)
|
96
|
+
else
|
96
97
|
self.send :define_method, column_name do
|
97
98
|
self.attributes[column_name.to_sym]
|
98
99
|
end
|
99
|
-
# end
|
100
100
|
|
101
|
-
# unless self.methods.include?("#{column_name}=")
|
102
101
|
self.send :define_method, "#{column_name}=" do |value|
|
103
102
|
self.attributes[column_name.to_sym] = value
|
104
103
|
end
|
105
|
-
|
104
|
+
end
|
106
105
|
end
|
107
106
|
end
|
108
107
|
private :create_column_accessors
|