charta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a519ab8c11228ddf45e3aa026fd28ed199d21d1
4
+ data.tar.gz: 93830bdbc7cc63b2e049a7071e235fcab76a496d
5
+ SHA512:
6
+ metadata.gz: 4b30a72a075cc7b48a4894d3bfa1f9ab62472d40abb509aeeb2a0f778e4e328e8da8249c745fb1ca880f664158f619b5fb951353e5c5396f9328ef9bb38ea68e
7
+ data.tar.gz: 1cbfc29635b9a5874e20a83ed78902a2b762ebbd6431df4997b0b0afa57dda335231cecedec51fa71fdcb9bd50bc19b23ae948aae1194442f4edeaab078ce2d9
@@ -0,0 +1,11 @@
1
+ *~
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ .byebug_history
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at brice@ekylibre.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in charta.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Brice TEXIER
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Charta
2
+
3
+ Charta is an overlay to RGeo. Basically, it was developped to permit to manipulate more easily spatial data without using factory system. It's always the case. The code was extracted from [Ekylibre](https://github.com/ekylibre/ekylibre).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'charta'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install charta
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ geom = Charta.new_geometry(<WKT, WKB, GeoJSON...>) #=> <#Charta::Geometry>
25
+ geom.point_on_surface #=> <#Charta::Point>
26
+
27
+ geom.transform(2154) # => <#Charta::Geometry>
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bundle` to install dependencies. Then, run `rake test` to run the tests.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ekylibre/charta. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
44
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'charta/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'charta'
9
+ spec.version = Charta::VERSION
10
+ spec.authors = ['Brice TEXIER']
11
+ spec.email = ['brice@ekylibre.com']
12
+
13
+ spec.summary = 'Simple tool over geos and co'
14
+ spec.homepage = 'https://github.com/ekylibre/charta'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'nokogiri', '>= 1.7.0'
23
+ spec.add_dependency 'rgeo', '~> 0.6.0'
24
+ spec.add_dependency 'json', '>= 1.8.0'
25
+ spec.add_dependency 'rgeo-geojson', '~> 0.4.3'
26
+ spec.add_development_dependency 'bundler', '~> 1.14'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'minitest', '~> 5.0'
29
+ spec.add_development_dependency 'byebug'
30
+ end
@@ -0,0 +1,176 @@
1
+ # Gathers geomatic calculations
2
+ # Completes RGeo
3
+ require 'rgeo'
4
+ require 'charta/geometry'
5
+ require 'charta/geometry_collection'
6
+ require 'charta/point'
7
+ require 'charta/line_string'
8
+ require 'charta/polygon'
9
+ require 'charta/multi_polygon'
10
+ require 'charta/bounding_box'
11
+ require 'charta/geo_json'
12
+ require 'charta/gml'
13
+ require 'charta/kml'
14
+
15
+ unless RGeo::CoordSys::Proj4.supported?
16
+ puts "Proj4 is not supported. Some actions won't work"
17
+ end
18
+
19
+ # Charta aims to supply easy geom/geog tools
20
+ module Charta
21
+ SRS = {
22
+ WGS84: 4326,
23
+ CRS84: 4326,
24
+ RGF93: 2143
25
+ }.freeze
26
+
27
+ class << self
28
+ def new_geometry(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {})
29
+ geom_ewkt = nil
30
+ if coordinates.to_s =~ /\A[[:space:]]*\z/
31
+ geom_ewkt = empty_geometry(srs).to_ewkt
32
+ elsif coordinates.is_a?(::Charta::Geometry)
33
+ geom_ewkt = coordinates.to_ewkt
34
+ elsif coordinates.is_a?(Hash) || (coordinates.is_a?(String) && ::Charta::GeoJSON.valid?(coordinates)) # GeoJSON
35
+ srid = srs ? find_srid(srs) : :WGS84
36
+ geom_ewkt = ::Charta::GeoJSON.new(coordinates, srid).to_ewkt
37
+ elsif coordinates.is_a?(String)
38
+ geom_ewkt = if coordinates =~ /\A[A-F0-9]+\z/ # WKB
39
+ if srs && srid = find_srid(srs)
40
+ generate_ewkt RGeo::Geos.factory(srid: srid).parse_wkb(coordinates)
41
+ else
42
+ generate_ewkt Geometry.factory.parse_wkb(coordinates)
43
+ end
44
+ elsif format == 'gml' && ::Charta::GML.valid?(coordinates)
45
+ # required format 'cause kml geometries return empty instead of failing
46
+ ::Charta::GML.new(coordinates, srid).to_ewkt
47
+ elsif format == 'kml' && ::Charta::KML.valid?(coordinates)
48
+ ::Charta::KML.new(coordinates).to_ewkt
49
+ elsif coordinates =~ /^SRID\=\d+\;/i
50
+ generate_ewkt Geometry.feature(coordinates)
51
+ else # WKT expected
52
+ if srs && srid = find_srid(srs)
53
+ begin
54
+ f = RGeo::Geos.factory(srid: srid).parse_wkt(coordinates)
55
+ rescue RGeo::Error::ParseError => e
56
+ raise "Invalid EWKT (#{e.message}): #{coordinates}"
57
+ end
58
+ generate_ewkt f
59
+ else
60
+ generate_ewkt Geometry.feature(coordinates)
61
+ end
62
+ end
63
+ else # Default for RGeo
64
+ geom_ewkt = generate_ewkt coordinates
65
+ end
66
+ if geom_ewkt.to_s =~ /\A[[:space:]]*\z/
67
+ raise ArgumentError, "Invalid data: coordinates=#{coordinates.inspect}, srid=#{srid.inspect}"
68
+ end
69
+ type = Geometry.feature(geom_ewkt).geometry_type
70
+ # puts type.inspect
71
+ geom = case type
72
+ when RGeo::Feature::Point then
73
+ Point.new(geom_ewkt)
74
+ when RGeo::Feature::LineString then
75
+ LineString.new(geom_ewkt)
76
+ when RGeo::Feature::Polygon then
77
+ Polygon.new(geom_ewkt)
78
+ when RGeo::Feature::MultiPolygon then
79
+ MultiPolygon.new(geom_ewkt)
80
+ when RGeo::Feature::GeometryCollection then
81
+ GeometryCollection.new(geom_ewkt)
82
+ else
83
+ Geometry.new(geom_ewkt)
84
+ end
85
+ geom
86
+ end
87
+
88
+ def new_point(lat, lon, srid = 4326)
89
+ Point.new("SRID=#{srid};POINT(#{lon} #{lat})")
90
+ end
91
+
92
+ def make_line(points, options = {})
93
+ options[:srid] ||= new_geometry(points.first).srid if points.any?
94
+ options[:srid] ||= 4326
95
+
96
+ ewkt = "SRID=#{options[:srid]};LINESTRING(" + points.map { |wkt| p = new_geometry(wkt); "#{p.x} #{p.y}" }.join(', ') + ')'
97
+ new_geometry(ewkt)
98
+ end
99
+
100
+ def empty_geometry(srid = :WGS84)
101
+ GeometryCollection.empty(srid)
102
+ end
103
+
104
+ def generate_ewkt(feature)
105
+ generator = RGeo::WKRep::WKTGenerator.new(tag_format: :ewkt, emit_ewkt_srid: true)
106
+ generator.generate(feature)
107
+ end
108
+
109
+ def parse_ewkt(coordinates)
110
+ # parser = RGeo::WKRep::WKTParser.new(factory, support_ewkt: true)
111
+ # factory.parse_wkt(coordinates)
112
+ coordinates
113
+ end
114
+
115
+ def find_system_by_srid(_srid)
116
+ nil
117
+ end
118
+
119
+ def find_system_by_urn(_urn)
120
+ nil
121
+ end
122
+
123
+ def find_system(_srname)
124
+ nil
125
+ end
126
+
127
+ # Check and returns the SRID matching with srname or SRID.
128
+ def find_srid(srname_or_srid)
129
+ if srname_or_srid.to_s =~ /\Aurn:ogc:def:crs:.*\z/
130
+ x = srname_or_srid.split(':').last.upcase.to_sym
131
+ SRS[x] || x
132
+ elsif srname_or_srid.to_s =~ /\AEPSG::?(\d{4,5})\z/
133
+ srname_or_srid.split(':').last
134
+ elsif srname_or_srid.to_s =~ /\A\d+\z/
135
+ srname_or_srid.to_i
136
+ else
137
+ SRS[srname_or_srid] || srname_or_srid
138
+ end
139
+ end
140
+
141
+ def from(format, data)
142
+ unless respond_to?("from_#{format}")
143
+ raise "Unknown format: #{format.inspect}"
144
+ end
145
+ send("from_#{format}", data)
146
+ end
147
+
148
+ def from_gml(data, srid = nil, flatten_collection = false)
149
+ new_geometry(::Charta::GML.new(data, srid).to_ewkt, nil, nil, flatten_collection)
150
+ end
151
+
152
+ def from_kml(data, flatten_collection = false)
153
+ new_geometry(::Charta::KML.new(data).to_ewkt, nil, nil, flatten_collection)
154
+ end
155
+
156
+ def from_geojson(data, srid = nil)
157
+ new_geometry(::Charta::GeoJSON.new(data, srid).to_ewkt)
158
+ end
159
+
160
+ # Utility methods
161
+
162
+ def underscore(text)
163
+ text.gsub(/::/, '/')
164
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
165
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
166
+ .tr('-', '_')
167
+ .downcase
168
+ end
169
+
170
+ def camelcase(text, first_letter = :upper)
171
+ ret = text.split(/[_\-]+/).map { |word| word[0..0].upcase + word[1..-1].downcase }.join
172
+ ret = text[0..0].downcase + text[1..-1] if first_letter == :lower
173
+ ret
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,28 @@
1
+ module Charta
2
+ class BoundingBox
3
+ attr_reader :y_min, :x_min, :y_max, :x_max
4
+
5
+ def initialize(y_min, x_min, y_max, x_max)
6
+ @y_min = y_min
7
+ @x_min = x_min
8
+ @y_max = y_max
9
+ @x_max = x_max
10
+ end
11
+
12
+ def width
13
+ @x_max - @x_min
14
+ end
15
+
16
+ def height
17
+ @y_max - @y_min
18
+ end
19
+
20
+ def svg_view_box
21
+ [x_min, -y_max, width, height]
22
+ end
23
+
24
+ def to_a
25
+ [[@y_min, @x_min], [@y_max, @x_max]]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,168 @@
1
+ require 'json'
2
+
3
+ module Charta
4
+ # Represents a Geometry with SRID
5
+ class GeoJSON
6
+ attr_reader :srid
7
+
8
+ def initialize(data, srid = :WGS84)
9
+ srid ||= :WGS84
10
+ @json = self.class.flatten(data.is_a?(Hash) ? data : JSON.parse(data))
11
+ lsrid = @json['crs']['properties']['name'] if @json.is_a?(Hash) &&
12
+ @json['crs'].is_a?(Hash) &&
13
+ @json['crs']['properties'].is_a?(Hash)
14
+ lsrid ||= srid
15
+ @srid = ::Charta.find_srid(lsrid)
16
+ end
17
+
18
+ def geom
19
+ Charta.new_geometry(to_ewkt)
20
+ end
21
+
22
+ def to_hash
23
+ @json
24
+ end
25
+
26
+ def to_ewkt
27
+ "SRID=#{@srid};" + self.class.object_to_ewkt(@json)
28
+ end
29
+
30
+ def valid?
31
+ to_ewkt
32
+ true
33
+ rescue
34
+ false
35
+ end
36
+
37
+ class << self
38
+ # Test is given data is a valid GeoJSON
39
+ def valid?(data, srid = :WGS84)
40
+ new(data, srid).valid?
41
+ rescue
42
+ false
43
+ end
44
+
45
+ # Force coordinates to 2D
46
+ def 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
54
+ end
55
+
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
+ alias geometry_collection_to_ewkt feature_collection_to_ewkt
102
+
103
+ def feature_to_ewkt(hash)
104
+ object_to_ewkt(hash['geometry'])
105
+ end
106
+
107
+ def point_to_ewkt(hash)
108
+ return 'POINT EMPTY' if hash['coordinates'].nil?
109
+ 'POINT(' + hash['coordinates'].join(' ') + ')'
110
+ end
111
+
112
+ def line_string_to_ewkt(hash)
113
+ return 'LINESTRING EMPTY' if hash['coordinates'].nil?
114
+ 'LINESTRING(' + hash['coordinates'].collect do |point|
115
+ point.join(' ')
116
+ end.join(', ') + ')'
117
+ end
118
+
119
+ def polygon_to_ewkt(hash)
120
+ return 'POLYGON EMPTY' if hash['coordinates'].nil?
121
+ 'POLYGON(' + hash['coordinates'].collect do |hole|
122
+ '(' + hole.collect do |point|
123
+ point.join(' ')
124
+ end.join(', ') + ')'
125
+ end.join(', ') + ')'
126
+ end
127
+
128
+ def multi_point_to_ewkt(hash)
129
+ return 'MULTIPOINT EMPTY' if hash['coordinates'].nil?
130
+ 'MULTIPOINT(' + hash['coordinates'].collect do |point|
131
+ '(' + point.join(' ') + ')'
132
+ end.join(', ') + ')'
133
+ end
134
+
135
+ def multi_line_string_to_ewkt(hash)
136
+ return 'MULTILINESTRING EMPTY' if hash['coordinates'].nil?
137
+ 'MULTILINESTRING(' + hash['coordinates'].collect do |line|
138
+ '(' + line.collect do |point|
139
+ point.join(' ')
140
+ end.join(', ') + ')'
141
+ end.join(', ') + ')'
142
+ end
143
+
144
+ def multipolygon_to_ewkt(hash)
145
+ return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
146
+ 'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
147
+ '(' + polygon.collect do |hole|
148
+ '(' + hole.collect do |point|
149
+ point.join(' ')
150
+ end.join(', ') + ')'
151
+ end.join(', ') + ')'
152
+ end.join(', ') + ')'
153
+ end
154
+
155
+ # for PostGIS ST_ASGeoJSON compatibility
156
+ def multi_polygon_to_ewkt(hash)
157
+ return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
158
+ 'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
159
+ '(' + polygon.collect do |hole|
160
+ '(' + hole.collect do |point|
161
+ point.join(' ')
162
+ end.join(', ') + ')'
163
+ end.join(', ') + ')'
164
+ end.join(', ') + ')'
165
+ end
166
+ end
167
+ end
168
+ end