CREATE TABLE av_intersection (
  gid int UNIQUE,
  t_name varchar(50),
  PRIMARY KEY (gid)
) ;
SELECT AddGeometryColumn ('public', 'av_intersection', 'the_geom', '-1', 'MULTIPOLYGON', 2);
CREATE INDEX av_intersection_gidx ON av_intersection USING GIST (the_geom) ;


CREATE OR REPLACE VIEW av_intersection_union AS
SELECT
min(p.gid) as gid,
t_name,
st_union(p.the_geom) AS the_geom
FROM av_intersection p
GROUP BY p.t_name;


CREATE OR REPLACE VIEW av_intersection_multiple_ring AS
SELECT
gid,
the_geom,
(ST_NRings(the_geom) - ST_NumInteriorRings(the_geom)) as nrings
FROM av_intersection
WHERE ST_NRings(the_geom) > (1 + ST_NumInteriorRings(the_geom));


CREATE OR REPLACE VIEW av_intersection_overlaps AS
SELECT DISTINCT 
(a.gid+b.gid*100) as gid,
a.gid a_gid,
b.gid b_gid,
a.the_geom
FROM
av_intersection a,
av_intersection b
WHERE
a.gid != b.gid AND ST_Overlaps(b.the_geom, a.the_geom);

INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (3, 't', '01060000000100000001030000000100000010000000BEC71FD635263741CF603CF5C87E55419B0BF51B36263741F3F01CB5C67E55414CB8CABE342637419BEE6769C57E5541E4D321A72D2637415EF952E9C37E5541C367CE03262637418A640892C37E5541C8077D3116263741D1DC5D06C37E5541358CD4760D26374144C06823C27E55412F01D7A4032637419C96D374C17E5541D7D2D75E00263741561E7E00C27E5541CFF70276FE253741A571A85DC37E5541E8888130042637417806F3B4C37E5541C8077D31162637413A697229C67E5541E198FBEB1B263741EA4192C6C67E5541645EFAD420263741B527522FC77E5541B0614D1B2A26374142444712C87E5541BEC71FD635263741CF603CF5C87E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (4, 't', '0106000000020000000103000000010000000C000000834FA0084926374131BB0CE1C87E55413CED6F914A26374130E63135C87E554147181C9144263741B6FB0735C57E5541774FCA6136263741DA8BE8F4C27E5541E4D321A72D2637415EF952E9C37E55414CB8CABE342637419BEE6769C57E55419B0BF51B36263741F3F01CB5C67E5541BEC71FD635263741CF603CF5C87E554139B249D638263741047B7C8CC87E5541B49C73D63B2637411F881C58C87E55413F3D47A8422637410D2A077BC87E5541834FA0084926374131BB0CE1C87E554101030000000100000004000000712E6C6F2E263741DF664697CA7E5541732E6C6F2E263741E0664697CA7E55417A9FA1782E263741FBF73B98CA7E5541712E6C6F2E263741DF664697CA7E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (8, 't', '0106000000010000000103000000010000001B0000006FA293FC07263741A331A5A3B67E5541007E519410263741BFBEB9C6B97E5541ABCCCC66232637417BB94F2FB77E55416D7EF54F2B2637418068DA1DB77E5541B9C71DDC34263741942405D8B67E554151E79968442637418F757AE9B67E5541729E1598552637410EAF64AFB87E55418440E9695C263741BA0F2FD8B97E554152F7A98C59263741858A3998BA7E55419A994FBC6D26374148C98DF5BE7E5541ABAEF719782637416456A218C27E5541934B4A0383263741CF4636B0C77E55411DE349A684263741BAFDDF81CB7E5541445B9F1A842637413937CA47CD7E5541160819799D263741124CA047CA7E5541D9466DD6A1263741586B61C7C57E55419F9F181C9F263741DABEA275C07E5541B4E86E4A9B2637413D6B7818BF7E554142B41CBE8E263741349A8EAFBB7E55416C46C91A87263741C91CCFA3B97E554137D3223176263741F0942418B97E5541B0557A766D2637410F3C9023B57E5541F3642BD2532637417C467BA3B37E554186648716392637415E2C3B0CB47E5541C53F8AA12D2637414112FB74B47E554190CCE3B71C263741FC7F6569B57E55416FA293FC07263741A331A5A3B67E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (10, 't', '0106000000020000000103000000010000000C00000087054113FD2537417A22DDF6D67E55411D7584AB26263741CB3C46D1D37E5541BF1523951F263741805E9359D37E55415188536508263741CF4E3E42D27E55414200D77BFA25374169800925D07E55413580D53BE825374144E10895C87E5541A39C9CFAE3253741B9FDDF81CB7E5541A39C9CFAE3253741746B4A76CC7E5541A39C9CFAE3253741B870B40DCF7E55412C349C9DE5253741D4FDC830D27E55412C349C9DE525374168F3DDB0D37E554187054113FD2537417A22DDF6D67E55410103000000010000000400000080A4D818FD253741775E87A9C77E55410F34848DFD2537412F78697AC77E55410E34848DFD2537412F78697AC77E554180A4D818FD253741775E87A9C77E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (0, 't', '0106000000010000000103000000010000001500000080A4D818FD253741775E87A9C77E55417E5401020526374139C1061ECA7E55412CB1FF8D0B26374199724658CB7E55416023D4190F2637416D0791AFCB7E5541E3E8D20214263741419CDB06CC7E554130EC25491D2637411531265ECC7E554112BBF86027263741999E9052CD7E55413B0221ED302637417F65A68CCB7E55417A9FA1782E263741FBF73B98CA7E5541712E6C6F2E263741DF664697CA7E55412371A2322B2637412763F140CA7E5541E623F9BD2526374139C1061ECA7E554153A850031D263741652CBCC6C97E55412A61287713263741778AD1A3C97E5541A79B298E0E263741E1BE51D2C87E5541B2C6D58D08263741775E87A9C77E55417E54010205263741BED6DC1DC77E55414AE22C760126374131BAE73AC67E5541B416ADA40026374131BAE73AC67E55410D34848DFD2537412F78697AC77E554180A4D818FD253741775E87A9C77E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (11, 't', '01060000000500000001030000000100000004000000C5CCB5152A263741E7360F3BB97E554160F06ABA44263741D9880EFFB97E554161F06ABA44263741D9880EFFB97E5541C5CCB5152A263741E7360F3BB97E5541010300000001000000310000006FA293FC07263741A331A5A3B67E554190CCE3B71C263741FC7F6569B57E5541C53F8AA12D2637414112FB74B47E554186648716392637415E2C3B0CB47E5541F3642BD2532637417C467BA3B37E5541B0557A766D2637410F3C9023B57E5541E249574A642637413A808A65B17E5541D282B4A544263741C4A4B57CAF7E5541C1A1BA182C263741B0E88AC2AF7E5541275D6FB90326374161F8DFD9B07E5541EDCF71E7F925374130227588B17E5541A12DCCB7E525374192411FB7B37E5541A14723A0DE25374126373437B57E5541929AD7E3B72537411161B294BC7E5541580DDA11AE253741C2E3DB37C17E55416C563040AA2537411A184515C77E5541F6ED2FE3AB253741E8B4AE4FCB7E5541929AD7E3B725374140E9172DD17E55412E477FE4C325374185615650D77E55414042D059D525374118CA3F5CDC7E554179B57643E6253741662DBFD0DE7E5541B20EC644FE253741C94C69FFE07E554174FF14E9172637417A5CBE16E27E554197DB5FEB41263741488653C5E27E55416BADA81C7426374170FEA839E27E5541F2F6A2068B263741FA22D450E07E554153E29E649B26374153FEBF8ADB7E554117079CD9A62637418FBF6B2DD77E554179264608A92637417B906CE7D37E554179409DF0A126374105B597FED17E5541160819799D263741124CA047CA7E5541445B9F1A842637413937CA47CD7E55415BBE4C3179263741371D735FD47E55418A84A75E63263741E79F9C02D97E55412B7F54185A263741C9855C6BD97E5541DA150C44262637417B22DDF6D67E55411D7584AB26263741CB3C46D1D37E554187054113FD2537417A22DDF6D67E55412C349C9DE525374168F3DDB0D37E55412C349C9DE5253741D4FDC830D27E5541A39C9CFAE3253741B870B40DCF7E5541A39C9CFAE3253741746B4A76CC7E5541A39C9CFAE3253741B9FDDF81CB7E55413580D53BE825374144E10895C87E55413480D53BE825374144E10895C87E5541E7A10692E6253741C03996E4C77E5541258750E1BE25374145AF360DC67E554169F54740E12537410EAF64AFB87E55416FA293FC07263741A331A5A3B67E554101030000000100000004000000102680FAEA253741D53CCAEEBD7E55410F2680FAEA253741D53CCAEEBD7E5541E514DB1DEA25374188AC9812BE7E5541102680FAEA253741D53CCAEEBD7E554101030000000100000004000000281ABA9152263741858F7D4ECB7E5541271ABA9152263741858F7D4ECB7E554116E16DC05226374190C3BB69CB7E5541281ABA9152263741858F7D4ECB7E5541010300000001000000040000001B811CEE422637412F3EC629CC7E5541AA63AF7441263741C3358C8ACB7E5541A963AF7441263741C3358C8ACB7E55411B811CEE422637412F3EC629CC7E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (1, 't', '0106000000010000000103000000010000001F00000012BBF86027263741999E9052CD7E55419B0BF51B36263741AA28F029CF7E5541DF3373793D263741DF4230C1CE7E5541CC2DF290412637416C33DBA9CD7E55411B811CEE422637412F3EC629CC7E5541A963AF7441263741C3358C8ACB7E55415243C8903E2637411EB46652CA7E5541BEC71FD635263741CF603CF5C87E5541B0614D1B2A26374142444712C87E5541645EFAD420263741B527522FC77E5541E198FBEB1B263741EA4192C6C67E5541C8077D31162637413A697229C67E5541E8888130042637417806F3B4C37E5541CFF70276FE253741A571A85DC37E554193AA5901F925374120308863C47E5541DA22AF75F8253741EA4192C6C67E554180A4D818FD253741775E87A9C77E55410E34848DFD2537412F78697AC77E5541B416ADA40026374131BAE73AC67E55414AE22C760126374131BAE73AC67E55417E54010205263741BED6DC1DC77E5541B2C6D58D08263741775E87A9C77E5541A79B298E0E263741E1BE51D2C87E55412A61287713263741778AD1A3C97E554153A850031D263741652CBCC6C97E5541E623F9BD2526374139C1061ECA7E55412371A2322B2637412763F140CA7E5541712E6C6F2E263741DF664697CA7E55417A9FA1782E263741FBF73B98CA7E55413B0221ED302637417F65A68CCB7E554112BBF86027263741999E9052CD7E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (2, 't', '01060000000100000001030000000100000012000000DF3373793D263741DF4230C1CE7E5541FE4FEF054D263741750EB092CF7E5541783A190650263741BB860507CF7E5541D068184C5326374190EF0564CD7E554116E16DC05226374190C3BB69CB7E5541261ABA9152263741848F7D4ECB7E5541557EEE4B502637414A1F1CFBC97E554160A99A4B4A263741CF603CF5C87E5541844FA0084926374131BB0CE1C87E55413F3D47A8422637410D2A077BC87E5541B49C73D63B2637411F881C58C87E554139B249D638263741047B7C8CC87E5541BEC71FD635263741CF603CF5C87E55415243C8903E2637411EB46652CA7E5541AA63AF7441263741C3358C8ACB7E55411B811CEE422637412F3EC629CC7E5541CC2DF290412637416C33DBA9CD7E5541DF3373793D263741DF4230C1CE7E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (5, 't', '010600000001000000010300000002000000130000004200D77BFA25374169800925D07E55415188536508263741CF4E3E42D27E5541BF1523951F263741805E9359D37E55411D7584AB26263741CB3C46D1D37E55412E301E3933263741226148A5D47E5541D97E990B462637414F882808D47E5541470C693B5D263741A8D6E8CDD27E55412FA9BB2468263741BD1F3FFCCE7E55411BED906A6826374111BF74D3CD7E5541BB5A12B062263741A3AC81C1C47E554198166B0C552637410C950DC7BF7E5541EEC7EF39422637415B85B8AFBE7E5541FD5C387629263741EC8ACD14BE7E5541357E23F21D2637419B68C3CCBD7E554152157FD9042637418D5B2301BE7E5541A7C60307F22537410237F8E9BF7E554196243035EB2537417DC157C1C17E5541F843DA63ED25374180E3B63BC57E55414200D77BFA25374169800925D07E55411F000000CFF70276FE253741A571A85DC37E5541D7D2D75E00263741561E7E00C27E55412F01D7A4032637419C96D374C17E5541358CD4760D26374144C06823C27E5541C8077D3116263741D1DC5D06C37E5541C367CE03262637418A640892C37E5541E4D321A72D2637415EF952E9C37E5541774FCA6136263741DA8BE8F4C27E554147181C9144263741B6FB0735C57E55413CED6F914A26374130E63135C87E5541834FA0084926374131BB0CE1C87E5541844FA0084926374131BB0CE1C87E554160A99A4B4A263741CF603CF5C87E5541557EEE4B502637414A1F1CFBC97E5541271ABA9152263741858F7D4ECB7E554116E16DC05226374190C3BB69CB7E5541D068184C5326374190EF0564CD7E5541783A190650263741BB860507CF7E5541FE4FEF054D263741750EB092CF7E5541DF3373793D263741DF4230C1CE7E55419B0BF51B36263741AA28F029CF7E554112BBF86027263741999E9052CD7E554130EC25491D2637411531265ECC7E5541E3E8D20214263741419CDB06CC7E55416023D4190F2637416D0791AFCB7E55412CB1FF8D0B26374199724658CB7E55417E5401020526374139C1061ECA7E554180A4D818FD253741775E87A9C77E5541DA22AF75F8253741EA4192C6C67E554193AA5901F925374120308863C47E5541CFF70276FE253741A571A85DC37E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (9, 't', '0106000000010000000103000000010000000A000000258750E1BE25374145AF360DC67E5541E7A10692E6253741C03996E4C77E5541D472074CE3253741907D827BC17E5541E514DB1DEA25374188AC9812BE7E5541112680FAEA253741D53CCAEEBD7E55417E3457AAF92537413FF8A38CBB7E5541007E519410263741BFBEB9C6B97E55416FA293FC07263741A331A5A3B67E554169F54740E12537410EAF64AFB87E5541258750E1BE25374145AF360DC67E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (6, 't', '010600000001000000010300000001000000170000001D7584AB26263741CB3C46D1D37E5541DA150C44262637417B22DDF6D67E55412B7F54185A263741C9855C6BD97E55418A84A75E63263741E79F9C02D97E55415BBE4C3179263741371D735FD47E5541445B9F1A842637413937CA47CD7E55411DE349A684263741BAFDDF81CB7E5541934B4A0383263741CF4636B0C77E5541ABAEF719782637416456A218C27E55419A994FBC6D26374148C98DF5BE7E554152F7A98C59263741858A3998BA7E554160F06ABA44263741D9880EFFB97E5541C5CCB5152A263741E7360F3BB97E5541FD5C387629263741EC8ACD14BE7E5541EEC7EF39422637415B85B8AFBE7E554198166B0C552637410C950DC7BF7E5541BB5A12B062263741A3AC81C1C47E55411BED906A6826374111BF74D3CD7E55412FA9BB2468263741BD1F3FFCCE7E5541470C693B5D263741A8D6E8CDD27E5541D97E990B462637414F882808D47E55412E301E3933263741226148A5D47E55411D7584AB26263741CB3C46D1D37E5541');
INSERT INTO av_intersection (gid, t_name, the_geom) VALUES (7, 't', '01060000000100000001030000000100000018000000FD5C387629263741EC8ACD14BE7E5541C5CCB5152A263741E7360F3BB97E554161F06ABA44263741D9880EFFB97E554152F7A98C59263741858A3998BA7E55418440E9695C263741BA0F2FD8B97E5541729E1598552637410EAF64AFB87E554151E79968442637418F757AE9B67E5541B9C71DDC34263741942405D8B67E55416D7EF54F2B2637418068DA1DB77E5541ABCCCC66232637417BB94F2FB77E5541007E519410263741BFBEB9C6B97E55417E3457AAF92537413FF8A38CBB7E5541102680FAEA253741D53CCAEEBD7E5541E514DB1DEA25374188AC9812BE7E5541D472074CE3253741907D827BC17E5541E7A10692E6253741C03996E4C77E55413480D53BE825374144E10895C87E55414200D77BFA25374169800925D07E5541F843DA63ED25374180E3B63BC57E554196243035EB2537417DC157C1C17E5541A7C60307F22537410237F8E9BF7E554152157FD9042637418D5B2301BE7E5541357E23F21D2637419B68C3CCBD7E5541FD5C387629263741EC8ACD14BE7E5541');
