Module: Otus::GisHelper
- Defined in:
- app/helpers/otus/gis_helper.rb
Instance Method Summary collapse
- #add_aggregate_geo_json(otu, target) ⇒ Object
- #add_distribution_geo_json(otu, target, seen_shapes = nil) ⇒ Object
-
#aggregate_geo_json(otu, target, cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object
TODO: cleanup.
-
#build_geo_json_feature_deduped(seen, shape_key) ⇒ Object
private
Yields once with skip_geometry true or false depending on whether
shape_key[shape_type, shape_id] has already been recorded inseen. - #geojson_feature_collection ⇒ Object
- #geojson_for_otu(otu) ⇒ Object
- #geojson_target_for_otu(otu) ⇒ Object
-
#otu_cached_map(otu, target, cached_map_type = 'CachedMapItem::WebLevel1', cache = true, force = false) ⇒ Object
NOT USED Caching the cached map.
-
#otu_distribution(otu, children = true, cutoff = 200) ⇒ Object
TODO:.
-
#otu_distribution_is_absent(otu, descendants: false) ⇒ Hash
GeoJSON FeatureCollection of absent FieldOccurrences and AssertedDistributions for the OTU, its coordinate OTUs, and ancestor taxon names (traversed via TaxonNameHierarchies).
-
#otus_distribution_geo_json(otus, target: geojson_feature_collection) ⇒ Hash
GeoJSON FeatureCollection of present distribution data for exactly
otus(coordinate OTUs are not added). -
#otus_distribution_is_absent_geo_json(otus, only_requested: false, target: geojson_feature_collection) ⇒ Hash
GeoJSON FeatureCollection of absent FieldOccurrences and AssertedDistributions for exactly
otus(coordinate OTUs are not added) and their ancestor taxon names.
Instance Method Details
#add_aggregate_geo_json(otu, target) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'app/helpers/otus/gis_helper.rb', line 192 def add_aggregate_geo_json(otu, target) h = target if g = aggregate_geo_json(otu, h) g['properties'] = {'aggregate': true} g['properties']['target'] = geojson_target_for_otu(otu) h['features'].push g end h end |
#add_distribution_geo_json(otu, target, seen_shapes = nil) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'app/helpers/otus/gis_helper.rb', line 254 def add_distribution_geo_json(otu, target, seen_shapes = nil) h = target o = otu # internal target t = geojson_target_for_otu(otu) o.current_field_occurrences.where(is_absent: [nil, false]).each do |f| shape_key = seen_shapes && f.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes&.fetch(:field_occurrences), shape_key) do |skip_geometry| field_occurrence_to_geo_json_feature(f, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end o.current_collection_objects.each do |c| shape_key = seen_shapes && c.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes&.fetch(:collection_objects), shape_key) do |skip_geometry| collection_object_to_geo_json_feature(c, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end o.asserted_distributions.without_is_absent.each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end ba_ids = ::Queries::BiologicalAssociation::Filter.new(otu_query: { otu_id: [o.id] }).all.pluck(:id) unless ba_ids.empty? ::AssertedDistribution .where( asserted_distribution_object_type: 'BiologicalAssociation', asserted_distribution_object_id: ba_ids ) .without_is_absent .each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end bag_ids = ::BiologicalAssociationsGraph .joins(:biological_associations_biological_associations_graphs) .where(biological_associations_biological_associations_graphs: { biological_association_id: ba_ids }) .select(:id) unless bag_ids.empty? ::AssertedDistribution .where( asserted_distribution_object_type: 'BiologicalAssociationsGraph', asserted_distribution_object_id: bag_ids ) .without_is_absent .each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end end end [ [o.depictions, 'Depiction', :depiction_object_id], [o.conveyances, 'Conveyance', :conveyance_object_id], [o.observations, 'Observation', :observation_object_id], ].each do |, object_type, _id_method| = .map(&:id) next if .empty? ::AssertedDistribution .where( asserted_distribution_object_type: object_type, asserted_distribution_object_id: ) .without_is_absent .each do |a| shape_key = seen_shapes && [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes&.fetch(:asserted_distributions), shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end end o.type_materials.includes(:protonym).each do |e| next unless type_material_is_primary_type(e) && o.taxon_name.cached_is_valid shape_key = seen_shapes && e.collection_object&.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes&.fetch(:type_materials), shape_key) do |skip_geometry| type_material_to_geo_json_feature(e, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end h end |
#aggregate_geo_json(otu, target, cached_map_type = 'CachedMapItem::WebLevel1') ⇒ Object
TODO: cleanup
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'app/helpers/otus/gis_helper.rb', line 221 def aggregate_geo_json(otu, target, cached_map_type = 'CachedMapItem::WebLevel1') h = target if gj = otu.cached_map_geo_json(cached_map_type) i = { **gj, # 'type' => gj['type'], # 'Feature', 'properties' => { 'base' => geojson_target_for_otu(otu), # 'shape' => { # 'type' => cached_map_type, # 'id' => 99999 }, # was nil 'updated_at' => 'foo' # last updated at on CachedMapItem scope, possibly } } if gj.keys.include?('coordinates') i['coordinates'] = gj['coordinates'] # was 'coordinates' TODO: might not work elsif gj.keys.include?('geometries') i['geometries'] = gj['geometries'] # was 'coordinates' TODO: might not work end i else nil end end |
#build_geo_json_feature_deduped(seen, shape_key) ⇒ Object (private)
Yields once with skip_geometry true or false depending on whether shape_key
[shape_type, shape_id] has already been recorded in seen.
First occurrence of a shape: yields skip_geometry=false (full geometry fetched).
The shape is recorded in seen only if the block returns a non-nil feature,
so shapes that produce no feature (e.g. no geographic item) are never marked seen.
Subsequent occurrences: yields skip_geometry=true (geometry skipped, nil in feature).
When seen or shape_key is nil (deduplication disabled or no shape available),
yields skip_geometry=false unconditionally.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'app/helpers/otus/gis_helper.rb', line 385 def build_geo_json_feature_deduped(seen, shape_key) if seen.nil? || shape_key.nil? || shape_key.first.nil? return yield(false) end key = "#{shape_key[0]}_#{shape_key[1]}" if seen.key?(key) yield(true) else g = yield(false) seen[key] = true if g g end end |
#geojson_feature_collection ⇒ Object
167 168 169 170 171 172 |
# File 'app/helpers/otus/gis_helper.rb', line 167 def geojson_feature_collection { 'type' => 'FeatureCollection', 'features' => [] } end |
#geojson_for_otu(otu) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'app/helpers/otus/gis_helper.rb', line 174 def geojson_for_otu(otu) { 'type' => 'FeatureCollection', 'features' => [], 'properties' => { 'target' => geojson_target_for_otu(otu) } } end |
#geojson_target_for_otu(otu) ⇒ Object
184 185 186 187 188 189 190 |
# File 'app/helpers/otus/gis_helper.rb', line 184 def geojson_target_for_otu(otu) { 'id' => otu.id, 'label' => label_for_otu(otu), 'type' => 'Otu' } end |
#otu_cached_map(otu, target, cached_map_type = 'CachedMapItem::WebLevel1', cache = true, force = false) ⇒ Object
NOT USED Caching the cached map
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'app/helpers/otus/gis_helper.rb', line 207 def otu_cached_map(otu, target, cached_map_type = 'CachedMapItem::WebLevel1', cache = true, force = false) r = nil if force r = aggregate_geo_json(otu, target, cached_map_type) else # Check for map # TODO: extend with synced check if a = CachedMap.where(project_id: sessions_current_project_id).where(otu_id: otu.id, cached_map_type: ) end end end |
#otu_distribution(otu, children = true, cutoff = 200) ⇒ Object
TODO:
- make properties universal type: 'Model', id: id, label:
- merge origin_otu_id: id to reference coordinate OTUs
target:
type: Otu
label:
id
base: # one level above target (or one level below shape?!) type:
id:
shape: # Either GeographicArea or Georeference type id
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/helpers/otus/gis_helper.rb', line 37 def otu_distribution(otu, children = true, cutoff = 200) return {} if otu.nil? otus = if children otu.coordinate_otus_with_children else Otu.coordinate_otus(otu.id) end h = geojson_for_otu(otu) if otu.taxon_name && otu.taxon_name.is_protonym? && !otu.taxon_name.is_species_rank? add_aggregate_geo_json(otu, h) else otus = if children otu.coordinate_otus_with_children else Otu.coordinate_otus(otu.id) end otus_distribution_geo_json(otus, target: h) end h end |
#otu_distribution_is_absent(otu, descendants: false) ⇒ Hash
Returns GeoJSON FeatureCollection of absent FieldOccurrences and AssertedDistributions for the OTU, its coordinate OTUs, and ancestor taxon names (traversed via TaxonNameHierarchies).
112 113 114 115 116 117 118 |
# File 'app/helpers/otus/gis_helper.rb', line 112 def otu_distribution_is_absent(otu, descendants: false) return {} if otu.nil? otus = descendants ? otu.coordinate_otus_with_children : Otu.coordinate_otus(otu.id) otus_distribution_is_absent_geo_json(otus, target: geojson_for_otu(otu)) end |
#otus_distribution_geo_json(otus, target: geojson_feature_collection) ⇒ Hash
Returns GeoJSON FeatureCollection of present distribution data for exactly otus
(coordinate OTUs are not added).
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/otus/gis_helper.rb', line 67 def otus_distribution_geo_json(otus, target: geojson_feature_collection) h = target # Batch-load everything the downstream helpers touch. otus = otus.includes( :taxon_name, current_field_occurrences: [ :identifiers, { collecting_event: [ :georeferences, :geographic_area, :geographic_items ] } ], current_collection_objects: [ :identifiers, { collecting_event: [ :georeferences, :geographic_area, :geographic_items ] } ], asserted_distributions: { asserted_distribution_shape: :geographic_items }, type_materials: [ { collection_object: [ :identifiers, { collecting_event: [ :georeferences, :geographic_area, :geographic_items ] } ] } ] ) seen_shapes = { field_occurrences: {}, collection_objects: {}, asserted_distributions: {}, type_materials: {} } otus.each do |o| add_distribution_geo_json(o, h, seen_shapes) end h end |
#otus_distribution_is_absent_geo_json(otus, only_requested: false, target: geojson_feature_collection) ⇒ Hash
Returns GeoJSON FeatureCollection of absent FieldOccurrences and AssertedDistributions
for exactly otus (coordinate OTUs are not added) and their ancestor taxon names.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/helpers/otus/gis_helper.rb', line 126 def otus_distribution_is_absent_geo_json(otus, only_requested: false, target: geojson_feature_collection) h = target otu_ids = otus.pluck(:id) if only_requested seen_shapes = { field_occurrences: {}, asserted_distributions: {} } otus.each do |o| t = geojson_target_for_otu(o) excluded_otu_ids = only_requested ? Otu.coordinate_otus(o.id).where.not(id: otu_ids).pluck(:id) : [] o.absent_and_ancestor_absent_field_occurrences .where.not(taxon_determinations: { otu_id: excluded_otu_ids }) .each do |f| shape_key = f.collecting_event&.geo_json_shape_key g = build_geo_json_feature_deduped(seen_shapes[:field_occurrences], shape_key) do |skip_geometry| field_occurrence_to_geo_json_feature(f, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end o.absent_and_ancestor_absent_asserted_distributions .where.not(asserted_distribution_object_id: excluded_otu_ids) .each do |a| shape_key = [a.asserted_distribution_shape_type, a.asserted_distribution_shape_id] g = build_geo_json_feature_deduped(seen_shapes[:asserted_distributions], shape_key) do |skip_geometry| asserted_distribution_to_geo_json_feature(a, skip_geometry:) end next unless g g['properties']['target'] = t h['features'].push g end end h end |