gis_scraper 0.1.3.pre → 0.1.4.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d095719f299da91d96069b76373e675a37a81842
4
- data.tar.gz: 4d35174846e8bb601151e5a862e4ad39f40ee7e0
3
+ metadata.gz: 79913e4d0eaa973a3d7395a2a411253b7548aaaf
4
+ data.tar.gz: f9991565da85450b9cf9933ed693ab886023cda6
5
5
  SHA512:
6
- metadata.gz: 69eb9967e11ff58f9de54feb8032c6097a6e7855e817acf62451b08c3df378c1c7a7c7e579f790c35fb0dacf4a1db82d13eb6d3b1c7221a3b1cdb013be705e34
7
- data.tar.gz: b114a1f53b9fa97a0960e819f2ac26ae010dad9d417782451687f72f7884a4f950e0562f76ac27de09108809cda00d5c83cbe05d272d980fd393a09317869776
6
+ metadata.gz: 5d8f18966bcb062ffffe0aa594317322f9ffb1fea45db118f664b2df96cd896925fb444c10b6ad2dedad750f06fbb4480cdfb4f5573b1e517de98ef4467a5a91
7
+ data.tar.gz: 25d3d263e3be058d95849546f794fc2438cc05b7fdfde8bbc2c9068c1479c0b2fea6c53ce611031c1b1119214bb0482713ea62f4489dcbf07da71552ceb81693
@@ -15,7 +15,7 @@ class Layer
15
15
  class NoDatabase < StandardError; end
16
16
  class OgrMissing < StandardError; end
17
17
 
18
- attr_reader :type, :id, :name
18
+ attr_reader :type
19
19
 
20
20
  TYPES = ['Group Layer',
21
21
  'Feature Layer',
@@ -25,19 +25,19 @@ class Layer
25
25
 
26
26
  CONN = [:host, :port, :dbname, :user, :password] # PG connection options
27
27
 
28
- GEOM_TYPES = {esriGeometryPoint: 'POINT',
29
- esriGeometryMultipoint: 'MULTIPOINT',
30
- esriGeometryLine: 'LINESTRING',
31
- esriGeometryPolyline: 'MULTILINESTRING',
32
- esriGeometryPolygon: 'MULTIPOLYGON'}
28
+ GEOM_TYPES = {'esriGeometryPoint' => 'POINT',
29
+ 'esriGeometryMultipoint' => 'MULTIPOINT',
30
+ 'esriGeometryLine' => 'LINESTRING',
31
+ 'esriGeometryPolyline' => 'MULTILINESTRING',
32
+ 'esriGeometryPolygon' => 'MULTIPOLYGON'}
33
33
 
34
34
 
35
35
  OGR2OGR = 'ogr2ogr -f "PostgreSQL" PG:'
36
36
 
37
- def initialize(url, output_path = nil)
37
+ def initialize(url, path = nil)
38
38
  @conn_hash = CONN.zip(CONN.map { |key| GisScraper.config[key] }).to_h
39
39
  @url = url
40
- @output_path = output_path || config_path
40
+ @output_path = output_path(path) || config_path
41
41
  @ms_url = ms_url # map server url ending '../MapServer'
42
42
  @id = id
43
43
  @agent = Mechanize.new
@@ -46,22 +46,35 @@ class Layer
46
46
  @page_json = page_json
47
47
  @type = type
48
48
  @name = name
49
+ @sub_layer_ids = sub_layer_ids
49
50
  end
50
51
 
51
52
  def output_json
52
- QUERYABLE.any? { |l| @type == l } ? write_json_files : process_sub_layers
53
+ output(:json)
53
54
  end
54
55
 
55
56
  def output_to_db
56
57
  raise OgrMissing.new, 'ogr2ogr missing, is GDAL installed?' if !ogr2ogr?
57
58
  raise NoDatabase.new, "No db connection: #{@conn_hash.inspect}" if !db?
58
- @output_path = 'tmp' # write all files to the Gem's tmp dir
59
- output_json
60
- write_json_files_to_db_tables
59
+ output(:db)
61
60
  end
62
61
 
63
62
  private
64
63
 
64
+ def output(format) # recurses sub-layers :)
65
+ QUERYABLE.any? { |l| @type == l } ? method(format) : do_sub_layers(format)
66
+ end
67
+
68
+ def method(format)
69
+ return write_json if format == :json
70
+ return write_to_db if format == :db
71
+ raise "Unknown output format: #{format}"
72
+ end
73
+
74
+ def output_path(path)
75
+ File.expand_path(path) if path
76
+ end
77
+
65
78
  def db?
66
79
  PG.connect(@conn_hash) rescue nil
67
80
  end
@@ -104,31 +117,31 @@ class Layer
104
117
  type
105
118
  end
106
119
 
107
- def sub_layer_id_names
108
- @page_json['subLayers'] || []
120
+ def sub_layer_ids
121
+ @page_json['subLayers'].map { |hash| hash['id'] } || []
109
122
  end
110
123
 
111
124
  def json_data(url)
112
125
  FeatureScraper.new(url).json_data
113
126
  end
114
127
 
115
- def write_json_files
128
+ def write_json
116
129
  File.write "#{@output_path}/#{@name}.json", json_data("#{@ms_url}/#{@id}")
117
130
  end
118
131
 
119
- def write_json_files_to_db_tables
120
- files.each do |f|
121
- `#{OGR2OGR}"#{conn}" "#{f}" -nln #{base(f)} #{srs} -nlt #{geom(f)}`
122
- end
132
+ def write_to_db
133
+ @output_path = 'tmp'
134
+ write_json
135
+ `#{OGR2OGR}"#{conn}" "tmp/#{@name}.json" -nln #{table} #{srs} -nlt #{geom}`
123
136
  end
124
137
 
125
- def geom(file)
126
- esri = esri_geom(file)
127
- GEOM_TYPES[esri.to_sym] || raise("Unknown geometry type: '#{esri}'")
138
+ def geom
139
+ esri = esri_geom
140
+ GEOM_TYPES[esri] || raise("Unknown geometry: '#{esri}' for layer #{@name}")
128
141
  end
129
142
 
130
- def esri_geom(file)
131
- JSON.parse(File.read(file))['geometryType']
143
+ def esri_geom
144
+ @page_json['geometryType']
132
145
  end
133
146
 
134
147
  def srs
@@ -136,12 +149,8 @@ class Layer
136
149
  "-a_srs #{GisScraper.config[:srs]}" || ''
137
150
  end
138
151
 
139
- def base(full_file_name)
140
- full_file_name.split('/').last[0..-6].downcase
141
- end
142
-
143
- def files
144
- Dir.glob('tmp/**/*.json')
152
+ def table
153
+ Shellwords.escape @name.downcase
145
154
  end
146
155
 
147
156
  def conn
@@ -149,21 +158,14 @@ class Layer
149
158
  "host=#{host} port=#{port} dbname=#{db} user=#{user} password=#{pwd}"
150
159
  end
151
160
 
152
- def process_sub_layers
153
- sub_layer_id_names.each do |hash|
154
- name, id = hash['name'], hash['id']
155
- path = "#{@output_path}/#{name}"
156
- recurse_json sub_layer(id, path), path
157
- end
158
- end
159
-
160
- def recurse_json(layer, dir)
161
- FileUtils.mkdir dir
162
- layer.output_json
161
+ def do_sub_layers(format)
162
+ FileUtils.mkdir File.join(@output_path, @name) if format == :json
163
+ path = @output_path << "/#{@name}"
164
+ @sub_layer_ids.each { |n| sub_layer(n, path).send(:output, format) }
163
165
  end
164
166
 
165
167
  def sub_layer(id, path)
166
- Layer.new "#{@ms_url}/#{id}", path
168
+ Layer.new("#{@ms_url}/#{id}", path)
167
169
  end
168
170
 
169
171
  def replace_forwardslashes_with_underscores(string)
@@ -1,3 +1,3 @@
1
1
  module GisScraper
2
- VERSION = '0.1.3.pre'
2
+ VERSION = '0.1.4.pre'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gis_scraper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3.pre
4
+ version: 0.1.4.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruce Steedman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-29 00:00:00.000000000 Z
11
+ date: 2016-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler