Class: Shape2work::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/shape2work.rb

Instance Method Summary collapse

Instance Method Details

#convert_to(shapefile = nil, file_out = 'output', chosen_format = 'geojson') ⇒ Object

Parameters:

  • _chosen_format (string)
  • _file_out (string)
  • _shapefile (string)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shape2work.rb', line 11

def convert_to(shapefile = nil, file_out = 'output', chosen_format = 'geojson')
  unless shapefile.nil?
    RGeo::Shapefile::Reader.open(shapefile) do |file|
      open("#{file_out}.#{chosen_format}", 'w+') do |f|
        f.puts '{' + '"type":' + '"FeatureCollection"' + ','
        f.puts '"features":' + '['
        file.each do |record|
          f.puts '{ "type": "Feature",'
          f.puts '"id": ' + "#{record.index},"
          f.puts '"geometry":' + "#{walk_geometry(record.geometry)},"
          if file.next
            f.puts '"properties": ' + " #{record.attributes.to_json}},"
          else
            f.puts '"properties": ' + " #{record.attributes.to_json}}"
          end
        end
        f.puts ']}'
      end
    end
  end
end