Class: ActiveRecord::ConnectionAdapters::SpatialOracleColumn
- Inherits:
-
Column
- Object
- Column
- ActiveRecord::ConnectionAdapters::SpatialOracleColumn
- Includes:
- SpatialColumn
- Defined in:
- lib/spatial_adapter/oracle_enhanced.rb
Class Method Summary collapse
- .geomcollection_from_sdo_geometry(elem_info, ordinates, ndim) ⇒ Object
- .group_ordinates(ordinates, num_dims) ⇒ Object
- .linestrings_from_sdo_geometry(elem_info, ordinates, ndim) ⇒ Object
- .point_from_sdo_geometry(elem_info, ordinates, ndim, sdo_point) ⇒ Object
- .point_from_sdo_point(sdo_point, ndim) ⇒ Object
- .polygons_from_sdo_geometry(elem_info, ordinates, ndim) ⇒ Object
-
.string_to_geometry(obj) ⇒ Object
With the ruby-oci8 adapter, we get objects back from spatial columns so this method name is a bit of a misnomer.
Class Method Details
.geomcollection_from_sdo_geometry(elem_info, ordinates, ndim) ⇒ Object
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 493 def self.geomcollection_from_sdo_geometry(elem_info, ordinates, ndim) num_elems = elem_info.size / 3 geometries = [] 0.upto(num_elems-1) do |idx| (start_ord, etype, interpretation, end_ord) = elem_info.slice(idx*3,4) end_ord ||= ordinates.size + 1 num_ords = end_ord - start_ord geom = nil case etype when 1 if ndim == 2 geom = Point.from_x_y(ordinates[start_ord-1].to_f, ordinates[start_ord].to_f) elsif ndim == 3 geom = Point.from_x_y_z(ordinates[start_ord-1].to_f, ordinates[start_ord].to_f, ordinates[start_ord+1].to_f) else raise "Not supporting #{ndim} dimensional points" end when 2 geom = LineString.from_coordinates(group_ordinates(ordinates.slice(start_ord-1, num_ords), ndim)) when 1003 raise "Unsupported interpretation #{interpretation} for etype 1003" if interpretation != 1 ring = LinearRing.from_coordinates(group_ordinates(ordinates.slice(start_ord-1, num_ords), ndim)) geom = Polygon.from_linear_rings([ring]) when 2003 raise "Unsupported interpretation #{interpretation} for etype 2003" if interpretation != 1 ring = LinearRing.from_coordinates(group_ordinates(ordinates.slice(start_ord-1, num_ords), ndim)) last_geom = geometries.pop geom = Polygon.from_linear_rings(last_geom.rings + [ring]) else raise "Unsupported type in collection: etype = #{etype}, interpretation = #{interpretation}" end geometries << geom if geom end GeometryCollection.from_geometries(geometries) end |
.group_ordinates(ordinates, num_dims) ⇒ Object
427 428 429 430 431 432 433 434 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 427 def self.group_ordinates(ordinates, num_dims) coords = [] ordinates.each_slice(num_dims) do |coord| x,y = coord coords << [x.to_f, y.to_f] end coords end |
.linestrings_from_sdo_geometry(elem_info, ordinates, ndim) ⇒ Object
459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 459 def self.linestrings_from_sdo_geometry(elem_info, ordinates, ndim) num_ls = elem_info.size / 3 linestrings = [] 0.upto(num_ls-1) do |ring_idx| (start_ord, ring_type, gtype, end_ord) = elem_info.slice(ring_idx*3,4) end_ord ||= ordinates.size + 1 num_ords = end_ord - start_ord linestrings << LineString.from_coordinates(group_ordinates(ordinates.slice(start_ord-1, num_ords), ndim)) end linestrings end |
.point_from_sdo_geometry(elem_info, ordinates, ndim, sdo_point) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 446 def self.point_from_sdo_geometry(elem_info, ordinates, ndim, sdo_point) if sdo_point point_from_sdo_point(sdo_point, ndim) else coords = ordinates.slice(0,ndim) if ndim == 2 Point.from_x_y(ordinates[0].to_f, ordinates[1].to_f) elsif ndim == 3 Point.from_x_y_z(ordinates[0].to_f, ordinates[1].to_f, ordinates[2].to_f) end end end |
.point_from_sdo_point(sdo_point, ndim) ⇒ Object
436 437 438 439 440 441 442 443 444 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 436 def self.point_from_sdo_point(sdo_point, ndim) if ndim == 2 Point.from_x_y(sdo_point.x.to_f, sdo_point.y.to_f) elsif ndim == 3 Point.from_x_y_z(sdo_point.x.to_f, sdo_point.y.to_f, sdo_point.z.to_f) else raise "Not supporting #{ndim} dimensional points" end end |
.polygons_from_sdo_geometry(elem_info, ordinates, ndim) ⇒ Object
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 471 def self.polygons_from_sdo_geometry(elem_info, ordinates, ndim) num_rings = elem_info.size / 3 polygons = [] rings = [] cur_polygon = nil 0.upto(num_rings-1) do |ring_idx| (start_ord, etype, interpretation, end_ord) = elem_info.slice(ring_idx*3,4) end_ord ||= ordinates.size + 1 num_ords = end_ord - start_ord if etype == 1003 && rings.size > 0 # exterior ring polygons << Polygon.from_linear_rings(rings) rings = [] end rings << LinearRing.from_coordinates(group_ordinates(ordinates.slice(start_ord-1, num_ords), ndim)) end if rings.size > 0 # exterior ring polygons << Polygon.from_linear_rings(rings) rings = [] end polygons end |
.string_to_geometry(obj) ⇒ Object
With the ruby-oci8 adapter, we get objects back from spatial columns so this method name is a bit of a misnomer. TODO: change this method name to ‘to_geometry’
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/spatial_adapter/oracle_enhanced.rb', line 396 def self.string_to_geometry(obj) return obj unless obj && obj.class.to_s == "OCI8::Object::Mdsys::SdoGeometry" raise "Bad #{obj.class} object: #{obj.inspect}" if obj.sdo_gtype.nil? ndim = obj.sdo_gtype.to_int/1000 gtype = obj.sdo_gtype.to_int%1000 eleminfo = obj.sdo_elem_info.instance_variable_get('@attributes') ordinates = obj.sdo_ordinates.instance_variable_get('@attributes') case gtype when 1 geom = point_from_sdo_geometry(eleminfo, ordinates, ndim, obj.sdo_point) when 2 geom = linestrings_from_sdo_geometry(eleminfo, ordinates, ndim)[0] when 3 geom = polygons_from_sdo_geometry(eleminfo, ordinates, ndim)[0] when 4 geom = geomcollection_from_sdo_geometry(eleminfo, ordinates, ndim) when 5 geom = MultiPoint.from_coordinates(group_ordinates(ordinates, ndim)) when 6 linestrings = linestrings_from_sdo_geometry(eleminfo, ordinates, ndim) geom = MultiLineString.from_line_strings(linestrings) when 7 polygons = polygons_from_sdo_geometry(eleminfo, ordinates, ndim) geom = MultiPolygon.from_polygons(polygons) else raise "Unhandled geometry type #{obj.sdo_gtype.to_int}" end geom.srid = obj.sdo_srid.to_i geom end |