2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: champs
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# private :boolean default(FALSE), not null
|
|
|
|
# row :integer
|
|
|
|
# type :string
|
|
|
|
# value :string
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# dossier_id :integer
|
|
|
|
# etablissement_id :integer
|
|
|
|
# parent_id :bigint
|
|
|
|
# type_de_champ_id :integer
|
|
|
|
#
|
2018-10-16 13:01:12 +02:00
|
|
|
class Champs::CarteChamp < Champ
|
2020-04-16 10:22:07 +02:00
|
|
|
# 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 quartiers_prioritaires
|
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(:quartier_prioritaire)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-23 15:38:20 +02:00
|
|
|
def parcelles_agricoles
|
2019-09-12 11:26:22 +02:00
|
|
|
geo_areas.filter do |area|
|
2018-10-23 15:38:20 +02:00
|
|
|
area.source == GeoArea.sources.fetch(:parcelle_agricole)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
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
|
|
|
|
|
2018-10-17 12:42:06 +02:00
|
|
|
def cadastres?
|
|
|
|
type_de_champ&.cadastres && type_de_champ.cadastres != '0'
|
|
|
|
end
|
|
|
|
|
|
|
|
def quartiers_prioritaires?
|
|
|
|
type_de_champ&.quartiers_prioritaires && type_de_champ.quartiers_prioritaires != '0'
|
|
|
|
end
|
|
|
|
|
2018-10-23 15:38:20 +02:00
|
|
|
def parcelles_agricoles?
|
|
|
|
type_de_champ&.parcelles_agricoles && type_de_champ.parcelles_agricoles != '0'
|
|
|
|
end
|
|
|
|
|
2020-09-02 12:56:45 +02:00
|
|
|
def mnhn?
|
|
|
|
type_de_champ&.mnhn && type_de_champ.mnhn != '0'
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_options
|
|
|
|
{
|
|
|
|
ign: Flipper.enabled?(:carte_ign, procedure),
|
|
|
|
mnhn: mnhn?,
|
|
|
|
cadastres: cadastres?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-10-16 13:01:12 +02:00
|
|
|
def position
|
|
|
|
if dossier.present?
|
|
|
|
dossier.geo_position
|
|
|
|
else
|
2020-04-16 10:22:07 +02:00
|
|
|
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
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
def bounding_box
|
|
|
|
factory = RGeo::Geographic.simple_mercator_factory
|
|
|
|
bounding_box = RGeo::Cartesian::BoundingBox.new(factory)
|
2018-12-19 11:09:13 +01:00
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
if geo_areas.present?
|
2020-05-05 10:26:13 +02:00
|
|
|
geo_areas.map(&:rgeo_geometry).compact.each do |geometry|
|
|
|
|
bounding_box.add(geometry)
|
2018-11-30 13:19:19 +01:00
|
|
|
end
|
2020-04-09 17:32:20 +02:00
|
|
|
elsif dossier.present?
|
|
|
|
point = dossier.geo_position
|
|
|
|
bounding_box.add(factory.point(point[:lon], point[:lat]))
|
|
|
|
else
|
2020-04-16 10:22:07 +02:00
|
|
|
bounding_box.add(factory.point(DEFAULT_LON, DEFAULT_LAT))
|
2018-11-30 13:19:19 +01:00
|
|
|
end
|
2020-04-09 17:32:20 +02:00
|
|
|
|
|
|
|
[bounding_box.max_point, bounding_box.min_point].compact.flat_map(&:coordinates)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_feature_collection
|
|
|
|
{
|
|
|
|
type: 'FeatureCollection',
|
2020-04-30 15:49:31 +02:00
|
|
|
id: stable_id,
|
2020-04-09 17:32:20 +02:00
|
|
|
bbox: bounding_box,
|
2020-04-30 16:59:38 +02:00
|
|
|
features: geo_areas.map(&:to_feature)
|
2020-04-09 17:32:20 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def geometry?
|
|
|
|
geo_areas.present?
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
2018-10-31 13:29:25 +01:00
|
|
|
|
2020-04-09 17:32:20 +02: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
|
|
|
|
)
|
2018-12-03 12:49:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
def for_api
|
|
|
|
nil
|
|
|
|
end
|
2018-12-19 11:09:13 +01:00
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
def for_export
|
|
|
|
nil
|
2018-10-31 13:29:25 +01:00
|
|
|
end
|
2018-12-28 17:59:14 +01:00
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
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
|
|
|
|
}
|
2020-04-09 17:32:20 +02:00
|
|
|
else
|
2020-04-30 16:59:38 +02:00
|
|
|
nil
|
2020-04-09 17:32:20 +02:00
|
|
|
end
|
2018-12-28 17:59:14 +01:00
|
|
|
end
|
2018-10-16 13:01:12 +02:00
|
|
|
end
|