· 8 years ago · Mar 14, 2018, 10:38 PM
1sql = "DROP TABLE IF Exists tmpTable; create table tmpTable ( ID varchar(50), Geom geometry(Geometry,4326), Touchin varchar(50) ); create index idx_tmp on tmpTable using GIST(Geom); ";
2 CommandText = sql;
3 ExecuteNonQuery();
4
5 sql = "";
6 for (int i = 0; i < infos.Count(); i++)
7 {
8 sql += "INSERT INTO tmpTable SELECT '" + infos[i].ID + "', ST_GeomFromText('" + infos[i].wkt + "', 4326), '0';";
9 }
10 CommandText = sql;
11 ExecuteNonQuery();
12
13 CommandText = "update tmpTable set touchin = (select id from tmpTable as t where st_intersects(st_buffer(geom, 0.0001), (select geom from tmpTable as t2 where t2.ID = tmpTable.ID ) ) and t.ID <> tmpTable.ID limit 1)";
14 ExecuteNonQuery();
15
16 CommandText = "select count(*) from tmpTable where touchin is not null";
17 long touching = (long)ExecuteScalar();
18 string thisId = "";
19 // string otherId = "";
20 while (touching > 0)
21 {
22 CommandText = "select touchin, count(*) from tmpTable where touchin is not null group by touchin order by 2 desc limit 1";
23 //CommandText = "select id, touchin from tmpTable where touchin is not null";
24 using (var prdr = ExecuteReader())
25 {
26 CommandText = "";
27 if (prdr.Read())
28 {
29 thisId = prdr.GetString(0);
30 // otherID = prdr.GetString(1);
31 CommandText = @"update tmpTable set geom = st_union(unioned)
32 from (select array_agg(geom) as unioned from tmpTable where touchin = '" + thisId + "' or id = '" + thisId + @"') as data
33 where id = '" + thisId + "'";
34 // CommandText = "update tmpTable set geom = st_union(geom, (select geom from tmpTable where ID = '" + otherId + "')) where id = '" + thisId + "'";
35 }
36 }
37
38 if (!string.IsNullOrEmpty(CommandText))
39 {
40 ExecuteNonQuery();
41 //CommandText = "update tmpTable set geom = null, touchin = null where ID = '" + otherId + "'";
42 CommandText = "update tmpTable set geom = null, touchin = null where touchin = '" + thisId + "'";
43 ExecuteNonQuery();
44 }
45
46 CommandText = "update tmpTable set touchin = (select id from tmpTable as t where st_intersects(st_buffer(geom, 0.0001), (select geom from tmpTable as t2 where t2.ID = tmpTable.ID ) ) and t.ID <> tmpTable.ID limit 1)";
47 ExecuteNonQuery();
48
49 CommandText = "select count(*) from tmpTable where touchin is not null";
50 touching = (long)ExecuteScalar();
51 }
52
53INSERT INTO tmpTable SELECT '872538', ST_GeomFromText('POLYGON((-101.455035985 26.8835084441,-101.455035985 26.8924915559,-101.444964015 26.8924915559,-101.444964015 26.8835084441,-101.455035985 26.8835084441))', 4326), '0';
54INSERT INTO tmpTable SELECT '872550', ST_GeomFromText('POLYGON((-93.9484752173 46.0755084441,-93.9484752173 46.0844915559,-93.9355247827 46.0844915559,-93.9355247827 46.0755084441,-93.9484752173 46.0755084441))', 4326), '0';
55INSERT INTO tmpTable SELECT '872552', ST_GeomFromText('POLYGON((-116.060688575 47.8105084441,-116.060688575 47.8194915559,-116.047311425 47.8194915559,-116.047311425 47.8105084441,-116.060688575 47.8105084441))', 4326), '0';
56INSERT INTO tmpTable SELECT '872553', ST_GeomFromText('POLYGON((-116.043688832 47.8125084441,-116.043688832 47.8214915559,-116.030311168 47.8214915559,-116.030311168 47.8125084441,-116.043688832 47.8125084441))', 4326), '0';
57INSERT INTO tmpTable SELECT '872557', ST_GeomFromText('POLYGON((-80.6380222359 26.5725084441,-80.6380222359 26.5814915559,-80.6279777641 26.5814915559,-80.6279777641 26.5725084441,-80.6380222359 26.5725084441))', 4326), '0';
58INSERT INTO tmpTable SELECT '872558', ST_GeomFromText('POLYGON((-80.6520223675 26.5755084441,-80.6520223675 26.5844915559,-80.6419776325 26.5844915559,-80.6419776325 26.5755084441,-80.6520223675 26.5755084441))', 4326), '0';
59INSERT INTO tmpTable SELECT '872559', ST_GeomFromText('POLYGON((-80.6400224991 26.5785084441,-80.6400224991 26.5874915559,-80.6299775009 26.5874915559,-80.6299775009 26.5785084441,-80.6400224991 26.5785084441))', 4326), '0';
60INSERT INTO tmpTable SELECT '872560', ST_GeomFromText('POLYGON((-80.6530226307 26.5815084441,-80.6530226307 26.5904915559,-80.6429773693 26.5904915559,-80.6429773693 26.5815084441,-80.6530226307 26.5815084441))', 4326), '0';
61INSERT INTO tmpTable SELECT '872568', ST_GeomFromText('POLYGON((-90.7892258584 30.7365084441,-90.7892258584 30.7454915559,-90.7787741416 30.7454915559,-90.7787741416 30.7365084441,-90.7892258584 30.7365084441))', 4326), '0';
62INSERT INTO tmpTable SELECT '872569', ST_GeomFromText('POLYGON((-90.7832259127 30.7375084441,-90.7832259127 30.7464915559,-90.7727740873 30.7464915559,-90.7727740873 30.7375084441,-90.7832259127 30.7375084441))', 4326), '0';
63
64SELECT ST_Union(geom) FROM tmpTable;
65
66SELECT (ST_Dump(geom)).geom FROM (SELECT ST_Union(geom) AS geom FROM tmpTable) sq;
67
68SELECT unnest(ST_ClusterWithin(geom, 0.0001)) AS grp FROM tmpTable;
69
70SELECT ST_UnaryUnion(grp) FROM
71(SELECT unnest(ST_ClusterWithin(geom, 0.0001)) AS grp FROM tmpTable) sq;