Sha256: bc4cef2f44bb932ffbd5e8cd1a55f79829cdbe90341ea4ad364505081fa2539b

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require 'open-uri'

module SpatialFeatures
  module Importers
    class File < SimpleDelegator
      INVALID_ARCHIVE = "Archive did not contain a .kml or .shp file. Supported formats are KMZ, KML, and zipped ArcGIS shapefiles.".freeze

      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

      # 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.
      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spatial_features-2.20.0 lib/spatial_features/importers/file.rb
spatial_features-2.19.1 lib/spatial_features/importers/file.rb
spatial_features-2.19.0 lib/spatial_features/importers/file.rb
spatial_features-2.18.0 lib/spatial_features/importers/file.rb
spatial_features-2.17.3 lib/spatial_features/importers/file.rb