stanford-geo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9084a2315eada6391743c282970117cd111473c80987d24ad858bc6efe044c3
4
+ data.tar.gz: b91d4f6689a79e01a381d52b3575a313be663309a07b7f35d9403f5c8a9265b7
5
+ SHA512:
6
+ metadata.gz: 22fdb6a5944c360c0d88d56b4f3dd03c266c10b804b08f7295ba6868a6c6763e02823a3ce224c1636e6ff3866e7421f2027eda7a21755b0387df673604f579ff
7
+ data.tar.gz: 431f1ccd94e6df58a2c04f0071f6e7ee50f4037552f059889f760e119e28be0495ca1244499aef5e9244f5aa3f40fa11840be479bc0fc40541b65db4750873d9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Metrics/BlockLength:
16
+ AllowedMethods: ['describe', 'context']
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Stanford::Geo
2
+
3
+ This gem provides some shared classes for dealing with Geo data.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sul-dlss/stanford-geo.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stanford
4
+ module Geo
5
+ ##
6
+ # Geospatial coordinate parsing
7
+ class Coordinate
8
+ COORD_TO_DECIMAL_REGEX = Regexp.union(
9
+ /(?<dir>[NESW])\s*(?<deg>\d+)[°⁰º](?:(?<min>\d+)[ʹ'])?(?:(?<sec>\d+)[ʺ"])?/,
10
+ /^\s*(?<dir>[NESW])\s*(?<deg>\d+(?:[.]\d+)?)\s*$/
11
+ )
12
+
13
+ attr_reader :value
14
+
15
+ def initialize(value)
16
+ @value = value
17
+ end
18
+
19
+ # @return [String] the coordinate in WKT/CQL ENVELOPE representation
20
+ def as_envelope
21
+ return unless valid?
22
+
23
+ "ENVELOPE(#{bounds[:min_x]}, #{bounds[:max_x]}, #{bounds[:max_y]}, #{bounds[:min_y]})"
24
+ end
25
+
26
+ # @return [String] the coordinate in Solr 4.x+ bbox-format representation
27
+ def as_bbox
28
+ return unless valid?
29
+
30
+ "#{bounds[:min_x]} #{bounds[:min_y]} #{bounds[:max_x]} #{bounds[:max_y]}"
31
+ end
32
+
33
+ # @return [Boolean] true iff the coordinates are geographically valid
34
+ def valid?
35
+ return false if bounds.empty?
36
+
37
+ range_x = -180.0..180.0
38
+ range_y = -90.0..90.0
39
+
40
+ range_x.include?(bounds[:min_x]) &&
41
+ range_x.include?(bounds[:max_x]) &&
42
+ range_y.include?(bounds[:min_y]) &&
43
+ range_y.include?(bounds[:max_y])
44
+ end
45
+
46
+ private
47
+
48
+ def bounds
49
+ @bounds ||= begin
50
+ matches = cleaner_coordinate(value).match %r{\A(?<lat>[EW].+-+.+)\s*/\s*(?<lng>[NS].+-+.+)\Z}
51
+ return {} unless matches
52
+
53
+ min_x, max_x = matches["lat"].split(/-+/).map { |x| coord_to_decimal(x) }.minmax
54
+ min_y, max_y = matches["lng"].split(/-+/).map { |y| coord_to_decimal(y) }.minmax
55
+ { min_x: min_x, min_y: min_y, max_x: max_x, max_y: max_y }
56
+ end
57
+ end
58
+
59
+ # @deprecated see GeoUtils
60
+ def coord
61
+ cleaner_coordinate(value)
62
+ end
63
+
64
+ # @param [String] val Coordinates value
65
+ # @return [String] cleaned value (strips parens and period), or the original value
66
+ def cleaner_coordinate(val)
67
+ matches = val.match(/^\(?([^)]+)\)?\.?$/)
68
+ matches ? matches[1] : val
69
+ end
70
+
71
+ # @param [String] point coordinate point in degrees notation
72
+ # @return [Float] converted value in decimal notation
73
+ def coord_to_decimal(point)
74
+ match = COORD_TO_DECIMAL_REGEX.match(point)
75
+ return Float::INFINITY unless match
76
+
77
+ dec = match["deg"].to_f
78
+ dec += match["min"].to_f / 60
79
+ dec += match["sec"].to_f / 60 / 60
80
+ dec = -1 * dec if match["dir"] == "W" || match["dir"] == "S"
81
+ dec
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stanford
4
+ module Geo
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "geo/version"
4
+ require_relative "geo/coordinate"
5
+
6
+ module Stanford
7
+ module Geo
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ module Stanford
2
+ module Geo
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stanford-geo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Coyne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - jcoyne@justincoyne.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - README.md
23
+ - Rakefile
24
+ - lib/stanford/geo.rb
25
+ - lib/stanford/geo/coordinate.rb
26
+ - lib/stanford/geo/version.rb
27
+ - sig/stanford/geo.rbs
28
+ homepage: https://github.com/sul-dlss/stanford-geo
29
+ licenses: []
30
+ metadata:
31
+ homepage_uri: https://github.com/sul-dlss/stanford-geo
32
+ source_code_uri: https://github.com/sul-dlss/stanford-geo
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.0.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.5.4
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Geo related data for Stanford Libraries
52
+ test_files: []