I'm working on geographic web app with postgis and postgresql
@slopes = Slope.order(:id)
@array_slopes = @slopes.chunk_while { |i, j| i.average_slope == j.average_slope }.to_a
I get an array of arrays of slopes (with geo coordinates) like :
[[slope_1, slope_2, slope3][slope_4, slope_5, slope_6, slope_7]][[slope_8, slope_9]....]
I m create a geojson linestring with @array_slopes, but get a broken line because I should have :
[[slope_1, slope_2, slope3][slope3, slope_4, slope_5, slope_6, slope_7]] [[slope_7 slope_8, slope_9]...]
I need to get the last slope from each array en push it in the next array for generate à continuous line with my geojson builder :
..slope3] => [slope3..
or
..slope_7]] => [[slope_7..
Something like this (but in my controller) :
<% @array_slopes.each_with_index do |array, index| %>
<% array.each do |slope| %>
<% @array_slopes[index 1 ].unshift(array.last) %>
<% end %>
<% end %>
How can I do that ?
CodePudding user response:
This is a straightforward way of doing that.
arr =[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr.each_index.map do |i|
if i.zero?
arr[0]
else
[arr[i-1].last] arr[i]
end
end
#=> [[1, 2, 3], [3, 4, 5, 6], [6, 7, 8, 9]]
CodePudding user response:
I have found another solution :
@array_slopes.each_with_index do |group, i|
next if group.empty? || i == @array.length - 1
last_value = group.last
@array[i 1].unshift(last_value)
end