demarches-normaliennes/app/models/geo_area.rb

96 lines
2 KiB
Ruby
Raw Normal View History

2018-10-16 13:01:12 +02:00
class GeoArea < ApplicationRecord
belongs_to :champ
store :properties, accessors: [
: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
}
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
def to_feature
{
type: 'Feature',
geometry: geometry,
properties: properties.symbolize_keys.merge(
source: source,
area: area,
length: length,
id: id,
champ_id: champ.stable_id,
dossier_id: champ.dossier_id
).compact
}
end
def rgeo_geometry
RGeo::GeoJSON.decode(geometry.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
rescue RGeo::Error::InvalidGeometry
nil
2018-12-19 11:09:13 +01:00
end
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
def area
if polygon? && RGeo::Geos.supported?
rgeo_geometry.area.round(1)
end
end
def length
if line? && RGeo::Geos.supported?
rgeo_geometry.length.round(1)
end
end
def location
if point?
Geo::Coord.new(*rgeo_geometry.coordinates).to_s
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