CREATE TABLE location
(
  location_id serial NOT NULL,
  sourceid character varying(30),
  reference_id integer,
  parent_id integer,
  reallatitude double precision,
  reallongitude double precision,
  locationaccuracy double precision,
  confidentialitystatus integer NOT NULL,
  confidentialityreason character varying(200),
  latitude double precision,
  longitude double precision,
  -- ...
  accessioncode character varying(255),
  sublocationxposition double precision,
  sublocationyposition double precision,
  namedplace_id integer,
  CONSTRAINT location_pkey PRIMARY KEY (location_id ),
  CONSTRAINT location_namedplace_id FOREIGN KEY (namedplace_id)
      REFERENCES namedplace (namedplace_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT location_parent_id FOREIGN KEY (parent_id)
      REFERENCES location (location_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT location_reference_id FOREIGN KEY (reference_id)
      REFERENCES reference (reference_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT location_keys UNIQUE (reference_id , parent_id , sourceid )
);

CREATE TABLE locationevent -- VegBank's observation table.
(
  locationevent_id serial NOT NULL,
  previous_id integer,
  location_id integer NOT NULL,
  project_id integer,
  sourceid character varying(30),
  -- ...
  accessioncode character varying(255),
  CONSTRAINT locationevent_pkey PRIMARY KEY (locationevent_id ),
  CONSTRAINT locationevent_location_id FOREIGN KEY (location_id)
      REFERENCES location (location_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT locationevent_project_id FOREIGN KEY (project_id)
      REFERENCES project (project_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT locationevent_keys UNIQUE (location_id , project_id , sourceid )
);

CREATE TABLE taxonoccurrence -- VegBank's taxonobservation table.
(
  taxonoccurrence_id serial NOT NULL,
  locationevent_id integer NOT NULL,
  authorplantname character varying(255),
  reference_id integer,
  taxoninferencearea double precision,
  emb_taxonoccurrence integer,
  int_origplantconcept_id integer,
  int_origplantscifull character varying(255),
  int_origplantscinamenoauth character varying(255),
  int_origplantcommon character varying(255),
  int_origplantcode character varying(255),
  int_currplantconcept_id integer,
  int_currplantscifull character varying(255),
  int_currplantscinamenoauth character varying(255),
  int_currplantcommon character varying(255),
  int_currplantcode character varying(255),
  accessioncode character varying(255),
  CONSTRAINT taxonoccurrence_pkey PRIMARY KEY (taxonoccurrence_id ),
  CONSTRAINT taxonoccurrence_locationevent_id FOREIGN KEY (locationevent_id)
      REFERENCES locationevent (locationevent_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT taxonoccurrence_reference_id FOREIGN KEY (reference_id)
      REFERENCES reference (reference_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE aggregateoccurrence -- VegBank's taxonimportance table.
(
  aggregateoccurrence_id serial NOT NULL,
  taxonoccurrence_id integer NOT NULL,
  taxonbin_id integer,
  cover double precision,
  basalarea double precision,
  biomass double precision,
  inferencearea double precision,
  stratumbase double precision,
  stratumheight double precision,
  emb_aggregateoccurrence integer,
  covercode character varying(10),
  count integer NOT NULL,
  accessioncode character varying(255),
  CONSTRAINT aggregateoccurrence_pkey PRIMARY KEY (aggregateoccurrence_id ),
  CONSTRAINT aggregateoccurrence_taxonbin_id FOREIGN KEY (taxonbin_id)
      REFERENCES taxonbin (taxonbin_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT aggregateoccurrence_taxonoccurrence_id FOREIGN KEY (taxonoccurrence_id)
      REFERENCES taxonoccurrence (taxonoccurrence_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT aggregateoccurrence_keys UNIQUE (taxonoccurrence_id , taxonbin_id )
);

CREATE TABLE individualplant -- VegBank's stemcount table.
(
  individualplant_id serial NOT NULL,
  aggregateoccurrence_id integer NOT NULL,
  height double precision,
  heightaccuracy double precision,
  emb_individualplant integer,
  sourceid character varying(20),
  accessioncode character varying(255),
  stemcount integer,
  CONSTRAINT individualplant_pkey PRIMARY KEY (individualplant_id ),
  CONSTRAINT individualplant_aggregateoccurrence_id FOREIGN KEY (aggregateoccurrence_id)
      REFERENCES aggregateoccurrence (aggregateoccurrence_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT individualplant_keys UNIQUE (aggregateoccurrence_id , sourceid )
);

CREATE TABLE stem -- VegBank's stemlocation table.
(
  stem_id serial NOT NULL,
  individualplant_id integer NOT NULL,
  sourceid character varying(20),
  xposition double precision,
  yposition double precision,
  health character varying(50),
  emb_stem integer,
  diameter double precision,
  height double precision,
  heightaccuracy double precision,
  age double precision,
  accessioncode character varying(255),
  CONSTRAINT stem_pkey PRIMARY KEY (stem_id ),
  CONSTRAINT stem_individualplant_id FOREIGN KEY (individualplant_id)
      REFERENCES individualplant (individualplant_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT stem_keys UNIQUE (individualplant_id , sourceid )
);

CREATE TABLE taxondetermination -- VegBank's taxoninterpretation table.
(
  taxondetermination_id serial NOT NULL,
  taxonoccurrence_id integer NOT NULL,
  plantconcept_id integer NOT NULL,
  party_id integer NOT NULL,
  role_id integer NOT NULL,
  determinationtype character varying(30),
  reference_id integer,
  originaldetermination boolean NOT NULL,
  currentdetermination boolean NOT NULL,
  taxonfit character varying(50),
  taxonconfidence character varying(50),
  grouptype character varying(20),
  notes text,
  notespublic boolean,
  notesmgt boolean,
  revisions boolean,
  determinationdate timestamp with time zone NOT NULL,
  emb_taxondetermination integer,
  accessioncode character varying(255),
  CONSTRAINT taxondetermination_pkey PRIMARY KEY (taxondetermination_id ),
  CONSTRAINT taxondetermination_party_id FOREIGN KEY (party_id)
      REFERENCES party (party_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT taxondetermination_plantconcept_id FOREIGN KEY (plantconcept_id)
      REFERENCES plantconcept (plantconcept_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT taxondetermination_reference_id FOREIGN KEY (reference_id)
      REFERENCES reference (reference_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT taxondetermination_role_id FOREIGN KEY (role_id)
      REFERENCES aux_role (role_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT taxondetermination_taxonoccurrence_id FOREIGN KEY (taxonoccurrence_id)
      REFERENCES taxonoccurrence (taxonoccurrence_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE specimen -- A herbarium specimen. Contains Darwin Core specimen data.
(
  specimen_id serial NOT NULL,
  individualplant_id integer,
  reference_id integer NOT NULL,
  collectioncode_dwc character varying(255),
  catalognumber_dwc character varying(255),
  collectiondate timestamp with time zone,
  collector_id integer,
  museum_id integer,
  sourceaccessionnumber character varying(100),
  accessioncode character varying(255),
  taxonoccurrence_id integer,
  CONSTRAINT specimen_pkey PRIMARY KEY (specimen_id ),
  CONSTRAINT specimen_collector_id FOREIGN KEY (collector_id)
      REFERENCES party (party_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT specimen_individualplant_id_fkey FOREIGN KEY (individualplant_id)
      REFERENCES individualplant (individualplant_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT specimen_museum_id FOREIGN KEY (museum_id)
      REFERENCES party (party_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT specimen_reference_id_fkey FOREIGN KEY (reference_id)
      REFERENCES reference (reference_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT specimen_taxonoccurrence_id FOREIGN KEY (taxonoccurrence_id)
      REFERENCES taxonoccurrence (taxonoccurrence_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT specimen_keys UNIQUE (reference_id , collectioncode_dwc , catalognumber_dwc )
);

CREATE TABLE taxonbin -- Defines a set of aggregate measurements. Used by aggregateoccurrence to define how measurements were aggregated.
(
  taxonbin_id integer NOT NULL,
  label character varying(255) NOT NULL,
  stratum_id integer,
  sizeclass_id integer,
  coverindex_id integer,
  accessioncode character varying(255),
  CONSTRAINT taxonbin_pkey PRIMARY KEY (taxonbin_id ),
  CONSTRAINT taxonbin_sizeclass_id FOREIGN KEY (sizeclass_id)
      REFERENCES sizeclass (sizeclass_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE sizeclass -- A range of size measurements used to aggregate organisms.
(
  sizeclass_id serial NOT NULL,
  mindiameter double precision,
  diameteraccuracy double precision,
  minheight double precision,
  heightaccuracy double precision,
  maxdiameter double precision,
  maxheight double precision,
  accessioncode character varying(255),
  CONSTRAINT sizeclass_pkey PRIMARY KEY (sizeclass_id )
);
