kml_polygon 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +23 -0
- data/README.md +82 -0
- data/Rakefile +1 -0
- data/kml_polygon.gemspec +21 -0
- data/lib/kml_polygon.rb +193 -0
- data/lib/kml_polygon/version.rb +3 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OWQ1ZTIzMTI4MzNiNTE3OTI3NWMzOThlNDRiN2RlMTk2MmY3Y2JjMQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OTc4NWRkN2JjZGZkNmViMzk5NDMxMDZiODM2MTkzNzViMjQ5YzRjYg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTJlMjBmNGRjYjdkMTM2NjBmMjc3YzMzNmQ5NDlhYzkyYjg3MmIxZTEyMGUx
|
10
|
+
OWJiNGJjOTNiZjcwNzZmZmNiZDBmZjQ1M2ZhMDRmOGY3NTAwZGIzMzlmOTk1
|
11
|
+
NmYzNDFkZGU5NGI5MjZmZTM5NGM3YTdkOGQ5NzEwNWRhN2MyMzE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODkxZTM0YjI1MzAyZWFiMjM1ZTE3NThhOTVhMDRhZDVkYmViNDE2NmE3YzEz
|
14
|
+
YmJjY2Y2ZTk4NjBkZjU3MWUyZGI4YTM2MjFkYWQxZWZhZDA5YmNlOGUzYzk0
|
15
|
+
MjAxM2VhMTY5ODYzN2Q2YjJiNmMzNDgzMTFjMjdmMTY4NDIwYTI=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2007 Nick Galbreath, (c) 2013 Matthew Brookes
|
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.
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
|
2
|
+
=======
|
3
|
+
kml_polygon
|
4
|
+
===========
|
5
|
+
|
6
|
+
Google earth doesn't provide polygon primitives, leaving you to generate them as a series of points.
|
7
|
+
|
8
|
+
Given only a center-point (longitude/latitude) and radius, this Gem makes it easy to create
|
9
|
+
(n) sided polygons (sufficient sides giving a circle) or stars, for inclusion in a KML file.
|
10
|
+
|
11
|
+
All credit to Nick Galbreath for the original code, and for providing it an a portable format. Thanks!
|
12
|
+
(http://blog.client9.com/2007/09/drawing-circles-and-stars-on-google.html)
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'kml_polygon', :git => 'git://github.com/mbrookes/kml_polygon.git'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install kml_polygon
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
kml_regular_polygon(longitude, latitude, radius, [sides=20, rotate=0])
|
32
|
+
kml_star(longitude, latitude, radius, inner_radius, [sides=10, rotate=0])
|
33
|
+
|
34
|
+
Returns a string suitable for adding into a KML file.
|
35
|
+
|
36
|
+
### kml_regular_polygon
|
37
|
+
|
38
|
+
kml_regular_polygon(longitude, latitude, radius, [sides=0, rotate=0])
|
39
|
+
|
40
|
+
Regular polygon
|
41
|
+
|
42
|
+
longitude, latitude - center point in decimal degrees
|
43
|
+
radius - radius in meters
|
44
|
+
segments - number of sides, > 20 looks like a circle (optional, default: 20)
|
45
|
+
rotate - rotate polygon <rotate> degrees (optional, default: 0)
|
46
|
+
|
47
|
+
### kml_star
|
48
|
+
|
49
|
+
kml_star(longitude, latitude, radius, inner_radius, [sides=10, rotate=0])
|
50
|
+
|
51
|
+
Make a "star" or "burst" pattern
|
52
|
+
|
53
|
+
longitude, latitude - center point in decimal degrees
|
54
|
+
radius - radius in meters
|
55
|
+
inner_radius - radius in meters, typically < outer_radius
|
56
|
+
segments - number of "points" on the star (optional, default: 10)
|
57
|
+
rotate - rotate polygon by <rotate> degrees (optional, default: 0)
|
58
|
+
|
59
|
+
### For Example:
|
60
|
+
# kml_polygon_example.rb
|
61
|
+
include kml_polygon
|
62
|
+
circle = kml_regular_polygon(-95, 50, 70000)
|
63
|
+
star = kml_star(-95,45, 70000, 50000)
|
64
|
+
puts "<kml><Document>
|
65
|
+
<Style id='polygon1'><PolyStyle><color>7fff0000</color></PolyStyle></Style>
|
66
|
+
<Style id='polygon2'><PolyStyle><color>7f0000ff</color></PolyStyle></Style>
|
67
|
+
<Placemark><styleUrl>polygon1</styleUrl><Polygon>" << circle << "</Polygon></Placemark>
|
68
|
+
<Placemark><styleUrl>polygon2</styleUrl><Polygon>" << star << "</Polygon></Placemark>
|
69
|
+
</Document></kml>"
|
70
|
+
|
71
|
+
Outputs a minimal but funtional KML file with one circle and one star in semi-transparent colors.
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it (`git clone git://github.com/mbrookes/kml_polygon.git`)
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
80
|
+
|
81
|
+
|
82
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/kml_polygon.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require './lib/kml_polygon/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "kml_polygon"
|
8
|
+
gem.version = KmlPolygon::VERSION
|
9
|
+
gem.authors = ["Nick Galbreath", "Matthew Brookes"]
|
10
|
+
gem.email = ["kml_polygon@nospam.33m.co"]
|
11
|
+
gem.description = %q{Generate KML polygons (circle, star etc.) from lat/long and radius
|
12
|
+
A port of http://blog.client9.com/2007/09/drawing-circles-and-stars-on-google.html}
|
13
|
+
gem.summary = %q{Output a KML fragment defining a polygon (eg. a circle or star) about a point}
|
14
|
+
gem.homepage = "https://github.com/mbrookes/kml_polygon"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.license = "MIT"
|
21
|
+
end
|
data/lib/kml_polygon.rb
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
#
|
2
|
+
#The MIT License
|
3
|
+
#
|
4
|
+
# Copyright (c) 2007 Nick Galbreath, (c) 2013 Matthew Brookes
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
# THE SOFTWARE.
|
23
|
+
#
|
24
|
+
|
25
|
+
#
|
26
|
+
# Version 2.0.2 - 29-Mar-2013 More idomatic Ruby,
|
27
|
+
# Renamed to kml_polygon to better reflection function
|
28
|
+
# Version 2.0.1 - 29-Mar-2013 Translation to Ruby Gem
|
29
|
+
# No CLI
|
30
|
+
# Version 2 - 12-Sept-2007 Simplified XML output
|
31
|
+
# Added commandline interface
|
32
|
+
#
|
33
|
+
# Version 1 - 10-Sept-2007 Initial release
|
34
|
+
#
|
35
|
+
|
36
|
+
#require 'kml_polygon/version'
|
37
|
+
|
38
|
+
module KmlPolygon
|
39
|
+
extend self
|
40
|
+
|
41
|
+
include Math
|
42
|
+
|
43
|
+
# constant to convert to degrees
|
44
|
+
DEGREES = 180.0 / PI
|
45
|
+
# constant to convert to radians
|
46
|
+
RADIANS = PI / 180.0
|
47
|
+
# Mean Radius of Earth, radius
|
48
|
+
EARTH_MEAN_RADIUS = 6378.1 * 1000.0
|
49
|
+
|
50
|
+
#
|
51
|
+
# Convert (x,y,z) on unit sphere
|
52
|
+
# back to (longitude, latitude)
|
53
|
+
#
|
54
|
+
# p is vector of three elements
|
55
|
+
#
|
56
|
+
def to_earth(p)
|
57
|
+
p[0] == 0.0 ? longitude = PI / 2.0 :longitude = Math.atan(p[1]/p[0])
|
58
|
+
latitude = PI / 2.0 - Math.acos(p[2])
|
59
|
+
|
60
|
+
# select correct branch of arctan
|
61
|
+
(p[1] <= 0.0 ? longitude = -(PI - longitude) : longitude = PI + longitude) if p[0] < 0.0
|
62
|
+
[longitude * DEGREES, latitude * DEGREES]
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# convert longitude, latitude IN RADIANS to (x,y,z)
|
67
|
+
#
|
68
|
+
def to_cart(longitude, latitude)
|
69
|
+
theta = longitude
|
70
|
+
# spherical coordinate use "co-latitude", not "latitude"
|
71
|
+
# lattiude = [-90, 90] with 0 at equator
|
72
|
+
# co-latitude = [0, 180] with 0 at north pole
|
73
|
+
phi = PI / 2.0 - latitude
|
74
|
+
[Math.cos(theta) * Math.sin(phi), Math.sin(theta) * Math.sin(phi), Math.cos(phi)]
|
75
|
+
end
|
76
|
+
|
77
|
+
# spoints -- get raw list of points in longitude,latitude format
|
78
|
+
#
|
79
|
+
# radius: radius of polygon in meters
|
80
|
+
# sides: number of sides
|
81
|
+
# rotate: rotate polygon by number of degrees
|
82
|
+
#
|
83
|
+
# Returns a list of points comprising the object
|
84
|
+
#
|
85
|
+
def spoints(longitude, latitude, radius, sides, rotate=0)
|
86
|
+
|
87
|
+
rotate_radians = rotate * RADIANS
|
88
|
+
|
89
|
+
# compute longitude degrees (in radians) at given latitude
|
90
|
+
r = radius / (EARTH_MEAN_RADIUS * Math.cos(latitude * RADIANS))
|
91
|
+
|
92
|
+
vector = to_cart(longitude * RADIANS, latitude * RADIANS)
|
93
|
+
point = to_cart(longitude * RADIANS + r, latitude * RADIANS)
|
94
|
+
points = []
|
95
|
+
|
96
|
+
for side in 0...sides
|
97
|
+
points << to_earth(rotate_point(vector, point, rotate_radians + (2.0 * PI/sides)*side))
|
98
|
+
end
|
99
|
+
|
100
|
+
# Connect to starting point exactly
|
101
|
+
# Not sure if required, but seems to help when the polygon is not filled
|
102
|
+
points << points[0]
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# rotate point around unit vector by phi radians
|
107
|
+
# http://blog.modp.com/2007/09/rotating-point-around-vector.html
|
108
|
+
#
|
109
|
+
def rotate_point(vector, point, phi)
|
110
|
+
# remap vector for sanity
|
111
|
+
u, v, w, x, y, z = vector[0], vector[1], vector[2], point[0], point[1], point[2]
|
112
|
+
|
113
|
+
a = u*x + v*y + w*z
|
114
|
+
d = Math.cos(phi)
|
115
|
+
e = Math.sin(phi)
|
116
|
+
|
117
|
+
[(a*u + (x - a*u)*d + (v*z - w*y) * e),
|
118
|
+
(a*v + (y - a*v)*d + (w*x - u*z) * e),
|
119
|
+
(a*w + (z - a*w)*d + (u*y - v*x) * e)]
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Output points formatted as a KML string
|
124
|
+
#
|
125
|
+
# You may want to edit this function to change "extrude" and other XML nodes.
|
126
|
+
#
|
127
|
+
def points_to_kml(points)
|
128
|
+
|
129
|
+
kml_string = "<Polygon>\n"
|
130
|
+
kml_string << " <outerBoundaryIs><LinearRing><coordinates>\n"
|
131
|
+
|
132
|
+
points.each do |point|
|
133
|
+
kml_string << " " << point[0].to_s << "," << point[1].to_s << "\n"
|
134
|
+
end
|
135
|
+
|
136
|
+
kml_string << " </coordinates></LinearRing></outerBoundaryIs>\n"
|
137
|
+
kml_string << "</Polygon>\n"
|
138
|
+
|
139
|
+
# kml_string << " <extrude>1</extrude>\sides"
|
140
|
+
# kml_string << " <altitudeMode>clampToGround</altitudeMode>\sides"
|
141
|
+
end
|
142
|
+
|
143
|
+
#
|
144
|
+
# kml_regular_polygon - Regular polygon
|
145
|
+
#
|
146
|
+
# (longitude, latitude) - center point in decimal degrees
|
147
|
+
# radius - radius in meters
|
148
|
+
# segments - number of sides, > 20 looks like a circle (optional, default: 20)
|
149
|
+
# rotate - rotate polygon by number of degrees (optional, default: 0)
|
150
|
+
#
|
151
|
+
# Returns a string suitable for adding into a KML file.
|
152
|
+
#
|
153
|
+
def kml_regular_polygon(longitude, latitude, radius, segments=20, rotate=0)
|
154
|
+
points_to_kml(spoints(longitude, latitude, radius, segments, rotate))
|
155
|
+
end
|
156
|
+
|
157
|
+
#
|
158
|
+
# kml_star - Make a "star" or "burst" pattern
|
159
|
+
#
|
160
|
+
# (longitude, latitude) - center point in decimal degrees
|
161
|
+
# radius - radius in meters
|
162
|
+
# innner_radius - radius in meters, typically < outer_radius
|
163
|
+
# segments - number of "points" on the star (optional, default: 10)
|
164
|
+
# rotate - rotate polygon by number of degrees (optional, default: 0)
|
165
|
+
#
|
166
|
+
# Returns a string suitable for adding into a KML file.
|
167
|
+
#
|
168
|
+
def kml_star(longitude, latitude, radius, inner_radius, segments=10, rotate=0)
|
169
|
+
outer_points = spoints(longitude, latitude, radius, segments, rotate)
|
170
|
+
inner_points = spoints(longitude, latitude, inner_radius, segments, rotate + 180.0 / segments)
|
171
|
+
|
172
|
+
# interweave the radius and inner_radius points
|
173
|
+
# I'm sure there is a better way
|
174
|
+
points = []
|
175
|
+
for point in 0...outer_points.length
|
176
|
+
points << outer_points[point]
|
177
|
+
points << inner_points[point]
|
178
|
+
end
|
179
|
+
|
180
|
+
# MTB - Drop the last overlapping point leaving start and end points connecting
|
181
|
+
# (resulting output differs from orig, but is more correct)
|
182
|
+
points.pop
|
183
|
+
|
184
|
+
points_to_kml(points)
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
#
|
190
|
+
# Examples
|
191
|
+
#
|
192
|
+
# puts KmlPolygon::kml_star(45,45, 70000, 50000)
|
193
|
+
# puts KmlPolygon::kml_regular_polygon(50, 50, 70000)
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kml_polygon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Galbreath
|
8
|
+
- Matthew Brookes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "Generate KML polygons (circle, star etc.) from lat/long and radius\n
|
15
|
+
\ A port of http://blog.client9.com/2007/09/drawing-circles-and-stars-on-google.html"
|
16
|
+
email:
|
17
|
+
- kml_polygon@nospam.33m.co
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- kml_polygon.gemspec
|
28
|
+
- lib/kml_polygon.rb
|
29
|
+
- lib/kml_polygon/version.rb
|
30
|
+
homepage: https://github.com/mbrookes/kml_polygon
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
metadata: {}
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 2.0.3
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Output a KML fragment defining a polygon (eg. a circle or star) about a point
|
54
|
+
test_files: []
|