Class: Document::BboxValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/document/bbox_validator.rb

Overview

BboxValidator

Instance Method Summary collapse

Instance Method Details

#proper_bounding_box(record, valid_geom) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/document/bbox_validator.rb', line 25

def proper_bounding_box(record, valid_geom)
  # Min/Max
  min_max = [-180.0, -90.0, 180.0, 90.0]

  # "W,S,E,N" to [W,S,E,N]
  unless record.send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).split(",").nil?
    geom = record.send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).split(",")

    if geom.empty?
      valid_geom = true
    elsif geom.size != 4
      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box], "invalid W,S,E,N syntax")
    # W
    elsif geom[0].to_f < min_max[0]
      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box], "invalid minX present")
    # S
    elsif geom[1].to_f < min_max[1]
      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box], "invalid minY present")
    # E
    elsif geom[2].to_f > min_max[2]
      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box], "invalid maX present")
    # N
    elsif geom[3].to_f > min_max[3]
      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box], "invalid maxY present")
    # Solr - maxY must be >= minY
    elsif geom[1].to_f >= geom[3].to_f
      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box], "maxY must be >= minY")
    end

    # Reject ENVELOPE(-118.00.0000,-88.00.0000,51.00.0000,42.00.0000
    # - Double period float-ish things?
    geom.each do |val|
      next unless val.count(".") >= 2

      valid_geom = false
      record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box],
        "invalid ENVELOPE(W,E,N,S) syntax - found multiple periods in a coordinate value.")
    end
  end

  valid_geom
end

#validate(record) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/document/bbox_validator.rb', line 12

def validate(record)
  # Assume true for empty values
  valid_geom = true

  # Sane for W,S,E,N?
  unless record.send(GeoblacklightAdmin::Schema.instance.solr_fields[:bounding_box]).nil?
    proper_bounding_box(record,
      valid_geom)
  end

  valid_geom
end