spatial_features 3.0.1 → 3.0.2

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
  SHA256:
3
- metadata.gz: 8a6a8cf38e1cf6d83445e02a7b3c7cfa6955480e9e00b3532402d5d859946700
4
- data.tar.gz: 0dedcbcd0eee00e0a0280fb0cd89bfd266d32d9bb4bafbafbd19b6566ad3c51f
3
+ metadata.gz: 57499f73e1190669ab1f3b5893c718db536fda282cce4a91bb0936908c5b2548
4
+ data.tar.gz: 0eb2e143c321b8e4e0dc498673d4a3c82e6d8d66df131a6ce445654a2f874772
5
5
  SHA512:
6
- metadata.gz: 80085428e855ef6db9d6b5536769c2bf0aeda8ded9a5b041d47b8446f92f97fc2aa3611388c4cf04f93af8043c5401be8af7a4f8121b029d20a3d308fc4db898
7
- data.tar.gz: 123f63f2096cc6d3ca335cd5c8344db46b915edefe9690ef9701f3c1c9c81619928f7403fb096553b790bdbbd97cd5694b40626a47ed7e34bbe717d83ae4c834
6
+ metadata.gz: 8dca234339ac5c209752e01ae0eea025f8ad88e51480b73d4d5ec3beed2ebd019c58f4c96ba56a25f0655611474eea396ee0da0be8d13ece5db0b279d5de2e27
7
+ data.tar.gz: 72f3fbceb533ffad03a849cf7d3caed587749d0d3d3a6830538beb59787588fc67803c965b3ad7fed10512d3cc3aecdc9878f45c8cbf52a1656746289b3e598d
@@ -2,13 +2,6 @@ require 'open-uri'
2
2
 
3
3
  module SpatialFeatures
4
4
  module Download
5
- # file can be a url, path, or file, any of which can return be a zipped archive
6
- def self.read(file, unzip: nil, **unzip_options)
7
- file = Download.open_each(file, unzip: unzip, **unzip_options).first
8
- path = ::File.path(file)
9
- return ::File.read(path)
10
- end
11
-
12
5
  # file can be a url, path, or file, any of which can return be a zipped archive
13
6
  def self.open(file)
14
7
  file = Kernel.open(file)
@@ -1,4 +1,5 @@
1
1
  require 'digest/md5'
2
+ require 'fileutils'
2
3
 
3
4
  module SpatialFeatures
4
5
  module FeatureImport
@@ -21,9 +22,10 @@ module SpatialFeatures
21
22
 
22
23
  def update_features!(skip_invalid: false, **options)
23
24
  options = options.reverse_merge(spatial_features_options)
25
+ tmpdir = options.fetch(:tmpdir) { Dir.mktmpdir("ruby_spatial_features") }
24
26
 
25
27
  ActiveRecord::Base.transaction do
26
- imports = spatial_feature_imports(options[:import], options[:make_valid])
28
+ imports = spatial_feature_imports(options[:import], options[:make_valid], options[:tmpdir])
27
29
  cache_key = Digest::MD5.hexdigest(imports.collect(&:cache_key).join)
28
30
 
29
31
  return if features_cache_key_matches?(cache_key)
@@ -49,6 +51,8 @@ module SpatialFeatures
49
51
  else
50
52
  raise ImportError, e.message, e.backtrace
51
53
  end
54
+ ensure
55
+ FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir)
52
56
  end
53
57
 
54
58
  def update_features_cache_key(cache_key)
@@ -73,10 +77,11 @@ module SpatialFeatures
73
77
 
74
78
  private
75
79
 
76
- def spatial_feature_imports(import_options, make_valid)
80
+ def spatial_feature_imports(import_options, make_valid, tmpdir)
77
81
  import_options.flat_map do |data_method, importer_name|
78
82
  Array.wrap(send(data_method)).flat_map do |data|
79
- spatial_importer_from_name(importer_name).create_all(data, :make_valid => make_valid) if data.present?
83
+ next unless data.present?
84
+ spatial_importer_from_name(importer_name).create_all(data, :make_valid => make_valid, :tmpdir => tmpdir)
80
85
  end
81
86
  end.compact
82
87
  end
@@ -5,10 +5,11 @@ module SpatialFeatures
5
5
  class Base
6
6
  attr_reader :errors
7
7
 
8
- def initialize(data, make_valid: false)
8
+ def initialize(data, make_valid: false, tmpdir: nil)
9
9
  @make_valid = make_valid
10
10
  @data = data
11
11
  @errors = []
12
+ @tmpdir = tmpdir
12
13
  end
13
14
 
14
15
  def features
@@ -8,7 +8,7 @@ module SpatialFeatures
8
8
 
9
9
  FILE_PATTERNS = [/\.kml$/, /\.shp$/, /\.json$/, /\.geojson$/]
10
10
  def self.create_all(data, **options)
11
- Download.open_each(data, unzip: FILE_PATTERNS, downcase: true).map do |file|
11
+ Download.open_each(data, unzip: FILE_PATTERNS, downcase: true, tmpdir: options[:tmpdir]).map do |file|
12
12
  new(data, **options, current_file: file)
13
13
  end
14
14
  rescue Unzip::PathNotFound
@@ -23,7 +23,7 @@ module SpatialFeatures
23
23
  # If no `current_file` is passed then we just take the first valid file that we find.
24
24
  def initialize(data, *args, current_file: nil, **options)
25
25
  begin
26
- current_file ||= Download.open_each(data, unzip: FILE_PATTERNS, downcase: true).first
26
+ current_file ||= Download.open_each(data, unzip: FILE_PATTERNS, downcase: true, tmpdir: options[:tmpdir]).first
27
27
  rescue Unzip::PathNotFound
28
28
  raise ImportError, INVALID_ARCHIVE
29
29
  end
@@ -16,21 +16,19 @@ module SpatialFeatures
16
16
  return Array(paths)
17
17
  end
18
18
 
19
- def self.extract(file_path, output_dir = Dir.mktmpdir, downcase: false)
19
+ def self.extract(file_path, tmpdir: nil, downcase: false)
20
+ tmpdir ||= Dir.mktmpdir
20
21
  [].tap do |paths|
21
22
  entries(file_path).each do |entry|
22
23
  next if entry.name =~ IGNORED_ENTRY_PATHS
23
24
  output_filename = entry.name
24
25
  output_filename = output_filename.downcase if downcase
25
- path = "#{output_dir}/#{output_filename}"
26
+ path = "#{tmpdir}/#{output_filename}"
26
27
  FileUtils.mkdir_p(File.dirname(path))
27
28
  entry.extract(path)
28
29
  paths << path
29
30
  end
30
31
  end
31
- rescue => e
32
- FileUtils.remove_entry(output_dir)
33
- raise(e)
34
32
  end
35
33
 
36
34
  def self.names(file_path)
@@ -1,3 +1,3 @@
1
1
  module SpatialFeatures
2
- VERSION = "3.0.1"
2
+ VERSION = "3.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spatial_features
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wallace
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-02-03 00:00:00.000000000 Z
12
+ date: 2022-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails