 ORDER BY loc.cd_ref;
 
 
-
+-----------
+--INDEXES--
+-----------
+CREATE INDEX _synthese_t_sources ON synthese USING btree (id_source);
+
+CREATE INDEX i_synthese_cd_nom ON synthese USING btree (cd_nom);
+
+CREATE INDEX i_synthese_date_min ON synthese USING btree (date_min DESC);
+
+CREATE INDEX i_synthese_date_max ON synthese USING btree (date_max DESC);
+
+CREATE INDEX i_synthese_altitude_min ON synthese USING btree (altitude_min);
+
+CREATE INDEX i_synthese_altitude_max ON synthese USING btree (altitude_max);
+
+CREATE INDEX i_synthese_id_dataset ON synthese USING btree (id_dataset);
+
+CREATE INDEX i_synthese_the_geom_local ON synthese USING gist (the_geom_local);
+
+CREATE INDEX i_synthese_the_geom_4326 ON synthese USING gist (the_geom_4326);
+
+CREATE INDEX i_synthese_the_geom_point ON synthese USING gist (the_geom_point);
+
+CREATE UNIQUE INDEX i_unique_cd_ref_vm_min_max_for_taxons ON gn_synthese.vm_min_max_for_taxons USING btree (cd_ref);
+--REFRESH MATERIALIZED VIEW CONCURRENTLY gn_synthese.vm_min_max_for_taxons;
+
+-------------
+--FUNCTIONS--
+-------------
+CREATE OR REPLACE FUNCTION gn_synthese.fct_calculate_min_max_for_taxon(mycdnom integer)
+  RETURNS TABLE(cd_ref int, nbobs bigint,  daymin int, daymax int, altitudemin int, altitudemax int, bbox4326 geometry) AS
+$BODY$
+  BEGIN
+    --USAGE (getting all fields): SELECT * FROM gn_synthese.fct_calculate_min_max_for_taxon(351);
+    --USAGE (getting one or more field) : SELECT cd_ref, bbox4326 FROM gn_synthese.fct_calculate_min_max_for_taxon(351)
+    --See field names and types in TABLE declaration above
+    --RETURN one row for the supplied cd_ref or cd_nom
+    --This function can be use in a FROM clause, like a table or a view
+	RETURN QUERY SELECT * FROM gn_synthese.vm_min_max_for_taxons WHERE cd_ref = taxonomie.find_cdref(mycdnom);
+  END;
+$BODY$
+LANGUAGE plpgsql VOLATILE
+COST 100;
+
+CREATE OR REPLACE FUNCTION fct_tri_refresh_vm_min_max_for_taxons()
+  RETURNS trigger AS
+$BODY$
+begin
+        PERFORM REFRESH MATERIALIZED VIEW CONCURRENTLY gn_synthese.vm_min_max_for_taxons;
+end;
+$BODY$
+  LANGUAGE plpgsql VOLATILE
+  COST 100;
+
+
+---------
+--VIEWS--
+---------
 CREATE OR REPLACE VIEW v_synthese_for_web_app AS
 WITH nomenclatures AS (
   SELECT

     JOIN ref_nomenclatures.t_nomenclatures n19 ON n19.cd_nomenclature = s.cd_nomenclature_source_status
     JOIN ref_nomenclatures.t_nomenclatures n20 ON n19.cd_nomenclature = s.cd_nomenclature_determination_method
     ;
-----------
--INDEXES--
-----------
CREATE INDEX _synthese_t_sources ON synthese USING btree (id_source);

CREATE INDEX i_synthese_cd_nom ON synthese USING btree (cd_nom);

CREATE INDEX i_synthese_date_min ON synthese USING btree (date_min DESC);

CREATE INDEX i_synthese_date_max ON synthese USING btree (date_max DESC);

CREATE INDEX i_synthese_altitude_min ON synthese USING btree (altitude_min);

CREATE INDEX i_synthese_altitude_max ON synthese USING btree (altitude_max);

CREATE INDEX i_synthese_id_dataset ON synthese USING btree (id_dataset);

CREATE INDEX i_synthese_the_geom_local ON synthese USING gist (the_geom_local);

CREATE INDEX i_synthese_the_geom_4326 ON synthese USING gist (the_geom_4326);

CREATE INDEX i_synthese_the_geom_point ON synthese USING gist (the_geom_point);

CREATE UNIQUE INDEX i_unique_cd_ref_vm_min_max_for_taxons ON gn_synthese.vm_min_max_for_taxons USING btree (cd_ref);
--REFRESH MATERIALIZED VIEW CONCURRENTLY gn_synthese.vm_min_max_for_taxons;

-------------
--FUNCTIONS--
-------------
CREATE OR REPLACE FUNCTION gn_synthese.fct_calculate_min_max_for_taxon(mycdnom integer)
  RETURNS TABLE(cd_ref int, nbobs bigint,  daymin int, daymax int, altitudemin int, altitudemax int, bbox4326 geometry) AS
$BODY$
  BEGIN
    --USAGE (getting all fields): SELECT * FROM gn_synthese.fct_calculate_min_max_for_taxon(351);
    --USAGE (getting one or more field) : SELECT cd_ref, bbox4326 FROM gn_synthese.fct_calculate_min_max_for_taxon(351)
    --See field names and types in TABLE declaration above
    --RETURN one row for the supplied cd_ref or cd_nom
    --This function can be use in a FROM clause, like a table or a view
	RETURN QUERY SELECT * FROM gn_synthese.vm_min_max_for_taxons WHERE cd_ref = taxonomie.find_cdref(mycdnom);
  END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

CREATE OR REPLACE FUNCTION fct_tri_refresh_vm_min_max_for_taxons()
  RETURNS trigger AS
$BODY$
begin
        PERFORM REFRESH MATERIALIZED VIEW CONCURRENTLY gn_synthese.vm_min_max_for_taxons;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;


------------

  EXECUTE PROCEDURE fct_tri_refresh_vm_min_max_for_taxons();


--------
--DATA--
--------
