charta 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91f9f91e9ceb0d429d73dcc7ac1223f82853607f
4
- data.tar.gz: 57434c3c458690a13b12ce33dc6f71c9d534cd47
3
+ metadata.gz: cf89b408699b34e871d1b85a23315e3e2d735b5c
4
+ data.tar.gz: '02795fccd17495a483a93d3a1752c874a28d7e48'
5
5
  SHA512:
6
- metadata.gz: '07914f06a851f73c3ee4dfef1e37c8904367c926173d2521982b9e72713491e8119963b74612faed35200e67b717c68f037201f8addd7e5cdbcd80d345e4530b'
7
- data.tar.gz: 4b249d994fe723c1a2da0b9366709bcc9bd193d11e3477a7c117f933c2e2ea53d1a77f4fb39b0618f856a22a7ce35b6f0159763a6a216d3b2f0be21dee364bad
6
+ metadata.gz: b9c290de3bebb7d8d10ef5a3b179fa7adaedc20555a82552500cf30dd3131594824efb8292f9a618f030aeedaf9e4775f4f165f32074e755df860204abbf1988
7
+ data.tar.gz: 363f6dc4da4a6f296460f9e38ec34051a8a980069829c362827e6d29bb3f110c51976b9e9890ea5b0badd52af5093f5658cf6ab7ee43558bb985b2d5b3b2e1ca
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
- task :default => :test
10
+ task default: :test
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  lib = File.expand_path('../lib', __FILE__)
4
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
3
  require 'charta/version'
@@ -6,9 +6,8 @@ module Charta
6
6
  # Represents a Geometry with SRID
7
7
  class Geometry
8
8
  def initialize(feature, properties = {})
9
- @feature = feature
9
+ self.feature = feature
10
10
  @properties = properties
11
- # raise ArgumentError, 'Need EWKT to instantiate Geometry' if @ewkt.to_s =~ /\A[[:space:]]*\z/
12
11
  end
13
12
 
14
13
  def inspect
@@ -18,7 +17,7 @@ module Charta
18
17
  # Returns the type of the geometry as a string. Example: point,
19
18
  # multi_polygon, geometry_collection...
20
19
  def type
21
- Charta.underscore(@feature.geometry_type.type_name).to_sym
20
+ Charta.underscore(feature.geometry_type.type_name).to_sym
22
21
  end
23
22
 
24
23
  # Returns the type of the geometry as a string. EG: 'ST_Linestring', 'ST_Polygon',
@@ -26,29 +25,30 @@ module Charta
26
25
  # in the case of the string and ST in front that is returned, as well as the fact
27
26
  # that it will not indicate whether the geometry is measured.
28
27
  def collection?
29
- @feature.geometry_type == RGeo::Feature::GeometryCollection
28
+ feature.geometry_type == RGeo::Feature::GeometryCollection
30
29
  end
31
30
 
32
31
  # Return the spatial reference identifier for the ST_Geometry
33
32
  def srid
34
- @feature.srid.to_i
33
+ feature.srid.to_i
35
34
  end
36
35
 
37
36
  # Returns the underlaying object managed by Charta: the RGeo feature
38
37
  def to_rgeo
39
- @feature
38
+ feature
40
39
  end
41
40
 
42
- # Returns the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata
41
+ # Returns the Well-Known Text (WKT) representation of the geometry/geography
42
+ # without SRID metadata
43
43
  def to_text
44
- @feature.as_text.match(/\ASRID=.*;(.*)/)[1]
44
+ feature.as_text.match(/\ASRID=.*;(.*)/)[1]
45
45
  end
46
46
  alias as_text to_text
47
47
  alias to_wkt to_text
48
48
 
49
49
  # Returns EWKT: WKT with its SRID
50
50
  def to_ewkt
51
- Charta.generate_ewkt(@feature).to_s
51
+ Charta.generate_ewkt(feature).to_s
52
52
  end
53
53
  alias to_s to_ewkt
54
54
 
@@ -60,7 +60,7 @@ module Charta
60
60
  # Return the Well-Known Binary (WKB) representation of the geometry with SRID meta data.
61
61
  def to_binary
62
62
  generator = RGeo::WKRep::WKBGenerator.new(tag_format: :ewkbt, emit_ewkbt_srid: true)
63
- generator.generate(@feature)
63
+ generator.generate(feature)
64
64
  end
65
65
  alias to_ewkb to_binary
66
66
 
@@ -78,7 +78,7 @@ module Charta
78
78
 
79
79
  # Return the geometry as Scalar Vector Graphics (SVG) path data.
80
80
  def to_svg_path
81
- RGeo::SVG.encode(@feature)
81
+ RGeo::SVG.encode(feature)
82
82
  end
83
83
 
84
84
  # Return the geometry as a Geometry Javascript Object Notation (GeoJSON) element.
@@ -89,7 +89,7 @@ module Charta
89
89
 
90
90
  # Returns object in JSON (Hash)
91
91
  def to_json_object
92
- RGeo::GeoJSON.encode(@feature)
92
+ RGeo::GeoJSON.encode(feature)
93
93
  end
94
94
 
95
95
  # Test if the other measure is equal to self
@@ -97,7 +97,7 @@ module Charta
97
97
  other_geometry = Charta.new_geometry(other).transform(srid)
98
98
  return true if empty? && other_geometry.empty?
99
99
  return inspect == other_geometry.inspect if collection? && other_geometry.collection?
100
- @feature.equals?(other_geometry.feature)
100
+ feature.equals?(other_geometry.feature)
101
101
  end
102
102
 
103
103
  # Test if the other measure is equal to self
@@ -105,38 +105,38 @@ module Charta
105
105
  other_geometry = Charta.new_geometry(other).transform(srid)
106
106
  return true if empty? && other_geometry.empty?
107
107
  return inspect == other_geometry.inspect if collection? && other_geometry.collection?
108
- !@feature.equals?(other_geometry.feature)
108
+ !feature.equals?(other_geometry.feature)
109
109
  end
110
110
 
111
111
  # Returns true if Geometry is a Surface
112
112
  def surface?
113
- [RGeo::Feature::Polygon, RGeo::Feature::MultiPolygon].include? @feature.geometry_type
113
+ [RGeo::Feature::Polygon, RGeo::Feature::MultiPolygon].include? feature.geometry_type
114
114
  end
115
115
 
116
116
  # Returns area in unit corresponding to the SRS
117
117
  def area
118
- surface? ? @feature.area : 0
118
+ surface? ? feature.area : 0
119
119
  end
120
120
 
121
121
  # Returns true if this Geometry is an empty geometrycollection, polygon,
122
122
  # point etc.
123
123
  def empty?
124
- @feature.is_empty?
124
+ feature.is_empty?
125
125
  end
126
126
  alias blank? empty?
127
127
 
128
128
  # Computes the geometric center of a geometry, or equivalently, the center
129
129
  # of mass of the geometry as a POINT.
130
130
  def centroid
131
- return nil unless surface? && !@feature.is_empty?
132
- point = @feature.centroid
131
+ return nil unless surface? && !feature.is_empty?
132
+ point = feature.centroid
133
133
  [point.y, point.x]
134
134
  end
135
135
 
136
136
  # Returns a POINT guaranteed to lie on the surface.
137
137
  def point_on_surface
138
138
  return nil unless surface?
139
- point = @feature.point_on_surface
139
+ point = feature.point_on_surface
140
140
  [point.y, point.x]
141
141
  end
142
142
 
@@ -153,9 +153,9 @@ module Charta
153
153
  items = []
154
154
  as_multi_type = "multi_#{as_type}".to_sym
155
155
  if type == as_type
156
- items << @feature
156
+ items << feature
157
157
  elsif type == :geometry_collection
158
- @feature.each do |geom|
158
+ feature.each do |geom|
159
159
  type_name = Charta.underscore(geom.geometry_type.type_name).to_sym
160
160
  if type_name == as_type
161
161
  items << geom
@@ -166,7 +166,7 @@ module Charta
166
166
  end
167
167
  end
168
168
  end
169
- Charta.new_geometry(@feature.factory.send(as_multi_type, items))
169
+ Charta.new_geometry(feature.factory.send(as_multi_type, items))
170
170
  end
171
171
 
172
172
  # Returns a new geometry with the coordinates converted into the new SRS
@@ -179,7 +179,7 @@ module Charta
179
179
  raise "Cannot find proj for SRID: #{new_srid}" if new_proj_entry.nil?
180
180
  new_feature = RGeo::CoordSys::Proj4.transform(
181
181
  database.get(srid).proj4,
182
- @feature,
182
+ feature,
183
183
  new_proj_entry.proj4,
184
184
  self.class.factory(new_srid)
185
185
  )
@@ -189,29 +189,29 @@ module Charta
189
189
 
190
190
  # Produces buffer
191
191
  def buffer(radius)
192
- @feature.buffer(radius)
192
+ feature.buffer(radius)
193
193
  end
194
194
 
195
195
  def merge(other)
196
196
  other_geometry = Charta.new_geometry(other).transform(srid)
197
- @feature.union(other_geometry.feature)
197
+ feature.union(other_geometry.feature)
198
198
  end
199
199
  alias + merge
200
200
 
201
201
  def intersection(other)
202
202
  other_geometry = Charta.new_geometry(other).transform(srid)
203
- @feature.intersection(other_geometry.feature)
203
+ feature.intersection(other_geometry.feature)
204
204
  end
205
205
 
206
206
  def difference(other)
207
207
  other_geometry = Charta.new_geometry(other).transform(srid)
208
- @feature.difference(other_geometry.feature)
208
+ feature.difference(other_geometry.feature)
209
209
  end
210
210
  alias - difference
211
211
 
212
212
  def bounding_box
213
213
  unless defined? @bounding_box
214
- bbox = RGeo::Cartesian::BoundingBox.create_from_geometry(@feature)
214
+ bbox = RGeo::Cartesian::BoundingBox.create_from_geometry(feature)
215
215
  instance_variable_set('@x_min', bbox.min_x || 0)
216
216
  instance_variable_set('@y_min', bbox.min_y || 0)
217
217
  instance_variable_set('@x_max', bbox.max_x || 0)
@@ -231,8 +231,23 @@ module Charta
231
231
  Charta.find_srid(name_or_srid)
232
232
  end
233
233
 
234
+ # TODO: Manage YAML domain type to ensure maintainability of YAML
235
+ # serialization in time.
234
236
  def feature
235
- @feature
237
+ if @feature.nil?
238
+ if @ewkt.nil?
239
+ raise StandardError, 'Invalid geometry (no feature, no EWKT)'
240
+ else
241
+ @feature = ::Charta::Geometry.from_ewkt(@ewkt)
242
+ @properties = @options.dup if @options
243
+ end
244
+ end
245
+ @feature.dup
246
+ end
247
+
248
+ def feature=(new_feature)
249
+ raise ArgumentError, "Feature can't be nil" if new_feature.nil?
250
+ @feature = new_feature
236
251
  end
237
252
 
238
253
  def to_json_feature(properties = {})
@@ -254,8 +269,6 @@ module Charta
254
269
  from_ewkt(ewkt_or_rgeo)
255
270
  end
256
271
 
257
- private
258
-
259
272
  def from_rgeo(rgeo)
260
273
  srid = rgeo.srid
261
274
  RGeo::Feature.cast(rgeo, factory: Geometry.factory(srid))
@@ -273,6 +286,8 @@ module Charta
273
286
  raise "Invalid EWKT (#{e.class.name}: #{e.message}): #{ewkt}"
274
287
  end
275
288
 
289
+ private
290
+
276
291
  def geos_factory(srid)
277
292
  RGeo::Geos.factory(
278
293
  srid: srid,
@@ -3,12 +3,12 @@ module Charta
3
3
  class GeometryCollection < Geometry
4
4
  def self.empty(srid = nil)
5
5
  srid = Charta.find_srid(srid.nil? ? :WGS84 : srid)
6
- feature = Charta.new_feature("GEOMETRYCOLLECTION EMPTY", srid)
6
+ feature = Charta.new_feature('GEOMETRYCOLLECTION EMPTY', srid)
7
7
  new(feature)
8
8
  end
9
9
 
10
10
  def to_json_feature_collection(collection_properties = [])
11
- features = feature.each.with_index.collect do |f,i|
11
+ features = feature.each.with_index.collect do |f, i|
12
12
  properties = {}
13
13
  properties = collection_properties[i] unless collection_properties[i].nil?
14
14
 
@@ -1,3 +1,3 @@
1
1
  module Charta
2
- VERSION = '0.1.8'.freeze
2
+ VERSION = '0.1.9'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: charta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice TEXIER
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-12 00:00:00.000000000 Z
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri