Class: Shp2Wkt::Shp2Wkt

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

Overview

  • This class will convert a shapefile to WKT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Shp2Wkt

  • filename - The path to the shape file. E.g. /home/user/bangalore_roads.shp. The name is stored in @filename



17
18
19
20
21
# File 'lib/shp2wkt.rb', line 17

def initialize(filename)
  # absolute path to the shapefile e.g. /home/user/roads.shp
  @filename = filename
  read
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



13
14
15
# File 'lib/shp2wkt.rb', line 13

def filename
  @filename
end

Instance Method Details

#readObject

Will read the shapefile in @filename and convert each geometry in text_geom array as text.



24
25
26
27
28
29
30
31
32
33
# File 'lib/shp2wkt.rb', line 24

def read
  @text_geom = []
  RGeo::Shapefile::Reader.open(@filename) do |file|

    file.each do |record|
      @text_geom << record.geometry.as_text
    end
  end
  @text_geom
end

#writeObject

Will write the geometries in text_geom to a file. One geom per line.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shp2wkt.rb', line 36

def write

  outfile_filename = @filename.gsub(".shp", ".txt")

  begin
    File.open(outfile_filename, 'w') do |op|
      @text_geom.each do |geom|
        op.puts "#{geom}"
      end
    end
    @text_geom.length
  rescue
    raise Exception
  end
end