Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/d28a1cb59696f91b198c to your computer and use it in GitHub Desktop.
Save komasaru/d28a1cb59696f91b198c to your computer and use it in GitHub Desktop.
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