osm-import 0.0.0 → 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.
data/lib/osm_import.rb
CHANGED
@@ -13,7 +13,7 @@ class OsmImport::Mapper::Type < OsmImport::Mapper::Base
|
|
13
13
|
|
14
14
|
def assigns
|
15
15
|
if @multi
|
16
|
-
{ :type => expression, :
|
16
|
+
{ :type => expression, :types => expression_multi }
|
17
17
|
else
|
18
18
|
{ :type => expression }
|
19
19
|
end
|
@@ -21,7 +21,7 @@ class OsmImport::Mapper::Type < OsmImport::Mapper::Base
|
|
21
21
|
|
22
22
|
def fields
|
23
23
|
if @multi
|
24
|
-
{ :type => 'VARCHAR(100) NOT NULL', :
|
24
|
+
{ :type => 'VARCHAR(100) NOT NULL', :types => 'VARCHAR(100)[]' }
|
25
25
|
else
|
26
26
|
{ :type => 'VARCHAR(100) NOT NULL' }
|
27
27
|
end
|
@@ -29,7 +29,7 @@ class OsmImport::Mapper::Type < OsmImport::Mapper::Base
|
|
29
29
|
|
30
30
|
def indexes
|
31
31
|
if @multi
|
32
|
-
{ :type => "BTREE(type)", :
|
32
|
+
{ :type => "BTREE(type)", :types => "GIN(types)" }
|
33
33
|
else
|
34
34
|
{ :type => "BTREE(type)" }
|
35
35
|
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'osm_import'
|
2
|
+
require 'pg'
|
3
|
+
|
4
|
+
class OsmImport::Target::PgBase < Struct.new(:options)
|
5
|
+
|
6
|
+
def prefix
|
7
|
+
options[:prefix] or 'osm_'
|
8
|
+
end
|
9
|
+
|
10
|
+
def raw_prefix
|
11
|
+
options[:raw_prefix] or 'raw_osm_'
|
12
|
+
end
|
13
|
+
|
14
|
+
def new_prefix
|
15
|
+
options[:new_prefix] or 'new_osm_'
|
16
|
+
end
|
17
|
+
|
18
|
+
def projection
|
19
|
+
options[:projection]
|
20
|
+
end
|
21
|
+
|
22
|
+
GEOTYPES = { 'point' => 'POINT', 'line' => 'LINESTRING', 'polygon' => 'POLYGON', 'multiline' => 'MULTILINESTRING', 'multipolygon' => 'MULTIPOLYGON' }
|
23
|
+
|
24
|
+
def geometry_type(type)
|
25
|
+
GEOTYPES[type.to_s] or raise StandardError.new("Unknown geometry type #{type.inspect}")
|
26
|
+
end
|
27
|
+
|
28
|
+
class Connection < Struct.new(:options)
|
29
|
+
|
30
|
+
def initialize(*args)
|
31
|
+
super
|
32
|
+
@conn = PG.connect options[:pg]
|
33
|
+
end
|
34
|
+
|
35
|
+
def exec(q)
|
36
|
+
puts " #{q}"
|
37
|
+
@conn.exec q
|
38
|
+
end
|
39
|
+
|
40
|
+
def close
|
41
|
+
@conn.close
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
class TargetEntry
|
47
|
+
|
48
|
+
attr_reader :target, :conn, :table, :type_mapping, :types_mapping, :name_mapping, :fields, :conditions, :assigns
|
49
|
+
|
50
|
+
def initialize(target, conn, table)
|
51
|
+
@target = target
|
52
|
+
@conn = conn
|
53
|
+
@table = table
|
54
|
+
|
55
|
+
@type_mapping = table.type_mapper.expression
|
56
|
+
@types_mapping = table.type_mapper.expression_multi
|
57
|
+
@name_mapping = "NULLIF(COALESCE(src.tags->'name:ru', src.tags->'name', src.tags->'int_name'), '')"
|
58
|
+
|
59
|
+
@fields = table.type_mapper.fields.merge :id => 'INT8 PRIMARY KEY', :name => 'VARCHAR(255)', :tags => 'HSTORE', :osm_type => 'VARCHAR(10)'
|
60
|
+
@assigns = table.type_mapper.assigns.merge :id => osm_id_expr, :name => name_mapping, :tags => 'src.tags', :osm_type => osm_type_expr
|
61
|
+
@conditions = table.type_mapper.conditions
|
62
|
+
|
63
|
+
table.mappers.each do |key, mapper|
|
64
|
+
@fields.merge! mapper.fields
|
65
|
+
@assigns.merge! mapper.assigns
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def osm_id_expr
|
70
|
+
"ABS(src.osm_id)"
|
71
|
+
end
|
72
|
+
|
73
|
+
def osm_type_expr(t = table.type)
|
74
|
+
"CASE WHEN src.osm_id < 0 THEN 'relation' ELSE '#{if t.to_s == 'point' then 'node' else 'way' end}' END"
|
75
|
+
end
|
76
|
+
|
77
|
+
def geometry_expr
|
78
|
+
if table.type =~ /^multi/
|
79
|
+
'ST_Multi(way)'
|
80
|
+
else
|
81
|
+
'way'
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def name
|
86
|
+
"#{target.new_prefix}#{table.name}"
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -1,23 +1,9 @@
|
|
1
1
|
require 'osm_import'
|
2
|
-
require 'pg'
|
3
2
|
|
4
|
-
class OsmImport::Target::
|
5
|
-
|
6
|
-
def projection
|
7
|
-
options[:projection]
|
8
|
-
end
|
9
|
-
|
10
|
-
def prefix
|
11
|
-
'osm_'
|
12
|
-
end
|
13
|
-
|
14
|
-
def new_prefix
|
15
|
-
'new_osm_'
|
16
|
-
end
|
3
|
+
class OsmImport::Target::PgTables < OsmImport::Target::PgBase
|
17
4
|
|
18
5
|
def import(schema)
|
19
6
|
conn = Connection.new options
|
20
|
-
|
21
7
|
conn.exec "BEGIN TRANSACTION"
|
22
8
|
|
23
9
|
puts "Dropping old tables:"
|
@@ -51,60 +37,7 @@ class OsmImport::Target::Pg < Struct.new(:options)
|
|
51
37
|
conn.close
|
52
38
|
end
|
53
39
|
|
54
|
-
|
55
|
-
|
56
|
-
def geometry_type(type)
|
57
|
-
GEOTYPES[type.to_s] or raise StandardError.new("Unknown geometry type #{type.inspect}")
|
58
|
-
end
|
59
|
-
|
60
|
-
class Connection < Struct.new(:options)
|
61
|
-
|
62
|
-
def initialize(*args)
|
63
|
-
super
|
64
|
-
@conn = PG.connect options[:pg]
|
65
|
-
end
|
66
|
-
|
67
|
-
def exec(q)
|
68
|
-
puts " #{q}"
|
69
|
-
@conn.exec q
|
70
|
-
end
|
71
|
-
|
72
|
-
def close
|
73
|
-
@conn.close
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
class TargetTable
|
79
|
-
|
80
|
-
attr_reader :target, :conn, :table, :type_mapping, :type_array_mapping, :name_mapping, :fields, :conditions, :assigns
|
81
|
-
|
82
|
-
def initialize(target, conn, table)
|
83
|
-
@target = target
|
84
|
-
@conn = conn
|
85
|
-
@table = table
|
86
|
-
|
87
|
-
@type_mapping = table.type_mapper.expression
|
88
|
-
@type_array_mapping = table.type_mapper.expression_multi
|
89
|
-
@name_mapping = "NULLIF(COALESCE(src.tags->'name:ru', src.tags->'name', src.tags->'int_name'), '')"
|
90
|
-
|
91
|
-
@fields = table.type_mapper.fields.merge :id => 'INT8 PRIMARY KEY', :name => 'VARCHAR(255)', :tags => 'HSTORE', :osm_type => 'VARCHAR(10)'
|
92
|
-
@assigns = table.type_mapper.assigns.merge :id => osm_id_expr, :name => name_mapping, :tags => 'src.tags', :osm_type => osm_type_expr
|
93
|
-
@conditions = table.type_mapper.conditions
|
94
|
-
|
95
|
-
table.mappers.each do |key, mapper|
|
96
|
-
@fields.merge! mapper.fields
|
97
|
-
@assigns.merge! mapper.assigns
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def osm_id_expr
|
102
|
-
"ABS(src.osm_id)"
|
103
|
-
end
|
104
|
-
|
105
|
-
def osm_type_expr(t = table.type)
|
106
|
-
"CASE WHEN src.osm_id < 0 THEN 'relation' ELSE '#{if t.to_s == 'point' then 'node' else 'way' end}' END"
|
107
|
-
end
|
40
|
+
class TargetTable < TargetEntry
|
108
41
|
|
109
42
|
def add_geometry_column(column, type)
|
110
43
|
if target.projection
|
@@ -114,10 +47,6 @@ class OsmImport::Target::Pg < Struct.new(:options)
|
|
114
47
|
end
|
115
48
|
end
|
116
49
|
|
117
|
-
def name
|
118
|
-
"#{target.new_prefix}#{table.name}"
|
119
|
-
end
|
120
|
-
|
121
50
|
def create!
|
122
51
|
conn.exec "CREATE TABLE #{name}(#{fields.map{|k,v| "#{k} #{v}"}.join(', ')})"
|
123
52
|
add_geometry_column :geometry, table.type
|
@@ -148,12 +77,6 @@ class OsmImport::Target::Pg < Struct.new(:options)
|
|
148
77
|
|
149
78
|
def import!
|
150
79
|
unless table.type_mapper.mappings.empty?
|
151
|
-
if table.type =~ /^multi/
|
152
|
-
geometry_expr = 'ST_Multi(way)'
|
153
|
-
else
|
154
|
-
geometry_expr = 'way'
|
155
|
-
end
|
156
|
-
|
157
80
|
field_keys = fields.keys.to_a
|
158
81
|
conn.exec "INSERT INTO #{name}(geometry, #{field_keys.join(', ')}) SELECT #{geometry_expr} AS geometry, #{field_keys.map{|k| "#{assigns[k]} AS #{k}"}.join(',')} FROM raw_osm_#{table.type.gsub('multi','')} src WHERE #{conditions.join(' AND ')}"
|
159
82
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'osm_import'
|
2
|
+
|
3
|
+
class OsmImport::Target::PgViews < OsmImport::Target::PgBase
|
4
|
+
|
5
|
+
def new_prefix
|
6
|
+
prefix
|
7
|
+
end
|
8
|
+
|
9
|
+
def import(schema)
|
10
|
+
conn = Connection.new options
|
11
|
+
conn.exec "BEGIN TRANSACTION"
|
12
|
+
|
13
|
+
puts "Preparing target views"
|
14
|
+
tvs = schema.tables.map{|table| TargetView.new self, conn, table }
|
15
|
+
|
16
|
+
puts "Dropping old views"
|
17
|
+
tvs.each(&:drop!)
|
18
|
+
|
19
|
+
puts "Creating views"
|
20
|
+
tvs.each(&:create!)
|
21
|
+
|
22
|
+
puts "Restoring geometry columns"
|
23
|
+
conn.exec "TRUNCATE geometry_columns"
|
24
|
+
conn.exec "SELECT probe_geometry_columns()"
|
25
|
+
|
26
|
+
puts "Commiting"
|
27
|
+
conn.exec "COMMIT"
|
28
|
+
conn.close
|
29
|
+
end
|
30
|
+
|
31
|
+
class TargetView < TargetEntry
|
32
|
+
|
33
|
+
def drop!
|
34
|
+
conn.exec "DROP VIEW IF EXISTS #{name}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def create!
|
38
|
+
return if table.type_mapper.mappings.empty?
|
39
|
+
|
40
|
+
conn.exec "CREATE VIEW #{name} AS SELECT #{geometry_expr} AS geometry, #{fields.keys.map{|k| "#{assigns[k]} AS #{k}"}.join(',')} FROM #{target.raw_prefix}#{table.type.gsub('multi','')} src WHERE #{conditions.join(' AND ')}"
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/lib/osm_import/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: osm-import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alexey Noskov
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-06-17 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: pg
|
@@ -67,7 +67,9 @@ files:
|
|
67
67
|
- lib/osm_import/mapper/address.rb
|
68
68
|
- lib/osm_import/version.rb
|
69
69
|
- lib/osm_import/target.rb
|
70
|
-
- lib/osm_import/target/
|
70
|
+
- lib/osm_import/target/pg_tables.rb
|
71
|
+
- lib/osm_import/target/pg_base.rb
|
72
|
+
- lib/osm_import/target/pg_views.rb
|
71
73
|
- lib/osm_import/schema.rb
|
72
74
|
- lib/osm_import.rb
|
73
75
|
- MIT-LICENSE
|