geojson_model 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +3 -0
- data/Gemfile +10 -0
- data/LICENSE.md +8 -0
- data/README.md +93 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/geojson_model.gemspec +37 -0
- data/lib/geojson_model.rb +9 -0
- data/lib/geojson_model/base.rb +32 -0
- data/lib/geojson_model/coder.rb +34 -0
- data/lib/geojson_model/feature.rb +16 -0
- data/lib/geojson_model/feature_collection.rb +15 -0
- data/lib/geojson_model/geometry.rb +15 -0
- data/lib/geojson_model/geometry_collection.rb +15 -0
- data/lib/geojson_model/version.rb +3 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07c8e007d3e18cca725835bc13a1d1d0a0bca447
|
4
|
+
data.tar.gz: fe2017fda973fe8babb271b31e42277a1f3b3f61
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e39777d65ec1b75e83904efa019098e7cc3985a65d4c4a5e1e67e0426878da0077126e32f408e162030b202d87a69a3e2ddd3b4436e9b47c819d27b17941a9c7
|
7
|
+
data.tar.gz: 0ebeb812420fe9783822ed61320c6217a729f77fc84dfb7b71fe31ba797f66d436209e8b09dd55a2bc7d32dc2e1ff8df9148454a3bfbb9c6131a48b1fb67b72f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Copyright (c) 2015, Can-Explore All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
4
|
+
|
5
|
+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
6
|
+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
7
|
+
Neither the name of the Mirego nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
8
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# GeojsonModel
|
2
|
+
|
3
|
+
Geojson as a model. Provides simple objects to manipulate geojson data.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/geojson_model.png)](http://badge.fury.io/rb/geojson_model)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/can-explore/geojson_model/badges/gpa.svg)](https://codeclimate.com/github/can-explore/geojson_model)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/can-explore/geojson_model/badge.svg)](https://coveralls.io/r/can-explore/geojson_model)
|
8
|
+
[![Build Status](https://travis-ci.org/can-explore/geojson_model.png)](https://travis-ci.org/can-explore/geojson_model)
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
g = GeojsonModel::Geometry.new(type: 'Point', coordinates: [0, 0]) # => #<GeojsonModel::Geometry ...>
|
14
|
+
g.to_h # => { type: 'Point', coordinates: [0, 0] }
|
15
|
+
g.to_json # => "{"type":"Point","coordinates":[0,0]}"
|
16
|
+
|
17
|
+
f = GeojsonModel::Feature.new(properties: {foo: 'bar'}, geometry: g) # => #<GeojsonModel::Feature ...>
|
18
|
+
f.to_h # => { type:'Feature', geometry:#<GeojsonModel::Geometry ...>, properties:{foo: 'bar'} }
|
19
|
+
f.to_json # => "{"type":"Feature","geometry":{"type":"Point","coordinates":[0,0]},"properties":{"foo":"bar"}}"
|
20
|
+
|
21
|
+
gc = GeojsonModel::GeometryCollection.new(geometries: [g]) # => #<GeojsonModel::Feature ...>
|
22
|
+
gc.to_h # => { type: 'GeometryCollection', geometries: [#<GeojsonModel::Geometry ...>] }
|
23
|
+
gc.to_json # => "{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[0,0]}]}"
|
24
|
+
|
25
|
+
fc = GeojsonModel::FeatureCollection.new(features: [f]) # => #<GeojsonModel::Feature ...>
|
26
|
+
fc.to_h # => { type: 'FeatureCollection', features: [#<GeojsonModel::Feature geometry=#<GeojsonModel::Geometry ...> ...>] }
|
27
|
+
fc.to_json # => "{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[0,0]},"properties":{"foo":"bar"}}]}"
|
28
|
+
|
29
|
+
g.to_feature # => #<GeojsonModel::Feature ...>
|
30
|
+
f.to_geometry # => #<GeojsonModel::Geometry ...>
|
31
|
+
gc.to_feature_collection # => #<GeojsonModel::FeatureCollection ...>
|
32
|
+
fc.to_geometry_collection # => #<GeojsonModel::GeometryCollection ...>
|
33
|
+
```
|
34
|
+
|
35
|
+
Plays well with `ActiveRecord` and PostgreSQL json and jsonb data type.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# db/migrations/create_places.rb
|
39
|
+
create_table :places do |t|
|
40
|
+
t.jsonb :geojson # or t.json :geojson
|
41
|
+
end
|
42
|
+
|
43
|
+
# models/place.rb
|
44
|
+
class Place < ActiveRecord::Base
|
45
|
+
store :geojson, coder: GeojsonModel::Coder
|
46
|
+
end
|
47
|
+
|
48
|
+
p = Place.new
|
49
|
+
p.geojson = GeojsonModel::Feature.new
|
50
|
+
p.save
|
51
|
+
p.geojson # => #<GeojsonModel::Feature ...>
|
52
|
+
```
|
53
|
+
|
54
|
+
## Installation
|
55
|
+
|
56
|
+
Add this line to your application's Gemfile:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
gem 'geojson_model'
|
60
|
+
```
|
61
|
+
|
62
|
+
And then execute:
|
63
|
+
|
64
|
+
$ bundle
|
65
|
+
|
66
|
+
Or install it yourself as:
|
67
|
+
|
68
|
+
$ gem install geojson_model
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it ( https://github.com/can-explore/geojson_model/fork )
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create a new Pull Request
|
83
|
+
6. Thank you!
|
84
|
+
|
85
|
+
## License
|
86
|
+
|
87
|
+
`geojson_model` is © 2015 Can-Explore and may be freely distributed under the New BSD license. See the `LICENSE.md` file.
|
88
|
+
|
89
|
+
## About Can-Explore
|
90
|
+
|
91
|
+
Can-Explore is a team of passionate people brdiging the gap between technology and the world of civil engineering. We love building new things and get out of our comfort zone.
|
92
|
+
|
93
|
+
We love [open-source](https://github.com/can-explore) and we try to give back to the community as much as we can.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "geojson_model"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'geojson_model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "geojson_model"
|
8
|
+
spec.version = GeojsonModel::VERSION
|
9
|
+
spec.authors = ["Philippe Dionne"]
|
10
|
+
spec.email = ["dionne.phil@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Geojson as a model.}
|
13
|
+
spec.description = %q{Geojson as a model.}
|
14
|
+
spec.homepage = "http://github.com/can-explore/geojson_model"
|
15
|
+
spec.license = "BSD 3-Clause"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "activesupport", ">= 3.0.0"
|
31
|
+
spec.add_dependency "activemodel", ">= 3.0.0"
|
32
|
+
spec.add_dependency "virtus", "~> 1.0.5"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler"
|
35
|
+
spec.add_development_dependency "rake"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
37
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'virtus'
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
module GeojsonModel
|
6
|
+
module Base
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
include Virtus.model
|
11
|
+
include ActiveModel::Serializers::JSON
|
12
|
+
|
13
|
+
alias_method :to_geojson, :to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
# include ActiveModel::Model
|
17
|
+
# include ActiveModel::Dirty
|
18
|
+
# include ActiveModel::Validations
|
19
|
+
|
20
|
+
# validates :geojson, geojson: true
|
21
|
+
|
22
|
+
def attributes=(hash)
|
23
|
+
hash.each do |key, value|
|
24
|
+
send("#{key}=", value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def attributes
|
29
|
+
instance_values
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'geojson_model/base'
|
2
|
+
|
3
|
+
module GeojsonModel
|
4
|
+
class Coder
|
5
|
+
|
6
|
+
# takes the database value and returns an instance of the GeojsonModel class
|
7
|
+
def self.load(value)
|
8
|
+
dispatch(JSON.load(value))
|
9
|
+
end
|
10
|
+
|
11
|
+
# takes an object and returns the value to be stored in the database
|
12
|
+
def self.dump(object)
|
13
|
+
object.to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# @param data [Hash]
|
20
|
+
def self.dispatch(data)
|
21
|
+
if hash.has_key?('geometries')
|
22
|
+
GeometryCollection.new(data)
|
23
|
+
elsif data.has_key?('coordinates')
|
24
|
+
Geometry.new(data)
|
25
|
+
elsif data.has_key?('features')
|
26
|
+
FeatureCollection.new(data)
|
27
|
+
elsif data.has_key?('properties')
|
28
|
+
Feature.new(data)
|
29
|
+
else
|
30
|
+
raise TypeError
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'geojson_model/base'
|
2
|
+
|
3
|
+
module GeojsonModel
|
4
|
+
class Feature
|
5
|
+
include Base
|
6
|
+
|
7
|
+
attribute :type, String, default: 'Feature'
|
8
|
+
attribute :geometry, GeojsonModel::Geometry
|
9
|
+
attribute :properties, Hash
|
10
|
+
|
11
|
+
# @return [Geometry]
|
12
|
+
def to_geometry
|
13
|
+
geometry || Geometry.new
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'geojson_model/base'
|
2
|
+
|
3
|
+
module GeojsonModel
|
4
|
+
class FeatureCollection
|
5
|
+
include Base
|
6
|
+
|
7
|
+
attribute :type, String, default: 'FeatureCollection'
|
8
|
+
attribute :features, Array[GeojsonModel::Feature]
|
9
|
+
|
10
|
+
# @return [GeometryCollection]
|
11
|
+
def to_geometry_collection
|
12
|
+
GeometryCollection.new(geometries: features.map(&:geometry))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'geojson_model/base'
|
2
|
+
|
3
|
+
module GeojsonModel
|
4
|
+
class GeometryCollection
|
5
|
+
include Base
|
6
|
+
|
7
|
+
attribute :type, String, default: 'GeometryCollection'
|
8
|
+
attribute :geometries, Array[GeojsonModel::Geometry]
|
9
|
+
|
10
|
+
# @return [FeatureCollection]
|
11
|
+
def to_feature_collection
|
12
|
+
FeatureCollection.new(features: geometries.map { |geo| Feature.new(geometry: geo) })
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geojson_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philippe Dionne
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: virtus
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.5
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.5
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.2'
|
97
|
+
description: Geojson as a model.
|
98
|
+
email:
|
99
|
+
- dionne.phil@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.md
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- geojson_model.gemspec
|
114
|
+
- lib/geojson_model.rb
|
115
|
+
- lib/geojson_model/base.rb
|
116
|
+
- lib/geojson_model/coder.rb
|
117
|
+
- lib/geojson_model/feature.rb
|
118
|
+
- lib/geojson_model/feature_collection.rb
|
119
|
+
- lib/geojson_model/geometry.rb
|
120
|
+
- lib/geojson_model/geometry_collection.rb
|
121
|
+
- lib/geojson_model/version.rb
|
122
|
+
homepage: http://github.com/can-explore/geojson_model
|
123
|
+
licenses:
|
124
|
+
- BSD 3-Clause
|
125
|
+
metadata:
|
126
|
+
allowed_push_host: https://rubygems.org
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.4.5
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: Geojson as a model.
|
147
|
+
test_files: []
|