ogrinfo2migration 0.0.1
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 +7 -0
- data/bin/ogrinfo2migration +16 -0
- data/lib/ogrinfo2migration.rb +59 -0
- data/lib/tmpl.erb +11 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4a6d271af628573c0b5a1b1da9656dcae2f39539
|
4
|
+
data.tar.gz: 20f6373f36c490018b5a8b882829b42e184e79e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c31bdc560f50f7d57bbb57f23da1531442c69016f2a6aa99db345200d3a23b750c677685d5bccf8cf9cd619e8e85306aea096471b6930b07451890d8256d923
|
7
|
+
data.tar.gz: 1fe2c0f318f931db023de790dded2caaa2b2fda7b34d81eb716cd1b88ecf33f9ee5bbb61894c144df61f2d4ab6b66b1677c9e4d0830705cc5564d35297cc97d5
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "#{File.expand_path(File.dirname(__FILE__ ))}/../lib/ogrinfo2migration.rb"
|
4
|
+
|
5
|
+
caveats = <<-TXT
|
6
|
+
Usage:
|
7
|
+
ogrinfo2migration <input> <output>
|
8
|
+
TXT
|
9
|
+
if ARGV.length < 2
|
10
|
+
puts caveats
|
11
|
+
exit 1
|
12
|
+
end
|
13
|
+
input = ARGV[0]
|
14
|
+
output = ARGV[1]
|
15
|
+
f = Ogrinfo2Migration.new(input, output)
|
16
|
+
f.to_migration
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'erb'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
|
6
|
+
class Ogrinfo2Migration
|
7
|
+
RESERVED = [
|
8
|
+
"INFO",
|
9
|
+
"Geometry",
|
10
|
+
"Feature Count",
|
11
|
+
"Extent",
|
12
|
+
"Layer SRS WKT"
|
13
|
+
]
|
14
|
+
|
15
|
+
TYPES = {
|
16
|
+
"real" => "decimal"
|
17
|
+
}
|
18
|
+
|
19
|
+
attr_reader :info, :attributes, :wkt
|
20
|
+
|
21
|
+
def initialize(file, outdir)
|
22
|
+
@outdir = outdir
|
23
|
+
@info = %x(ogrinfo -so -al #{file}).split("\n")
|
24
|
+
@info.shift(3)
|
25
|
+
@attributes = @info.reduce(Hash.new(0)) do |memo, it|
|
26
|
+
if it =~ /:/ && it !~ Regexp.new("(#{RESERVED.join("|")})")
|
27
|
+
name, type = it.split(":")
|
28
|
+
name = name.downcase.gsub(/ /,"_")
|
29
|
+
type = type.gsub(/\([\d\.]+\)/,"").strip.downcase
|
30
|
+
type = TYPES[type] ? TYPES[type] : type
|
31
|
+
memo[name] = type
|
32
|
+
end
|
33
|
+
memo
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def wkt
|
38
|
+
# the SRS wkt are the only lines
|
39
|
+
# that aren't a key-value pair
|
40
|
+
wkt = @info.reject {|q| q =~ /:/ }.join("\n")
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_epsg
|
44
|
+
if wkt == "(unknown)"
|
45
|
+
@epsg = nil
|
46
|
+
return
|
47
|
+
end
|
48
|
+
j = JSON.parse(RestClient.get("http://prj2epsg.org/search.json?mode=wkt&terms=#{URI.encode(wkt)}"))
|
49
|
+
@epsg = j['codes'][0]['code']
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_migration
|
53
|
+
get_epsg
|
54
|
+
migration = ERB.new(File.open("#{File.expand_path(File.dirname(__FILE__))}/tmpl.erb",'r').read).result(binding)
|
55
|
+
File.open("#{@outdir}#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_add_#{@attributes["layer_name"]}.rb", "w") do |f|
|
56
|
+
f.write migration
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/tmpl.erb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class Add<%= @attributes["layer_name"].camelize %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :<%= @attributes["layer_name"].pluralize %> do |t|
|
4
|
+
<% @attributes.reject {|k,v| k == "layer_name" }.each do |name, type| %>
|
5
|
+
t.<%= type %> :<%= name %>
|
6
|
+
<% end %>
|
7
|
+
t.geometry :the_geom, limit: {:srid=><%= @epsg %>, :type=>"geometry"}
|
8
|
+
end
|
9
|
+
add_index :<%= @attributes["layer_name"].pluralize %>, ["the_geom"], :name => "<%= @attributes["layer_name"].pluralize %>_geometry_gist", :using => :gist
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ogrinfo2migration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Al Shaw
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ogrinfo2migration
|
14
|
+
email: almshaw@gmail.com
|
15
|
+
executables:
|
16
|
+
- ogrinfo2migration
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/ogrinfo2migration
|
21
|
+
- lib/ogrinfo2migration.rb
|
22
|
+
- lib/tmpl.erb
|
23
|
+
homepage: https://github.com/ashaw/ogrinfo2migration
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.5
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: ogrinfo2migration
|
47
|
+
test_files: []
|