 
 -- Cutting
 
-CREATE TABLE IF NOT EXISTS calc_ego_grid_district.ego_deu_LV_cut
+CREATE TABLE IF NOT EXISTS model_draft."ego_grid_lv_cut"
 (
   id serial NOT NULL,
   geom geometry(Polygon,3035),



-- Add Dummy points to substations (18 Points)
ALTER TABLE calc_ego_substation.substations_dummy 
DROP COLUMN IF EXISTS is_dummy;

ALTER TABLE calc_ego_substation.substations_dummy 
ADD COLUMN is_dummy boolean;

UPDATE calc_ego_substation.substations_dummy 
SET is_dummy = TRUE;

ALTER TABLE calc_ego_substation.ego_deu_onts 
DROP COLUMN IF EXISTS is_dummy;

ALTER TABLE calc_ego_substation.ego_deu_onts
ADD COLUMN is_dummy boolean;

INSERT INTO calc_ego_substation.ego_deu_onts (id,geom, is_dummy)
SELECT	
	dummy.subst_id+800000,
	ST_TRANSFORM(dummy.geom,3035),
	dummy.is_dummy
FROM 	calc_ego_substation.substations_dummy AS dummy;

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


-- Execute voronoi algorithm
CREATE TABLE IF NOT EXISTS calc_ego_grid_district.ego_deu_LV_grid_districts_voronoi
(
  id serial NOT NULL,
  geom geometry(Polygon,3035),
  subst_id integer,
  CONSTRAINT ego_deu_lv_grid_districts_voronoi_pkey PRIMARY KEY (id)
);

TRUNCATE TABLE calc_ego_grid_district.ego_deu_LV_grid_districts_voronoi;

-- Begin loop over MS grid districts
DO

'WITH 
    -- Sample set of points to work with
    Sample AS (SELECT   ST_SetSRID(ST_Union(pts.geom), 0) AS geom
		FROM	model_draft.ego_grid_mvlv_substation AS pts
		WHERE pts.subst_id = ' || gd_id || ' OR is_dummy = TRUE
		),  -- INPUT 1/2
    -- Build edges and circumscribe points to generate a centroid
