Class: SpatialFeatures::Importers::File
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- SpatialFeatures::Importers::File
- Defined in:
- lib/spatial_features/importers/file.rb
Constant Summary collapse
- SUPPORTED_SPATIAL_FORMATS =
%w(application/zip application/x-shp+zip application/vnd.google-earth.kml+xml application/vnd.google-earth.kmz application/vnd.geo+json).freeze
- SUPPORTED_SPATIAL_FILE_EXTENSIONS =
%w(.zip .zap .piz .shpz .kml .kmz .json .geojson).freeze
- SUPPORTED_FORMATS =
"Supported formats are KMZ, KML, zipped ArcGIS shapefiles, ESRI JSON, and GeoJSON.".freeze
- FILE_PATTERNS =
[/\.kml$/, /\.shp$/, /\.json$/, /\.geojson$/]
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data, current_file: nil, **options) ⇒ File
constructor
The File importer may be initialized multiple times by ‘::create_all` if it receives ZIP data containing multiple KML or SHP files.
Constructor Details
#initialize(data, 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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/spatial_features/importers/file.rb', line 31 def initialize(data, current_file: nil, **) begin current_file ||= Download.open_each(data, unzip: FILE_PATTERNS, downcase: true, tmpdir: [:tmpdir]).first rescue Unzip::PathNotFound invalid_archive!(data) end filename = current_file.path.downcase case ::File.extname(filename) when '.kml' __setobj__(KMLFile.new(current_file, **)) when '.shp' __setobj__(Shapefile.new(current_file, **)) when '.json', '.geojson' __setobj__(ESRIGeoJSON.new(current_file.path, **)) else import_error!(filename) end end |
Class Method Details
.create_all(data, **options) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/spatial_features/importers/file.rb', line 17 def self.create_all(data, **) Download.open_each(data, unzip: FILE_PATTERNS, downcase: true, tmpdir: [:tmpdir]).map do |file| new(data, **, current_file: file) end rescue Unzip::PathNotFound invalid_archive!(data) end |
.invalid_archive!(filename) ⇒ Object
10 11 12 13 |
# File 'lib/spatial_features/importers/file.rb', line 10 def self.invalid_archive!(filename) filename = ::File.basename(filename.to_s) raise ImportError, "#{filename} did not contain a spatial file. #{SUPPORTED_FORMATS}" end |