demarches-normaliennes/app/models/champs/carte_champ.rb

107 lines
2.3 KiB
Ruby
Raw Normal View History

2018-10-16 13:01:12 +02:00
class Champs::CarteChamp < Champ
# Default map location. Center of the World, ahm, France...
DEFAULT_LON = 2.428462
DEFAULT_LAT = 46.538192
2018-10-16 13:01:12 +02:00
# We are not using scopes here as we want to access
# the following collections on unsaved records.
def cadastres
2019-09-12 11:26:22 +02:00
geo_areas.filter do |area|
2018-10-16 13:01:12 +02:00
area.source == GeoArea.sources.fetch(:cadastre)
end
end
def selections_utilisateur
geo_areas.filter do |area|
area.source == GeoArea.sources.fetch(:selection_utilisateur)
end
2018-12-19 11:09:13 +01:00
end
2020-10-15 16:03:07 +02:00
def cadastres?
2021-06-23 15:54:12 +02:00
type_de_champ.layer_enabled?(:cadastres)
2018-10-23 15:38:20 +02:00
end
def optional_layers
2021-06-23 15:54:12 +02:00
type_de_champ.carte_optional_layers
end
def render_options
2021-05-06 18:48:24 +02:00
{ layers: optional_layers }
end
2018-10-16 13:01:12 +02:00
def position
if dossier.present?
dossier.geo_position
else
lon = DEFAULT_LON.to_s
lat = DEFAULT_LAT.to_s
2018-10-16 13:01:12 +02:00
zoom = "13"
{ lon: lon, lat: lat, zoom: zoom }
end
end
2018-10-17 12:02:34 +02:00
def bounding_box
if geo_areas.present?
GeojsonService.bbox(type: 'FeatureCollection', features: geo_areas.map(&:to_feature))
elsif dossier.present?
point = dossier.geo_position
GeojsonService.bbox(type: 'Feature', geometry: { type: 'Point', coordinates: [point[:lon], point[:lat]] })
else
GeojsonService.bbox(type: 'Feature', geometry: { type: 'Point', coordinates: [DEFAULT_LON, DEFAULT_LAT] })
2018-11-30 13:19:19 +01:00
end
end
def to_feature_collection
{
type: 'FeatureCollection',
2020-04-30 15:49:31 +02:00
id: stable_id,
bbox: bounding_box,
2020-04-30 16:59:38 +02:00
features: geo_areas.map(&:to_feature)
}
end
def geometry?
geo_areas.present?
2018-10-17 12:02:34 +02:00
end
2018-10-31 13:29:25 +01:00
def selection_utilisateur_legacy_geo_area
geometry = selection_utilisateur_legacy_geometry
if geometry.present?
GeoArea.new(
source: GeoArea.sources.fetch(:selection_utilisateur),
geometry: geometry
)
end
end
def for_api
nil
end
2018-12-19 11:09:13 +01:00
def for_export
2022-08-31 13:09:00 +02:00
geo_areas.map(&:label).join("\n")
2018-10-31 13:29:25 +01:00
end
2018-12-28 17:59:14 +01:00
2021-07-21 10:45:59 +02:00
def blank?
geo_areas.blank?
end
private
2020-04-30 16:59:38 +02:00
def selection_utilisateur_legacy_geometry
if selections_utilisateur.present?
{
type: 'MultiPolygon',
coordinates: selections_utilisateur.filter do |selection_utilisateur|
selection_utilisateur.geometry['type'] == 'Polygon'
end.map do |selection_utilisateur|
selection_utilisateur.geometry['coordinates']
end
}
else
2020-04-30 16:59:38 +02:00
nil
end
2018-12-28 17:59:14 +01:00
end
2018-10-16 13:01:12 +02:00
end