geopoint_factory 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +62 -0
- data/Rakefile +8 -0
- data/geopoint_factory.gemspec +23 -0
- data/lib/geopoint_factory.rb +67 -0
- data/lib/geopoint_factory/version.rb +3 -0
- data/spec/lib/geopoint_factory_spec.rb +29 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/sf_shapefile.dbf +0 -0
- data/spec/support/sf_shapefile.shp +0 -0
- data/spec/support/sf_shapefile.shx +0 -0
- metadata +126 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Josh Avant
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
copy of this software and associated documentation files (the "Software"),
|
5
|
+
to deal in the Software without restriction, including without limitation
|
6
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
7
|
+
and/or sell copies of the Software, and to permit persons to whom the
|
8
|
+
Software is furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
19
|
+
DEALINGS IN THE SOFTWARE.
|
20
|
+
|
21
|
+
If we meet some day, and you think this stuff is worth it, you can buy me a
|
22
|
+
beer in return.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# GeoPoint Factory
|
2
|
+
**Generate useful, RGeo-compatible geographic points located within a supplied shapefile.**
|
3
|
+
|
4
|
+
## Quick Start
|
5
|
+
* Follow the [RGeo installation instructions](https://github.com/dazuma/rgeo) and install RGeo.
|
6
|
+
|
7
|
+
* `gem install geopoint_factory`
|
8
|
+
|
9
|
+
* Download an ESRI-compatible shapefile. ([Cartographic boundary files from the US Census](http://www.census.gov/geo/www/cob/bdy_files.html) are a convenient source for these.)
|
10
|
+
|
11
|
+
* Initialize an instance of GeoPointFactory::Generator with a shapefile and generate points:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'geopoint_factory'
|
15
|
+
|
16
|
+
generator = GeoPointFactory::Generator.new("path/to/shapefile")
|
17
|
+
point_inside = generator.generate
|
18
|
+
different_point_inside = generator.generate
|
19
|
+
yet_another_point_inside = generator.generate
|
20
|
+
generator.finalize
|
21
|
+
```
|
22
|
+
|
23
|
+
## Discussion
|
24
|
+
GeoPoint Factory was developed as a way to easily generate geographic coordinate data within a specific geographic area, while avoiding areas that may not be useful, such as in the ocean.
|
25
|
+
|
26
|
+
For example, using the US Census-supplied shapefile of San Francisco's boundaries, GeoPoint Factory can generate random points specifically within San Francisco's borders and avoid generating points within the Pacific Ocean or San Francisco Bay.
|
27
|
+
|
28
|
+
GeoPoint Factory was developed for use with the [RGeo](https://github.com/dazuma/rgeo) library.
|
29
|
+
|
30
|
+
## Compatibility
|
31
|
+
* Ruby 1.9.2+
|
32
|
+
|
33
|
+
**Contributions, corrections, and improvements are always appreciated!**
|
34
|
+
|
35
|
+
## Created By
|
36
|
+
Josh Avant
|
37
|
+
|
38
|
+
## License
|
39
|
+
This is licensed under a MIT/Beerware License:
|
40
|
+
|
41
|
+
Copyright (c) 2012 Josh Avant
|
42
|
+
|
43
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
44
|
+
copy of this software and associated documentation files (the "Software"),
|
45
|
+
to deal in the Software without restriction, including without limitation
|
46
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
47
|
+
and/or sell copies of the Software, and to permit persons to whom the
|
48
|
+
Software is furnished to do so, subject to the following conditions:
|
49
|
+
|
50
|
+
The above copyright notice and this permission notice shall be included in
|
51
|
+
all copies or substantial portions of the Software.
|
52
|
+
|
53
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
54
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
55
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
56
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
57
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
58
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
59
|
+
DEALINGS IN THE SOFTWARE.
|
60
|
+
|
61
|
+
If we meet some day, and you think this stuff is worth it, you can buy me a
|
62
|
+
beer in return.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/geopoint_factory/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Josh Avant"]
|
6
|
+
gem.email = ["joshavant@gmail.com"]
|
7
|
+
gem.description = %q{Generate geographic points from a shapefile.}
|
8
|
+
gem.summary = %q{Generate useful, RGeo-compatible geographic points located within a supplied shapefile.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "geopoint_factory"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = GeoPointFactory::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
|
21
|
+
gem.add_runtime_dependency 'rgeo'
|
22
|
+
gem.add_runtime_dependency 'rgeo-shapefile'
|
23
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "geopoint_factory/version"
|
2
|
+
require "rgeo/shapefile"
|
3
|
+
|
4
|
+
# USAGE NOTE:
|
5
|
+
# '#finalize' should be called, whenever you're done with an instance
|
6
|
+
# of GeoPointFactory::Generator
|
7
|
+
|
8
|
+
module GeoPointFactory
|
9
|
+
class Generator
|
10
|
+
# omit file extensions for `path_to_shapefile`
|
11
|
+
def initialize(path_to_shapefile)
|
12
|
+
@factory = RGeo::Geographic.simple_mercator_factory
|
13
|
+
@shapefile = RGeo::Shapefile::Reader.open(path_to_shapefile, :factory => @factory)
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate
|
17
|
+
begin
|
18
|
+
generated_point = random_point_in_bounding_box
|
19
|
+
end while !@shapefile.get(0).geometry.contains?(generated_point)
|
20
|
+
generated_point
|
21
|
+
end
|
22
|
+
|
23
|
+
def finalize
|
24
|
+
@shapefile.close
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def convert_to_rad(deg)
|
29
|
+
(deg / 360) * (2 * Math::PI)
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_to_deg(rad)
|
33
|
+
(rad * 360) / (2 * Math::PI)
|
34
|
+
end
|
35
|
+
|
36
|
+
def random_point_in_bounding_box
|
37
|
+
northlimit = convert_to_rad(@shapefile.ymax)
|
38
|
+
southlimit = convert_to_rad(@shapefile.ymin)
|
39
|
+
westlimit = convert_to_rad(@shapefile.xmin)
|
40
|
+
eastlimit = convert_to_rad(@shapefile.xmax)
|
41
|
+
|
42
|
+
# source: http://www.geomidpoint.com/random/calculation.html
|
43
|
+
# section: 'Rectangular region calculation detail'
|
44
|
+
rand_lat = Math.asin(rand * (Math.sin(northlimit) - Math.sin(southlimit)) + Math.sin(southlimit))
|
45
|
+
|
46
|
+
width = eastlimit - westlimit
|
47
|
+
|
48
|
+
if width < 0
|
49
|
+
width = width + (2 * Math::PI)
|
50
|
+
end
|
51
|
+
|
52
|
+
rand_lon = westlimit + width * rand
|
53
|
+
|
54
|
+
if rand_lon < -Math::PI
|
55
|
+
rand_lon = rand_lon + 2 * Math::PI
|
56
|
+
elsif rand_lon > Math::PI
|
57
|
+
rand_lon = rand_lon - 2 * Math::PI
|
58
|
+
end
|
59
|
+
|
60
|
+
rand_lat = convert_to_deg(rand_lat)
|
61
|
+
rand_lon = convert_to_deg(rand_lon)
|
62
|
+
|
63
|
+
@factory.point(rand_lon, rand_lat)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
PATH_TO_TEST_SHAPEFILE = "spec/support/sf_shapefile"
|
4
|
+
|
5
|
+
describe GeoPointFactory::Generator do
|
6
|
+
before(:all) do
|
7
|
+
@point_generator = GeoPointFactory::Generator.new(PATH_TO_TEST_SHAPEFILE)
|
8
|
+
@point = @point_generator.generate
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should generate a RGeo::Feature::Point module" do
|
12
|
+
RGeo::Feature::Point.check_type(@point).should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should generate random points with different locations" do
|
16
|
+
second_point = @point_generator.generate
|
17
|
+
@point.eql?(second_point).should be_false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should generate a point within the bounds of the supplied shapefile" do
|
21
|
+
factory = RGeo::Geographic.simple_mercator_factory
|
22
|
+
shapefile = RGeo::Shapefile::Reader.open(PATH_TO_TEST_SHAPEFILE, :factory => factory)
|
23
|
+
shapefile.get(0).geometry.contains?(@point).should be_true
|
24
|
+
end
|
25
|
+
|
26
|
+
after(:all) do
|
27
|
+
@point_generator.finalize
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'geopoint_factory'
|
Binary file
|
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geopoint_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josh Avant
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rgeo
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rgeo-shapefile
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Generate geographic points from a shapefile.
|
79
|
+
email:
|
80
|
+
- joshavant@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- LICENSE
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- geopoint_factory.gemspec
|
89
|
+
- lib/geopoint_factory.rb
|
90
|
+
- lib/geopoint_factory/version.rb
|
91
|
+
- spec/lib/geopoint_factory_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/support/sf_shapefile.dbf
|
94
|
+
- spec/support/sf_shapefile.shp
|
95
|
+
- spec/support/sf_shapefile.shx
|
96
|
+
homepage: ''
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.21
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Generate useful, RGeo-compatible geographic points located within a supplied
|
120
|
+
shapefile.
|
121
|
+
test_files:
|
122
|
+
- spec/lib/geopoint_factory_spec.rb
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
- spec/support/sf_shapefile.dbf
|
125
|
+
- spec/support/sf_shapefile.shp
|
126
|
+
- spec/support/sf_shapefile.shx
|