Last active
August 29, 2015 14:06
-
-
Save komasaru/d28a1cb59696f91b198c to your computer and use it in GitHub Desktop.
Ruby to read a shapefile with georuby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#********************************************* | |
# 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