Module: SpatialFeatures::FeatureImport

Extended by:
ActiveSupport::Concern
Includes:
QueuedSpatialProcessing
Defined in:
lib/spatial_features/has_spatial_features/feature_import.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

EMPTY_IMPORT_MESSAGE =

Read by submitters as often as by staff, so it says what happened rather than which method failed. Any per-file reasons are appended after it.

"No mapped areas could be imported.".freeze

Constants included from QueuedSpatialProcessing

QueuedSpatialProcessing::WARNINGS_CACHE_KEY

Instance Method Summary collapse

Methods included from QueuedSpatialProcessing

#clear_feature_update_error_status, #delay_update_features!, #failed_feature_update_jobs, #feature_update_error, #feature_update_warnings, #queue_update_spatial_cache, #queued_feature_update_jobs, #running_feature_update_jobs, #spatial_processing_jobs, #spatial_processing_status, #spatial_processing_status_cache, #store_feature_update_warnings, update_cached_status, #update_spatial_processing_status, #updating_features?, #updating_features_failed?

Instance Method Details

#update_features!(skip_invalid: false, allow_blank: false, force: false, **options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/spatial_features/has_spatial_features/feature_import.rb', line 27

def update_features!(skip_invalid: false, allow_blank: false, force: false, **options)
  options = options.reverse_merge(spatial_features_options)
  tmpdir = options.fetch(:tmpdir) { Dir.mktmpdir("ruby_spatial_features") }

  ActiveRecord::Base.transaction do
    imports = spatial_feature_imports(options[:import], options[:make_valid], tmpdir)
    cache_key = Digest::MD5.hexdigest(imports.collect(&:cache_key).join)

    return if !force && features_cache_key_matches?(cache_key)

    run_callbacks :update_features do
      features = import_features(imports, skip_invalid)
      update_features_cache_key(cache_key)
      update_features_area

      if options[:spatial_cache].present? && options[:queue_spatial_cache]
        queue_update_spatial_cache(options.slice(:spatial_cache))
      else
        update_spatial_cache(options.slice(:spatial_cache))
      end

      # Attribute each warning to the file it came from (e.g. `archive.zip/layer.kml`)
      # so a multi-file or multi-source import makes clear which file was affected.
      import_warnings = imports.flat_map do |import|
        import.warnings.map {|warning| [import.source_identifier.presence, warning].compact.join(': ') }
      end
      store_feature_update_warnings(import_warnings)

      if imports.present? && features.compact_blank.empty? && !allow_blank
        raise EmptyImportError, [EMPTY_IMPORT_MESSAGE, *import_warnings].join(' ')
      end
    end
  end

  return true
rescue StandardError => e
  raise e if e.is_a?(EmptyImportError)

  if skip_invalid
    Rails.logger.warn "Error updating #{self.class} #{self.id}. #{e.message}"
    return nil
  elsif ENCODING_ERROR.match?(e.message)
    raise ImportEncodingError,
          "One or more features you are trying to import has text encoded in an un-supported format (#{e.message})",
          e.backtrace
  else
    raise ImportError, e.message, e.backtrace
  end
ensure
  FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir)
end

#update_features_areaObject



85
86
87
88
89
# File 'lib/spatial_features/has_spatial_features/feature_import.rb', line 85

def update_features_area
  return unless has_attribute?(:features_area)
  self.features_area = features.area(cache: false)
  update_column :features_area, features_area unless new_record?
end

#update_features_cache_key(cache_key) ⇒ Object



79
80
81
82
83
# File 'lib/spatial_features/has_spatial_features/feature_import.rb', line 79

def update_features_cache_key(cache_key)
  return unless has_spatial_features_hash?
  self.features_hash = cache_key
  update_column(:features_hash, features_hash) unless new_record?
end

#update_spatial_cache(options = {}) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/spatial_features/has_spatial_features/feature_import.rb', line 91

def update_spatial_cache(options = {})
  options = options.reverse_merge(spatial_features_options)

  Array.wrap(options[:spatial_cache]).select(&:present?).each do |klass|
    SpatialFeatures.cache_record_proximity(self, klass.to_s.constantize)
  end
end