Ruby on GeoTools で境界ボックスフィルタ

境界ボックスによる地球地図日本の道路

geotools.rb の Geo::Reader に境界ボックスフィルタを実装しました。
Geo::Reader#foreach の第二引数 options に、{:bbox => [136, 35, 137, 36]} のように境界ボックスを指定します。境界ボックスは要素数4の配列で、[xmin, ymin, xmax, ymax] の順番で記述してください。
現時点で options に使えるキーとその値をまとめると、以下のようになります。

キー
:sjis_workaround true のとき、Shift_JIS の主題属性を (UTF-8)文字列として戻すようにします。
:whitelist Ruby レベルまで取り出す属性名を配列として渡します。例 {:whitelist => ['exs]}
:bbox 取り出すデータの空間範囲を、[xmin, ymin, xmax, ymax] という配列で]指定します。例 {:bbox => [136 35 137 36]}

利用

以下のようなコードで、本エントリの画像のように、Shapefile の一部分を別の Shapefile に切り出すことができます。

require 'geotools'
src = '../transl_1_1.shp'

xmin = 136
ymin = 35
xmax = 137
ymax = 36

Geo::Writer::open('bbox_box.shp') do |w|
  w.write(Geo::bbox2polygon([xmin, ymin, xmax, ymax]))
end

Geo::Writer::open('bbox.shp') do |w|
  Geo::Reader::foreach(src, {:whitelist => [], :bbox => [xmin, ymin, xmax, ymax]}) do |geom, attrs|
    w.write(geom)
  end
end

境界にかかるデータも、特にはみ出し部分を取り除いたりすることなく、そのまま含まれます。

geotools.rb

現時点での geotools.rb の内容は、以下の通りです。境界ボックスフィルタを実装したほか、Geo::bbox2polygon などの、比較的自明なメソッドを追加したりしています。

# this code is under development and subject to major change.
require 'iconv'

module Geo
  # Geo::Tools module, to include nesessary classes from Geotools
  module Tools
    QUALIFIED_NAMES = %w{java.lang.System java.lang.String java.lang.Integer java.lang.Double java.lang.Long java.io.File java.util.HashMap org.geotools.data.shapefile.ShapefileDataStore org.geotools.feature.AttributeTypeFactory org.geotools.feature.FeatureTypeBuilder org.geotools.feature.type.GeometricAttributeType com.vividsolutions.jts.io.WKTReader org.geotools.referencing.crs.EPSGCRSAuthorityFactory org.geotools.referencing.operation.DefaultCoordinateOperationFactory org.geotools.geometry.DirectPosition2D org.geotools.data.vpf.file.VPFFile org.geotools.data.vpf.VPFLibrary org.geotools.gce.geotiff.GeoTiffWriter org.geotools.gce.geotiff.GeoTiffWriteParams org.geotools.coverage.grid.GridCoverageFactory com.vividsolutions.jts.geom.Envelope org.geotools.geometry.Envelope2D org.geotools.factory.Hints org.geotools.geometry.jts.JTS org.geotools.filter.text.cql2.CQL}
    begin
      require 'rjb'
      QUALIFIED_NAMES.each do |qn|
        sn = qn.split('.').last
        module_eval "#{sn} = Rjb::import('#{qn}')"
      end
      IMPLEMENTATION = 'rjb'
    rescue LoadError
      require 'java'
      QUALIFIED_NAMES.each do |qn|
        include_class qn
      end
      IMPLEMENTATION = 'java'
    end
  end

  # Geo module variables
  @@wkt_reader = nil
  @@epsg_crs_authority_factory = nil

  # Geo module 'good-wrapper' / 'Grossklasstum' classes
  class Reader
    if Tools::IMPLEMENTATION == 'java'
      print "DEBUG: jruby mode\n"
      def iterate
        while(@iter.hasNext)
          feat = @iter.next
          attrs = {}
          @attr_names.each do |attr_name|
            attrs[attr_name] = feat.getAttribute(attr_name)
          end
          yield feat.getDefaultGeometry, attrs
        end
      end
    else
      begin
        Rjb::primitive_conversion = true
        print "DEBUG: rjb primitive_conversion mode\n"
        def iterate
          while(@iter.hasNext)
            feat = @iter.next
            attrs = {}
            @attr_names.each do |attr_name|
              attr = feat.getAttribute(attr_name)
              if attr.respond_to?('_invoke') # java.lang.String を変換
                attr = attr.toString if attr.getClass == Geo::Tools::String
              end # 今のところ、これで遅くなっている
              attrs[attr_name] = attr
            end
            yield feat.getDefaultGeometry, attrs
          end
        end
      rescue NoMethodError
        print "DEBUG: rjb conventional mode\n"
        def iterate
          while(@iter.hasNext)
            feat = @iter.next
            attrs = {}
            @attr_names.each do |attr_name|
              attr = feat.getAttribute(attr_name)
              if attr.getClass.equals(Tools::Integer)
                attr = attr.intValue
              elsif attr.getClass.equals(Tools::Double)
                attr = attr.doubleValue
              elsif attr.getClass.equals(Tools::String)
                if @sjis_workaround
                  attr = Iconv.conv('UTF-8', 'Shift_JIS', attr.getBytes('iso-8859-1'))
                else
                  attr = attr.toString
                end
              elsif attr.getClass.equals(Tools::Long)
                attr = attr.longValue
              end
              attrs[attr_name] = attr
            end
            yield feat.getDefaultGeometry, attrs
          end
        end
      end
    end

    # keys for options: :sjis_workaround (boolean), :whitelist (array)
    # :bbox (array [xmin, ymin, xmax, ymax])
    def Reader::foreach(shapefile, options = {})
      r = Reader.new(shapefile, options)
      r.iterate do |geom, attrs|
        yield geom, attrs
      end
      r.close
    end

    def initialize(shapefile, options = {})
      @sjis_workaround = options[:sjis_workaround]
      @sjis_workaround = false if @sjis_workaround == nil
      if(Tools::IMPLEMENTATION == 'java' && @sjis_workaround)
        raise "sjis_workaround for JRuby is not implemented."
      end
      store = Tools::ShapefileDataStore.new(Tools::File.new(shapefile).toURL)
      if options.has_key?(:bbox)
        b = options[:bbox]
        f = Geo::Tools::CQL::toFilter("BBOX(the_geom, #{b[0]}, #{b[1]}, #{b[2]}, #{b[3]})") # TODO: NONE and ALL are warned to be already initialized constants [Rjb]
        @iter = store.getFeatureSource.getFeatures(f).features
      else
        @iter = store.getFeatureSource.getFeatures.features
      end
      feat_type = store.getFeatureSource.getSchema
      if options[:whitelist] == nil
        @attr_names = []
        feat_type.getAttributeCount.times do |i|
          name = feat_type.getAttributeType(i).getName
          @attr_names << name unless name == 'the_geom'
        end
      else
        @attr_names = options[:whitelist]
        #TODO: whitelist checking necessary?
      end
    end

    def close
      @iter.close
    end
  end

  class Writer
    def Writer::open(shapefile)
      w = Writer.new(shapefile)
      yield w
      w.close
    end

    def initialize(shapefile)
      @shapefile = shapefile
      @writer = nil
      @first = true
    end
    
    def setup(geom, attrs)
      attrs.delete('the_geom')
      ftb = Tools::FeatureTypeBuilder.newInstance(@shapefile)
      attrs.each do |key, value|
        if value.methods.include?('_classname')
          attr_class = value.getClass
        elsif value.class == String
          attr_class = Tools::String
        elsif value.class == Fixnum
          attr_class = Tools::Integer
        elsif value.class == Float
          attr_class = Tools::Double
        else
          raise "attribute #{key} has unrecognizable class #{value.class}"
        end
        ftb.addType(Tools::AttributeTypeFactory.newAttributeType(key, attr_class))
      end
      if geom.class == String
        geom = import_wkt_geometry(geom)
      end
      ftb.setDefaultGeometry(Tools::GeometricAttributeType.new('the_geom', geom.getClass, true, nil, nil, nil))
      ft = ftb.getFeatureType
      store = Tools::ShapefileDataStore.new(Tools::File.new(@shapefile).toURL)
      store.createSchema(ft)
      @writer = store.getFeatureWriter(@shapefile, store.getFeatureSource(@shapefile).getTransaction)
      @first = false
    end
    private :setup

    def write(geom, attrs = {})
      setup(geom, attrs) if @first
      feat = @writer.next
      if geom.class == String
        geom = import_wkt_geometry(geom)
      end
      feat.setDefaultGeometry(geom)
      attrs.each do |key, value|
        feat.setAttribute(key, value)
      end
      @writer.write
    end

    def close
      @writer.close unless @writer == nil
    end
  end

  class Transform
    def initialize(src_crs, dst_crs)
      cof = Geo::Tools::DefaultCoordinateOperationFactory.new
      @co = cof.createOperation(src_crs, dst_crs)
      @mt = @co.getMathTransform
    end

    def transform(x, y) # z?
      r = Geo::Tools::DirectPosition2D.new
      @mt.transform(Geo::Tools::DirectPosition2D.new(x, y), r)
      return r.x, r.y
    end

    def transform(geom)
      Geo::Tools::JTS::transform(geom, @mt)
    end

    ## TODO: accessor to @mt or @co
  end

  # create GridCoverage of buf which just fits bbox
  def Geo::create_grid_coverage(buf, crs, bbox)
    Geo::Tools::System::setProperty('com.apple.eawt.CocoaComponent.CompatibilityMode', 'false')
    f = Tools::GridCoverageFactory.new
    n_pix_x = buf[0].size
    n_pix_y = buf.size
    env = Tools::Envelope2D.new(crs, 
                                bbox[0] - (bbox[2] - bbox[0]) / n_pix_x / 2,
                                bbox[1] + (bbox[3] - bbox[1]) / n_pix_y / 2,
                                bbox[2] - bbox[0], bbox[3] - bbox[1])
    if Tools::IMPLEMENTATION == 'java'
      f.create('', buf.to_java(:float), env)
    else
      f._invoke('create', 'Ljava.lang.CharSequence;[[FLorg.opengis.geometry.Envelope;', '', buf, env)
    end
  end

  def Geo::write_grid_coverage(coverage, filename)
    w = Tools::GeoTiffWriter.new(Tools::File.new(filename))
    # p = Tools::GeoTiffWriteParams.new
    # p.setCompressionMode(Tools::GeoTiffWriteParams.MODE_EXPLICIT)
    # p.setCompressionType('LZW')
    # p.setCompressionQuality(0.75)
    # format = w.getFormat
    ### -> rt.java problem: no sun.jdbc.odbc.ee.DataSource for Mac OS X
    ### so, no compression for GeoTiff file
    # params = format.getWriteParameters
    # params.parameter(
    #  format.GEOTOOLS_WRITE_PARAMS.getName.toString).setValue(p)
    # w.write(coverage, params.values.toArray)
    w.write(coverage, nil)
  end

  # Geo module convenient methods
  def Geo::import_wkt_geometry(wkt)
    @@wkt_reader = Geo::Tools::WKTReader.new if @@wkt_reader == nil
    @@wkt_reader.read(wkt)
  end

  def Geo::import_epsg_crs(epsg_code)
    @@epsg_crs_authority_factory = Geo::Tools::EPSGCRSAuthorityFactory.new if @@epsg_crs_authority_factory == nil
    if epsg_code.class == Fixnum
      return @@epsg_crs_authority_factory.createCoordinateReferenceSystem("EPSG:#{epsg_code}")
    elsif epsg_code.class == String
      return @@epsg_crs_authority_factory.createCoordinateReferenceSystem(epsg_code)
    else
      raise "Geo::import_epsg_crs: can not handle epsg_code = #{epsg_code}"
    end
  end

  def Geo::bbox2polygon(b)
    Geo::import_wkt_geometry("POLYGON ((#{b[0]} #{b[1]}, #{b[2]} #{b[1]}, #{b[2]} #{b[3]}, #{b[0]} #{b[3]}, #{b[0]} #{b[1]}))")
  end
  def dms2dec(d, m, s)
    d + m / 60.0 + s / 3600.0
  end

  def dec2dms(dec)
    #TODO
    raise "not implemented."
  end
end

# ad hoc tests
if __FILE__ == $0
  ## TODO: better separate tests as unit tests.
  start_time = Time.now
  Geo::Writer.open('test.shp') do |w|
    Geo::Reader.foreach('transl_1_1.shp', {:whitelist => ['exs', 'soc'], :bbox => [135, 35, 136, 36]}) do |geom, attrs|
      #print "#{attrs.inspect}\n"
      w.write(geom, attrs)
    end
  end
  print "#{Time.now - start_time} sec.\n"
  
  exit # ここから下は別の話題
  ix = Geo::import_epsg_crs(2451)       # EPSG:2451 - 平面直角座標系 IX 系
  wgs84 = Geo::import_epsg_crs(4326)    # EPSG:4326 - WGS84
  
  t = Geo::Transform.new(ix, wgs84)     # IX 系から WGS84 への座標変換器
  t_inv = Geo::Transform.new(wgs84, ix) # WGS84 から IX 系への座標変換器
  
  pt = t.transform(0, 0)                 # IX 系の原点を WGS84 に座標変換
  pt_inv = t_inv.transform(pt[0], pt[1]) # その点を IX 系に戻す。元に戻るか?
  
  print "IX origin is #{pt.inspect} in WGS84\n"
  print "#{pt_inv.inspect} must be (0, 0)\n"
end

TODO

Rjb 版で :bbox 指定をすると、Geo::Tools::CQL::toFilter を実行するところで、「NONE と ALL はすでに初期化された定数である」という警告が出ます:

$ ruby bbox.rb
DEBUG: rjb primitive_conversion mode
/opt/local/lib/ruby/site_ruby/1.8/i686-darwin8.9.1/geotools.rb:108: warning: already initialized constant NONE
/opt/local/lib/ruby/site_ruby/1.8/i686-darwin8.9.1/geotools.rb:108: warning: already initialized constant ALL

JRuby ではこの警告は出ません。この原因はあとで調べるかもしれませんし、この警告を表示しないようにする姑息な手段を講じたりするかもしれません。