lib/rgeo/geos/factory.rb in rgeo-0.3.3 vs lib/rgeo/geos/factory.rb in rgeo-0.3.4

- old
+ new

@@ -1,26 +1,26 @@ # ----------------------------------------------------------------------------- -# +# # GEOS factory implementation -# +# # ----------------------------------------------------------------------------- -# Copyright 2010 Daniel Azuma -# +# Copyright 2010-2012 Daniel Azuma +# # All rights reserved. -# +# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: -# +# # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * Neither the name of the copyright holder, nor the names of any other # contributors to this software, may be used to endorse or promote products # derived from this software without specific prior written permission. -# +# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR @@ -33,48 +33,48 @@ # ----------------------------------------------------------------------------- ; module RGeo - + module Geos - - + + # This the GEOS CAPI implementation of ::RGeo::Feature::Factory. - + class Factory - - + + include Feature::Factory::Instance - - + + class << self - - + + # Create a new factory. Returns nil if the GEOS CAPI implementation # is not supported. - # + # # See ::RGeo::Geos.factory for a list of supported options. - + def create(opts_={}) # Make sure GEOS is available return nil unless respond_to?(:_create) - + # Get flags to pass to the C extension flags_ = 0 flags_ |= 1 if opts_[:lenient_multi_polygon_assertions] || opts_[:uses_lenient_multi_polygon_assertions] flags_ |= 2 if opts_[:has_z_coordinate] flags_ |= 4 if opts_[:has_m_coordinate] if flags_ & 6 == 6 raise Error::UnsupportedOperation, "GEOS cannot support both Z and M coordinates at the same time." end flags_ |= 8 unless opts_[:auto_prepare] == :disabled - + # Buffer resolution buffer_resolution_ = opts_[:buffer_resolution].to_i buffer_resolution_ = 1 if buffer_resolution_ < 1 - + # Interpret the generator options wkt_generator_ = opts_[:wkt_generator] case wkt_generator_ when :geos wkt_generator_ = nil @@ -90,11 +90,11 @@ when ::Hash wkb_generator_ = WKRep::WKBGenerator.new(wkb_generator_) else wkb_generator_ = WKRep::WKBGenerator.new end - + # Coordinate system (srid, proj4, and coord_sys) srid_ = opts_[:srid] proj4_ = opts_[:proj4] if CoordSys::Proj4.supported? if proj4_.kind_of?(::String) || proj4_.kind_of?(::Hash) @@ -113,14 +113,14 @@ proj4_ ||= entry_.proj4 coord_sys_ ||= entry_.coord_sys end end srid_ ||= coord_sys_.authority_code if coord_sys_ - + # Create the factory and set instance variables result_ = _create(flags_, srid_.to_i, buffer_resolution_, wkt_generator_, wkb_generator_) - + # Interpret parser options wkt_parser_ = opts_[:wkt_parser] case wkt_parser_ when :geos wkt_parser_ = nil @@ -136,67 +136,67 @@ when ::Hash wkb_parser_ = WKRep::WKBParser.new(result_, wkb_parser_) else wkb_parser_ = WKRep::WKBParser.new(result_) end - + # Set instance variables result_.instance_variable_set(:@proj4, proj4_) result_.instance_variable_set(:@coord_sys, coord_sys_) result_.instance_variable_set(:@wkt_parser, wkt_parser_) result_.instance_variable_set(:@wkb_parser, wkb_parser_) result_.instance_variable_set(:@wkt_generator, wkt_generator_) result_.instance_variable_set(:@wkb_generator, wkb_generator_) - + # Return the result result_ end alias_method :new, :create - - + + end - - + + def inspect # :nodoc: "#<#{self.class}:0x#{object_id.to_s(16)} srid=#{_srid} bufres=#{_buffer_resolution} flags=#{_flags}>" end - - + + # Factory equivalence test. - + def eql?(rhs_) rhs_.is_a?(Factory) && rhs_.srid == _srid && rhs_._buffer_resolution == _buffer_resolution && rhs_._flags == _flags && rhs_.proj4 == @proj4 end alias_method :==, :eql? - - + + # Returns the SRID of geometries created by this factory. - + def srid _srid end - - + + # Returns the resolution used by buffer calculations on geometries # created by this factory - + def buffer_resolution _buffer_resolution end - - + + # Returns true if this factory is lenient with MultiPolygon assertions - + def lenient_multi_polygon_assertions? _flags & 0x1 != 0 end - - + + # See ::RGeo::Feature::Factory#property - + def property(name_) case name_ when :has_z_coordinate _flags & 0x2 != 0 when :has_m_coordinate @@ -211,124 +211,124 @@ _flags & 0x8 != 0 ? :simple : :disabled else nil end end - - + + # See ::RGeo::Feature::Factory#parse_wkt - + def parse_wkt(str_) if @wkt_parser @wkt_parser.parse(str_) else _parse_wkt_impl(str_) end end - - + + # See ::RGeo::Feature::Factory#parse_wkb - + def parse_wkb(str_) if @wkb_parser @wkb_parser.parse(str_) else _parse_wkb_impl(str_) end end - - + + # See ::RGeo::Feature::Factory#point - + def point(x_, y_, *extra_) if extra_.length > (_flags & 6 == 0 ? 0 : 1) nil else PointImpl.create(self, x_, y_, extra_[0].to_f) rescue nil end end - - + + # See ::RGeo::Feature::Factory#line_string - + def line_string(points_) points_ = points_.to_a unless points_.kind_of?(::Array) LineStringImpl.create(self, points_) rescue nil end - - + + # See ::RGeo::Feature::Factory#line - + def line(start_, end_) LineImpl.create(self, start_, end_) rescue nil end - - + + # See ::RGeo::Feature::Factory#linear_ring - + def linear_ring(points_) points_ = points_.to_a unless points_.kind_of?(::Array) LinearRingImpl.create(self, points_) rescue nil end - - + + # See ::RGeo::Feature::Factory#polygon - + def polygon(outer_ring_, inner_rings_=nil) inner_rings_ = inner_rings_.to_a unless inner_rings_.kind_of?(::Array) PolygonImpl.create(self, outer_ring_, inner_rings_) rescue nil end - - + + # See ::RGeo::Feature::Factory#collection - + def collection(elems_) elems_ = elems_.to_a unless elems_.kind_of?(::Array) GeometryCollectionImpl.create(self, elems_) rescue nil end - - + + # See ::RGeo::Feature::Factory#multi_point - + def multi_point(elems_) elems_ = elems_.to_a unless elems_.kind_of?(::Array) MultiPointImpl.create(self, elems_) rescue nil end - - + + # See ::RGeo::Feature::Factory#multi_line_string - + def multi_line_string(elems_) elems_ = elems_.to_a unless elems_.kind_of?(::Array) MultiLineStringImpl.create(self, elems_) rescue nil end - - + + # See ::RGeo::Feature::Factory#multi_polygon - + def multi_polygon(elems_) elems_ = elems_.to_a unless elems_.kind_of?(::Array) MultiPolygonImpl.create(self, elems_) rescue nil end - - + + # See ::RGeo::Feature::Factory#proj4 - + def proj4 @proj4 end - - + + # See ::RGeo::Feature::Factory#coord_sys - + def coord_sys @coord_sys end - - + + # See ::RGeo::Feature::Factory#override_cast - + def override_cast(original_, ntype_, flags_) return nil unless Geos.supported? keep_subtype_ = flags_[:keep_subtype] force_new_ = flags_[:force_new] project_ = flags_[:project] @@ -363,13 +363,13 @@ return Feature.cast(original_.m_geometry, ntype_, flags_) end end false end - - + + end - - + + end - + end