2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: geo_areas
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# geometry :jsonb
|
|
|
|
# properties :jsonb
|
|
|
|
# source :string
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# champ_id :bigint
|
|
|
|
# geo_reference_id :string
|
|
|
|
#
|
2018-10-16 13:01:12 +02:00
|
|
|
class GeoArea < ApplicationRecord
|
2020-07-20 16:50:12 +02:00
|
|
|
belongs_to :champ, optional: false
|
2018-10-16 13:01:12 +02:00
|
|
|
|
|
|
|
store :properties, accessors: [
|
2020-06-04 11:32:59 +02:00
|
|
|
:description,
|
2018-10-16 13:01:12 +02:00
|
|
|
:surface_intersection,
|
|
|
|
:surface_parcelle,
|
|
|
|
:numero,
|
|
|
|
:feuille,
|
|
|
|
:section,
|
|
|
|
:code_dep,
|
|
|
|
:nom_com,
|
|
|
|
:code_com,
|
|
|
|
:code_arr,
|
|
|
|
:code,
|
|
|
|
:nom,
|
2018-10-23 15:38:20 +02:00
|
|
|
:commune,
|
|
|
|
:culture,
|
|
|
|
:code_culture,
|
|
|
|
:surface,
|
|
|
|
:bio
|
2018-10-16 13:01:12 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
enum source: {
|
|
|
|
quartier_prioritaire: 'quartier_prioritaire',
|
2018-10-23 15:38:20 +02:00
|
|
|
cadastre: 'cadastre',
|
2018-10-31 13:29:25 +01:00
|
|
|
parcelle_agricole: 'parcelle_agricole',
|
|
|
|
selection_utilisateur: 'selection_utilisateur'
|
2018-10-16 13:01:12 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
scope :selections_utilisateur, -> { where(source: sources.fetch(:selection_utilisateur)) }
|
2018-10-16 13:01:12 +02:00
|
|
|
scope :quartiers_prioritaires, -> { where(source: sources.fetch(:quartier_prioritaire)) }
|
|
|
|
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
|
2018-10-23 15:38:20 +02:00
|
|
|
scope :parcelles_agricoles, -> { where(source: sources.fetch(:parcelle_agricole)) }
|
2018-12-19 11:09:13 +01:00
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
def to_feature
|
|
|
|
{
|
|
|
|
type: 'Feature',
|
2020-10-08 16:02:52 +02:00
|
|
|
geometry: safe_geometry,
|
2020-04-30 15:49:43 +02:00
|
|
|
properties: properties.symbolize_keys.merge(
|
|
|
|
source: source,
|
|
|
|
area: area,
|
|
|
|
length: length,
|
|
|
|
id: id,
|
|
|
|
champ_id: champ.stable_id,
|
|
|
|
dossier_id: champ.dossier_id
|
|
|
|
).compact
|
2020-04-09 17:32:20 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-10-08 16:02:52 +02:00
|
|
|
def safe_geometry
|
|
|
|
RGeo::GeoJSON.encode(rgeo_geometry)
|
|
|
|
end
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
def rgeo_geometry
|
|
|
|
RGeo::GeoJSON.decode(geometry.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
|
2020-05-05 10:26:13 +02:00
|
|
|
rescue RGeo::Error::InvalidGeometry
|
|
|
|
nil
|
2018-12-19 11:09:13 +01:00
|
|
|
end
|
2020-04-14 10:24:30 +02:00
|
|
|
|
|
|
|
def self.from_feature_collection(feature_collection)
|
|
|
|
feature_collection[:features].map do |feature|
|
|
|
|
GeoArea.new(
|
|
|
|
source: feature[:properties].delete(:source),
|
|
|
|
properties: feature[:properties],
|
|
|
|
geometry: feature[:geometry]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-04-15 15:43:38 +02:00
|
|
|
|
|
|
|
def area
|
2020-10-01 11:32:56 +02:00
|
|
|
if polygon?
|
|
|
|
GeojsonService.area(geometry.deep_symbolize_keys).round(1)
|
2020-04-15 15:43:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def length
|
2020-10-01 11:33:15 +02:00
|
|
|
if line?
|
|
|
|
GeojsonService.length(geometry.deep_symbolize_keys).round(1)
|
2020-04-15 15:43:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
|
|
|
if point?
|
2020-09-23 11:34:10 +02:00
|
|
|
Geo::Coord.new(*rgeo_geometry.coordinates.reverse).to_s
|
2020-04-15 15:43:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def line?
|
|
|
|
geometry['type'] == 'LineString'
|
|
|
|
end
|
|
|
|
|
|
|
|
def polygon?
|
|
|
|
geometry['type'] == 'Polygon'
|
|
|
|
end
|
|
|
|
|
|
|
|
def point?
|
|
|
|
geometry['type'] == 'Point'
|
|
|
|
end
|
2018-10-16 13:01:12 +02:00
|
|
|
end
|