shp2db 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE +202 -0
- data/NOTICE +12 -0
- data/README.rdoc +74 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/bin/shp2db +205 -0
- data/lib/stdout.rb +35 -0
- data/shp2db.gemspec +70 -0
- data/test/helper.rb +18 -0
- data/test/test_shp2db.rb +7 -0
- metadata +156 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.2)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.8.7)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.5.2)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/NOTICE
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Copyright 2011 Ryan J. Geyer
|
2
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
you may not use this file except in compliance with the License.
|
4
|
+
You may obtain a copy of the License at
|
5
|
+
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
|
8
|
+
Unless required by applicable law or agreed to in writing, software
|
9
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
See the License for the specific language governing permissions and
|
12
|
+
limitations under the License.
|
data/README.rdoc
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
= shp2db
|
2
|
+
|
3
|
+
Shapefile to Database was born out of the necessity to convert the polygons found in TIGER/ESRI files into records in a relational database.
|
4
|
+
|
5
|
+
You can use this application to populate any database that Ruby {ActiveRecord}[http://ar.rubyonrails.org/] supports
|
6
|
+
|
7
|
+
== Database Schema
|
8
|
+
shp2db is intended to work with just about any reasonable database schema, but does make a few assumptions about your schema.
|
9
|
+
|
10
|
+
=== Prerequisites
|
11
|
+
* There are two databases
|
12
|
+
* A "shapes" database which will contain information about each polygon stored in the shapefile
|
13
|
+
* A "points" database which contains vertices for the polygons in the "shapes" database
|
14
|
+
* The "points" database has a foreign key reference to the "shapes" database. I.E. shapes_id
|
15
|
+
* The "points" database has 3 mandatory fields.
|
16
|
+
* longitude (float)
|
17
|
+
* latitude (float)
|
18
|
+
* ordering (int)
|
19
|
+
|
20
|
+
Beyond that, there aren't any hard requirements.
|
21
|
+
|
22
|
+
=== Table Names
|
23
|
+
The points and shapes database tables can have any name that works in your schema. By default shp2db assumes they are called "shapes" and "points", but you can override that by supplying the --db_shape_table and --db_point_table switches.
|
24
|
+
|
25
|
+
=== Shapes Table Options
|
26
|
+
shp2db will populate the following fields of the "shapes" table, if they exist;
|
27
|
+
* latitude_min (float)
|
28
|
+
* latitude_max (float)
|
29
|
+
* longitude_min (float)
|
30
|
+
* longitude_max (float)
|
31
|
+
* source (varchar(32)) - The name of the original shapefile
|
32
|
+
|
33
|
+
shp2db will also populate any fields in the "shapes" table which match the name of attributes found in the shapefile. You can compare the database schema for the "shapes" table against the attributes available in the shapefile by using the --fields_only switch.
|
34
|
+
|
35
|
+
Example Output:
|
36
|
+
Database Shapefile
|
37
|
+
name name
|
38
|
+
latitude_max
|
39
|
+
latitude_min
|
40
|
+
longitude_max
|
41
|
+
longitude_min
|
42
|
+
source
|
43
|
+
aiannhce
|
44
|
+
aiannhns
|
45
|
+
aiannhid
|
46
|
+
namelsad
|
47
|
+
lsad
|
48
|
+
comptyp
|
49
|
+
classfp
|
50
|
+
aiannhr
|
51
|
+
mtfcc
|
52
|
+
funcstat
|
53
|
+
aland
|
54
|
+
awater
|
55
|
+
intptlat
|
56
|
+
intptlon
|
57
|
+
|
58
|
+
In the above example, only the "name" attribute (in addition to the bounding box & source fields) for each shape in the shapefile will be stored. If we wanted to also store the "funcstat" value, we would just extend the schema of our "shapes" table to accomodate it.
|
59
|
+
|
60
|
+
== Contributing to shp2db
|
61
|
+
|
62
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
63
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
64
|
+
* Fork the project
|
65
|
+
* Start a feature/bugfix branch
|
66
|
+
* Commit and push until you are happy with your contribution
|
67
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
68
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
69
|
+
|
70
|
+
== Copyright
|
71
|
+
|
72
|
+
Copyright (c) 2011 Ryan J. Geyer. See LICENSE for
|
73
|
+
further details.
|
74
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "shp2db"
|
16
|
+
gem.homepage = "http://github.com/rgeyer/shp2db"
|
17
|
+
gem.license = "Apache2"
|
18
|
+
gem.summary = %Q{Converts TIGER shapefiles to relational databases using ActiveRecord}
|
19
|
+
gem.description = gem.summary
|
20
|
+
gem.email = "me@ryangeyer.com"
|
21
|
+
gem.authors = ["Ryan J. Geyer"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
gem.add_runtime_dependency 'shapelib', '>= 0.7.0'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "shp2db #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
rdoc.rdoc_files.include('bin/*')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/shp2db
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# Copyright 2011 Ryan J. Geyer
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'shapelib'
|
18
|
+
require 'optparse'
|
19
|
+
require 'active_record'
|
20
|
+
require 'yaml'
|
21
|
+
|
22
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
23
|
+
require 'stdout'
|
24
|
+
|
25
|
+
include Shapelib
|
26
|
+
|
27
|
+
options = {}
|
28
|
+
|
29
|
+
optparse = OptionParser.new do |opts|
|
30
|
+
# Set a banner, displayed at the top
|
31
|
+
# of the help screen.
|
32
|
+
opts.banner = "Usage: shp2db [options]"
|
33
|
+
options[:shapefile]
|
34
|
+
opts.on( '-f', '--shape_file FILE', 'Full path to a TIGER shapefile' ) do |shapefile|
|
35
|
+
options[:shapefile] = shapefile
|
36
|
+
end
|
37
|
+
options[:dbtype]
|
38
|
+
opts.on( '-a', '--db_adapter ADAPTER', 'The ActiveRecord db adapter type' ) do |dbtype|
|
39
|
+
options[:dbtype] = dbtype.downcase
|
40
|
+
end
|
41
|
+
options[:dbhost] = 'localhost'
|
42
|
+
opts.on( '-H', '--db_host HOSTNAME', 'The database hostname. Default: localhost' ) do |dbhost|
|
43
|
+
options[:dbhost] = dbhost
|
44
|
+
end
|
45
|
+
options[:dbname]
|
46
|
+
opts.on( '-n', '--db_name DBNAME', 'The database name') do |dbname|
|
47
|
+
options[:dbname] = dbname
|
48
|
+
end
|
49
|
+
options[:dbuser]
|
50
|
+
opts.on( '-u', '--db_user DBUSER', 'The database username') do |dbuser|
|
51
|
+
options[:dbuser] = dbuser
|
52
|
+
end
|
53
|
+
options[:dbpass] = nil
|
54
|
+
opts.on( '-p', '--db_pass DBPASS', 'The database password') do |dbpass|
|
55
|
+
options[:dbpass] = dbpass
|
56
|
+
end
|
57
|
+
options[:db_shape_table] = 'shapes'
|
58
|
+
opts.on( '-s', '--db_shape_table', 'The name of the table in the database which will store shape file information. Default: shapes') do |db_shape_table|
|
59
|
+
options[:db_shape_table] = db_shape_table
|
60
|
+
end
|
61
|
+
options[:db_point_table] = 'points'
|
62
|
+
opts.on( '-P', '--db_point_table', 'The name of the table in the database which will store shape vertices. Default: points') do |db_point_table|
|
63
|
+
options[:db_point_table] = db_point_table
|
64
|
+
end
|
65
|
+
options[:offset] = 0
|
66
|
+
opts.on( '-O', '--offset OFFSET', 'The offset (0 indexed) of the first shape to convert. Useful for chunking a very big shape file') do |offset|
|
67
|
+
options[:offset] = offset.to_i
|
68
|
+
end
|
69
|
+
options[:count] = nil
|
70
|
+
opts.on( '-C', '--count COUNT', 'The number of shapes to convert, starting at --offset. If --offset + --count is greater than the number of available shapes, only the available shapes in the range will be converted. Useful for chunking a very big shape file') do |count|
|
71
|
+
options[:count] = count.to_i
|
72
|
+
end
|
73
|
+
options[:fields_only] = false
|
74
|
+
opts.on( '-F', '--fields_only', 'Prints the fields in the database and the shapefile, and which fields intersect.') do |fields_only|
|
75
|
+
options[:fields_only] = true
|
76
|
+
end
|
77
|
+
opts.on( '-h', '--help', 'Display this screen' ) do
|
78
|
+
puts opts
|
79
|
+
exit
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
begin
|
84
|
+
optparse.parse!
|
85
|
+
raise OptionParser::MissingArgument if options[:shapefile].nil?
|
86
|
+
raise OptionParser::MissingArgument if options[:dbtype].nil?
|
87
|
+
raise OptionParser::MissingArgument if options[:dbname].nil?
|
88
|
+
raise OptionParser::MissingArgument if options[:dbuser].nil?
|
89
|
+
rescue Exception => e
|
90
|
+
STDERR.puts e
|
91
|
+
STDERR.puts optparse
|
92
|
+
exit(-1)
|
93
|
+
end
|
94
|
+
|
95
|
+
file_basename = File.basename(options[:shapefile])
|
96
|
+
|
97
|
+
puts "Loading Shapefile (#{file_basename})..."
|
98
|
+
|
99
|
+
spfile = Shapelib::ShapeFile::open(options[:shapefile], 'r')
|
100
|
+
|
101
|
+
puts "Establishing Database Connection - #{options[:dbname]} on #{options[:dbhost]} with adapter #{options[:dbtype]} identified by #{options[:dbuser]}"
|
102
|
+
ActiveRecord::Base.establish_connection(
|
103
|
+
:adapter => options[:dbtype],
|
104
|
+
:host => options[:dbhost],
|
105
|
+
:database => options[:dbname],
|
106
|
+
:username => options[:dbuser],
|
107
|
+
:password => options[:dbpass]
|
108
|
+
)
|
109
|
+
|
110
|
+
# Create the classes for activerecord
|
111
|
+
shpklass = Class.new(ActiveRecord::Base)
|
112
|
+
Object.const_set(options[:db_shape_table].capitalize, shpklass)
|
113
|
+
shpklass.class_eval do
|
114
|
+
has_many :points, :class_name => options[:db_point_table].capitalize
|
115
|
+
end
|
116
|
+
|
117
|
+
pntklass = Class.new(ActiveRecord::Base)
|
118
|
+
Object.const_set(options[:db_point_table].capitalize, pntklass)
|
119
|
+
|
120
|
+
# Fetch the available fields from the DB, and from the shapefile
|
121
|
+
db_shpattrs = shpklass.new().attribute_names().collect {|it| it.downcase}
|
122
|
+
file_shpattrs = spfile.fields.collect {|it| it.downcase}
|
123
|
+
|
124
|
+
# Just show the fields
|
125
|
+
if options[:fields_only]
|
126
|
+
stdoutlib = FormattedStdout.new(24)
|
127
|
+
stdoutlib.put_columns([{:value => 'Database', :ansi_codes => [4,1,34]}, {:value => 'Shapefile', :ansi_codes => [4,1,34]}])
|
128
|
+
|
129
|
+
# Intersect
|
130
|
+
(db_shpattrs & file_shpattrs).each do |attr|
|
131
|
+
stdoutlib.put_columns([
|
132
|
+
{:value => attr, :ansi_codes => [32]},
|
133
|
+
{:value => attr, :ansi_codes => [32]}
|
134
|
+
])
|
135
|
+
end
|
136
|
+
|
137
|
+
# DB Diff
|
138
|
+
(db_shpattrs - file_shpattrs).each do |attr|
|
139
|
+
stdoutlib.put_columns([
|
140
|
+
{:value => attr}
|
141
|
+
])
|
142
|
+
end
|
143
|
+
|
144
|
+
# Shapefile Diff
|
145
|
+
(file_shpattrs - db_shpattrs).each do |attr|
|
146
|
+
stdoutlib.put_columns([
|
147
|
+
{:value => ''},
|
148
|
+
{:value => attr}
|
149
|
+
])
|
150
|
+
end
|
151
|
+
|
152
|
+
exit 0
|
153
|
+
end
|
154
|
+
|
155
|
+
# Not really compatible with chunking..
|
156
|
+
|
157
|
+
# Check to see if we've already processed this source file, and bail if so
|
158
|
+
#if db_shpattrs.include?('source')
|
159
|
+
# if shpklass.where(:source => file_basename).count >= 1
|
160
|
+
# puts "Looks like you've already processed a file named #{file_basename}. Exiting..."
|
161
|
+
# exit 0
|
162
|
+
# end
|
163
|
+
#end
|
164
|
+
|
165
|
+
# Push them shapefiles into the DB
|
166
|
+
shape_idx = options[:offset]
|
167
|
+
range_end = spfile.size
|
168
|
+
range_end = options[:count] + options[:offset] if options[:count]
|
169
|
+
shape = spfile.read(shape_idx)
|
170
|
+
while (shape_idx < range_end && shape)
|
171
|
+
shapehash = shape.to_h
|
172
|
+
minbound = shape.minbound
|
173
|
+
maxbound = shape.maxbound
|
174
|
+
|
175
|
+
dbhash = {
|
176
|
+
:longitude_min => minbound[0],
|
177
|
+
:latitude_min => minbound[1],
|
178
|
+
:longitude_max => maxbound[0],
|
179
|
+
:latitude_max => maxbound[1],
|
180
|
+
:source => file_basename
|
181
|
+
}
|
182
|
+
|
183
|
+
shapehash.each do |key,val|
|
184
|
+
if db_shpattrs.include?(key.to_s.downcase)
|
185
|
+
dbhash[key.to_s.downcase] = val
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
dbhash.delete_if do |key,val|
|
190
|
+
!db_shpattrs.include?(key.to_s.downcase)
|
191
|
+
end
|
192
|
+
|
193
|
+
dbshape = shpklass.create(dbhash)
|
194
|
+
idx = 0
|
195
|
+
shape.each do |point|
|
196
|
+
pntklass.create("#{options[:db_shape_table]}_id" => dbshape.id, :latitude => point[0], :longitude => point[1], :ordering => idx)
|
197
|
+
idx += 1
|
198
|
+
end
|
199
|
+
shape = spfile.read
|
200
|
+
shape_idx += 1
|
201
|
+
end
|
202
|
+
|
203
|
+
puts "Converted #{range_end - options[:offset]}/#{spfile.size} shapes"
|
204
|
+
|
205
|
+
spfile.close
|
data/lib/stdout.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Copyright 2011 Ryan J. Geyer
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
# you may not use this file except in compliance with the License.
|
4
|
+
# You may obtain a copy of the License at
|
5
|
+
#
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
#
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
+
# See the License for the specific language governing permissions and
|
12
|
+
# limitations under the License.
|
13
|
+
|
14
|
+
class FormattedStdout
|
15
|
+
attr_accessor :column_width
|
16
|
+
|
17
|
+
def initialize(column_width = 0)
|
18
|
+
@column_width = column_width
|
19
|
+
end
|
20
|
+
|
21
|
+
def put_columns(col_hash_ary)
|
22
|
+
outval = ''
|
23
|
+
col_hash_ary.each do |column|
|
24
|
+
if column.include?(:ansi_codes)
|
25
|
+
outval += "\e["
|
26
|
+
outval += column[:ansi_codes].join(';')
|
27
|
+
outval += "m"
|
28
|
+
outval += "#{column[:value].ljust(@column_width)}\e[0m"
|
29
|
+
else
|
30
|
+
outval += "#{column[:value].ljust(@column_width)}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
puts outval
|
34
|
+
end
|
35
|
+
end
|
data/shp2db.gemspec
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{shp2db}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ryan J. Geyer"]
|
12
|
+
s.date = %q{2011-03-14}
|
13
|
+
s.default_executable = %q{shp2db}
|
14
|
+
s.description = %q{Converts TIGER shapefiles to relational databases using ActiveRecord}
|
15
|
+
s.email = %q{me@ryangeyer.com}
|
16
|
+
s.executables = ["shp2db"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE",
|
26
|
+
"NOTICE",
|
27
|
+
"README.rdoc",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"bin/shp2db",
|
31
|
+
"lib/stdout.rb",
|
32
|
+
"shp2db.gemspec",
|
33
|
+
"test/helper.rb",
|
34
|
+
"test/test_shp2db.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/rgeyer/shp2db}
|
37
|
+
s.licenses = ["Apache2"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.5.2}
|
40
|
+
s.summary = %q{Converts TIGER shapefiles to relational databases using ActiveRecord}
|
41
|
+
s.test_files = [
|
42
|
+
"test/helper.rb",
|
43
|
+
"test/test_shp2db.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
53
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
54
|
+
s.add_runtime_dependency(%q<shapelib>, [">= 0.7.0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
57
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
58
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
59
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
60
|
+
s.add_dependency(%q<shapelib>, [">= 0.7.0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
64
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
66
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
67
|
+
s.add_dependency(%q<shapelib>, [">= 0.7.0"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'shp2db'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/test_shp2db.rb
ADDED
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shp2db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ryan J. Geyer
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-14 00:00:00 -07:00
|
19
|
+
default_executable: shp2db
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :development
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
name: shoulda
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 23
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
version: 1.0.0
|
49
|
+
name: bundler
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
type: :development
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 7
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 5
|
63
|
+
- 2
|
64
|
+
version: 1.5.2
|
65
|
+
name: jeweler
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
type: :development
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
name: rcov
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
prerelease: false
|
83
|
+
type: :runtime
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
- 7
|
93
|
+
- 0
|
94
|
+
version: 0.7.0
|
95
|
+
name: shapelib
|
96
|
+
version_requirements: *id005
|
97
|
+
description: Converts TIGER shapefiles to relational databases using ActiveRecord
|
98
|
+
email: me@ryangeyer.com
|
99
|
+
executables:
|
100
|
+
- shp2db
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files:
|
104
|
+
- LICENSE
|
105
|
+
- README.rdoc
|
106
|
+
files:
|
107
|
+
- .document
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- LICENSE
|
111
|
+
- NOTICE
|
112
|
+
- README.rdoc
|
113
|
+
- Rakefile
|
114
|
+
- VERSION
|
115
|
+
- bin/shp2db
|
116
|
+
- lib/stdout.rb
|
117
|
+
- shp2db.gemspec
|
118
|
+
- test/helper.rb
|
119
|
+
- test/test_shp2db.rb
|
120
|
+
has_rdoc: true
|
121
|
+
homepage: http://github.com/rgeyer/shp2db
|
122
|
+
licenses:
|
123
|
+
- Apache2
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
hash: 3
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
version: "0"
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
hash: 3
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
version: "0"
|
147
|
+
requirements: []
|
148
|
+
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 1.5.2
|
151
|
+
signing_key:
|
152
|
+
specification_version: 3
|
153
|
+
summary: Converts TIGER shapefiles to relational databases using ActiveRecord
|
154
|
+
test_files:
|
155
|
+
- test/helper.rb
|
156
|
+
- test/test_shp2db.rb
|