-1

I have an array like that

Array(11) [ (2) […], (2) […], (2) […], (2) […], (2) […], (2) […], (2) […], (2) […], (2) […], (2) […], … ]
​
0: Array [ 21.0560137444799, 34.739720318378 ]
​
1: Array [ 21.0560779456378, 34.7397100033393 ]
​
2: Array [ 21.056080278408, 34.7397458213187 ]
​
3: Array [ 21.0561253817111, 34.7397439687819 ]
​
4: Array [ 21.0561232577343, 34.739797555191 ]
​
5: Array [ 21.0562660652913, 34.7397921848872 ]
​
6: Array [ 21.0562570608315, 34.7397353403419 ]
​
7: Array [ 21.0562048139691, 34.7397280744531 ]
​
8: Array [ 21.0561893739781, 34.7396442492899 ]
​
9: Array [ 21.0560014634615, 34.7396514720213 ]
​
10: Array [ 21.0560137444799, 34.739720318378 ]
​
length: 11

I want to create a $multipolygon = these 11 points that is compatible in postgresql.

1° How can I create this multipolygon?

2° And then how I can I send it into a php request and ask to select only features that intersect inside this polygon? ST_MakeEnvelope works only with four points.

 $sql="SELECT * from $table where $coverage in ($multipolygon)";
lekythos
  • 113
  • 1
  • 8

1 Answers1

0

You can remap the array to be like vectors

$polygons = [
    [21.0560137444799, 34.739720318378],
    [21.0560779456378, 34.7397100033393],
    [21.056080278408, 34.7397458213187],
    [21.0561253817111, 34.7397439687819],
];
$multipolygon = join(',', array_map(fn($poly) => vsprintf("(%.13f, %.13f)", $poly), $polygons));

$sql = "SELECT * FROM table WHERE coverage IN ($multipolygon)";
echo $sql;

And the query will look like

SELECT * from table where coverage in ((21.0560137444799, 34.7397203183780),(21.0560779456378, 34.7397100033393),(21.0560802784080, 34.7397458213187),(21.0561253817111, 34.7397439687819))
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35