 
 -- -- Find double entries (OK!) -> =18
 -- SELECT 		gid, count(*)
--- FROM 		orig_geo_rli.osm_deu_polygon_spf
+-- FROM 		orig_geo_ego.osm_deu_polygon_spf
 -- GROUP BY 	gid
 -- HAVING 		count(*) > 1;
 -- 
 -- -- --  IF double 
 -- -- Create nodubs (OK!)
--- DROP TABLE IF EXISTS	orig_geo_rli.osm_deu_polygon_spf_nodubs;
--- CREATE TABLE		orig_geo_rli.osm_deu_polygon_spf_nodubs AS
+-- DROP TABLE IF EXISTS	orig_geo_ego.osm_deu_polygon_spf_nodubs;
+-- CREATE TABLE		orig_geo_ego.osm_deu_polygon_spf_nodubs AS
 -- 	SELECT	osm.*
--- 	FROM	orig_geo_rli.osm_deu_polygon_spf AS osm
+-- 	FROM	orig_geo_ego.osm_deu_polygon_spf AS osm
 -- 	LIMIT 	0;
 -- 
 -- -- Add id (OK!)
--- ALTER TABLE 	orig_geo_rli.osm_deu_polygon_spf_nodubs
+-- ALTER TABLE 	orig_geo_ego.osm_deu_polygon_spf_nodubs
 -- 	ADD column id serial;
 -- 
 -- -- Insert data (OK!) 94ms =1567
--- INSERT INTO	orig_geo_rli.osm_deu_polygon_spf_nodubs
+-- INSERT INTO	orig_geo_ego.osm_deu_polygon_spf_nodubs
 -- 	SELECT	osm.*
--- 	FROM	orig_geo_rli.osm_deu_polygon_spf AS osm
+-- 	FROM	orig_geo_ego.osm_deu_polygon_spf AS osm
 -- ORDER BY 	gid;
 -- 
 -- -- Delete double entries (OK!) 62ms =-18
--- DELETE FROM orig_geo_rli.osm_deu_polygon_spf_nodubs
+-- DELETE FROM orig_geo_ego.osm_deu_polygon_spf_nodubs
 -- WHERE id IN (SELECT id
 --               FROM (SELECT id,
 --                              ROW_NUMBER() OVER (partition BY gid ORDER BY id) AS rnum
---                      FROM orig_geo_rli.osm_deu_polygon_spf_nodubs) t
+--                      FROM orig_geo_ego.osm_deu_polygon_spf_nodubs) t
 --               WHERE t.rnum > 1);
 -- 
 -- -- Check for doubles again (OK!)
 -- SELECT 		gid, count(*)
--- FROM 		orig_geo_rli.osm_deu_polygon_spf_nodubs
+-- FROM 		orig_geo_ego.osm_deu_polygon_spf_nodubs
 -- GROUP BY 	gid
 -- HAVING 		count(*) > 1;
 -- 
 -- -- Set PK (OK!)
--- ALTER TABLE 	orig_geo_rli.osm_deu_polygon_spf_nodubs ADD PRIMARY KEY (gid);
+-- ALTER TABLE 	orig_geo_ego.osm_deu_polygon_spf_nodubs ADD PRIMARY KEY (gid);
 -- 
 -- -- Create new index (OK!)
 -- CREATE INDEX  	osm_deu_polygon_spf_nodubs_geom_idx
---     ON    	orig_geo_rli.osm_deu_polygon_spf_nodubs
+--     ON    	orig_geo_ego.osm_deu_polygon_spf_nodubs
 --     USING     	GIST (geom);
 
 

	)AS t2
WHERE  	t1.uid = t2.uid;

---------- ---------- ----------
-- "Calculate Sectors"
---------- ---------- ----------

-- -- UPDATE area_lg (OK!) -> 100ms :812
-- UPDATE 	orig_geo_rli_spf.rli_deu_lastgebiete_spf_buffer100_unbuff AS t1
-- SET  	area_lg = t2.area
-- FROM    (
-- 	SELECT	lg.uid,
-- 		ST_AREA(ST_TRANSFORM(lg.geom,3035))/10000 AS area
-- 	FROM	orig_geo_rli_spf.rli_deu_lastgebiete_spf_buffer100_unbuff AS lg
-- 	ORDER BY lg.uid
-- 	) AS t2
-- JOIN    orig_geo_rli_spf.rli_deu_lastgebiete_spf_buffer100_unbuff USING (uid)
-- WHERE  	t1.uid = t2.uid;

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

-- "Calculate Sector Residential"   (OK!) -> 70.000ms =111.340
UPDATE 	orig_geo_rli.osm_deu_polygon_urban_buffer100_unbuffer AS t1
SET  	sector_area_residential = t2.sector_area,
	sector_count_residential = t2.sector_count,
	sector_share_residential = t2.sector_area / t2.area_ha

		osm.way_area,
		ST_AREA(ST_TRANSFORM(osm.geom,3035))/10000 AS area_ha,
		osm.tags,
		ST_TRANSFORM(osm.geom,3035) ::geometry(MultiPolygon,3035) AS geom
	FROM	orig_geo_ego.osm_deu_polygon_urban_mview AS osm
	WHERE	osm.landuse='farmyard' OR 
		osm.landuse='greenhouse_horticulture'
ORDER BY	osm.gid;
    
-- "Create Index GIST (geom)"   (OK!) -> 1.000ms =0
CREATE INDEX  	osm_deu_polygon_urban_sector_4_agricultural_mview_geom_idx
	ON	orig_geo_ego.osm_deu_polygon_urban_sector_4_agricultural_mview
	USING	GIST (geom);

-- "Create Index B-tree (gid)"   (OK!) -> 1.000ms =0
CREATE UNIQUE INDEX  	osm_deu_polygon_urban_sector_4_agricultural_mview_gid_idx
	ON		orig_geo_ego.osm_deu_polygon_urban_sector_4_agricultural_mview (gid);
	
-- "Grant oeuser"   (OK!) -> 100ms =0
GRANT ALL ON TABLE 	orig_geo_ego.osm_deu_polygon_urban_sector_4_agricultural_mview TO oeuser WITH GRANT OPTION;
ALTER TABLE		orig_geo_ego.osm_deu_polygon_urban_sector_4_agricultural_mview OWNER TO oeuser;


---------- ---------- ---------- ---------- ---------- ----------
-- "Urban Buffer (100m)"
---------- ---------- ---------- ---------- ---------- ----------

-- "Create Table"   (OK!) 200ms =0
DROP TABLE IF EXISTS  	orig_geo_ego.osm_deu_polygon_urban_buffer100;
CREATE TABLE         	orig_geo_ego.osm_deu_polygon_urban_buffer100 (
		id SERIAL,
		geom geometry(Polygon,3035),
CONSTRAINT 	osm_deu_polygon_urban_buffer100_pkey PRIMARY KEY (id));

-- "Insert Buffer"   (OK!) 1.216.000ms =129.322
INSERT INTO     orig_geo_ego.osm_deu_polygon_urban_buffer100(geom)
	SELECT	(ST_DUMP(ST_MULTI(ST_UNION(
			ST_BUFFER(ST_TRANSFORM(osm.geom,3035), 100)
		)))).geom ::geometry(Polygon,3035) AS geom
	FROM	orig_geo_ego.osm_deu_polygon_urban_mview AS osm;

-- "Create Index GIST (geom)"   (OK!) 2.000ms =0
CREATE INDEX  	osm_deu_polygon_urban_buffer100_geom_idx
    ON    	orig_geo_ego.osm_deu_polygon_urban_buffer100
    USING     	GIST (geom);
    
-- "Grant oeuser"   (OK!) -> 100ms =0
GRANT ALL ON TABLE 	orig_geo_ego.osm_deu_polygon_urban_buffer100 TO oeuser WITH GRANT OPTION;
ALTER TABLE		orig_geo_ego.osm_deu_polygon_urban_buffer100 OWNER TO oeuser;

---------- ---------- ---------- ---------- ---------- ----------
-- "Unbuffer (-100m)"
---------- ---------- ---------- ---------- ---------- ----------

-- "Create Table"   (OK!) 200ms =0
DROP TABLE IF EXISTS  	orig_geo_ego.osm_deu_polygon_urban_buffer100_unbuffer;
CREATE TABLE         	orig_geo_ego.osm_deu_polygon_urban_buffer100_unbuffer (
		uid SERIAL NOT NULL,
		geom geometry(Polygon,3035),
CONSTRAINT 	osm_deu_polygon_urban_buffer100_unbuffer_pkey PRIMARY KEY (uid));

-- "Insert Buffer"   (OK!) 93.000ms =170.088
INSERT INTO     orig_geo_ego.osm_deu_polygon_urban_buffer100_unbuffer(geom)
	SELECT	(ST_DUMP(ST_MULTI(ST_UNION(
			ST_BUFFER(ST_TRANSFORM(osm.geom,3035), -100)
		)))).geom ::geometry(Polygon,3035) AS geom
	FROM	orig_geo_ego.osm_deu_polygon_urban_buffer100 AS osm
	GROUP BY osm.id
	ORDER BY osm.id;

-- "Extend Table"   (OK!) 150ms =0
ALTER TABLE	orig_geo_ego.osm_deu_polygon_urban_buffer100_unbuffer
	ADD COLUMN zensus_sum integer,
	ADD COLUMN zensus_count integer,
	ADD COLUMN zensus_density numeric,
