Guest User

Untitled

a guest
Jun 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #MODULE IS AT THE BOTTOM
  2.  
  3. class ShapeFile
  4.  
  5. include GeoRuby::Shp4r
  6. include PortableFiles
  7.  
  8. attr_accessor :files, :columns
  9. attr_reader :shape_file, :table_name
  10.  
  11. def initialize(options ={})
  12. @files = {}
  13. @files[:shp_file] = options[:shp_file]
  14. @files[:shx_file] = options[:shx_file]
  15. @files[:dbf_file]= options[:dbf_file]
  16. end
  17.  
  18. def read_columns
  19. @columns = Array.new
  20. each_column do |field|
  21. @columns << { :name => field.name.downcase, :type => field_type_to_rails(field.type) }
  22. end
  23. end
  24.  
  25. def import_data(fields = [])
  26. cl = classify
  27. ShpFile.open(self) do |shp|
  28. shp.each do |shape|
  29. record = cl.new
  30. shp.fields.each do |field|
  31. record[field.name.downcase] = shape.data[field.name] if fields.include? field.name.downcase or fields.blank?
  32. end
  33. record.the_geom = shape.geometry
  34. record.save
  35. end
  36. end
  37. end
  38.  
  39.  
  40. def each_column
  41. ShpFile.open(self) do |shp|
  42. shp.fields.each do |field|
  43. yield(field)
  44. end
  45. end
  46. end
  47.  
  48. private
  49.  
  50. def field_type_to_rails(type)
  51. case type
  52. when 'N' then :integer
  53. when 'F' then :float
  54. when 'D' then :date
  55. else
  56. :string
  57. end
  58. end
  59.  
  60. end
  61.  
  62.  
  63.  
  64.  
  65. module PortableFiles
  66.  
  67. attr_accessor :file_paths
  68.  
  69. #adds table prefix 'layer_' to each layer
  70. def table_name=(name)
  71. @table_name = "layer_table_#{name}"
  72. end
  73.  
  74. # adds the layer to the layers table
  75. def add_layer
  76. Layer.create(:name => @table_name, :table_name => @table_name.to_s, :geometric_column_name => "the_geom")
  77. end
  78.  
  79. def table_has_unique_name?
  80. ActiveRecord::Base.connection.execute("SELECT * FROM information_schema.tables WHERE \"table_name\" = '#{@table_name}';").to_a.size == 0
  81. end
  82.  
  83. def generate_table_name
  84. until table_has_unique_name? and @table_name
  85. self.table_name = ((rand*10000000000000).to_i).to_s
  86. end
  87. end
  88.  
  89. def create_table
  90. #empty block : the columns will be added afterwards
  91. unless @table_name.blank?
  92. ActiveRecord::Schema.create_table(@table_name){}
  93. @table_created = true
  94. end
  95. end
  96.  
  97. def create_table_structure
  98. if @table_created
  99. @columns.each do |column|
  100. begin
  101. ActiveRecord::Schema.add_column(@table_name, column[:name], column[:type])
  102. rescue
  103. puts "Couldnt add field #{column[:name].downcase}"
  104. end
  105. end
  106. ActiveRecord::Schema.add_column(table_name,"the_geom", :geometry,:null => false)
  107. ActiveRecord::Schema.add_index(table_name,"the_geom",:spatial => true)
  108. end
  109. end
  110.  
  111. def classify
  112. temp_table_name = self.table_name
  113. Class.new(ActiveRecord::Base) do
  114. set_table_name temp_table_name
  115. end
  116. end
  117.  
  118. # takes the contents of the files hash and copies them from temp files
  119. # and stores them in /temp_uploads. On upload, filename should get a random seed prefixed
  120. # to the filename, but i've added an extra 4 digit random seed to each filename just for
  121. # extra protextion. returns a hash with paths pointing to files as values. this is a workaround
  122. # because you cant store files in the session variable.
  123. def copy_files
  124. require 'ftools'
  125. @files.inject({}) do |result, file|
  126. seed = (rand * 1000).to_i
  127. File.syscopy(file[1].path, "temp_uploads/#{seed}" + file[1].path.split('/').last)
  128. result[file[0].to_sym] = "#{seed}#{file[1].path}"
  129. result
  130. end
  131. end
  132.  
  133. def clear_files
  134. @files = nil
  135. end
  136.  
  137. def prepare_for_session
  138. @file_paths = copy_files
  139. clear_files
  140. end
  141.  
  142. def load_from_session
  143. @files = {}
  144. @file_paths.each do |file|
  145. @files[file[0]] = File.new("temp_uploads/#{file[1]}")
  146. end
  147. end
  148.  
  149. end
Add Comment
Please, Sign In to add comment