 		subst_id, subst_sum, area_ha, geom_type, geom
 	FROM	model_draft.ego_grid_mv_griddistrict;
 
-/* -- Create Test Area
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_ta CASCADE;
-CREATE TABLE 		model_draft.ego_grid_mv_griddistrict_ta AS
-	SELECT	dis.*
-	FROM	model_draft.ego_grid_mv_griddistrict AS dis
-	WHERE	subst_id = '372' OR
-		subst_id = '387' OR
-		subst_id = '373' OR
-		subst_id = '407' OR
-		subst_id = '403' OR
-		subst_id = '482' OR
-		subst_id = '416' OR
-		subst_id = '425' OR
-		subst_id = '491' OR
-		subst_id = '368' OR
-		subst_id = '360' OR
-		subst_id = '571' OR
-		subst_id = '593';
-
--- PK
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_ta
-	ADD PRIMARY KEY (subst_id);
-
--- index GIST (geom)
-CREATE INDEX	grid_district_ta_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_ta
-	USING	GIST (geom);
-
--- grant (oeuser)
-GRANT ALL ON TABLE	model_draft.ego_grid_mv_griddistrict_ta TO oeuser WITH GRANT OPTION;
-ALTER TABLE		model_draft.ego_grid_mv_griddistrict_ta OWNER TO oeuser; */
-
-
-
--- NEW PART
----------- ---------- ----------
-
--- Grid Districts with Multipolygons
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_union CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_union (
-	subst_id	integer,
-	geom		geometry(MultiPolygon,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_union_pkey PRIMARY KEY (subst_id) );
-
--- insert mvgd
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_union (subst_id,geom)
-	SELECT DISTINCT ON 	(subst_id)
-				subst_id AS subst_id,
-				ST_MULTI(ST_UNION(geom)) ::geometry(MultiPolygon,3035) AS geom
-			FROM	model_draft.ego_grid_mv_griddistrict_collect
-		GROUP BY 	subst_id;
-
--- index GIST (geom)
-CREATE INDEX  	ego_grid_mv_griddistrict_union_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_union USING GIST (geom);
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_union OWNER TO oeuser;
-
--- metadata
-COMMENT ON TABLE model_draft.ego_grid_mv_griddistrict_union IS '{
-	"comment": "eGoDP - Temporary table",
-	"version": "v0.2.5" }' ;
-
--- Clean Polygons and Snap to Grid
-UPDATE 	model_draft.ego_grid_mv_griddistrict_union AS t1
-	SET  	geom = t2.geom
-	FROM	(SELECT	dis.subst_id,
-			ST_SnapToGrid(ST_MULTI(ST_BUFFER(ST_BUFFER(dis.geom,0.1),-0.1)),1) ::geometry(MultiPolygon,3035) AS geom
-		FROM	model_draft.ego_grid_mv_griddistrict_union AS dis
-		)AS t2
-	WHERE  	t1.subst_id = t2.subst_id;
-
--- dump
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_dump (
-	id		serial,
-	subst_id	integer,
-	subst_cnt	integer,
-	geom_point	geometry(Point,3035),
-	geom		geometry(Polygon,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_dump_pkey PRIMARY KEY (id) );
-
--- insert dump
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump (subst_id,geom)
-	SELECT	subst_id,
-		(ST_DUMP(geom)).geom AS geom
-	FROM	model_draft.ego_grid_mv_griddistrict_union;
-
--- index GIST (geom)
-CREATE INDEX  	ego_grid_mv_griddistrict_dump_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump USING GIST (geom);
-
--- index GIST (geom_point)
-CREATE INDEX  	ego_grid_mv_griddistrict_dump_geom_point_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump USING GIST (geom_point);	
-
--- hvmv substation count
-UPDATE 	model_draft.ego_grid_mv_griddistrict_dump AS t1
-	SET  	subst_cnt = t2.subst_cnt
-	FROM	(SELECT	a.id AS id,
-			COUNT(b.geom)::integer AS subst_cnt
-		FROM	model_draft.ego_grid_mv_griddistrict_dump AS a,
-			model_draft.ego_grid_hvmv_substation AS b
-		WHERE  	a.geom && b.geom AND
-			ST_CONTAINS(a.geom,b.geom)
-		GROUP BY a.id
-		)AS t2
-	WHERE  	t1.id = t2.id;
-
--- geom_point
-UPDATE 	model_draft.ego_grid_mv_griddistrict_dump AS t1
-	SET  	geom_point = t2.geom_point
-	FROM	(SELECT	id,
-			ST_PointOnSurface(geom) AS geom_point
-		FROM	model_draft.ego_grid_mv_griddistrict_dump
-		WHERE  	subst_cnt IS NULL
-		)AS t2
-	WHERE  	t1.id = t2.id;
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump OWNER TO oeuser;
-
--- metadata
-COMMENT ON TABLE model_draft.ego_grid_mv_griddistrict_dump IS '{
-	"comment": "eGoDP - Temporary table",
-	"version": "v0.2.5" }' ;
-
-
--- dump centre fragments (a)
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump_0sub CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_dump_0sub (
-	id		serial,
-	subst_id	integer,
-	subst_cnt	integer,
-	geom_point	geometry(Point,3035),
-	geom		geometry(Polygon,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_dump_0sub_pkey PRIMARY KEY (id) );
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_0sub OWNER TO oeuser;
-
--- insert dump
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump_0sub (subst_id,subst_cnt,geom_point,geom)
-	SELECT	subst_id,
-		subst_cnt,
-		geom_point,
-		geom
-	FROM	model_draft.ego_grid_mv_griddistrict_dump
-	WHERE	subst_cnt IS NULL;
-
--- index GIST (geom)
-CREATE INDEX  	ego_grid_mv_griddistrict_dump_0sub_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump_0sub USING GIST (geom);
-
-
--- dump with substation without fragments (b)
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump_1sub CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_dump_1sub (
-	id		serial,
-	subst_id	integer,
-	subst_cnt	integer,
-	geom		geometry(Polygon,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_dump_1sub_pkey PRIMARY KEY (id) );
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_1sub OWNER TO oeuser;
-
--- insert dump
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump_1sub (subst_id,subst_cnt,geom)
-	SELECT	subst_id,
-		subst_cnt,
-		geom
-	FROM	model_draft.ego_grid_mv_griddistrict_dump
-	WHERE	subst_cnt = 1;
-
--- index GIST (geom)
-CREATE INDEX  	ego_grid_mv_griddistrict_dump_1sub_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump_1sub USING GIST (geom);
-
-
--- Next Neighbor
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump_nn CASCADE;
-CREATE TABLE 		model_draft.ego_grid_mv_griddistrict_dump_nn AS
-	SELECT DISTINCT ON (a.id)
-		a.id 		AS a_id,
-		a.geom_point	AS a_geom_point,
-		a.geom		AS a_geom,
-		b.id 		AS b_id,
-		b.subst_id 	AS subst_id,
-		b.subst_cnt	AS b_subst_cnt,
-		b.geom		AS b_geom,
-		ST_Distance(a.geom_point,ST_ExteriorRing(b.geom)) AS distance
-	FROM 	model_draft.ego_grid_mv_griddistrict_dump_0sub AS a,
-		model_draft.ego_grid_mv_griddistrict_dump_1sub AS b
-	WHERE 	ST_DWithin(a.geom_point,ST_ExteriorRing(b.geom), 50000) -- In a 50 km radius
-	ORDER BY a.id, ST_Distance(a.geom_point,ST_ExteriorRing(b.geom));
-
--- PK
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_nn
-	ADD PRIMARY KEY (a_id);
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_nn OWNER TO oeuser;
-
--- metadata
-COMMENT ON TABLE model_draft.ego_grid_mv_griddistrict_dump_nn IS '{
-	"comment": "eGoDP - Temporary table",
-	"version": "v0.2.5" }' ;
-
--- ego scenario log (version,io,schema_name,table_name,script_name,comment)
-SELECT ego_scenario_log('v0.2.5','temp','model_draft','ego_grid_mv_griddistrict_dump_nn','ego_dp_mv_griddistrict.sql',' ');
-
-
--- connect nn points
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump_nn_line CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_dump_nn_line (
-	id		serial,
-	a_id		integer,
-	subst_id	integer,
-	geom		geometry(LineString,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_dump_nn_line_pkey PRIMARY KEY (id) );
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_nn_line OWNER TO oeuser;
-
-
--- insert line
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump_nn_line (a_id,subst_id,geom)
-	SELECT	a.a_id,
-		a.subst_id,
-		ST_ShortestLine(
-			a_geom_point ::geometry(Point,3035),
-			ST_ExteriorRing(b.geom) ::geometry(LineString,3035)
-		) ::geometry(LineString,3035) AS geom
-	FROM	model_draft.ego_grid_mv_griddistrict_dump_nn AS a,
-		model_draft.ego_grid_mv_griddistrict_dump_1sub AS b
-	WHERE	b.subst_id = a.subst_id;
-
--- index GIST (geom)
-CREATE INDEX  	ego_grid_mv_griddistrict_dump_nn_line_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump_nn_line USING GIST (geom);
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_nn_line OWNER TO oeuser;
-
--- metadata
-COMMENT ON TABLE model_draft.ego_grid_mv_griddistrict_dump_nn_line IS '{
-	"comment": "eGoDP - Temporary table",
-	"version": "v0.2.5" }' ;
-
-
--- collect
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump_nn_collect CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_dump_nn_collect (
-	id		serial,
-	subst_id	integer,
-	geom		geometry(Polygon,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_dump_nn_collect_pkey PRIMARY KEY (id) );
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_nn_collect OWNER TO oeuser;
-
--- insert a
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump_nn_collect (subst_id,geom)
-	SELECT	subst_id,
-		a_geom
-	FROM	model_draft.ego_grid_mv_griddistrict_dump_nn
-	ORDER BY subst_id;
-
--- insert b
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump_nn_collect (subst_id,geom)
-	SELECT	subst_id,
-		b_geom
-	FROM	model_draft.ego_grid_mv_griddistrict_dump_nn
-	ORDER BY subst_id;
-
--- index GIST (geom)
-CREATE INDEX	ego_grid_mv_griddistrict_dump_nn_collect_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump_nn_collect USING GIST (geom);
-
--- clean
-UPDATE 	model_draft.ego_grid_mv_griddistrict_dump_nn_collect AS t1
-	SET  	geom = t2.geom
-	FROM	(SELECT	id,
-			ST_BUFFER(ST_BUFFER(geom,1,'join=mitre'),-1,'join=mitre') ::geometry(Polygon,3035) AS geom
-		FROM	model_draft.ego_grid_mv_griddistrict_dump_nn_collect
-		WHERE 	subst_id = subst_id
-		)AS t2
-	WHERE  	t1.id = t2.id;
-
-
--- union
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union CASCADE;
-CREATE TABLE         	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union (
-	subst_id	integer,
-	geom		geometry(MultiPolygon,3035),
-	CONSTRAINT ego_grid_mv_griddistrict_dump_nn_collect_union_pkey PRIMARY KEY (subst_id) );
-
--- grant (oeuser)
-ALTER TABLE	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union OWNER TO oeuser;
-
--- insert union
-INSERT INTO	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union (subst_id,geom)
-	SELECT	un.subst_id 	::integer AS subst_id,
-		un.geom 	::geometry(MultiPolygon,3035) AS geom
-	FROM	(SELECT DISTINCT ON (subst_id)
-			subst_id,
-			ST_MULTI(ST_UNION(geom)) AS geom
-		FROM	model_draft.ego_grid_mv_griddistrict_dump_nn_collect
-		GROUP BY subst_id) AS un;
-
--- index GIST (geom)
-CREATE INDEX	ego_grid_mv_griddistrict_dump_nn_collect_union_geom_idx
-	ON	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union USING GIST (geom);
-
--- metadata
-COMMENT ON TABLE model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union IS '{
-	"comment": "eGoDP - Temporary table",
-	"version": "v0.2.5" }' ;
-
--- ego scenario log (version,io,schema_name,table_name,script_name,comment)
-SELECT ego_scenario_log('v0.2.5','temp','model_draft','ego_grid_mv_griddistrict_dump_nn_collect_union','ego_dp_mv_griddistrict.sql',' ');
-
--- Clean Polygons and Snap to Grid
-UPDATE 	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union AS t1
-	SET  	geom = t2.geom
-	FROM	(SELECT	subst_id,
-			ST_MULTI(ST_BUFFER(ST_BUFFER(geom,2,'join=mitre'),-2,'join=mitre')) ::geometry(MultiPolygon,3035) AS geom
-		FROM	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union
-		)AS t2
-	WHERE  	t1.subst_id = t2.subst_id;
-
-
-
--- -- Validate (geom)
--- DROP VIEW IF EXISTS	model_draft.ego_grid_mv_griddistrict_union_error_geom_view CASCADE;
--- CREATE VIEW		model_draft.ego_grid_mv_griddistrict_union_error_geom_view AS
--- 	SELECT	test.id,
--- 		test.error,
--- 		reason(ST_IsValidDetail(test.geom)) AS error_reason,
--- 		ST_SetSRID(location(ST_IsValidDetail(test.geom)),3035) ::geometry(Point,3035) AS error_location
--- 	FROM	(
--- 		SELECT	source.subst_id AS id,				-- PK
--- 			ST_IsValid(source.geom) AS error,
--- 			source.geom AS geom
--- 		FROM	model_draft.ego_grid_mv_griddistrict_dump_nn_collect_union AS source	-- Table
--- 		) AS test
--- 	WHERE	test.error = FALSE;
--- 
--- -- grant (oeuser)
--- GRANT ALL ON TABLE	model_draft.ego_grid_mv_griddistrict_union_error_geom_view TO oeuser WITH GRANT OPTION;
--- ALTER TABLE		model_draft.ego_grid_mv_griddistrict_union_error_geom_view OWNER TO oeuser;
--- 
--- -- Drop empty view   (OK!) -> 100ms =1
--- SELECT f_drop_view('{ego_grid_mv_griddistrict_union_error_geom_view}', 'model_draft');
-
-
-
-
--- new mvgd
-
-
+
+-- OLD after restructuring
+
+/* 
 -- create grid district
-DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict_new CASCADE;
-CREATE TABLE 		model_draft.ego_grid_mv_griddistrict_new (
+DROP TABLE IF EXISTS	model_draft.ego_grid_mv_griddistrict CASCADE;
+CREATE TABLE 		model_draft.ego_grid_mv_griddistrict (
 	subst_id	integer,
 	subst_sum	integer,
 	type1 		integer,


-- grant (oeuser)
ALTER TABLE	model_draft.ego_grid_mv_griddistrict OWNER TO oeuser;

-- Count Substations in Grid Districts
UPDATE 	model_draft.ego_grid_mv_griddistrict AS t1

			ST_CONTAINS(a.geom,b.geom)
		GROUP BY a.id
		)AS t2
	WHERE  	t1.id = t2.id; */
