Module: Vendor::SimpleMapprHelper

Defined in:
app/helpers/vendor/simple_mappr_helper.rb

Instance Method Summary collapse

Instance Method Details

#simple_mappr_data(params) ⇒ Object

Returns CSV.

Returns:

  • CSV



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
# File 'app/helpers/vendor/simple_mappr_helper.rb', line 4

def simple_mappr_data(params)
  # TODO: Add support for FieldOccurrence, or by Otu Filter

  if params[:collection_object_query]
    a = ::Queries::DwcOccurrence::Filter.new(collection_object_query: params[:collection_object_query]).all.select(:id, :scientificName, :occurrenceID, :decimalLatitude, :decimalLongitude)

    return nil if a.size > 10_000

    d = {}

    a.find_each do |i|
      k = i.scientificName || "[Occurrence id: #{i.occurrenceID}]"
      if i.decimalLatitude && i.decimalLongitude
        d[k] ||= []
        d[k].push [i.decimalLatitude, i.decimalLongitude].join(',')
      end
    end

    return CSV::Table.new([CSV::Row.new([], [])]) if d.empty?

    if d.count == 1
      # SimpleMappr (currently) requires at least 2 columns - otherwise it
      # parses an input file incorrectly - so add an empty second column.
      d['sm_empty_column'] = []
    end

    h = d.keys.dup
    z =  CSV::Row.new(h,h,true)

    x = d.values
    max_points_count = x.max {|a, b| a.length <=> b.length}.length
    y = x.shift

    # The array zip here is based (in part) on the length of y, so make sure y
    # is as long as the longest d.values array.
    y.fill(nil, y.length...max_points_count)
    t = y.zip(*x)

    tbl = CSV::Table.new([z], headers: true ) # , col_sep: "\t", encoding: Encoding::UTF_8)

    t.each do |row|
      tbl << CSV::Row.new(h, row, true)
    end

    tbl
  end
end