to_wkt_ 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rvmrc +62 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +87 -0
- data/Rakefile +1 -0
- data/lib/to_wkt/adapter.rb +62 -0
- data/lib/to_wkt/version.rb +3 -0
- data/lib/to_wkt.rb +18 -0
- data/spec/array.rb +38 -0
- data/spec/spec_helper.rb +8 -0
- data/to_wkt.gemspec +24 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dee4aea85d29acdb3bdb8d39c0257ab634212a89
|
4
|
+
data.tar.gz: 6d1d18eeac5d3d338da06b413404d069054d4f51
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 72d20da2516c47f65891e79f291ecbe2298f2d4dd0a953d7fa8d8e192e90b5751058de4121f8077018df358423739495544a75fc9b7394470306f3d8f6a1cbbe
|
7
|
+
data.tar.gz: ec2ea6771ead74ab095e9f1e8316632d414203e5a63bc5c3c65151ddee42757b9d2e8d7a075e268d43721413ff086d59bb74b5a848202e7d0fdf500f26758b7d
|
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use head@to_wkt" > .rvmrc
|
9
|
+
environment_id="ruby-head@to_wkt"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.22.10 (stable)" # 1.10.1 seems like a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
|
27
|
+
do
|
28
|
+
if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
|
29
|
+
then \. "${__hook}" || true
|
30
|
+
fi
|
31
|
+
done
|
32
|
+
unset __hook
|
33
|
+
if (( ${rvm_use_flag:=1} >= 1 )) # display automatically
|
34
|
+
then
|
35
|
+
if [[ $- == *i* ]] # check for interactive shells
|
36
|
+
then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
|
37
|
+
" # show the user the ruby and gemset they are using in green
|
38
|
+
else printf "%b" "Using: $GEM_HOME
|
39
|
+
" # don't use colors in non-interactive shells
|
40
|
+
fi
|
41
|
+
fi
|
42
|
+
else
|
43
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
44
|
+
rvm --create use "$environment_id" || {
|
45
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
46
|
+
return 1
|
47
|
+
}
|
48
|
+
fi
|
49
|
+
|
50
|
+
# If you use bundler, this might be useful to you:
|
51
|
+
# if [[ -s Gemfile ]] && {
|
52
|
+
# ! builtin command -v bundle >/dev/null ||
|
53
|
+
# builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
|
54
|
+
# }
|
55
|
+
# then
|
56
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
57
|
+
# gem install bundler
|
58
|
+
# fi
|
59
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
60
|
+
# then
|
61
|
+
# bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
|
62
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Steven Dunlap
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# ToWkt
|
2
|
+
|
3
|
+
Quickly and easily convert arrays to [Well-Known Text (WKT)] format. Which are
|
4
|
+
commonly used in GIS Databases such as [PostGIS].
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'to_wkt_'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install to_wkt_
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
ToWkt automatically exposes the following methods on any instance of `Array`:
|
23
|
+
|
24
|
+
- `#to_wkt_point`
|
25
|
+
- converts an array to a WKT POINT
|
26
|
+
- e.g.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
[0,0].to_wkt_point # "POINT(0 0)"
|
30
|
+
```
|
31
|
+
|
32
|
+
- `#to_wkt_line_string`
|
33
|
+
- converts an array to a WKT LINESTRING
|
34
|
+
- e.g.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
[ [0,0], [1,1], [1,2] ].to_wkt_line_string # "LINESTRING(0 0,1 1,1 2)"
|
38
|
+
```
|
39
|
+
|
40
|
+
- `#to_wkt_polygon`
|
41
|
+
- converts an array to a WKT POLYGON
|
42
|
+
- e.g.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
polygon_array = [ [ [0,0], [4,0], [4,4], [0,4], [0,0] ], [ [1,1], [2,1], [2,2], [1,2], [1,1] ] ]
|
46
|
+
polygon_array.to_wkt_polygon # "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))"
|
47
|
+
```
|
48
|
+
|
49
|
+
- `#to_wkt_multi_point`
|
50
|
+
- converts an array to a WKT MULTIPOINT
|
51
|
+
- e.g.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
[ [0,0], [1,2] ].to_wkt_multi_point # "MULTIPOINT(0 0,1 2)"
|
55
|
+
```
|
56
|
+
|
57
|
+
- `#to_wkt_multi_line_string`
|
58
|
+
- converts an array to a WKT MULTILINESTRING
|
59
|
+
- e.g.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
multi_line_array = [ [ [0,0], [1,1], [1,2] ], [ [2,3], [3,2], [5,4] ] ]
|
63
|
+
multi_line_array.to_wkt_multi_line_string # "MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))"
|
64
|
+
```
|
65
|
+
|
66
|
+
- `#to_wkt_multi_polygon`
|
67
|
+
- converts an array to a WKT MULTIPOLYGON
|
68
|
+
- e.g.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
multi_polygon_array = [
|
72
|
+
[ [[0,0], [4,0], [4,4], [0,4], [0,0]], [[1,1], [2,1], [2,2], [1,2], [1,1]] ],
|
73
|
+
[ [[-1,-1], [-1,-2], [-2,-2], [-2,-1], [-1,-1]] ]
|
74
|
+
]
|
75
|
+
multi_polygon_array.to_wkt_multi_polygon= "MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)),((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))"
|
76
|
+
```
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
1. Fork it
|
81
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
82
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
83
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
84
|
+
5. Create new Pull Request
|
85
|
+
|
86
|
+
[Well-Known Text (WKT)]: http://en.wikipedia.org/wiki/Well-known_text
|
87
|
+
[PostGIS]: http://postgis.refractions.net/documentation/manual-1.3SVN/ch04.html#id2726203
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module ToWkt
|
2
|
+
|
3
|
+
class Adapter
|
4
|
+
|
5
|
+
WKT_TYPES = [:point, :line_string, :polygon, :multi_point, :multi_line_string, :multi_polygon]
|
6
|
+
|
7
|
+
def array_to_wkt(array, type)
|
8
|
+
raise "invalid WKT type" unless WKT_TYPES.include?(type)
|
9
|
+
make_prefix(type) + make_suffix(array, type)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def make_prefix(type)
|
15
|
+
type.to_s.tr('_', '').upcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def make_suffix(array, type)
|
19
|
+
output = '('
|
20
|
+
output += case type.to_sym
|
21
|
+
when :point
|
22
|
+
point_string(array)
|
23
|
+
when :line_string, :multi_point
|
24
|
+
line_string(array)
|
25
|
+
when :polygon, :multi_line_string
|
26
|
+
multi_line_string(array)
|
27
|
+
when :multi_polygon
|
28
|
+
multi_polygon_string(array)
|
29
|
+
else
|
30
|
+
raise "invalid WKT type"
|
31
|
+
end
|
32
|
+
output += ')'
|
33
|
+
end
|
34
|
+
|
35
|
+
def point_string(tuple)
|
36
|
+
"#{tuple[0]} #{tuple[1]}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def line_string(point_list)
|
40
|
+
output = ""
|
41
|
+
point_list.each{ |p| output += "#{point_string( p )}," }
|
42
|
+
output.chomp ','
|
43
|
+
end
|
44
|
+
|
45
|
+
def multi_line_string(line_list)
|
46
|
+
output = ""
|
47
|
+
line_list.each{ |l| output += "(#{ line_string( l ) })," }
|
48
|
+
output.chomp ','
|
49
|
+
end
|
50
|
+
|
51
|
+
def multi_polygon_string(polygon_list)
|
52
|
+
output = ""
|
53
|
+
polygon_list.each{ |p| output += "(#{ multi_line_string( p ) })," }
|
54
|
+
output.chomp ','
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
data/lib/to_wkt.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "to_wkt/version"
|
2
|
+
require "to_wkt/adapter"
|
3
|
+
|
4
|
+
module ToWkt
|
5
|
+
def to_wkt(type)
|
6
|
+
adapter = Adapter.new
|
7
|
+
adapter.array_to_wkt(self, type)
|
8
|
+
end
|
9
|
+
|
10
|
+
Adapter::WKT_TYPES.each do |type|
|
11
|
+
define_method( "to_wkt_#{type}".to_sym ) do
|
12
|
+
to_wkt(type)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
Array.include( ToWkt )
|
data/spec/array.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
it { should respond_to(:to_wkt) }
|
5
|
+
|
6
|
+
it 'should be able to convert to a WKT POINT' do
|
7
|
+
expect( [0,0].to_wkt_point ).to eq("POINT(0 0)")
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be able to convert to a WKT LINESTRING' do
|
11
|
+
expect( [ [0,0], [1,1], [1,2] ].to_wkt_line_string ).to eq("LINESTRING(0 0,1 1,1 2)")
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should be able to convert to a WKT POLYGON' do
|
15
|
+
polygon_array = [ [ [0,0], [4,0], [4,4], [0,4], [0,0] ], [ [1,1], [2,1], [2,2], [1,2], [1,1] ] ]
|
16
|
+
expect( polygon_array.to_wkt_polygon ).to eq("POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))")
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should be able to convert to a WKT MULTIPOINT' do
|
20
|
+
expect( [ [0,0], [1,2] ].to_wkt_multi_point ).to eq("MULTIPOINT(0 0,1 2)")
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should be able to convert to a WKT MULTILINESTRING' do
|
24
|
+
multi_line_array = [ [ [0,0], [1,1], [1,2] ], [ [2,3], [3,2], [5,4] ] ]
|
25
|
+
expect(multi_line_array.to_wkt_multi_line_string).to eq("MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))")
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be able to convert to a WKT MULTIPOLYGON' do
|
29
|
+
multi_polygon_array = [
|
30
|
+
[ [[0,0], [4,0], [4,4], [0,4], [0,0]], [[1,1], [2,1], [2,2], [1,2], [1,1]] ],
|
31
|
+
[ [[-1,-1], [-1,-2], [-2,-2], [-2,-1], [-1,-1]] ]
|
32
|
+
]
|
33
|
+
wkt_equivalent = "MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)),((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))"
|
34
|
+
expect( multi_polygon_array.to_wkt_multi_polygon ).to eq(wkt_equivalent)
|
35
|
+
end
|
36
|
+
|
37
|
+
pending 'should be able to convert to a WKT GEOMETRYCOLLECTION'
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/to_wkt.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'to_wkt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "to_wkt_"
|
8
|
+
spec.version = ToWkt::VERSION
|
9
|
+
spec.authors = ["Steven Dunlap"]
|
10
|
+
spec.email = ["steven@roadtrippers.com"]
|
11
|
+
spec.description = %q{Adds to_wkt_TYPE method to arrays}
|
12
|
+
spec.summary = %q{Convert an array to a POINT, LINESTRING, POLYGON, etc. with methods such as #to_wkt_point}
|
13
|
+
spec.homepage = "http://github.com/roadtrippers/to_wkt"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_wkt_
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steven Dunlap
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Adds to_wkt_TYPE method to arrays
|
56
|
+
email:
|
57
|
+
- steven@roadtrippers.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rvmrc"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/to_wkt.rb
|
69
|
+
- lib/to_wkt/adapter.rb
|
70
|
+
- lib/to_wkt/version.rb
|
71
|
+
- spec/array.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- to_wkt.gemspec
|
74
|
+
homepage: http://github.com/roadtrippers/to_wkt
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.1.5
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: 'Convert an array to a POINT, LINESTRING, POLYGON, etc. with methods such
|
98
|
+
as #to_wkt_point'
|
99
|
+
test_files:
|
100
|
+
- spec/array.rb
|
101
|
+
- spec/spec_helper.rb
|