class SpatialFeatures::Importers::Shapefile
Public Class Methods
new(data, *args, proj4: nil, **options)
click to toggle source
Calls superclass method
SpatialFeatures::Importers::Base::new
# File lib/spatial_features/importers/shapefile.rb, line 9 def initialize(data, *args, proj4: nil, **options) super(data, *args, **options) @proj4 = proj4 end
Public Instance Methods
cache_key()
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 14 def cache_key @cache_key ||= Digest::MD5.file(archive).to_s end
Private Instance Methods
archive()
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 65 def archive @archive ||= Download.open(@data) end
data_from_wkt(wkt, proj4)
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 46 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
each_record() { |open_struct data_from_wkt(geometry.as_text, proj4_projection).merge(:metadata => attributes)| ... }
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 20 def each_record(&block) RGeo::Shapefile::Reader.open(file.path) do |records| records.each do |record| yield OpenStruct.new data_from_wkt(record.geometry.as_text, proj4_projection).merge(:metadata => record.attributes) if record.geometry.present? end end rescue Errno::ENOENT => e case e.message when /No such file or directory @ rb_sysopen - (.+)/ raise IncompleteShapefileArchive, "Shapefile archive is missing a required file: #{::File.basename($1)}" else raise e end end
file()
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 53 def file @file ||= begin validate_file! Download.open(archive, unzip: /\.shp$/, downcase: true) end end
proj4_from_file()
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 39 def proj4_from_file # Sanitize: "'+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs '\n" and lately # "+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs \n" to # "+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs" `gdalsrsinfo "#{file.path}" -o proj4`.strip.remove(/^'|'$/).presence end
proj4_projection()
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 35 def proj4_projection @proj4 ||= proj4_from_file || default_proj4_projection || raise(IndeterminateShapefileProjection, 'Could not determine shapefile projection. Check that `gdalsrsinfo` is installed.') end
validate_file!()
click to toggle source
# File lib/spatial_features/importers/shapefile.rb, line 60 def validate_file! return unless Unzip.is_zip?(archive) Validation.validate_shapefile_archive!(Download.entries(archive), default_proj4_projection: default_proj4_projection) end