/
spatial.rb
52 lines (46 loc) · 1.56 KB
/
spatial.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module ActiveRecord
module Type
class Spatial < Value # :nodoc:
def type
:spatial
end
def spatial?
true
end
def klass
::RGeo::Feature::Geometry
end
def set_geo_params(factory_settings, table_name)
@factory_settings = factory_settings
@table_name = table_name
end
private
def cast_value(value)
case value
when ::RGeo::Feature::Geometry
factory = @factory_settings.get_column_factory(@table_name, @column, srid: value.srid)
::RGeo::Feature.cast(value, factory) rescue nil
when ::String
marker = value[4,1]
if marker == "\x00" || marker == "\x01"
factory = @factory_settings.get_column_factory(@table_name, @column,
srid: value[0, 4].unpack(marker == "\x01" ? 'V' : 'N').first)
::RGeo::WKRep::WKBParser.new(factory).parse(value[4..-1]) rescue nil
elsif value[0,10] =~ /[0-9a-fA-F]{8}0[01]/
srid = value[0,8].to_i(16)
if value[9,1] == '1'
srid = [srid].pack('V').unpack('N').first
end
factory = @factory_settings.get_column_factory(@table_name, @column, srid: srid)
::RGeo::WKRep::WKBParser.new(factory).parse(value[8..-1]) rescue nil
else
factory = @factory_settings.get_column_factory(@table_name, @column)
::RGeo::WKRep::WKTParser.new(factory, support_ewkt: true).parse(value) rescue nil
end
else
nil
end
end
end
end
end