I am using ST_MAKEPOLYGON function in BigQuery as follows:
with data AS (
SELECT
61680 AS id, 139.74862575531006 AS lon,
35.674973127377314 AS lat union all
SELECT
61680,
139.75087881088257,
35.673909836018375 union all
SELECT
61680,
139.747037887573,
35.6765767531247 union all
SELECT
61680,
139.75308895111,
35.6813525780394 union all
SELECT
61680,
139.747509956359,
35.6798884869144 union all
SELECT
61680,
139.754590988159,
35.6799930657428 union all
SELECT
61680,
139.754977226257,
35.6762281415729 union all
SELECT
61680,
139.750170707702,
35.6815268728124 union all
SELECT
61680,
139.755363464355,
35.6782500673754
)
SELECT
ST_makepolygon(ST_MAKELINE(ARRAY_AGG(st_geogpoint(lon,
lat)))) AS valid
FROM
`w_nagakawa.geo_test`
GROUP BY
id
and I get error like:
Error: ST_MakePolygon failed: Invalid polygon loop: Edge 3 has duplicate vertex with edge 10
Geography argument inside ST_MAKEPOLYGON is okay, and all lat-lon seems to be different.
I'd like to know why it happens and would like to know some ideas to solve this.
Thanks.
Your polygon is wrong formatted (probably the points are wrong ordered). I tried to visualize the polygon with the points and the order that you provided, it shows a mesh of edges. Therefore, I ordered the points in the way that make sense to be a polygon. After that, the query worked. The order that I tested is below (they are the same points just ordered in a different way):
This is because BigQuery validates the polygons before load or generate it.
Frist question first …
Using ST_MAKEPOLYGON with linestring input (via ST_MAKELINE) requires line to be properly assembled such that there is no intersections which happens with your data (as line is built using points in the [random] order of appearance)
Instead, you would need line like below in blue - where all geo-points ordered such that they form non-self-crossed line
Note: The linestring must be closed: that is, the first and last vertex have to be the same. If the first and last vertex differ, the function constructs a final edge from the first vertex to the last.
Building Polygon using the “proper_line” will perfectly work and produce below result
Second question now …
So, obviously, we need somehow properly order geo-points
This can be done manually (have fun with this option) or can be done programmatically
Below is idea of how to do so within BigQuery (Standard SQL) along with details of implementation
So, we want to assign proper sequence number to each point by following below steps:
Step 1 – let’s identify centroid (green pin in below image) for all the points (red pins)
We can use below statement for this:
Step 2 - Then, for each point we should calculate angle between centroid-to-point line and horizontal line crossing centroid
We are using anchors (blue circles on the image)
Step 3 - Now we want to convert those angles into proper sequence reflecting respective points’ quadrants
Step 4 - So now, finally we can use sequence column to properly order points Final query below:
Final result is:
Note: this idea/solution - still can be limited to just some obvious cases like yours - i did not have chance to explore and/or test it for generic cases