Sha256: cb31be8fadae32320720eb69a5809be489fa6486e5523e754840e92c95f31817

Contents?: true

Size: 1.14 KB

Versions: 14

Compression:

Stored size: 1.14 KB

Contents

require 'ostruct'
require 'digest/md5'

module SpatialFeatures
  module Importers
    class Shapefile < Base
      def cache_key
        @cache_key ||= Digest::MD5.hexdigest(features.to_json)
      end

      private

      def each_record(&block)
        file = Download.open(@data, unzip: '.shp')
        proj4 = proj4_from_file(file)
        RGeo::Shapefile::Reader.open(file.path) do |records|
          records.each do |record|
            yield OpenStruct.new data_from_wkt(record.geometry.as_text, proj4).merge(:metadata => record.attributes) if record.geometry.present?
          end
        end
      end

      def proj4_from_file(file)
        # Sanitize "'+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs '\n"
        `gdalsrsinfo "#{file.path}" -o proj4`[/'(.+)'/,1].presence || raise('Could not determine shapefile projection. Check that `gdalsrsinfo` is installed.')
      end

      def data_from_wkt(wkt, proj4)
        ActiveRecord::Base.connection.select_one <<-SQL
          SELECT ST_Transform(ST_GeomFromText('#{wkt}'), '#{proj4}', 4326) AS geog, GeometryType(ST_GeomFromText('#{wkt}')) AS feature_type
        SQL
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spatial_features-2.5.2 lib/spatial_features/importers/shapefile.rb
spatial_features-2.5.1 lib/spatial_features/importers/shapefile.rb
spatial_features-2.5.0 lib/spatial_features/importers/shapefile.rb
spatial_features-2.4.3 lib/spatial_features/importers/shapefile.rb
spatial_features-2.4.2 lib/spatial_features/importers/shapefile.rb
spatial_features-2.4.1 lib/spatial_features/importers/shapefile.rb
spatial_features-2.4.0 lib/spatial_features/importers/shapefile.rb
spatial_features-2.3.1 lib/spatial_features/importers/shapefile.rb
spatial_features-2.3.0 lib/spatial_features/importers/shapefile.rb
spatial_features-2.2.4 lib/spatial_features/importers/shapefile.rb
spatial_features-2.2.3 lib/spatial_features/importers/shapefile.rb
spatial_features-2.2.2 lib/spatial_features/importers/shapefile.rb
spatial_features-2.2.1 lib/spatial_features/importers/shapefile.rb
spatial_features-2.2.0 lib/spatial_features/importers/shapefile.rb