charta 0.1.18 → 0.2.0

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
- SHA256:
3
- metadata.gz: 305d47070fc660134d7a4bed344db7efb555c9729a30e771af105a263c6aee01
4
- data.tar.gz: 4cfab59ae1d9f4034eb066bcf05e3a4c49b117604879e3e1e282a50e2c234f64
2
+ SHA1:
3
+ metadata.gz: 94a236f9258dcbbbaa9a215a5af25e1ddeb904bb
4
+ data.tar.gz: 8251595d5fc8c7a70881108b37b43507b694f034
5
5
  SHA512:
6
- metadata.gz: 7cc82941e2062fa2309c294dc02b912528faf4a4893b34ee4a719a62ecd77422a1ff97d7c0255246dc4b9b014b2839bff6f7169e38d4be78cbb883d83352c26d
7
- data.tar.gz: 2193fbd5aa27ddca33cbd4ef3257168d2c567d1b8e4eb3def8ae2bcaed05e22aaeb82e8f6a17640955a7d92ff38835ab2d41a4f7f1e8dd01193db9fdab8dc31d
6
+ metadata.gz: 2440f9a26b7cd4d3d14c5f8b3840f3be1ba51ac7e11269c4ef2d5e615956d87f652b210b70861d803f11a1748533e67166c33732c946eac46f31e57470bf1496
7
+ data.tar.gz: a426cc2d10067261ad61666d63373cb7e28be1ac6c5b4887926b44d1ae7efe8e3d089e4092d19e791d6b5dc9df9ee6bbd96faa687568ae8253c3e367b7bfc6a2
data/.gitignore CHANGED
@@ -9,4 +9,3 @@
9
9
  /spec/reports/
10
10
  /tmp/
11
11
  .byebug_history
12
- .ruby-version
@@ -18,9 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
 
20
20
  spec.add_dependency 'nokogiri', '>= 1.7.0'
21
- spec.add_dependency 'rgeo', '~> 0.6.0'
21
+ spec.add_dependency 'rgeo', '~> 1.0.0'
22
22
  spec.add_dependency 'json', '>= 1.8.0'
23
- spec.add_dependency 'rgeo-geojson', '~> 0.4.3'
23
+ spec.add_dependency 'rgeo-geojson', '~> 1.0.0'
24
+ spec.add_dependency 'rgeo-proj4', '~> 1.0.0'
25
+ spec.add_dependency 'activesupport', '>= 5.1'
24
26
  spec.add_development_dependency 'bundler', '~> 1.14'
25
27
  spec.add_development_dependency 'rake', '~> 10.0'
26
28
  spec.add_development_dependency 'minitest', '~> 5.0'
@@ -1,10 +1,7 @@
1
1
  # Gathers geomatic calculations
2
2
  # Completes RGeo
3
- require 'bigdecimal'
4
- require 'bigdecimal/util'
5
3
  require 'rgeo'
6
- require 'charta/coordinates'
7
- require 'charta/ewkt_serializer'
4
+ require 'rgeo/proj4'
8
5
  require 'charta/geometry'
9
6
  require 'charta/geometry_collection'
10
7
  require 'charta/point'
@@ -24,9 +24,5 @@ module Charta
24
24
  def to_a
25
25
  [[@y_min, @x_min], [@y_max, @x_max]]
26
26
  end
27
-
28
- def to_bbox_string
29
- "#{@x_min}, #{@y_min}, #{x_max}, #{y_max}"
30
- end
31
27
  end
32
28
  end
@@ -7,14 +7,12 @@ module Charta
7
7
 
8
8
  def initialize(data, srid = :WGS84)
9
9
  srid ||= :WGS84
10
- @json = Coordinates.flatten(data.is_a?(Hash) ? data : JSON.parse(data))
10
+ @json = self.class.flatten(data.is_a?(Hash) ? data : JSON.parse(data))
11
11
  lsrid = @json['crs']['properties']['name'] if @json.is_a?(Hash) &&
12
12
  @json['crs'].is_a?(Hash) &&
13
13
  @json['crs']['properties'].is_a?(Hash)
14
14
  lsrid ||= srid
15
15
  @srid = ::Charta.find_srid(lsrid)
16
-
17
- @json = Coordinates.normalize_4326_geometry(@json) if @srid.to_i == 4326
18
16
  end
19
17
 
20
18
  def geom
@@ -26,7 +24,7 @@ module Charta
26
24
  end
27
25
 
28
26
  def to_ewkt
29
- "SRID=#{srid};" + EwktSerializer.object_to_ewkt(@json)
27
+ "SRID=#{@srid};" + self.class.object_to_ewkt(@json)
30
28
  end
31
29
 
32
30
  def valid?
@@ -44,14 +42,131 @@ module Charta
44
42
  false
45
43
  end
46
44
 
45
+ # Force coordinates to 2D
47
46
  def flatten(hash)
48
- Coordinates.flatten hash
47
+ if hash['type'] == 'FeatureCollection'
48
+ flatten_feature_collection(hash)
49
+ elsif hash['type'] == 'Feature'
50
+ flatten_feature(hash)
51
+ else
52
+ flatten_geometry(hash)
53
+ end
49
54
  end
50
55
 
51
- %i[object_to_ewkt feature_collection_to_ewkt geometry_collection_to_ewkt feature_to_ewkt point_to_ewkt line_string_to_ewkt polygon_to_ewkt multi_point_to_ewkt multi_line_string_to_ewkt multipolygon_to_ewkt multi_polygon_to_ewkt].each do |m|
52
- define_method m do |*args|
53
- EwktSerializer.send m, *args
54
- end
56
+ def flatten_feature_collection(hash)
57
+ hash.merge('features' => hash['features'].map { |f| flatten_feature(f) })
58
+ end
59
+
60
+ def flatten_feature(hash)
61
+ hash.merge('geometry' => flatten_geometry(hash['geometry']))
62
+ end
63
+
64
+ def flatten_geometry(hash)
65
+ coordinates = hash['coordinates']
66
+ flattened =
67
+ case hash['type']
68
+ when 'Point' then
69
+ flatten_position(coordinates)
70
+ when 'MultiPoint', 'LineString'
71
+ coordinates.map { |p| flatten_position(p) }
72
+ when 'MultiLineString', 'Polygon'
73
+ coordinates.map { |l| l.map { |p| flatten_position(p) } }
74
+ when 'MultiPolygon'
75
+ coordinates.map { |m| m.map { |l| l.map { |p| flatten_position(p) } } }
76
+ when 'GeometryCollection' then
77
+ return hash.merge('geometries' => hash['geometries'].map { |g| flatten_geometry(g) })
78
+ else
79
+ raise StandardError, "Cannot handle: #{hash['type'].inspect}. In #{hash.inspect}"
80
+ end
81
+
82
+ hash.merge('coordinates' => flattened)
83
+ end
84
+
85
+ def flatten_position(position)
86
+ position[0..1]
87
+ end
88
+
89
+ def object_to_ewkt(hash)
90
+ type = hash[:type] || hash['type']
91
+ send("#{type.gsub(/(.)([A-Z])/, '\1_\2').downcase}_to_ewkt", hash)
92
+ end
93
+
94
+ def feature_collection_to_ewkt(hash)
95
+ return 'GEOMETRYCOLLECTION EMPTY' if hash['features'].nil?
96
+ 'GEOMETRYCOLLECTION(' + hash['features'].collect do |feature|
97
+ object_to_ewkt(feature)
98
+ end.join(', ') + ')'
99
+ end
100
+
101
+ def geometry_collection_to_ewkt(hash)
102
+ return 'GEOMETRYCOLLECTION EMPTY' if hash['geometries'].nil?
103
+ 'GEOMETRYCOLLECTION(' + hash['geometries'].collect do |feature|
104
+ object_to_ewkt(feature)
105
+ end.join(', ') + ')'
106
+ end
107
+
108
+ def feature_to_ewkt(hash)
109
+ object_to_ewkt(hash['geometry'])
110
+ end
111
+
112
+ def point_to_ewkt(hash)
113
+ return 'POINT EMPTY' if hash['coordinates'].nil?
114
+ 'POINT(' + hash['coordinates'].join(' ') + ')'
115
+ end
116
+
117
+ def line_string_to_ewkt(hash)
118
+ return 'LINESTRING EMPTY' if hash['coordinates'].nil?
119
+ 'LINESTRING(' + hash['coordinates'].collect do |point|
120
+ point.join(' ')
121
+ end.join(', ') + ')'
122
+ end
123
+
124
+ def polygon_to_ewkt(hash)
125
+ return 'POLYGON EMPTY' if hash['coordinates'].nil?
126
+ 'POLYGON(' + hash['coordinates'].collect do |hole|
127
+ '(' + hole.collect do |point|
128
+ point.join(' ')
129
+ end.join(', ') + ')'
130
+ end.join(', ') + ')'
131
+ end
132
+
133
+ def multi_point_to_ewkt(hash)
134
+ return 'MULTIPOINT EMPTY' if hash['coordinates'].nil?
135
+ 'MULTIPOINT(' + hash['coordinates'].collect do |point|
136
+ '(' + point.join(' ') + ')'
137
+ end.join(', ') + ')'
138
+ end
139
+
140
+ def multi_line_string_to_ewkt(hash)
141
+ return 'MULTILINESTRING EMPTY' if hash['coordinates'].nil?
142
+ 'MULTILINESTRING(' + hash['coordinates'].collect do |line|
143
+ '(' + line.collect do |point|
144
+ point.join(' ')
145
+ end.join(', ') + ')'
146
+ end.join(', ') + ')'
147
+ end
148
+
149
+ def multipolygon_to_ewkt(hash)
150
+ return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
151
+ 'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
152
+ '(' + polygon.collect do |hole|
153
+ '(' + hole.collect do |point|
154
+ point.join(' ')
155
+ end.join(', ') + ')'
156
+ end.join(', ') + ')'
157
+ end.join(', ') + ')'
158
+ end
159
+
160
+ # for PostGIS ST_ASGeoJSON compatibility
161
+ def multi_polygon_to_ewkt(hash)
162
+ return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
163
+ 'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
164
+ '(' + polygon.collect do |hole|
165
+ '(' + hole.collect do |point|
166
+ point.join(' ')
167
+ end.join(', ') + ')'
168
+ end.join(', ') + ')'
169
+ end.join(', ') + ')'
55
170
  end
56
171
  end
57
172
  end
@@ -1,10 +1,13 @@
1
1
  require 'json'
2
2
  require 'rgeo/geo_json'
3
3
  require 'rgeo/svg' # integrated lib for now
4
+ require 'active_support/core_ext/module/delegation'
4
5
 
5
6
  module Charta
6
7
  # Represents a Geometry with SRID
7
8
  class Geometry
9
+ delegate_missing_to :to_rgeo
10
+
8
11
  def initialize(feature, properties = {})
9
12
  self.feature = feature
10
13
  @properties = properties
@@ -43,7 +46,6 @@ module Charta
43
46
  def to_text
44
47
  feature.as_text.match(/\ASRID=.*;(.*)/)[1]
45
48
  end
46
-
47
49
  alias as_text to_text
48
50
  alias to_wkt to_text
49
51
 
@@ -51,7 +53,6 @@ module Charta
51
53
  def to_ewkt
52
54
  Charta.generate_ewkt(feature).to_s
53
55
  end
54
-
55
56
  alias to_s to_ewkt
56
57
 
57
58
  def ewkt
@@ -64,7 +65,6 @@ module Charta
64
65
  generator = RGeo::WKRep::WKBGenerator.new(tag_format: :ewkbt, emit_ewkbt_srid: true)
65
66
  generator.generate(feature)
66
67
  end
67
-
68
68
  alias to_ewkb to_binary
69
69
 
70
70
  # Pas bien compris le fonctionnement
@@ -88,7 +88,6 @@ module Charta
88
88
  def to_geojson
89
89
  to_json_object.to_json
90
90
  end
91
-
92
91
  alias to_json to_geojson
93
92
 
94
93
  # Returns object in JSON (Hash)
@@ -114,24 +113,12 @@ module Charta
114
113
 
115
114
  # Returns true if Geometry is a Surface
116
115
  def surface?
117
- if collection?
118
- feature.any? { |geometry| Charta.new_geometry(geometry).surface? }
119
- else
120
- [RGeo::Feature::Polygon, RGeo::Feature::MultiPolygon].include? feature.geometry_type
121
- end
116
+ [RGeo::Feature::Polygon, RGeo::Feature::MultiPolygon].include? feature.geometry_type
122
117
  end
123
118
 
124
119
  # Returns area in unit corresponding to the SRS
125
120
  def area
126
- if surface?
127
- if collection?
128
- feature.sum { |geometry| Charta.new_geometry(geometry).area }
129
- else
130
- feature.area
131
- end
132
- else
133
- 0
134
- end
121
+ surface? ? feature.area : 0
135
122
  end
136
123
 
137
124
  # Returns true if this Geometry is an empty geometrycollection, polygon,
@@ -139,7 +126,6 @@ module Charta
139
126
  def empty?
140
127
  feature.is_empty?
141
128
  end
142
-
143
129
  alias blank? empty?
144
130
 
145
131
  # Computes the geometric center of a geometry, or equivalently, the center
@@ -159,16 +145,10 @@ module Charta
159
145
 
160
146
  def convert_to(new_type)
161
147
  case new_type
162
- when type then
163
- self
164
- when :multi_point then
165
- flatten_multi(:point)
166
- when :multi_line_string then
167
- flatten_multi(:line_string)
168
- when :multi_polygon then
169
- flatten_multi(:polygon)
170
- else
171
- self
148
+ when type then self
149
+ when :multi_point then flatten_multi(:point)
150
+ when :multi_line_string then flatten_multi(:line_string)
151
+ when :multi_polygon then flatten_multi(:polygon)
172
152
  end
173
153
  end
174
154
 
@@ -219,25 +199,17 @@ module Charta
219
199
  other_geometry = Charta.new_geometry(other).transform(srid)
220
200
  feature.union(other_geometry.feature)
221
201
  end
222
-
223
202
  alias + merge
224
203
 
225
204
  def intersection(other)
226
205
  other_geometry = Charta.new_geometry(other).transform(srid)
227
-
228
- Charta.new_geometry(feature.intersection(other_geometry.feature))
229
- end
230
-
231
- def intersects?(other)
232
- other_geometry = Charta.new_geometry(other).transform(srid)
233
- feature.intersects?(other_geometry.feature)
206
+ feature.intersection(other_geometry.feature)
234
207
  end
235
208
 
236
209
  def difference(other)
237
210
  other_geometry = Charta.new_geometry(other).transform(srid)
238
211
  feature.difference(other_geometry.feature)
239
212
  end
240
-
241
213
  alias - difference
242
214
 
243
215
  def bounding_box
@@ -265,12 +237,12 @@ module Charta
265
237
  # TODO: Manage YAML domain type to ensure maintainability of YAML
266
238
  # serialization in time.
267
239
  def feature
268
- if @feature.nil?
269
- if @ewkt.nil?
270
- raise StandardError, 'Invalid geometry (no feature, no EWKT)'
271
- else
240
+ unless defined? @feature
241
+ if defined? @ewkt
272
242
  @feature = ::Charta::Geometry.from_ewkt(@ewkt)
273
243
  @properties = @options.dup if @options
244
+ else
245
+ raise StandardError, 'Invalid geometry (no feature, no EWKT)'
274
246
  end
275
247
  end
276
248
  @feature.dup
@@ -319,52 +291,57 @@ module Charta
319
291
 
320
292
  private
321
293
 
322
- def geos_factory(srid)
323
- RGeo::Geos.factory(
324
- srid: srid,
325
- wkt_generator: {
326
- type_format: :ewkt,
327
- emit_ewkt_srid: true,
328
- convert_case: :upper
329
- },
330
- wkt_parser: {
331
- support_ewkt: true
332
- },
333
- wkb_generator: {
334
- type_format: :ewkb,
335
- emit_ewkb_srid: true,
336
- hex_format: true
337
- },
338
- wkb_parser: {
339
- support_ewkb: true
340
- }
341
- )
342
- end
294
+ def geos_factory(srid)
295
+ RGeo::Geos.factory(
296
+ srid: srid,
297
+ wkt_generator: {
298
+ type_format: :ewkt,
299
+ emit_ewkt_srid: true,
300
+ convert_case: :upper
301
+ },
302
+ wkt_parser: {
303
+ support_ewkt: true
304
+ },
305
+ wkb_generator: {
306
+ type_format: :ewkb,
307
+ emit_ewkb_srid: true,
308
+ hex_format: true
309
+ },
310
+ wkb_parser: {
311
+ support_ewkb: true
312
+ }
313
+ )
314
+ end
343
315
 
344
- def projected_factory(srid)
345
- proj4 = '+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
346
- RGeo::Geographic.projected_factory(
347
- srid: srid,
348
- wkt_generator: {
349
- type_format: :ewkt,
350
- emit_ewkt_srid: true,
351
- convert_case: :upper
352
- },
353
- wkt_parser: {
354
- support_ewkt: true
355
- },
356
- wkb_generator: {
357
- type_format: :ewkb,
358
- emit_ewkb_srid: true,
359
- hex_format: true
360
- },
361
- wkb_parser: {
362
- support_ewkb: true
363
- },
364
- projection_srid: 6933,
365
- projection_proj4: proj4
366
- )
367
- end
316
+ def projected_factory(srid)
317
+ proj4 = '+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
318
+ RGeo::Geographic.projected_factory(
319
+ srid: srid,
320
+ wkt_generator: {
321
+ type_format: :ewkt,
322
+ emit_ewkt_srid: true,
323
+ convert_case: :upper
324
+ },
325
+ wkt_parser: {
326
+ support_ewkt: true
327
+ },
328
+ wkb_generator: {
329
+ type_format: :ewkb,
330
+ emit_ewkb_srid: true,
331
+ hex_format: true
332
+ },
333
+ wkb_parser: {
334
+ support_ewkb: true
335
+ },
336
+ projection_srid: 6933,
337
+ projection_proj4: proj4
338
+ )
339
+ end
340
+ end
341
+
342
+ def respond_to_missing?(name, include_private = false)
343
+ return false if name == :init_with
344
+ super
368
345
  end
369
346
  end
370
347
  end
@@ -10,10 +10,5 @@ module Charta
10
10
  feature.y
11
11
  end
12
12
  alias latitude y
13
-
14
- def distance(point)
15
- raise ArgumentError, "wrong type: Charta::Point required" if point.class.name != "Charta::Point"
16
- to_rgeo.distance(point.to_rgeo)
17
- end
18
13
  end
19
14
  end
@@ -8,10 +8,5 @@ module Charta
8
8
  end
9
9
  @exterior_ring
10
10
  end
11
-
12
- def distance(point)
13
- polygon_centroid = Charta.new_point(*centroid, 4326)
14
- polygon_centroid.distance(point)
15
- end
16
11
  end
17
12
  end
@@ -1,3 +1,3 @@
1
1
  module Charta
2
- VERSION = '0.1.18'.freeze
2
+ VERSION = '0.2.0'.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.18
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice TEXIER
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-09 00:00:00.000000000 Z
11
+ date: 2019-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.6.0
33
+ version: 1.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.6.0
40
+ version: 1.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,42 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.4.3
61
+ version: 1.0.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.4.3
68
+ version: 1.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rgeo-proj4
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '5.1'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: bundler
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +150,7 @@ dependencies:
122
150
  - - ">="
123
151
  - !ruby/object:Gem::Version
124
152
  version: '0'
125
- description:
153
+ description:
126
154
  email:
127
155
  - brice@ekylibre.com
128
156
  executables: []
@@ -130,7 +158,6 @@ extensions: []
130
158
  extra_rdoc_files: []
131
159
  files:
132
160
  - ".gitignore"
133
- - ".gitlab-ci.yml"
134
161
  - ".travis.yml"
135
162
  - CODE_OF_CONDUCT.md
136
163
  - Gemfile
@@ -140,8 +167,6 @@ files:
140
167
  - charta.gemspec
141
168
  - lib/charta.rb
142
169
  - lib/charta/bounding_box.rb
143
- - lib/charta/coordinates.rb
144
- - lib/charta/ewkt_serializer.rb
145
170
  - lib/charta/geo_json.rb
146
171
  - lib/charta/geojson_import.rb
147
172
  - lib/charta/geometry.rb
@@ -159,7 +184,7 @@ homepage: https://github.com/ekylibre/charta
159
184
  licenses:
160
185
  - MIT
161
186
  metadata: {}
162
- post_install_message:
187
+ post_install_message:
163
188
  rdoc_options: []
164
189
  require_paths:
165
190
  - lib
@@ -174,8 +199,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
199
  - !ruby/object:Gem::Version
175
200
  version: '0'
176
201
  requirements: []
177
- rubygems_version: 3.0.3
178
- signing_key:
202
+ rubyforge_project:
203
+ rubygems_version: 2.6.14
204
+ signing_key:
179
205
  specification_version: 4
180
206
  summary: Simple tool over geos and co
181
207
  test_files: []
@@ -1,13 +0,0 @@
1
- stages:
2
- - test
3
-
4
- test:
5
- image: registry.gitlab.com/ekylibre/docker-base-images/ruby2.3:1
6
- before_script:
7
- - bundle install --path vendor/bundle
8
- cache:
9
- key: bundle
10
- paths:
11
- - vendor/bundle
12
- script:
13
- - bundle exec rake test
@@ -1,65 +0,0 @@
1
- module Charta
2
- module Coordinates
3
- class << self
4
-
5
- # Force coordinates to 2D
6
- def flatten(hash)
7
- map_coordinates(hash) { |position| position[0..1] }
8
- end
9
-
10
- def map_coordinates(hash, &block)
11
- case hash['type']
12
- when 'FeatureCollection'
13
- map_feature_collection_coordinates hash, &block
14
- when 'Feature'
15
- map_feature_coordinates hash, &block
16
- else
17
- map_geometry_coordinates hash, &block
18
- end
19
- end
20
-
21
- def normalize_4326_geometry(json)
22
- map_coordinates json do |(x, y)|
23
- [((x + 180.to_d) % 360.to_d) - 180.to_d, ((y + 90.to_d) % 180.to_d) - 90.to_d]
24
- end
25
- end
26
-
27
- private
28
-
29
- def map_feature_collection_coordinates(hash, &block)
30
- hash.merge 'features' => hash['features'].map { |feature| map_feature_coordinates feature, &block }
31
- end
32
-
33
- def map_feature_coordinates(hash, &block)
34
- hash.merge 'geometry' => map_geometry_coordinates(hash['geometry'], &block)
35
- end
36
-
37
- def map_geometry_coordinates(hash, &block)
38
- if hash['type'] == 'GeometryCollection'
39
- map_geometry_collection_coordinates hash, &block
40
- else
41
- coordinates = hash['coordinates']
42
- mapped =
43
- case hash['type']
44
- when 'Point' then
45
- block.call coordinates
46
- when 'MultiPoint', 'LineString'
47
- coordinates.map(&block)
48
- when 'MultiLineString', 'Polygon'
49
- coordinates.map { |line| line.map(&block) }
50
- when 'MultiPolygon'
51
- coordinates.map { |poly| poly.map { |line| line.map(&block) } }
52
- else
53
- raise StandardError, "Cannot handle: #{hash['type'].inspect}. In #{hash.inspect}"
54
- end
55
-
56
- hash.merge 'coordinates' => mapped
57
- end
58
- end
59
-
60
- def map_geometry_collection_coordinates(hash, &block)
61
- hash.merge 'geometries' => hash['geometries'].map { |geometry| map_geometry_coordinates(geometry, &block) }
62
- end
63
- end
64
- end
65
- end
@@ -1,92 +0,0 @@
1
- module Charta
2
- module EwktSerializer
3
-
4
- class << self
5
- def object_to_ewkt(hash)
6
- type = hash[:type] || hash['type']
7
- send("#{type.gsub(/(.)([A-Z])/, '\1_\2').downcase}_to_ewkt", hash)
8
- end
9
-
10
- private
11
-
12
- def feature_collection_to_ewkt(hash)
13
- return 'GEOMETRYCOLLECTION EMPTY' if hash['features'].nil?
14
- 'GEOMETRYCOLLECTION(' + hash['features'].collect do |feature|
15
- object_to_ewkt(feature)
16
- end.join(', ') + ')'
17
- end
18
-
19
- def geometry_collection_to_ewkt(hash)
20
- return 'GEOMETRYCOLLECTION EMPTY' if hash['geometries'].nil?
21
- 'GEOMETRYCOLLECTION(' + hash['geometries'].collect do |feature|
22
- object_to_ewkt(feature)
23
- end.join(', ') + ')'
24
- end
25
-
26
- def feature_to_ewkt(hash)
27
- object_to_ewkt(hash['geometry'])
28
- end
29
-
30
- def point_to_ewkt(hash)
31
- return 'POINT EMPTY' if hash['coordinates'].nil?
32
- 'POINT(' + hash['coordinates'].join(' ') + ')'
33
- end
34
-
35
- def line_string_to_ewkt(hash)
36
- return 'LINESTRING EMPTY' if hash['coordinates'].nil?
37
- 'LINESTRING(' + hash['coordinates'].collect do |point|
38
- point.join(' ')
39
- end.join(', ') + ')'
40
- end
41
-
42
- def polygon_to_ewkt(hash)
43
- return 'POLYGON EMPTY' if hash['coordinates'].nil?
44
- 'POLYGON(' + hash['coordinates'].collect do |hole|
45
- '(' + hole.collect do |point|
46
- point.join(' ')
47
- end.join(', ') + ')'
48
- end.join(', ') + ')'
49
- end
50
-
51
- def multi_point_to_ewkt(hash)
52
- return 'MULTIPOINT EMPTY' if hash['coordinates'].nil?
53
- 'MULTIPOINT(' + hash['coordinates'].collect do |point|
54
- '(' + point.join(' ') + ')'
55
- end.join(', ') + ')'
56
- end
57
-
58
- def multi_line_string_to_ewkt(hash)
59
- return 'MULTILINESTRING EMPTY' if hash['coordinates'].nil?
60
- 'MULTILINESTRING(' + hash['coordinates'].collect do |line|
61
- '(' + line.collect do |point|
62
- point.join(' ')
63
- end.join(', ') + ')'
64
- end.join(', ') + ')'
65
- end
66
-
67
- def multipolygon_to_ewkt(hash)
68
- return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
69
- 'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
70
- '(' + polygon.collect do |hole|
71
- '(' + hole.collect do |point|
72
- point.join(' ')
73
- end.join(', ') + ')'
74
- end.join(', ') + ')'
75
- end.join(', ') + ')'
76
- end
77
-
78
- # for PostGIS ST_ASGeoJSON compatibility
79
- def multi_polygon_to_ewkt(hash)
80
- return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
81
- 'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
82
- '(' + polygon.collect do |hole|
83
- '(' + hole.collect do |point|
84
- point.join(' ')
85
- end.join(', ') + ')'
86
- end.join(', ') + ')'
87
- end.join(', ') + ')'
88
- end
89
- end
90
-
91
- end
92
- end