Class: SpatialFeatures::Importers::File

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/spatial_features/importers/file.rb

Constant Summary collapse

INVALID_ARCHIVE =
"Archive did not contain a .kml or .shp file. Supported formats are KMZ, KML, and zipped ArcGIS shapefiles.".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, *args, current_file: nil, **options) ⇒ File

The File importer may be initialized multiple times by ‘::create_all` if it receives ZIP data containing multiple KML or SHP files. We use `current_file` to distinguish which file in the archive is currently being processed.

If no ‘current_file` is passed then we just take the first valid file that we find.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/spatial_features/importers/file.rb', line 22

def initialize(data, *args, current_file: nil, **options)
  begin
    current_file ||= Download.open_each(data, unzip: [/\.kml$/, /\.shp$/], downcase: true).first
  rescue Unzip::PathNotFound
    raise ImportError, INVALID_ARCHIVE
  end

  case ::File.extname(current_file.path.downcase)
  when '.kml'
    __setobj__(KMLFile.new(current_file, *args, **options))
  when '.shp'
    __setobj__(Shapefile.new(current_file, *args, **options))
  else
    raise ImportError, "Could not import file. Supported formats are KMZ, KML, and zipped ArcGIS shapefiles"
  end
end

Class Method Details

.create_all(data, **options) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/spatial_features/importers/file.rb', line 8

def self.create_all(data, **options)
  Download.open_each(data, unzip: [/\.kml$/, /\.shp$/], downcase: true).map do |file|
    new(data, **options, current_file: file)
  end
rescue Unzip::PathNotFound
  raise ImportError, INVALID_ARCHIVE
end