Skip to content

Instantly share code, notes, and snippets.

@komasaru

komasaru/geo_ruby.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Ruby to read a shapefile with georuby.
#*********************************************
# Ruby script to read shapefile.
#*********************************************
#
require 'geo_ruby'
require 'geo_ruby/shp4r/shp'
include GeoRuby::Shp4r
include GeoRuby::SimpleFeatures
class Shp
def initialize
@shpfile = "/path/to/shapefile.shp"
end
def exec
begin
ShpFile.open(@shpfile) do |shp|
fields = shp.fields
shp.each do |s|
datas = s.data
puts fields.map { |f| datas[f.name] }.join(",")
end
puts "---"
puts fields.map { |f| f.name }.join(",")
puts "---"
puts "Counts: #{shp.record_count}"
end
rescue => e
STDERR.puts "[ERROR][#{self.class.name}.#{__method__}] #{e}"
exit 1
end
end
end
Shp.new.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment