Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby script to get NorthSouthEastWest ends from shapefile.
#! /usr/local/bin/ruby
# coding: utf-8
#*********************************************
# Ruby script to get NorthSouthEastWest ends from shapefile.
#*********************************************
#
require 'georuby'
require 'geo_ruby/shp'
include GeoRuby::Shp4r
class Shp
def initialize
@shpfile = "/path/to/shapefile.shp"
end
def exec
# 北・南・東・西の緯度・経度初期値
n_end, s_end, e_end, w_end = [0, 0], [0, 90], [0, 0], [180, 0]
begin
# 東西南北端取得
ShpFile.open(@shpfile) do |shp|
shp.each do |s|
crds = s.geometry.as_json[:coordinates]
crds.each do |c_0|
c_0.each do |c_1|
c_1.each do |c|
e_end = c if c[0] > e_end[0]
w_end = c if c[0] < w_end[0]
n_end = c if c[1] > n_end[1]
s_end = c if c[1] < s_end[1]
end
end
end
end
end
# 結果出力
printf("North end: %8.4f%s, %7.4f%s\n", n_end[0], n_end[1])
printf("South end: %8.4f%s, %7.4f%s\n", s_end[0], s_end[1])
printf("East end: %8.4f%s, %7.4f%s\n", e_end[0], e_end[1])
printf("West end: %8.4f%s, %7.4f%s\n", w_end[0], w_end[1])
rescue => e
$stderr.puts "[ERROR][#{self.class.name}.#{__method__}] #{e}"
e.backtrace.each{ |trace| $stderr.puts "\t#{trace}" }
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
You can’t perform that action at this time.