python - BigQuery does not recognize Shapely Polygons - Stack Overflow

I have some polygons which I am trying to upload to BigQuery. The polygons are creating shapely based o

I have some polygons which I am trying to upload to BigQuery. The polygons are creating shapely based on a Point (lat & long) and a radius, i.e., polygon = shapely.geometry.Point(lng, lat).buffer(r). They are first converted into a dataframe of geojsons using geojson.dumps(mapping(polygon)) then uploaded to BigQuery and finally converted to a GEOGRAPHY type using SELECT SAFE.st_geogfromgeojson(polygon, make_valid => TRUE) FROM table_name.

However, as a part of this process, some entries became NULL (there were no missing values in the uploaded dataframe). I suspect this has to do with shapely polygons not being completely recognized by BigQuery and the ones that are invalid were automatically removed.

Is there a way to make sure this does not happen?

I have some polygons which I am trying to upload to BigQuery. The polygons are creating shapely based on a Point (lat & long) and a radius, i.e., polygon = shapely.geometry.Point(lng, lat).buffer(r). They are first converted into a dataframe of geojsons using geojson.dumps(mapping(polygon)) then uploaded to BigQuery and finally converted to a GEOGRAPHY type using SELECT SAFE.st_geogfromgeojson(polygon, make_valid => TRUE) FROM table_name.

However, as a part of this process, some entries became NULL (there were no missing values in the uploaded dataframe). I suspect this has to do with shapely polygons not being completely recognized by BigQuery and the ones that are invalid were automatically removed.

Is there a way to make sure this does not happen?

Share asked Nov 19, 2024 at 18:28 Winston LiWinston Li 595 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The query you used has SAFE. prefix - which is designed exactly to allow this to happen, and to convert something that BigQuery cannot accept into NULL.

Remove the SAFE. prefix, and the query would fail and you should get an error describing why something cannot be converted to Geography type.

What I usually do is create a table with failures:

CREATE TABLE tmp.failures AS
SELECT geoid, polygon
FROM table_name
WHERE polygon IS NOT NULL 
 AND SAFE.st_geogfromgeojson(polygon, make_valid => TRUE) IS NULL

This create a table with failures only - the rows where the original polygon is not NULL, but the result is NULL. You can then check the cause of each failure row one by one, by removing SAFE. prefix and checking error message, with queries like

SELECT st_geogfromgeojson(polygon, make_valid => TRUE)
WHERE geoid = 12345

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742406037a4437866.html

相关推荐

  • python - BigQuery does not recognize Shapely Polygons - Stack Overflow

    I have some polygons which I am trying to upload to BigQuery. The polygons are creating shapely based o

    12小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信