geojson2image 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
+ SHA1:
3
+ metadata.gz: 28f40062001e387eb73050e7ab8e58374b2b6906
4
+ data.tar.gz: 663faeebb08988d1d2af68b75031e9d4d6b7632f
5
+ SHA512:
6
+ metadata.gz: 49dece8d38b249e3e53cf6f4011c60444549b6318a80d5cfbe8151afeaab82df134cddf81dee41a2a26e35459c88b72e96ddbeff7668376c5985c3932ece37df
7
+ data.tar.gz: ed141b41854390a0b73bdfa41b3a34b62d345e3743886812cee8c8d8ad8c0bdce2444e4570384f0bd3ab0a7eb8637289481bbfe91fad51bb09fcc9a41346cf2d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.png
11
+ /*.jpg
12
+ /*.gif
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in geojson2image.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Bryce Johnston
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.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # geojson2image
2
+
3
+ [![Gem Version](http://img.shields.io/gem/v/geojson2image.svg)][gem]
4
+
5
+ [gem]: https://rubygems.org/gems/geojson2image
6
+
7
+ Ruby library for generating images from GeoJSON.
8
+
9
+ Currently, MultiPolygon and Polygon GeoJSON types should output images properly. Other GeoJSON types have not been thoroughly tested, and properties from the GeoJSON are not being parsed.
10
+
11
+ ## Installation
12
+
13
+ You will need [ImageMagick](http://imagemagick.org/) installed. Then Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'geojson2image'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install geojson2image
26
+
27
+ ## Usage
28
+
29
+ Initialize new Geojson2image::Convert object and call to_image
30
+ ```ruby
31
+ gjson = '{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[......'
32
+ g2i = Geojson2image::Convert.new(
33
+ json: gjson,
34
+ width: 500,
35
+ height: 500,
36
+ padding: 50,
37
+ background_color: 'white',
38
+ fill_color: 'rgba(0, 158, 40, 0.3)',
39
+ stroke_color: 'rgb(0, 107, 27)',
40
+ stroke_width: 3,
41
+ output: "output.jpg"
42
+ )
43
+ g2i.to_image
44
+ ```
45
+
46
+ ### Example Output
47
+ ![Example Output](example/example_output.jpg?raw=true "Example Output")
48
+
49
+ ### Color Options
50
+
51
+ ImageMagick Color Names Reference: https://www.imagemagick.org/script/color.php
52
+
53
+ ## Development
54
+
55
+ 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.
56
+
57
+ 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).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/brycejohnston/geojson2image.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geojson2image"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'geojson2image/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geojson2image"
8
+ spec.version = Geojson2image::VERSION
9
+ spec.authors = ["Bryce Johnston"]
10
+ spec.email = ["johnstonbrc@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby library for generating images from GeoJSON}
13
+ spec.description = %q{Ruby library for generating images from GeoJSON}
14
+ spec.homepage = "https://github.com/brycejohnston/geojson2image"
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.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "oj", "~> 2.18"
25
+ spec.add_dependency "mini_magick", "~> 4.6.0"
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,3 @@
1
+ module Geojson2image
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,243 @@
1
+ require "geojson2image/version"
2
+ require "oj"
3
+ require "mini_magick"
4
+
5
+ module Geojson2image
6
+ class Convert
7
+ attr_accessor :parsed_json, :width, :height, :background_color,
8
+ :border_color, :border_width, :padding, :output, :min_xy, :max_xy,
9
+ :coordinates, :width_padding, :height_padding, :global_ratio
10
+
11
+ def initialize(json: nil, width: nil, height: nil, padding: nil,
12
+ background_color: nil, fill_color: nil, stroke_color: nil,
13
+ stroke_width: nil, output: nil)
14
+ begin
15
+ @parsed_json = Oj.load(json)
16
+ @width = width || 500
17
+ @height = height || 500
18
+ @padding = padding || 50
19
+ @background_color = background_color || 'white'
20
+ @fill_color = fill_color || 'white'
21
+ @stroke_color = stroke_color || 'black'
22
+ @stroke_width = stroke_width || 3
23
+ @output = output || "output.jpg"
24
+ @min_xy = [-1, -1]
25
+ @max_xy = [-1, -1]
26
+ @coordinates = []
27
+ @width_padding = 0
28
+ @height_padding = 0
29
+ @global_ratio = 0
30
+ rescue Oj::ParseError
31
+ puts "GeoJSON parse error"
32
+ end
33
+ end
34
+
35
+ def get_points(json)
36
+ case json['type']
37
+ when 'GeometryCollection'
38
+ json['geometries'].each do |geometry|
39
+ get_points(geometry)
40
+ end
41
+
42
+ when 'FeatureCollection'
43
+ json['features'].each do |feature|
44
+ get_points(feature)
45
+ end
46
+
47
+ when 'Feature'
48
+ get_points(json['geometry'])
49
+
50
+ when 'Point'
51
+ @coordinates << [json['coordinates'][0], json['coordinates'][1]]
52
+
53
+ when 'MultiPoint'
54
+ json['coordinates'].each do |point|
55
+ @coordinates << [point[0], point[1]]
56
+ end
57
+
58
+ when 'LineString'
59
+ json['coordinates'].each do |point|
60
+ @coordinates << [point[0], point[1]]
61
+ end
62
+
63
+ when 'MultiLineString'
64
+ json['coordinates'].each do |linestrings|
65
+ linestrings.each do |point|
66
+ @coordinates << [point[0], point[1]]
67
+ end
68
+ end
69
+
70
+ when 'Polygon'
71
+ json['coordinates'].each do |linestrings|
72
+ linestrings.each do |point|
73
+ @coordinates << [point[0], point[1]]
74
+ end
75
+ end
76
+
77
+ when 'MultiPolygon'
78
+ json['coordinates'].each do |polygons|
79
+ polygons.each do |linestrings|
80
+ linestrings.each do |point|
81
+ @coordinates << [point[0], point[1]]
82
+ end
83
+ end
84
+ end
85
+
86
+ else
87
+ puts "get_boundary invalid GeoJSON parse error"
88
+ end
89
+ end
90
+
91
+ def get_boundary
92
+ quarter_pi = Math::PI / 4.0
93
+
94
+ @coordinates.each_with_index do |point,i|
95
+ lon = @coordinates[i][0].to_f * Math::PI / 180
96
+ lat = @coordinates[i][1].to_f * Math::PI / 180
97
+
98
+ @coordinates[i][0] = lon
99
+ @coordinates[i][1] = Math.log(Math.tan(quarter_pi + 0.5 * lat))
100
+
101
+ @min_xy[0] = (min_xy[0] == -1 ? @coordinates[i][0] : [min_xy[0], @coordinates[i][0]].min)
102
+ @min_xy[1] = (min_xy[1] == -1 ? @coordinates[i][1] : [min_xy[1], @coordinates[i][1]].min)
103
+ end
104
+
105
+ @coordinates.each_with_index do |point,i|
106
+ @coordinates[i][0] = @coordinates[i][0] - @min_xy[0]
107
+ @coordinates[i][1] = @coordinates[i][1] - @min_xy[1]
108
+
109
+ @max_xy[0] = (max_xy[0] == -1 ? @coordinates[i][0] : [max_xy[0], @coordinates[i][0]].max)
110
+ @max_xy[1] = (max_xy[1] == -1 ? @coordinates[i][1] : [max_xy[1], @coordinates[i][1]].max)
111
+ end
112
+ end
113
+
114
+ def transform_point(point)
115
+ quarter_pi = Math::PI / 4.0
116
+
117
+ lon = point[0] * Math::PI / 180
118
+ lat = point[1] * Math::PI / 180
119
+
120
+ xy = []
121
+ xy[0] = lon - @min_xy[0]
122
+ val = Math.log(Math.tan(quarter_pi + 0.5 * lat))
123
+ xy[1] = val - @min_xy[1]
124
+
125
+ xy[0] = (@width_padding + (xy[0] * @global_ratio)).to_i
126
+ xy[1] = (@height - @height_padding - (xy[1] * @global_ratio)).to_i
127
+
128
+ return xy
129
+ end
130
+
131
+ def draw(json)
132
+ case json['type']
133
+ when 'GeometryCollection'
134
+ json['geometries'].each do |geometry|
135
+ draw(geometry)
136
+ end
137
+
138
+ when 'FeatureCollection'
139
+ return_boundary = nil
140
+ json['features'].each do |feature|
141
+ draw(feature)
142
+ end
143
+
144
+ when 'Feature'
145
+ draw(json['geometry'])
146
+
147
+ when 'Point'
148
+ point = json['coordinates']
149
+ new_point = transform_point(point)
150
+ draw_point = "color #{new_point[0]},#{new_point[1]} point"
151
+ @convert.draw(draw_point)
152
+
153
+ when 'MultiPoint'
154
+ json['coordinates'].each do |coordinate|
155
+ point = {
156
+ "type" => "Point",
157
+ "coordinates" => coordinate
158
+ }
159
+ draw(point)
160
+ end
161
+
162
+ when 'LineString'
163
+ last_point = null
164
+
165
+ json['coordinates'].each do |point|
166
+ new_point = transform_point(point)
167
+ if !last_point.nil?
168
+ polyline = "polyline #{last_point[0]},#{last_point[1]}, #{new_point[0]},#{new_point[1]}"
169
+ @convert.draw(polyline)
170
+ end
171
+ last_point = new_point
172
+ end
173
+
174
+ when 'MultiLineString'
175
+ json['coordinates'].each do |coordinate|
176
+ linestring = {
177
+ "type" => "LineString",
178
+ "coordinates" => coordinate
179
+ }
180
+ draw(linestring)
181
+ end
182
+
183
+ when 'Polygon'
184
+ json['coordinates'].each do |linestrings|
185
+ border_points = []
186
+ if linestrings[0] != linestrings[linestrings.count - 1]
187
+ linestrings << linestrings[0]
188
+ end
189
+
190
+ linestrings.each do |point|
191
+ new_point = transform_point(point)
192
+ border_points << "#{new_point[0]},#{new_point[1]}"
193
+ end
194
+
195
+ border = "polygon " + border_points.join(", ")
196
+ @convert.draw(border)
197
+ end
198
+
199
+ when 'MultiPolygon'
200
+ json['coordinates'].each do |polygon|
201
+ poly = {
202
+ "type" => "Polygon",
203
+ "coordinates" => polygon
204
+ }
205
+ draw(poly)
206
+ end
207
+
208
+ else
209
+ puts "draw invalid GeoJSON parse error - #{json['type']}"
210
+ end
211
+ end
212
+
213
+ def to_image
214
+ @convert = MiniMagick::Tool::Convert.new
215
+ @convert.size("#{@width}x#{@height}")
216
+ @convert.xc(@background_color)
217
+ @convert.fill(@fill_color)
218
+ @convert.stroke(@stroke_color)
219
+ @convert.strokewidth(@stroke_width)
220
+
221
+ get_points(@parsed_json)
222
+ get_boundary
223
+
224
+ padding_both = @padding * 2
225
+
226
+ map_width = @width - padding_both
227
+ map_height = @height - padding_both
228
+
229
+ map_width_ratio = map_width / @max_xy[0]
230
+ map_height_ratio = map_height / @max_xy[1]
231
+
232
+ @global_ratio = [map_width_ratio, map_height_ratio].min
233
+ @width_padding = (@width - (@global_ratio * @max_xy[0])) / 2
234
+ @height_padding = (@height - (@global_ratio * @max_xy[1])) / 2
235
+
236
+ draw(@parsed_json)
237
+
238
+ @convert << @output
239
+ cmd_string = @convert.call
240
+ end
241
+
242
+ end
243
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geojson2image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bryce Johnston
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.18'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mini_magick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Ruby library for generating images from GeoJSON
84
+ email:
85
+ - johnstonbrc@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - example/example_output.jpg
100
+ - geojson2image.gemspec
101
+ - lib/geojson2image.rb
102
+ - lib/geojson2image/version.rb
103
+ homepage: https://github.com/brycejohnston/geojson2image
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Ruby library for generating images from GeoJSON
127
+ test_files: []