Examples
The original point cloud and the optimal alpha-shape share one comparison frame.
WITH data AS (
SELECT 'MULTIPOINT((63 84),(76 88),(68 73),(53 18),(91 50),(81 70),
(88 29),(24 82),(32 51),(37 23),(27 54),(84 19),(75 87),(44 42),(77 67),(90 30),(36 61),(32 65),
(81 47),(88 58),(68 73),(49 95),(81 60),(87 50),
(78 16),(79 21),(30 22),(78 43),(26 85),(48 34),(35 35),(36 40),(31 79),(83 29),(27 84),(52 98),(72 95),(85 71),
(75 84),(75 77),(81 29),(77 73),(41 42),(83 72),(23 36),(89 53),(27 57),(57 97),(27 77),(39 88),(60 81),
(80 72),(54 32),(55 26),(62 22),(70 20),(76 27),(84 35),(87 42),(82 54),(83 64),(69 86),(60 90),(50 86),(43 80),(36 73),
(36 68),(40 75),(24 67),(23 60),(26 44),(28 33),(40 32),(43 19),(65 16),(73 16),(38 46),(31 59),(34 86),(45 90),(64 97))'::geometry AS input_points
)
SELECT input_points AS input_points,
CG_OptimalAlphaShape(input_points) AS optimal_alpha_shape
FROM data;
MULTIPOINT((63 84),(76 88),(68 73),(53 18),(91 50),(81 70),(88 29),(24 82),(32 51),(37 23),(27 54),(84 19),(75 87),(44 42),(77 67),(90 30),(36 61),(32 65),(81 47),(88 58),(68 73),(49 95),(81 60),(87 50),(78 16),(79 21),(30 22),(78 43),(26 85),(48 34),(35 35),(36 40),(31 79),(83 29),(27 84),(52 98),(72 95),(85 71),(75 84),(75 77),(81 29),(77 73),(41 42),(83 72),(23 36),(89 53),(27 57),(57 97),(27 77),(39 88),(60 81),(80 72),(54 32),(55 26),(62 22),(70 20),(76 27),(84 35),(87 42),(82 54),(83 64),(69 86),(60 90),(50 86),(43 80),(36 73),(36 68),(40 75),(24 67),(23 60),(26 44),(28 33),(40 32),(43 19),(65 16),(73 16),(38 46),(31 59),(34 86),(45 90),(64 97)) | MULTIPOLYGON(((89 53,88 58,83 64,85 71,83 72,75 77,75 84,76 88,72 95,64 97,57 97,52 98,49 95,45 90,39 88,34 86,26 85,24 82,27 77,24 67,23 60,27 54,26 44,23 36,28 33,30 22,37 23,43 19,53 18,65 16,73 16,78 16,84 19,88 29,90 30,87 42,91 50,89 53)))
01040000005100000001010000000000000000804F40000000000000554001010000000000000000005340000000000000564001010000000000000000005140000000000040524001010000000000000000804A40000000000000324001010000000000000000C056400000000000004940010100000000000000004054400000000000805140010100000000000000000056400000000000003D4001010000000000000000003840000000000080544001010000000000000000004040000000000080494001010000000000000000804240000000000000374001010000000000000000003B400000000000004B4001010000000000000000005540000000000000334001010000000000000000C052400000000000C05540010100000000000000000046400000000000004540010100000000000000004053400000000000C05040010100000000000000008056400000000000003E40010100000000000000000042400000000000804E40010100000000000000000040400000000000405040010100000000000000004054400000000000804740010100000000000000000056400000000000004D40010100000000000000000051400000000000405240010100000000000000008048400000000000C05740010100000000000000004054400000000000004E4001010000000000000000C05540000000000000494001010000000000000000805340000000000000304001010000000000000000C05340000000000000354001010000000000000000003E40000000000000364001010000000000000000805340000000000080454001010000000000000000003A40000000000040554001010000000000000000004840000000000000414001010000000000000000804140000000000080414001010000000000000000004240000000000000444001010000000000000000003F400000000000C0534001010000000000000000C054400000000000003D4001010000000000000000003B40000000000000554001010000000000000000004A400000000000805840010100000000000000000052400000000000C05740010100000000000000004055400000000000C0514001010000000000000000C05240000000000000554001010000000000000000C052400000000000405340010100000000000000004054400000000000003D4001010000000000000000405340000000000040524001010000000000000000804440000000000000454001010000000000000000C054400000000000005240010100000000000000000037400000000000004240010100000000000000004056400000000000804A4001010000000000000000003B400000000000804C4001010000000000000000804C40000000000040584001010000000000000000003B40000000000040534001010000000000000000804340000000000000564001010000000000000000004E40000000000040544001010000000000000000005440000000000000524001010000000000000000004B40000000000000404001010000000000000000804B400000000000003A4001010000000000000000004F400000000000003640010100000000000000008051400000000000003440010100000000000000000053400000000000003B4001010000000000000000005540000000000080414001010000000000000000C055400000000000004540010100000000000000008054400000000000004B4001010000000000000000C05440000000000000504001010000000000000000405140000000000080554001010000000000000000004E400000000000805640010100000000000000000049400000000000805540010100000000000000008045400000000000005440010100000000000000000042400000000000405240010100000000000000000042400000000000005140010100000000000000000044400000000000C05240010100000000000000000038400000000000C05040010100000000000000000037400000000000004E4001010000000000000000003A40000000000000464001010000000000000000003C40000000000080404001010000000000000000004440000000000000404001010000000000000000804540000000000000334001010000000000000000405040000000000000304001010000000000000000405240000000000000304001010000000000000000004340000000000000474001010000000000000000003F400000000000804D40010100000000000000000041400000000000805540010100000000000000008046400000000000805640010100000000000000000050400000000000405840 | 0106000000010000000103000000010000002600000000000000004056400000000000804A4000000000000056400000000000004D400000000000C05440000000000000504000000000004055400000000000C051400000000000C0544000000000000052400000000000C0524000000000004053400000000000C0524000000000000055400000000000005340000000000000564000000000000052400000000000C05740000000000000504000000000004058400000000000804C4000000000004058400000000000004A40000000000080584000000000008048400000000000C057400000000000804640000000000080564000000000008043400000000000005640000000000000414000000000008055400000000000003A400000000000405540000000000000384000000000008054400000000000003B40000000000040534000000000000038400000000000C0504000000000000037400000000000004E400000000000003B400000000000004B400000000000003A400000000000004640000000000000374000000000000042400000000000003C4000000000008040400000000000003E40000000000000364000000000008042400000000000003740000000000080454000000000000033400000000000804A4000000000000032400000000000405040000000000000304000000000004052400000000000003040000000000080534000000000000030400000000000005540000000000000334000000000000056400000000000003D4000000000008056400000000000003E400000000000C0554000000000000045400000000000C05640000000000000494000000000004056400000000000804A40
Allowing holes keeps the same outer shell but carves a central hole.
WITH data AS (
SELECT 'MULTIPOINT((63 84),(76 88),(68 73),(53 18),(91 50),(81 70),(88 29),(24 82),(32 51),(37 23),(27 54),(84 19),(75 87),(44 42),(77 67),(90 30),(36 61),(32 65),(81 47),(88 58),(68 73),(49 95),(81 60),(87 50),
(78 16),(79 21),(30 22),(78 43),(26 85),(48 34),(35 35),(36 40),(31 79),(83 29),(27 84),(52 98),(72 95),(85 71),
(75 84),(75 77),(81 29),(77 73),(41 42),(83 72),(23 36),(89 53),(27 57),(57 97),(27 77),(39 88),(60 81),
(80 72),(54 32),(55 26),(62 22),(70 20),(76 27),(84 35),(87 42),(82 54),(83 64),(69 86),(60 90),(50 86),(43 80),(36 73),
(36 68),(40 75),(24 67),(23 60),(26 44),(28 33),(40 32),(43 19),(65 16),(73 16),(38 46),(31 59),(34 86),(45 90),(64 97))'::geometry AS input_points
)
SELECT input_points AS input_points,
CG_OptimalAlphaShape(input_points, allow_holes => true) AS optimal_alpha_shape
FROM data;
MULTIPOINT((63 84),(76 88),(68 73),(53 18),(91 50),(81 70),(88 29),(24 82),(32 51),(37 23),(27 54),(84 19),(75 87),(44 42),(77 67),(90 30),(36 61),(32 65),(81 47),(88 58),(68 73),(49 95),(81 60),(87 50),(78 16),(79 21),(30 22),(78 43),(26 85),(48 34),(35 35),(36 40),(31 79),(83 29),(27 84),(52 98),(72 95),(85 71),(75 84),(75 77),(81 29),(77 73),(41 42),(83 72),(23 36),(89 53),(27 57),(57 97),(27 77),(39 88),(60 81),(80 72),(54 32),(55 26),(62 22),(70 20),(76 27),(84 35),(87 42),(82 54),(83 64),(69 86),(60 90),(50 86),(43 80),(36 73),(36 68),(40 75),(24 67),(23 60),(26 44),(28 33),(40 32),(43 19),(65 16),(73 16),(38 46),(31 59),(34 86),(45 90),(64 97)) | MULTIPOLYGON(((89 53,88 58,83 64,85 71,83 72,75 77,75 84,76 88,72 95,64 97,57 97,52 98,49 95,45 90,39 88,34 86,26 85,24 82,27 77,24 67,23 60,27 54,26 44,23 36,28 33,30 22,37 23,43 19,53 18,65 16,73 16,78 16,84 19,88 29,90 30,87 42,91 50,89 53),(36 68,40 75,43 80,50 86,60 81,68 73,77 67,81 60,82 54,81 47,78 43,81 29,76 27,70 20,62 22,55 26,54 32,48 34,44 42,38 46,36 61,36 68)))
01040000005100000001010000000000000000804F40000000000000554001010000000000000000005340000000000000564001010000000000000000005140000000000040524001010000000000000000804A40000000000000324001010000000000000000C056400000000000004940010100000000000000004054400000000000805140010100000000000000000056400000000000003D4001010000000000000000003840000000000080544001010000000000000000004040000000000080494001010000000000000000804240000000000000374001010000000000000000003B400000000000004B4001010000000000000000005540000000000000334001010000000000000000C052400000000000C05540010100000000000000000046400000000000004540010100000000000000004053400000000000C05040010100000000000000008056400000000000003E40010100000000000000000042400000000000804E40010100000000000000000040400000000000405040010100000000000000004054400000000000804740010100000000000000000056400000000000004D40010100000000000000000051400000000000405240010100000000000000008048400000000000C05740010100000000000000004054400000000000004E4001010000000000000000C05540000000000000494001010000000000000000805340000000000000304001010000000000000000C05340000000000000354001010000000000000000003E40000000000000364001010000000000000000805340000000000080454001010000000000000000003A40000000000040554001010000000000000000004840000000000000414001010000000000000000804140000000000080414001010000000000000000004240000000000000444001010000000000000000003F400000000000C0534001010000000000000000C054400000000000003D4001010000000000000000003B40000000000000554001010000000000000000004A400000000000805840010100000000000000000052400000000000C05740010100000000000000004055400000000000C0514001010000000000000000C05240000000000000554001010000000000000000C052400000000000405340010100000000000000004054400000000000003D4001010000000000000000405340000000000040524001010000000000000000804440000000000000454001010000000000000000C054400000000000005240010100000000000000000037400000000000004240010100000000000000004056400000000000804A4001010000000000000000003B400000000000804C4001010000000000000000804C40000000000040584001010000000000000000003B40000000000040534001010000000000000000804340000000000000564001010000000000000000004E40000000000040544001010000000000000000005440000000000000524001010000000000000000004B40000000000000404001010000000000000000804B400000000000003A4001010000000000000000004F400000000000003640010100000000000000008051400000000000003440010100000000000000000053400000000000003B4001010000000000000000005540000000000080414001010000000000000000C055400000000000004540010100000000000000008054400000000000004B4001010000000000000000C05440000000000000504001010000000000000000405140000000000080554001010000000000000000004E400000000000805640010100000000000000000049400000000000805540010100000000000000008045400000000000005440010100000000000000000042400000000000405240010100000000000000000042400000000000005140010100000000000000000044400000000000C05240010100000000000000000038400000000000C05040010100000000000000000037400000000000004E4001010000000000000000003A40000000000000464001010000000000000000003C40000000000080404001010000000000000000004440000000000000404001010000000000000000804540000000000000334001010000000000000000405040000000000000304001010000000000000000405240000000000000304001010000000000000000004340000000000000474001010000000000000000003F400000000000804D40010100000000000000000041400000000000805540010100000000000000008046400000000000805640010100000000000000000050400000000000405840 | 0106000000010000000103000000020000002600000000000000004056400000000000804A4000000000000056400000000000004D400000000000C05440000000000000504000000000004055400000000000C051400000000000C0544000000000000052400000000000C0524000000000004053400000000000C0524000000000000055400000000000005340000000000000564000000000000052400000000000C05740000000000000504000000000004058400000000000804C4000000000004058400000000000004A40000000000080584000000000008048400000000000C057400000000000804640000000000080564000000000008043400000000000005640000000000000414000000000008055400000000000003A400000000000405540000000000000384000000000008054400000000000003B40000000000040534000000000000038400000000000C0504000000000000037400000000000004E400000000000003B400000000000004B400000000000003A400000000000004640000000000000374000000000000042400000000000003C4000000000008040400000000000003E40000000000000364000000000008042400000000000003740000000000080454000000000000033400000000000804A4000000000000032400000000000405040000000000000304000000000004052400000000000003040000000000080534000000000000030400000000000005540000000000000334000000000000056400000000000003D4000000000008056400000000000003E400000000000C0554000000000000045400000000000C05640000000000000494000000000004056400000000000804A40160000000000000000004240000000000000514000000000000044400000000000C0524000000000008045400000000000005440000000000000494000000000008055400000000000004E4000000000004054400000000000005140000000000040524000000000004053400000000000C0504000000000004054400000000000004E4000000000008054400000000000004B40000000000040544000000000008047400000000000805340000000000080454000000000004054400000000000003D4000000000000053400000000000003B40000000000080514000000000000034400000000000004F4000000000000036400000000000804B400000000000003A400000000000004B40000000000000404000000000000048400000000000004140000000000000464000000000000045400000000000004340000000000000474000000000000042400000000000804E4000000000000042400000000000005140