2018-10-16 13:01:12 +02:00
|
|
|
class Champs::CarteChamp < Champ
|
|
|
|
# We are not using scopes here as we want to access
|
|
|
|
# the following collections on unsaved records.
|
|
|
|
def cadastres
|
|
|
|
geo_areas.select do |area|
|
|
|
|
area.source == GeoArea.sources.fetch(:cadastre)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def quartiers_prioritaires
|
|
|
|
geo_areas.select do |area|
|
|
|
|
area.source == GeoArea.sources.fetch(:quartier_prioritaire)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-23 15:38:20 +02:00
|
|
|
def parcelles_agricoles
|
|
|
|
geo_areas.select do |area|
|
|
|
|
area.source == GeoArea.sources.fetch(:parcelle_agricole)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-19 11:09:13 +01:00
|
|
|
def selection_utilisateur
|
|
|
|
geo_areas.find(&:selection_utilisateur?)
|
|
|
|
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
|
|
|
|
|
2018-10-16 13:01:12 +02:00
|
|
|
def position
|
|
|
|
if dossier.present?
|
|
|
|
dossier.geo_position
|
|
|
|
else
|
|
|
|
lon = "2.428462"
|
|
|
|
lat = "46.538192"
|
|
|
|
zoom = "13"
|
|
|
|
|
|
|
|
{ lon: lon, lat: lat, zoom: zoom }
|
|
|
|
end
|
|
|
|
end
|
2018-10-17 12:02:34 +02:00
|
|
|
|
2018-10-31 13:27:56 +01:00
|
|
|
def geo_json
|
2018-11-30 13:19:19 +01:00
|
|
|
@geo_json ||= begin
|
2018-12-19 11:09:13 +01:00
|
|
|
geo_area = selection_utilisateur
|
|
|
|
|
|
|
|
if geo_area
|
|
|
|
geo_area.geometry
|
2018-11-30 13:19:19 +01:00
|
|
|
else
|
2018-12-19 11:09:13 +01:00
|
|
|
geo_json_from_value
|
2018-11-30 13:19:19 +01:00
|
|
|
end
|
|
|
|
end
|
2018-10-17 12:02:34 +02:00
|
|
|
end
|
2018-10-31 13:29:25 +01:00
|
|
|
|
2018-12-03 12:49:03 +01:00
|
|
|
def selection_utilisateur_size
|
|
|
|
if geo_json.present?
|
|
|
|
geo_json['coordinates'].size
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-30 13:19:19 +01:00
|
|
|
def to_render_data
|
|
|
|
{
|
|
|
|
position: position,
|
2018-12-19 11:09:13 +01:00
|
|
|
selection: user_geo_area&.geometry,
|
2018-11-30 13:19:19 +01:00
|
|
|
quartiersPrioritaires: quartiers_prioritaires? ? quartiers_prioritaires.as_json(except: :properties) : [],
|
|
|
|
cadastres: cadastres? ? cadastres.as_json(except: :properties) : [],
|
|
|
|
parcellesAgricoles: parcelles_agricoles? ? parcelles_agricoles.as_json(except: :properties) : []
|
|
|
|
}
|
2018-10-31 13:29:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_geo_area
|
2018-12-19 11:09:13 +01:00
|
|
|
geo_area = selection_utilisateur
|
|
|
|
|
|
|
|
if geo_area.present?
|
|
|
|
geo_area
|
|
|
|
elsif geo_json_from_value.present?
|
2018-10-31 13:29:25 +01:00
|
|
|
GeoArea.new(
|
2018-12-19 11:09:13 +01:00
|
|
|
geometry: geo_json_from_value,
|
2018-10-31 13:29:25 +01:00
|
|
|
source: GeoArea.sources.fetch(:selection_utilisateur)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-12-28 17:59:14 +01:00
|
|
|
|
2018-12-19 11:09:13 +01:00
|
|
|
def geo_json_from_value
|
|
|
|
@geo_json_from_value ||= begin
|
|
|
|
parsed_value = value.blank? ? nil : JSON.parse(value)
|
|
|
|
# We used to store in the value column a json array with coordinates.
|
|
|
|
if parsed_value.is_a?(Array)
|
|
|
|
# Empty array is sent instead of blank to distinguish between empty and error
|
|
|
|
if parsed_value.empty?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
# If it is a coordinates array, format it as a GEO-JSON
|
|
|
|
JSON.parse(GeojsonService.to_json_polygon_for_selection_utilisateur(parsed_value))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# It is already a GEO-JSON
|
|
|
|
parsed_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-28 17:59:14 +01:00
|
|
|
def for_api
|
2018-12-19 11:09:13 +01:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def for_export
|
|
|
|
nil
|
2018-12-28 17:59:14 +01:00
|
|
|
end
|
2018-10-16 13:01:12 +02:00
|
|
|
end
|