lib/rgeo/geos/ffi_factory.rb in rgeo-3.0.0.pre.rc.2 vs lib/rgeo/geos/ffi_factory.rb in rgeo-3.0.0.pre.rc.3

- old
+ new

@@ -31,79 +31,56 @@ @spatial_dimension = @has_z ? 3 : 2 @_has_3d = @has_z || @has_m @buffer_resolution = opts[:buffer_resolution].to_i @buffer_resolution = 1 if @buffer_resolution < 1 - @_auto_prepare = opts[:auto_prepare] == :disabled ? false : true + @_auto_prepare = opts[:auto_prepare] != :disabled # Interpret the generator options - wkt_generator_ = opts[:wkt_generator] - case wkt_generator_ - when :geos - @wkt_writer = ::Geos::WktWriter.new - @wkt_generator = nil + wkt_generator = opts[:wkt_generator] + case wkt_generator when Hash - @wkt_generator = WKRep::WKTGenerator.new(wkt_generator_) + @wkt_generator = WKRep::WKTGenerator.new(wkt_generator) @wkt_writer = nil else - @wkt_generator = WKRep::WKTGenerator.new(convert_case: :upper) - @wkt_writer = nil + @wkt_writer = ::Geos::WktWriter.new + @wkt_writer.trim = true + @wkt_generator = nil end - wkb_generator_ = opts[:wkb_generator] - case wkb_generator_ - when :geos - @wkb_writer = ::Geos::WkbWriter.new - @wkb_generator = nil + wkb_generator = opts[:wkb_generator] + case wkb_generator when Hash - @wkb_generator = WKRep::WKBGenerator.new(wkb_generator_) + @wkb_generator = WKRep::WKBGenerator.new(wkb_generator) @wkb_writer = nil else - @wkb_generator = WKRep::WKBGenerator.new - @wkb_writer = nil + @wkb_writer = ::Geos::WkbWriter.new + @wkb_generator = nil end - # Coordinate system (srid, proj4, and coord_sys) - @srid = opts[:srid] - @proj4 = opts[:proj4] - if @proj4 && CoordSys.check!(:proj4) - if @proj4.is_a?(String) || @proj4.is_a?(Hash) - @proj4 = CoordSys::Proj4.create(@proj4) - end - else - @proj4 = nil - end - @coord_sys = opts[:coord_sys] - if @coord_sys.is_a?(String) - @coord_sys = CoordSys::CS.create_from_wkt(@coord_sys) - end - @srid ||= @coord_sys.authority_code if @coord_sys - @srid = @srid.to_i + # Coordinate system (srid and coord_sys) + coord_sys_info = ImplHelper::Utils.setup_coord_sys(opts[:srid], opts[:coord_sys], opts[:coord_sys_class]) + @srid = coord_sys_info[:srid] + @coord_sys = coord_sys_info[:coord_sys] # Interpret parser options wkt_parser = opts[:wkt_parser] case wkt_parser - when :geos - @wkt_reader = ::Geos::WktReader.new - @wkt_parser = nil when Hash @wkt_parser = WKRep::WKTParser.new(self, wkt_parser) @wkt_reader = nil else - @wkt_parser = WKRep::WKTParser.new(self) - @wkt_reader = nil + @wkt_reader = ::Geos::WktReader.new + @wkt_parser = nil end wkb_parser = opts[:wkb_parser] case wkb_parser - when :geos - @wkb_reader = ::Geos::WkbReader.new - @wkb_parser = nil when Hash @wkb_parser = WKRep::WKBParser.new(self, wkb_parser) @wkb_reader = nil else - @wkb_parser = WKRep::WKBParser.new(self) - @wkb_reader = nil + @wkb_reader = ::Geos::WkbReader.new + @wkb_parser = nil end end attr_reader :coordinate_dimension, :spatial_dimension # Standard object inspection output @@ -117,62 +94,54 @@ def eql?(rhs) rhs.is_a?(self.class) && @srid == rhs.srid && @has_z == rhs.property(:has_z_coordinate) && @has_m == rhs.property(:has_m_coordinate) && @buffer_resolution == rhs.property(:buffer_resolution) && - @proj4.eql?(rhs.proj4) + @coord_sys.eql?(rhs.coord_sys) end alias == eql? # Standard hash code def hash - @hash ||= [@srid, @has_z, @has_m, @buffer_resolution, @proj4].hash + @hash ||= [@srid, @has_z, @has_m, @buffer_resolution, @coord_sys].hash end # Marshal support def marshal_dump # :nodoc: hash = { "hasz" => @has_z, "hasm" => @has_m, "srid" => @srid, "bufr" => @buffer_resolution, - "wktg" => @wkt_generator.properties, - "wkbg" => @wkb_generator.properties, - "wktp" => @wkt_parser.properties, - "wkbp" => @wkb_parser.properties, + "wktg" => @wkt_generator&.properties, + "wkbg" => @wkb_generator&.properties, + "wktp" => @wkt_parser&.properties, + "wkbp" => @wkb_parser&.properties, "apre" => @_auto_prepare } - hash["proj4"] = @proj4.marshal_dump if @proj4 hash["cs"] = @coord_sys.to_wkt if @coord_sys hash end def marshal_load(data) # :nodoc: - if (proj4_data = data["proj4"]) && CoordSys.check!(:proj4) - proj4 = CoordSys::Proj4.allocate - proj4.marshal_load(proj4_data) - else - proj4 = nil - end if (coord_sys_data = data["cs"]) - coord_sys = CoordSys::CS.create_from_wkt(coord_sys_data) + coord_sys = CoordSys::CONFIG.default_coord_sys_class.create_from_wkt(coord_sys_data) else coord_sys = nil end initialize( has_z_coordinate: data["hasz"], has_m_coordinate: data["hasm"], srid: data["srid"], buffer_resolution: data["bufr"], - wkt_generator: symbolize_hash(data["wktg"]), - wkb_generator: symbolize_hash(data["wkbg"]), - wkt_parser: symbolize_hash(data["wktp"]), - wkb_parser: symbolize_hash(data["wkbp"]), + wkt_generator: data["wktg"] && symbolize_hash(data["wktg"]), + wkb_generator: data["wkbg"] && symbolize_hash(data["wkbg"]), + wkt_parser: data["wktp"] && symbolize_hash(data["wktp"]), + wkb_parser: data["wkbp"] && symbolize_hash(data["wkbp"]), auto_prepare: (data["apre"] ? :simple : :disabled), - proj4: proj4, coord_sys: coord_sys ) end # Psych support @@ -180,49 +149,34 @@ def encode_with(coder) # :nodoc: coder["has_z_coordinate"] = @has_z coder["has_m_coordinate"] = @has_m coder["srid"] = @srid coder["buffer_resolution"] = @buffer_resolution - coder["wkt_generator"] = @wkt_generator.properties - coder["wkb_generator"] = @wkb_generator.properties - coder["wkt_parser"] = @wkt_parser.properties - coder["wkb_parser"] = @wkb_parser.properties + coder["wkt_generator"] = @wkt_generator&.properties + coder["wkb_generator"] = @wkb_generator&.properties + coder["wkt_parser"] = @wkt_parser&.properties + coder["wkb_parser"] = @wkb_parser&.properties coder["auto_prepare"] = @_auto_prepare ? "simple" : "disabled" - if @proj4 - str = @proj4.original_str || @proj4.canonical_str - coder["proj4"] = @proj4.radians? ? { "proj4" => str, "radians" => true } : str - end coder["coord_sys"] = @coord_sys.to_wkt if @coord_sys end def init_with(coder) # :nodoc: - if (proj4_data = coder["proj4"]) - CoordSys.check!(:proj4) - if proj4_data.is_a?(Hash) - proj4 = CoordSys::Proj4.create(proj4_data["proj4"], radians: proj4_data["radians"]) - else - proj4 = CoordSys::Proj4.create(proj4_data.to_s) - end - else - proj4 = nil - end if (coord_sys_data = coder["cs"]) - coord_sys = CoordSys::CS.create_from_wkt(coord_sys_data.to_s) + coord_sys = CoordSys::CONFIG.default_coord_sys_class.create_from_wkt(coord_sys_data.to_s) else coord_sys = nil end initialize( has_z_coordinate: coder["has_z_coordinate"], has_m_coordinate: coder["has_m_coordinate"], srid: coder["srid"], buffer_resolution: coder["buffer_resolution"], - wkt_generator: symbolize_hash(coder["wkt_generator"]), - wkb_generator: symbolize_hash(coder["wkb_generator"]), - wkt_parser: symbolize_hash(coder["wkt_parser"]), - wkb_parser: symbolize_hash(coder["wkb_parser"]), + wkt_generator: coder["wkt_generator"] && symbolize_hash(coder["wkt_generator"]), + wkb_generator: coder["wkb_generator"] && symbolize_hash(coder["wkb_generator"]), + wkt_parser: coder["wkt_parser"] && symbolize_hash(coder["wkt_parser"]), + wkb_parser: coder["wkb_parser"] && symbolize_hash(coder["wkb_parser"]), auto_prepare: coder["auto_prepare"] == "disabled" ? :disabled : :simple, - proj4: proj4, coord_sys: coord_sys ) end # Returns the SRID of geometries created by this factory. @@ -251,21 +205,30 @@ end # See RGeo::Feature::Factory#parse_wkt def parse_wkt(str) if @wkt_reader - wrap_fg_geom(@wkt_reader.read(str), nil) + begin + wrap_fg_geom(@wkt_reader.read(str), nil) + rescue ::Geos::WktReader::ParseError => e + raise RGeo::Error::ParseError, e.message.partition(":").last + end else @wkt_parser.parse(str) end end # See RGeo::Feature::Factory#parse_wkb def parse_wkb(str) if @wkb_reader - wrap_fg_geom(@wkb_reader.read(str), nil) + begin + meth = str[0].match?(/[0-9a-fA-F]/) ? :read_hex : :read + wrap_fg_geom(@wkb_reader.public_send(meth, str), nil) + rescue ::Geos::WkbReader::ParseError => e + raise RGeo::Error::ParseError, e.message.partition(":").last + end else @wkb_parser.parse(str) end end @@ -402,14 +365,10 @@ klasses = Array.new(elems.size, FFIPolygonImpl) fg_geom = ::Geos::Utils.create_collection(::Geos::GeomTypes::GEOS_MULTIPOLYGON, elems) FFIMultiPolygonImpl.new(self, fg_geom, klasses) end - # See RGeo::Feature::Factory#proj4 - - attr_reader :proj4 - # See RGeo::Feature::Factory#coord_sys attr_reader :coord_sys # See RGeo::Feature::Factory#override_cast @@ -503,9 +462,10 @@ end def write_for_psych(geom) if Utils.ffi_supports_set_output_dimension || !@_has_3d wkt_writer = ::Geos::WktWriter.new + wkt_writer.trim = true wkt_writer.output_dimensions = 3 if @_has_3d wkt_writer.write(geom.fg_geom) else Utils.psych_wkt_generator.generate(geom) end