Class: ActiveRecord::ConnectionAdapters::PostGIS::OID::Spatial
- Inherits:
-
Type::Value
- Object
- Type::Value
- ActiveRecord::ConnectionAdapters::PostGIS::OID::Spatial
- Defined in:
- lib/active_record/connection_adapters/postgis/oid/spatial.rb
Overview
OID used to represent geometry/geography database types and attributes.
Accepts ‘geo_type`, `srid`, `has_z`, `has_m`, and `geographic` as parameters. Responsible for parsing sql_types returned from the database and WKT features.
Class Method Summary collapse
-
.parse_sql_type(sql_type) ⇒ Object
sql_type: geometry, geometry(Point), geometry(Point,4326), …
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geographic: false) ⇒ Spatial
constructor
A new instance of Spatial.
-
#serialize(value) ⇒ Object
support setting an RGeo object or a WKT string.
- #spatial? ⇒ Boolean
- #spatial_factory ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geographic: false) ⇒ Spatial
Returns a new instance of Spatial.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 12 def initialize(geo_type: "geometry", srid: 0, has_z: false, has_m: false, geographic: false) super() @geographic = geographic.freeze @factory_attrs = { geo_type: geo_type.underscore.freeze, has_m: has_m.freeze, has_z: has_z.freeze, srid: srid.freeze, sql_type: type.to_s.freeze }.freeze end |
Class Method Details
.parse_sql_type(sql_type) ⇒ Object
sql_type: geometry, geometry(Point), geometry(Point,4326), …
returns [geo_type, srid, has_z, has_m]
geo_type: geography, geometry, point, line_string, polygon, ...
srid: 1234
has_z: false
has_m: false
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 32 def self.parse_sql_type(sql_type) geo_type = nil srid = 0 has_z = false has_m = false if sql_type =~ /(geography|geometry)\((.*)\)$/i # geometry(Point) # geometry(Point,4326) params = Regexp.last_match(2).split(",") if params.first =~ /([a-z]+[^zm])(z?)(m?)/i has_z = Regexp.last_match(2).length > 0 has_m = Regexp.last_match(3).length > 0 geo_type = Regexp.last_match(1) end if params.last =~ /(\d+)/ srid = Regexp.last_match(1).to_i end else # geometry # otherType(a,b) geo_type = sql_type end geographic = sql_type.match?(/geography/) [geo_type, srid, has_z, has_m, geographic] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
87 88 89 90 91 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 87 def ==(other) super && @geographic == other.geographic && @factory_attrs == other.factory_attrs end |
#hash ⇒ Object
94 95 96 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 94 def hash super ^ [@geographic, @factory_attrs].hash end |
#serialize(value) ⇒ Object
support setting an RGeo object or a WKT string
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 75 def serialize(value) return if value.nil? geo_value = cast_value(value) # TODO - only valid types should be allowed # e.g. linestring is not valid for point column # raise "maybe should raise" unless RGeo::Feature::Geometry.check_type(geo_value) RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true) .generate(geo_value) end |
#spatial? ⇒ Boolean
66 67 68 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 66 def spatial? true end |
#spatial_factory ⇒ Object
60 61 62 63 64 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 60 def spatial_factory RGeo::ActiveRecord::SpatialFactoryStore.instance.factory( factory_attrs ) end |
#type ⇒ Object
70 71 72 |
# File 'lib/active_record/connection_adapters/postgis/oid/spatial.rb', line 70 def type @geographic ? :geography : :geometry end |