turf-ruby 0.1.0 → 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
2
  SHA256:
3
- metadata.gz: 9f24676db8efcacbc042e80f6becc3656633e1c0a134159eec5e94273c2bb6f8
4
- data.tar.gz: 66b5a9abfeb752015cc6245ecaf54c89911aa9b7142748435460eef0830b7277
3
+ metadata.gz: f8ed0e8a70920277e4e8507100d1f14084e75d0904c03feea99dc8196530561a
4
+ data.tar.gz: baba690e3110b85c5ece7eed4f5bd3229b5ecfbbeeca7ad4a7ca8e05d182763f
5
5
  SHA512:
6
- metadata.gz: fe352b01c818eba0bc3e7f0be524b0a2c7c6c6be4609b8d85459998e12e16c9b18a96721df0afbb46c8d85e03ebe918075c380c2b31b10cdff23681c3c6c9d66
7
- data.tar.gz: d47f32281f41ccb1b5f3079c51ddb90f4efd39bd02d6e2e2c51c0a325865038db557e1708453e5069782fb244943b6f20bd57c57534ee68fa29b4435b25fd8b4
6
+ metadata.gz: 9e331328f338fff791262493ae0f63da0baea68b5ec457c911ce62711803fc5f91b989bb5a2abc5191ff354f25306c664cb1b00a75c857484da518da4367a475
7
+ data.tar.gz: 7d1814173daf223ec7d8f9bbd4ae080c18c85bcfe4c0d307ada85abde67772aaa5d58bf87024fb5757825b5faa7544ab908ff12bc67c3cc5302ecf5e5281a0d6
data/.hound.yml ADDED
@@ -0,0 +1,5 @@
1
+ fail_on_violations: true
2
+
3
+ rubocop:
4
+ config_file: .rubocop.yml
5
+ version: 0.80.0
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ Layout/FirstArrayElementIndentation:
2
+ EnforcedStyle: consistent
3
+
4
+ Metrics:
5
+ Enabled: false
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/HashEachMethods:
11
+ Enabled: false
12
+
13
+ Style/HashTransformKeys:
14
+ Enabled: true
15
+
16
+ Style/HashTransformValues:
17
+ Enabled: true
18
+
19
+ Style/IfUnlessModifier:
20
+ Enabled: false
21
+
22
+ Style/NegatedIf:
23
+ Enabled: false
24
+
25
+ Style/StringLiterals:
26
+ EnforcedStyle: double_quotes
27
+
28
+ Style/TrailingCommaInArguments:
29
+ EnforcedStyleForMultiline: comma
30
+
31
+ Style/TrailingCommaInArrayLiteral:
32
+ EnforcedStyleForMultiline: consistent_comma
data/.travis.yml CHANGED
@@ -3,4 +3,4 @@ language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
5
  - 2.7.0
6
- before_install: gem install bundler -v 2.1.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in turf-ruby.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
7
8
  gem "minitest", "~> 5.0"
9
+ gem "rake", "~> 12.0"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- turf-ruby (0.1.0)
4
+ turf-ruby (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -18,4 +18,4 @@ DEPENDENCIES
18
18
  turf-ruby!
19
19
 
20
20
  BUNDLED WITH
21
- 1.17.3
21
+ 2.1.4
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Turf Ruby
1
+ # Turf Ruby [![Build Status](https://travis-ci.com/formigarafa/turf-ruby.svg?branch=master)](https://travis-ci.com/formigarafa/turf-ruby)
2
2
 
3
3
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/turf`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rake/testtask"
3
5
 
@@ -7,4 +9,4 @@ Rake::TestTask.new(:test) do |t|
7
9
  t.test_files = FileList["test/**/*_test.rb"]
8
10
  end
9
11
 
10
- task :default => :test
12
+ task default: :test
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
- require "turf-ruby"
5
+ require "turf_ruby"
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
data/lib/turf.rb CHANGED
@@ -1,6 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "turf/version"
2
4
 
3
5
  module Turf
4
6
  class Error < StandardError; end
5
- # Your code goes here...
7
+
8
+ # A non-intrusive implemenation of Rails' Hash#deep_symbolize_keys
9
+ def self.deep_symbolize_keys(hash)
10
+ return hash unless hash.is_a? Hash
11
+
12
+ hash.transform_keys(&:to_sym).transform_values do |value|
13
+ deep_symbolize_keys(value)
14
+ end
15
+ end
6
16
  end
data/lib/turf/helper.rb CHANGED
@@ -1,26 +1,96 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Turf
4
+ EARTH_RADIUS = 6_371_008.8
5
+ FACTORS = {
6
+ "centimeters" => EARTH_RADIUS * 100,
7
+ "centimetres" => EARTH_RADIUS * 100,
8
+ "degrees" => EARTH_RADIUS / 111_325,
9
+ "feet" => EARTH_RADIUS * 3.28084,
10
+ "inches" => EARTH_RADIUS * 39.370,
11
+ "kilometers" => EARTH_RADIUS / 1000,
12
+ "kilometres" => EARTH_RADIUS / 1000,
13
+ "meters" => EARTH_RADIUS,
14
+ "metres" => EARTH_RADIUS,
15
+ "miles" => EARTH_RADIUS / 1609.344,
16
+ "millimeters" => EARTH_RADIUS * 1000,
17
+ "millimetres" => EARTH_RADIUS * 1000,
18
+ "nauticalmiles" => EARTH_RADIUS / 1852,
19
+ "radians" => 1,
20
+ "yards" => EARTH_RADIUS / 1.0936
21
+ }.freeze
22
+
2
23
  def self.feature(geom, properties = nil, options = {})
3
24
  feat = {
4
25
  type: "Feature",
5
26
  geometry: geom,
6
- properties: properties || {},
27
+ properties: properties || {}
7
28
  }
8
- if options[:id]
9
- feat[:id] = options[:id]
10
- end
29
+ feat[:id] = options[:id] if options[:id]
11
30
 
12
- if options[:bbox]
13
- feat[:bbox] = options[:bbox]
14
- end
31
+ feat[:bbox] = options[:bbox] if options[:bbox]
15
32
 
16
33
  feat
17
34
  end
18
35
 
36
+ def self.line_string(coordinates, properties = nil, options = {})
37
+ if coordinates.size < 2
38
+ raise Error, "coordinates must be an array of two or more positions"
39
+ end
40
+
41
+ geom = {
42
+ type: "LineString",
43
+ coordinates: coordinates
44
+ }
45
+ feature(geom, properties, options)
46
+ end
47
+
19
48
  def self.point(coordinates, properties = nil, options = {})
20
49
  geom = {
21
50
  type: "Point",
22
- coordinates: coordinates,
51
+ coordinates: coordinates
52
+ }
53
+ feature(geom, properties, options)
54
+ end
55
+
56
+ def self.polygon(coordinates, properties = nil, options = {})
57
+ coordinates.each do |ring|
58
+ if ring.size < 4
59
+ raise(
60
+ Error,
61
+ "Each LinearRing of a Polygon must have 4 or more Positions.",
62
+ )
63
+ end
64
+
65
+ ring.last.each_with_index do |number, idx|
66
+ if ring.first[idx] != number
67
+ raise Error, "First and last Position are not equivalent."
68
+ end
69
+ end
70
+ end
71
+ geom = {
72
+ type: "Polygon",
73
+ coordinates: coordinates
23
74
  }
24
75
  feature(geom, properties, options)
25
76
  end
77
+
78
+ def self.degrees_to_radians(degrees)
79
+ radians = degrees.remainder(360)
80
+ radians * Math::PI / 180
81
+ end
82
+
83
+ def self.radians_to_length(radians, units = "kilometers")
84
+ factor = FACTORS[units]
85
+ raise Error, "#{units} units is invalid" unless factor
86
+
87
+ radians * factor
88
+ end
89
+
90
+ def self.length_to_radians(distance, units = "kilometers")
91
+ factor = FACTORS[units]
92
+ raise Error, "#{units} units is invalid" unless factor
93
+
94
+ distance / factor
95
+ end
26
96
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turf
4
+ def self.get_coord(coord)
5
+ if !coord
6
+ raise Error, "coord is required"
7
+ end
8
+
9
+ is_numeric = ->(i) { i.is_a? Numeric }
10
+ if coord.is_a?(Array) && coord.length >= 2 && coord.all?(&is_numeric)
11
+ return coord
12
+ end
13
+
14
+ if coord.is_a? Hash
15
+ coord = deep_symbolize_keys(coord)
16
+
17
+ is_feature = coord[:type] == "Feature"
18
+ if is_feature && coord.fetch(:geometry, {})[:type] == "Point"
19
+ return coord[:geometry][:coordinates]
20
+ end
21
+
22
+ if coord[:type] == "Point"
23
+ return coord[:coordinates]
24
+ end
25
+ end
26
+
27
+ raise Error, "coord must be GeoJSON Point or an Array of numbers"
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turf
4
+ def self.distance(from, to, **options)
5
+ coordinates1 = get_coord from
6
+ coordinates2 = get_coord to
7
+
8
+ d_lat = degrees_to_radians coordinates2[1] - coordinates1[1]
9
+ d_lon = degrees_to_radians coordinates2[0] - coordinates1[0]
10
+ lat1 = degrees_to_radians coordinates1[1]
11
+ lat2 = degrees_to_radians coordinates2[1]
12
+
13
+ a =
14
+ (
15
+ (Math.sin(d_lat / 2)**2) +
16
+ (Math.sin(d_lon / 2)**2) * Math.cos(lat1) * Math.cos(lat2)
17
+ )
18
+
19
+ call_args = [
20
+ 2 * Math.atan2(
21
+ Math.sqrt(a),
22
+ Math.sqrt(1 - a),
23
+ ),
24
+ ]
25
+ if options[:units]
26
+ call_args << options[:units]
27
+ end
28
+ public_send(:radians_to_length, *call_args)
29
+ end
30
+ end
data/lib/turf/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Turf
2
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
3
5
  end
data/lib/turf_ruby.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "turf"
4
+ require "turf/helper"
5
+ require "turf/measurement"
6
+ require "turf/invariant"
data/turf-ruby.gemspec CHANGED
@@ -1,4 +1,6 @@
1
- require_relative 'lib/turf/version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/turf/version"
2
4
 
3
5
  Gem::Specification.new do |spec|
4
6
  spec.name = "turf-ruby"
@@ -6,8 +8,12 @@ Gem::Specification.new do |spec|
6
8
  spec.authors = ["Rafael Santos"]
7
9
  spec.email = ["santos@rafael.nz"]
8
10
 
9
- spec.summary = %q{A modular geospatial engine. Ruby port of TurfJS.}
10
- spec.description = %q{Turf Ruby is a Ruby library for spatial analysis. It includes traditional spatial operations, helper functions for creating GeoJSON data, and data classification and statistics tools.}
11
+ spec.summary = "A modular geospatial engine. Ruby port of TurfJS."
12
+ spec.description = [
13
+ "Turf Ruby is a Ruby library for spatial analysis. ",
14
+ "It includes traditional spatial operations, helper functions for creating",
15
+ " GeoJSON data, and data classification and statistics tools.",
16
+ ].join("")
11
17
  spec.homepage = "http://github.com/formigarafa/turf-ruby"
12
18
  spec.license = "MIT"
13
19
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
@@ -16,9 +22,12 @@ Gem::Specification.new do |spec|
16
22
  spec.metadata["source_code_uri"] = spec.homepage
17
23
 
18
24
  # Specify which files should be added to the gem when it is released.
19
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
26
+ # into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ end
22
31
  end
23
32
  spec.bindir = "exe"
24
33
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turf-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Santos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-19 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Turf Ruby is a Ruby library for spatial analysis. It includes traditional
14
14
  spatial operations, helper functions for creating GeoJSON data, and data classification
@@ -20,6 +20,8 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - ".gitignore"
23
+ - ".hound.yml"
24
+ - ".rubocop.yml"
23
25
  - ".travis.yml"
24
26
  - CODE_OF_CONDUCT.md
25
27
  - Gemfile
@@ -29,10 +31,12 @@ files:
29
31
  - Rakefile
30
32
  - bin/console
31
33
  - bin/setup
32
- - lib/turf-ruby.rb
33
34
  - lib/turf.rb
34
35
  - lib/turf/helper.rb
36
+ - lib/turf/invariant.rb
37
+ - lib/turf/measurement.rb
35
38
  - lib/turf/version.rb
39
+ - lib/turf_ruby.rb
36
40
  - turf-ruby.gemspec
37
41
  homepage: http://github.com/formigarafa/turf-ruby
38
42
  licenses:
data/lib/turf-ruby.rb DELETED
@@ -1,2 +0,0 @@
1
- require "turf"
2
- require "turf/helper"