Skip to content

Commit b5cff82

Browse files
committed
Better display multiple sample locations
1 parent 3132066 commit b5cff82

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

app/models/genome.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,18 @@ def source_attribute_groups
252252
@source_attribute_groups
253253
end
254254

255+
##
256+
# Finds the locations of all source samples associated to this genome, and
257+
# returns them as an Array of 2-element Arrays ([lat, lon]) or +nil+
255258
def source_sample_locations
256259
coord = /([-+] *)?(\d+(?:[\.\,]\d+)?|\d+°(?:\d+['"])*)( *[NSEW])?/
257260
keys = {
258261
lat: %i[lat geographic_location_latitude latitude_start latitude_end],
259262
lon: %i[lon geographic_location_longitude longitude_start longitude_end]
260263
}
261264

265+
coords = { lat: nil, lon: nil }
262266
@_source_sample_locations ||=
263-
coords = { lat: nil, lon: nil }
264267
source_cannonical_samples.map do |sample|
265268
# Try joint keys
266269
if sample[:lat_lon]
@@ -306,6 +309,29 @@ def source_sample_locations
306309
end
307310
end
308311

312+
##
313+
# Finds the rectangular bounds of all sample locations, with a minimum range
314+
# of latitudes of +minlat+ and longitudes of +minlon+, and returns it as an
315+
# array in the [south, west, north, east] order
316+
def source_sample_area(minlat = 0.1, minlon = 0.1)
317+
loc = source_sample_locations.compact
318+
return unless loc.present?
319+
320+
min = { lat: minlat, lon: minlon }
321+
rng = { lat: loc.map { |i| i[0] }.minmax, lon: loc.map { |i| i[1] }.minmax }
322+
323+
rng.each do |k, v|
324+
width = v.inject(:-).abs
325+
if width < min[k]
326+
pad = (min[k] - width) / 2
327+
rng[k][0] -= pad
328+
rng[k][1] += pad
329+
end
330+
end
331+
332+
[rng[:lat][0], rng[:lon][0], rng[:lat][1], rng[:lon][1]]
333+
end
334+
309335
##
310336
# TODO
311337
# Use source_cannonical_samples instead!

app/views/genomes/sample_map.html.erb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
<% end %>
2020

2121
<script>
22-
function load_map(lat, lon) {
22+
function load_map(lat1, lon1, lat2, lon2) {
2323
return(
2424
new maplibregl.Map({
2525
container: 'map',
2626
style:
2727
'https://api.maptiler.com/maps/hybrid/style.json?' +
28-
'key=<%= ENV["RAILS_SEQCODE_MAPTILER_KEY"] %>',
29-
center: [lon, lat], // starting position [lng, lat]
30-
zoom: 9 // starting zoom
28+
'key=<%= ENV["RAILS_SEQCODE_MAPTILER_KEY"] ||
29+
"get_your_own_OpIi9ZULNHzrESv6T2vL" %>',
30+
bounds: [lon1, lat1, lon2, lat2]
3131
})
3232
);
3333
}
@@ -38,7 +38,8 @@
3838
.addTo(map)
3939
);
4040
}
41-
var map = load_map(<%= locs.first[0] %>, <%= locs.first[1] %>);
41+
<% bounds = @genome.source_sample_area %>
42+
var map = load_map(<%= bounds.join(', ') %>);
4243
<% locs.each do |lat_lon| %>
4344
var marker = add_marker(map, <%= lat_lon[0] %>, <%= lat_lon[1] %>);
4445
<% end %>

0 commit comments

Comments
 (0)