gis_scraper 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.gitlab-ci.yml +30 -0
- data/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +20 -0
- data/Guardfile +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +124 -0
- data/Rakefile +8 -0
- data/bin/console +9 -0
- data/bin/setup +5 -0
- data/gis_scraper.gemspec +32 -0
- data/lib/gis_scraper/feature_scraper.rb +72 -0
- data/lib/gis_scraper/layer_writer.rb +175 -0
- data/lib/gis_scraper/version.rb +5 -0
- data/lib/gis_scraper.rb +40 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 825a965fbd6ae92b8189c58b90f011ce7cb7db08e0adb2a8d579c43a830acf20
|
4
|
+
data.tar.gz: 8b022521994f33f1e29eb4d75a1866d693c5e6f7b414bf2458ea970363f2a49f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f63e77607228561181b993c1a5cbace0142a785747dbce0d2650c1b64301a7f6d0cb7e5aed5d2ecebfeeeb39e7236017093a9884c0af4d1c8f51d31f7199e80f
|
7
|
+
data.tar.gz: 5bc0e6336c552d7d87f8194ead13f9866699f79f4e71aea0175bec78efe1a3d850661badd6eed41a1cd99657996dbb17dab36c6db286665760213aecf7d98e1e
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
---
|
2
|
+
before_script:
|
3
|
+
- apt-get update -qq
|
4
|
+
- apt-get install -y postgresql postgresql-client libpq-dev gdal-bin
|
5
|
+
- ogr2ogr --version
|
6
|
+
- psql -U postgres -h $PG_HOST -d postgres -c "SELECT PostGIS_Lib_version();"
|
7
|
+
- pg_lsclusters
|
8
|
+
- ruby -v
|
9
|
+
- which ruby
|
10
|
+
- gem install bundler
|
11
|
+
- bundle install --jobs $(nproc) "${FLAGS[@]}"
|
12
|
+
- export POSTGRES_HOST=mdillon__postgis
|
13
|
+
|
14
|
+
.job_template: &job_definition
|
15
|
+
image: ruby:3.3.3
|
16
|
+
|
17
|
+
services:
|
18
|
+
# - postgres:latest # must use host "postgres" to connect
|
19
|
+
- mdillon/postgis:11
|
20
|
+
|
21
|
+
variables:
|
22
|
+
PG_HOST: mdillon__postgis
|
23
|
+
POSTGRES_USER: postgres
|
24
|
+
|
25
|
+
test:
|
26
|
+
<<: *job_definition
|
27
|
+
timeout: 10m
|
28
|
+
script:
|
29
|
+
- bundle exec rake spec
|
30
|
+
- bundle exec rubocop
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.3
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
group :test, :development do
|
6
|
+
gem 'rubocop-rake', '~> 0.6', require: false
|
7
|
+
gem 'rubocop-rspec', '~> 3.0', require: false
|
8
|
+
end
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem 'bundler', '~> 2.1'
|
12
|
+
gem 'guard', '~> 2.18'
|
13
|
+
gem 'guard-rspec', '~> 4.7'
|
14
|
+
gem 'libnotify', '~> 0.9' # guard notifications
|
15
|
+
gem 'rake', '~> 13.0'
|
16
|
+
gem 'rspec', '~> 3.13'
|
17
|
+
gem 'rubocop', '~> 1.64'
|
18
|
+
end
|
19
|
+
|
20
|
+
gemspec
|
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
4
|
+
require 'guard/rspec/dsl'
|
5
|
+
dsl = Guard::RSpec::Dsl.new self
|
6
|
+
|
7
|
+
rspec = dsl.rspec
|
8
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
9
|
+
watch(rspec.spec_files)
|
10
|
+
|
11
|
+
# Ruby files
|
12
|
+
ruby = dsl.ruby
|
13
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
14
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Bruce Steedman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# gis_scraper Ruby Gem
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/gis_scraper.svg)](http://badge.fury.io/rb/gis_scraper)
|
3
|
+
[![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
|
4
|
+
|
5
|
+
Utility to recursively scrape ArcGIS MapServer data using the ArcGIS REST API.
|
6
|
+
|
7
|
+
ArcGIS MapServer REST queries are limited to 1,000 objects or another limit. This tool makes repeated calls until all data for a given layer (and all sub-layers) are extracted. Output can be GeoJSON file format or data may be written directly to Postgres database tables in PostGIS format. GIS clients - e.g. [QGIS](https://qgis.org) - can be configured to use vector layer data from PostGIS sources.
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
See `.gitlab-ci.yml` file for tested Ruby versions.
|
12
|
+
|
13
|
+
A Postgres database with the PostGIS extension enabled for database export.
|
14
|
+
|
15
|
+
For data import to a database [GDAL](http://gdal.org) must be installed and specifically the [ogr2ogr](http://www.gdal.org/ogr2ogr.html) executable must be available in your path.
|
16
|
+
|
17
|
+
## Known Limitations
|
18
|
+
|
19
|
+
*NIX systems only - Linux/Mac OS X. ArcGIS MapServer data is readable directly by ArcGIS Windows clients 😉
|
20
|
+
|
21
|
+
The following esri geometry types are so far supported:
|
22
|
+
|
23
|
+
- esriGeometryPoint, esriGeometryMultipoint, esriGeometryLine, esriGeometryPolyline, esriGeometryPolygon
|
24
|
+
|
25
|
+
Annotation layers are ignored, as are layers with no esri geometryType.
|
26
|
+
|
27
|
+
Currently the JSON data for a whole layer is held in memory before being output. For large layers - e.g. >100,000 objects - this can be multiple GB of memory. Is this causes a problem for you please add a comment to [issue #4](https://gitlab.com/matzfan/gis_scraper/issues/4).
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
gem 'gis_scraper'
|
35
|
+
```
|
36
|
+
|
37
|
+
And then execute:
|
38
|
+
|
39
|
+
$ bundle
|
40
|
+
|
41
|
+
Or install it yourself as:
|
42
|
+
|
43
|
+
$ gem install gis_scraper
|
44
|
+
|
45
|
+
## Configuration
|
46
|
+
|
47
|
+
Configuration options may be set via a hash or specified in a Yaml file. The following options are available:
|
48
|
+
|
49
|
+
- ```:threads``` Scraping is multi-threaded. The number of threads to use may be set with this option (default: 8)
|
50
|
+
- ```:output_path``` For JSON output, the path used to write files to (default: '~/Desktop')
|
51
|
+
|
52
|
+
The following options are used to connect to a database:
|
53
|
+
|
54
|
+
- ```:host``` (default: 'localhost')
|
55
|
+
- ```:port``` (default: 5432)
|
56
|
+
- ```:dbname``` (default: 'postgres')
|
57
|
+
- ```:user``` (default: 'postgres')
|
58
|
+
- ```:password``` (default: nil)
|
59
|
+
|
60
|
+
These additional options are available when using output to a database and are applied to the ```ogr2ogr``` command:
|
61
|
+
|
62
|
+
- ```:srs``` Used to overide the source spacial reference system. Currently only EPSG string format is valid - e.g. 'EPSG:3109' (default: no overide)
|
63
|
+
|
64
|
+
**To set via a hash**
|
65
|
+
|
66
|
+
```Ruby
|
67
|
+
GisScraper.configure(:threads => 16) # default is 8
|
68
|
+
```
|
69
|
+
|
70
|
+
**Using a Yaml configuration file**
|
71
|
+
|
72
|
+
```Ruby
|
73
|
+
GisScraper.configure_with 'path-to-Yaml-file'
|
74
|
+
```
|
75
|
+
|
76
|
+
```Ruby
|
77
|
+
GisScraper.config # returns the hash of configuration values
|
78
|
+
```
|
79
|
+
|
80
|
+
## Usage
|
81
|
+
|
82
|
+
A LayerWriter object must be instantiated with one required arg - a Service/Layer URL (ending in an integer representing the layer number). Example:
|
83
|
+
|
84
|
+
```Ruby
|
85
|
+
writer = LayerWriter.new(url: 'https://gps.digimap.gg/arcgis/rest/services/JerseyUtilities/JerseyUtilities/MapServer/0')
|
86
|
+
```
|
87
|
+
An optional second argument for the output path for JSON files may be specified. If so this overides the configuration option. Example:
|
88
|
+
```Ruby
|
89
|
+
writer = LayerWriter.new(url: 'https://gps.digimap.gg/arcgis/rest/services/JerseyUtilities/JerseyUtilities/MapServer/0', path: '~/Desktop')
|
90
|
+
```
|
91
|
+
The `gis_scraper` gem uses the `arcrest` Gem [README](https://gitlab.com/matzfan/arcrest) REST API to retrieve data from ArcGIS servers. A hash of arcrest options may be passed using the :arcrest_opts key:
|
92
|
+
```Ruby
|
93
|
+
writer = LayerWriter.new(url: 'https://gps.digimap.gg/arcgis/rest/services/JerseyUtilities/JerseyUtilities/MapServer/0', arcrest_opts: headers: { referer: 'https://some_referrer' })
|
94
|
+
```
|
95
|
+
|
96
|
+
**JSON output**
|
97
|
+
|
98
|
+
```Ruby
|
99
|
+
writer.output_json
|
100
|
+
```
|
101
|
+
|
102
|
+
If the layer is type 'Feature Layer', a single file of JSON data will be saved (named the same as the layer). If the layer is type 'Group Layer', the sub-group structure is traversed recursively thus: Directories for each sub-group layer are created and JSON data files for each constituent feature layer written to them.
|
103
|
+
|
104
|
+
**Output to a database**
|
105
|
+
|
106
|
+
Valid database config options must be set. The following command will convert JSON files, create tables for each layer (& sub-layers, if any) and import the data. Table names are lowercased, prefixed '_' and have spaces replaced with undescores. If a table with the same name exists the name is appended with '_'.
|
107
|
+
|
108
|
+
```Ruby
|
109
|
+
writer.output_to_db
|
110
|
+
```
|
111
|
+
|
112
|
+
## Specification and Tests
|
113
|
+
|
114
|
+
For the full specification clone this repo and run:
|
115
|
+
|
116
|
+
`bundle exec rake spec`
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports, pull requests (and feature requests) are welcome on GitLab at https://gitlab.com/matzfan/gis_scraper.
|
121
|
+
|
122
|
+
## License
|
123
|
+
|
124
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses)
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/gis_scraper.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'gis_scraper/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'gis_scraper'
|
10
|
+
spec.version = GisScraper::VERSION
|
11
|
+
spec.authors = ['matzfan']
|
12
|
+
|
13
|
+
spec.summary = 'Utility to scrape ArcGIS data'
|
14
|
+
spec.description = 'Scrape ArcGIS data from REST API and export to postgres db'
|
15
|
+
spec.homepage = 'https://gitlab.com/matzfan/gis_scraper'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
spec.required_ruby_version = '>= 3.3.3'
|
18
|
+
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://gitlab.com/matzfan/gis_scraper'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://gitlab.com/matzfan/gis_scraper/CHANGELOG.md'
|
22
|
+
spec.metadata = { 'rubygems_mfa_required' => 'true' }
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_runtime_dependency 'arcrest', '~> 1.1'
|
30
|
+
spec.add_runtime_dependency 'parallel', '~> 1.25'
|
31
|
+
spec.add_runtime_dependency 'pg', '~> 1.5'
|
32
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# scrapes feature layers
|
4
|
+
class FeatureScraper
|
5
|
+
class Ogr2ogrVersionError < StandardError; end
|
6
|
+
|
7
|
+
ESRIFIELDTYPEOID = 'esriFieldTypeOID'
|
8
|
+
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
def initialize(url:, arcrest_opts: {})
|
12
|
+
@url = url
|
13
|
+
@arcrest_opts = arcrest_opts # e.g. headers: { referer: '...' }
|
14
|
+
@layer = layer
|
15
|
+
@json = json
|
16
|
+
@name = @layer.name
|
17
|
+
@pk = pk
|
18
|
+
@max_record_count = max_record_count
|
19
|
+
@loops = loops
|
20
|
+
@threads = GisScraper.config[:threads]
|
21
|
+
end
|
22
|
+
|
23
|
+
def json_data
|
24
|
+
query_layer.merge('features' => all_features(@threads)).to_json
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def query_layer
|
30
|
+
@layer.query(where: '1=1')
|
31
|
+
end
|
32
|
+
|
33
|
+
def layer
|
34
|
+
ArcREST::Layer.new(@url, @arcrest_opts)
|
35
|
+
end
|
36
|
+
|
37
|
+
def json
|
38
|
+
@layer.json
|
39
|
+
end
|
40
|
+
|
41
|
+
def renderer
|
42
|
+
@layer.drawing_info['renderer']
|
43
|
+
end
|
44
|
+
|
45
|
+
def pk
|
46
|
+
@json['fields'].select { |f| f['type'] == ESRIFIELDTYPEOID }[0]['name']
|
47
|
+
end
|
48
|
+
|
49
|
+
def max_record_count
|
50
|
+
@layer.max_record_count
|
51
|
+
end
|
52
|
+
|
53
|
+
def count
|
54
|
+
@layer.count
|
55
|
+
end
|
56
|
+
|
57
|
+
def features(num)
|
58
|
+
@layer.features(where: where_text(num))
|
59
|
+
end
|
60
|
+
|
61
|
+
def all_features(threads)
|
62
|
+
Parallel.map(0...@loops, in_threads: threads) { |n| features(n) }.flatten
|
63
|
+
end
|
64
|
+
|
65
|
+
def loops
|
66
|
+
(count.to_f / @max_record_count).ceil
|
67
|
+
end
|
68
|
+
|
69
|
+
def where_text(num)
|
70
|
+
num ? "#{pk} > #{num * @max_record_count} AND #{pk} <= #{(num + 1) * @max_record_count}" : "#{pk} > 0"
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
require 'shellwords'
|
5
|
+
require 'tmpdir'
|
6
|
+
|
7
|
+
# tool to write ArcGIS layer(s) to json or database output
|
8
|
+
# rubocop:disable Metrics/ClassLength
|
9
|
+
class LayerWriter
|
10
|
+
attr_reader :type
|
11
|
+
|
12
|
+
GDAL = /GDAL (\d+\.\d+\.\d+)/
|
13
|
+
V1_11_4 = Gem::Version.new('1.11.4') # https://trac.osgeo.org/gdal/ticket/6529
|
14
|
+
TABLES = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"
|
15
|
+
TYPES = ['Group ', 'Feature ', 'Annotation ', 'Annotation Sub'].freeze
|
16
|
+
CONN = %i[host port dbname user password].freeze
|
17
|
+
GEOM_TYPES = { 'esriGeometryPoint' => 'POINT',
|
18
|
+
'esriGeometryMultipoint' => 'MULTIPOINT',
|
19
|
+
'esriGeometryLine' => 'LINESTRING',
|
20
|
+
'esriGeometryPolyline' => 'MULTILINESTRING',
|
21
|
+
'esriGeometryPolygon' => 'MULTIPOLYGON' }.freeze
|
22
|
+
OGR = 'ogr2ogr -overwrite -f "PostgreSQL" PG:'
|
23
|
+
|
24
|
+
def initialize(url:, path: nil, arcrest_opts: {})
|
25
|
+
@url = url
|
26
|
+
@output_path = output_path(path) || config_path
|
27
|
+
@arcrest_opts = arcrest_opts
|
28
|
+
@conn = conn
|
29
|
+
@id = id
|
30
|
+
@service_url = service_url
|
31
|
+
@layer = layer
|
32
|
+
@page_json = @layer.json
|
33
|
+
@type = layer_type
|
34
|
+
@name = name
|
35
|
+
end
|
36
|
+
|
37
|
+
def output_json
|
38
|
+
output(:json)
|
39
|
+
end
|
40
|
+
|
41
|
+
def output_to_db
|
42
|
+
raise 'ogr2ogr executable missing, is GDAL installed and in your PATH?' unless (v_string = ogr2ogr?)
|
43
|
+
raise 'ogr2ogr version must be > 1.11.4' unless Gem::Version.new(v_string.match(GDAL)[1]) > V1_11_4
|
44
|
+
|
45
|
+
output(:db)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def conn
|
51
|
+
CONN.zip(CONN.map { |key| GisScraper.config[key] }).to_h
|
52
|
+
end
|
53
|
+
|
54
|
+
def output(format)
|
55
|
+
@type == 'Feature Layer' ? _method(format) : do_sub_layers(format) # recurses sub-layers
|
56
|
+
end
|
57
|
+
|
58
|
+
def _method(format)
|
59
|
+
format == :db ? write_to_db : write_json
|
60
|
+
end
|
61
|
+
|
62
|
+
def output_path(path)
|
63
|
+
File.expand_path(path) if path
|
64
|
+
end
|
65
|
+
|
66
|
+
def connection
|
67
|
+
PG.connect @conn
|
68
|
+
end
|
69
|
+
|
70
|
+
def ogr2ogr?
|
71
|
+
`ogr2ogr --version`
|
72
|
+
rescue Errno::ENOENT
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def config_path
|
77
|
+
File.expand_path GisScraper.config[:output_path]
|
78
|
+
end
|
79
|
+
|
80
|
+
def service_url
|
81
|
+
@url.split('/')[0..-2].join('/')
|
82
|
+
end
|
83
|
+
|
84
|
+
def id
|
85
|
+
@url.split('/').last
|
86
|
+
end
|
87
|
+
|
88
|
+
def layer
|
89
|
+
ArcREST::Layer.new(@url, @arcrest_opts)
|
90
|
+
end
|
91
|
+
|
92
|
+
def layer_type
|
93
|
+
validate_layer @page_json['type']
|
94
|
+
end
|
95
|
+
|
96
|
+
def validate_layer(type)
|
97
|
+
raise "Bad Layer type: #{type}" unless TYPES.any? { |t| type == "#{t}Layer" }
|
98
|
+
|
99
|
+
type
|
100
|
+
end
|
101
|
+
|
102
|
+
def name
|
103
|
+
@page_json['name'].tr('/', '_') # make Postgres-safe
|
104
|
+
end
|
105
|
+
|
106
|
+
def sub_layer_ids
|
107
|
+
@page_json['subLayers'].map { |hash| hash['id'] } || []
|
108
|
+
end
|
109
|
+
|
110
|
+
def json_data
|
111
|
+
FeatureScraper.new(url: "#{@service_url}/#{@id}", arcrest_opts: @arcrest_opts).json_data
|
112
|
+
end
|
113
|
+
|
114
|
+
def write_json
|
115
|
+
File.write json_path, json_data
|
116
|
+
end
|
117
|
+
|
118
|
+
def json_path
|
119
|
+
"#{@output_path}/#{@name}.json"
|
120
|
+
end
|
121
|
+
|
122
|
+
def write_to_db
|
123
|
+
@output_path = Dir.mktmpdir('gis_scraper') # prefix for identification
|
124
|
+
write_json
|
125
|
+
`#{OGR}"#{conn_str}" "#{json_path}" -nln #{table} #{srs} -nlt #{pg_geom}`
|
126
|
+
ensure
|
127
|
+
FileUtils.remove_entry @output_path
|
128
|
+
end
|
129
|
+
|
130
|
+
def pg_geom
|
131
|
+
GEOM_TYPES[geo] || raise("Unknown geom: '#{geo}' for layer #{@name}")
|
132
|
+
end
|
133
|
+
|
134
|
+
def geo
|
135
|
+
@page_json['geometryType']
|
136
|
+
end
|
137
|
+
|
138
|
+
def srs
|
139
|
+
return '' unless GisScraper.config[:srs]
|
140
|
+
|
141
|
+
"-a_srs #{GisScraper.config[:srs]}" || ''
|
142
|
+
end
|
143
|
+
|
144
|
+
def tables
|
145
|
+
connection.exec(TABLES).map { |tup| tup['table_name'] } # list of current db table names
|
146
|
+
end
|
147
|
+
|
148
|
+
def table
|
149
|
+
table_name << table_suffix
|
150
|
+
end
|
151
|
+
|
152
|
+
def table_name
|
153
|
+
Shellwords.escape(@name.downcase.tr(' ', '_')).prepend('_')
|
154
|
+
end
|
155
|
+
|
156
|
+
def table_suffix
|
157
|
+
tables.any? { |t| t == table_name } ? '_' : ''
|
158
|
+
end
|
159
|
+
|
160
|
+
def conn_str
|
161
|
+
host, port, db, user, pwd = *@conn.values
|
162
|
+
"dbname='#{db}' host='#{host}' port='#{port}' user='#{user}' password='#{pwd}'"
|
163
|
+
end
|
164
|
+
|
165
|
+
def do_sub_layers(format)
|
166
|
+
FileUtils.mkdir File.join(@output_path, @name) if format == :json
|
167
|
+
path = @output_path << "/#{@name}"
|
168
|
+
sub_layer_ids.each { |n| sub_layer(n, path).send(:output, format) }
|
169
|
+
end
|
170
|
+
|
171
|
+
def sub_layer(id, path)
|
172
|
+
self.class.new(url: "#{@service_url}/#{id}", path: path, arcrest_opts: @arcrest_opts) # recurse
|
173
|
+
end
|
174
|
+
end
|
175
|
+
# rubocop:enable Metrics/ClassLength
|
data/lib/gis_scraper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'arcrest'
|
4
|
+
require 'parallel'
|
5
|
+
require 'pg'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
require 'gis_scraper/version'
|
9
|
+
require 'gis_scraper/feature_scraper'
|
10
|
+
require 'gis_scraper/layer_writer'
|
11
|
+
|
12
|
+
# stackoverflow.com/questions/6233124/where-to-place-access-config-file-in-gem
|
13
|
+
module GisScraper
|
14
|
+
@config = { threads: 8, output_path: '~/Desktop', host: ENV.fetch('PG_HOST', 'localhost'),
|
15
|
+
port: 5432, dbname: 'postgres', user: 'postgres', password: nil,
|
16
|
+
srs: nil }
|
17
|
+
@valid_keys = @config.keys
|
18
|
+
|
19
|
+
def self.configure(opts = {})
|
20
|
+
opts.each { |k, v| @config[k.to_sym] = v if @valid_keys.include? k.to_sym }
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configure_with(path_to_yaml_file)
|
24
|
+
begin
|
25
|
+
config = YAML.safe_load_file path_to_yaml_file
|
26
|
+
rescue Errno::ENOENT
|
27
|
+
puts "YAML configuration file couldn't be found. Using defaults"
|
28
|
+
return
|
29
|
+
rescue Psych::SyntaxError
|
30
|
+
puts 'YAML configuration file contains invalid syntax. Using defaults'
|
31
|
+
return
|
32
|
+
end
|
33
|
+
|
34
|
+
configure(config)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.config
|
38
|
+
@config
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gis_scraper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- matzfan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: arcrest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: parallel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.25'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.25'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
description: Scrape ArcGIS data from REST API and export to postgres db
|
56
|
+
email:
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- ".gitlab-ci.yml"
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".ruby-version"
|
66
|
+
- CHANGELOG.md
|
67
|
+
- Gemfile
|
68
|
+
- Guardfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- gis_scraper.gemspec
|
75
|
+
- lib/gis_scraper.rb
|
76
|
+
- lib/gis_scraper/feature_scraper.rb
|
77
|
+
- lib/gis_scraper/layer_writer.rb
|
78
|
+
- lib/gis_scraper/version.rb
|
79
|
+
homepage: https://gitlab.com/matzfan/gis_scraper
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
rubygems_mfa_required: 'true'
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 3.3.3
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubygems_version: 3.5.13
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Utility to scrape ArcGIS data
|
103
|
+
test_files: []
|