Search code examples
pythongoogle-bigquerygisgeojson

BigQuery GIS data to GeoJSON


When I upload a geojson file to a BigQuery table, it converts the geometry from a modified geojson format that looks like this:

"{\"type\": \"MultiPolygon\", \"coordinates\": [[[[-97.239093, 48.96826], [-97.239092, 48.968332], [-97.23909, 48.968404],

to a new datatype format for Google that looks like this one:

MULTIPOLYGON(((-87.056352 42.750018, -87.055587 42.744718, -87.043899 42.663729, -87.038404 42.625021, -87.036742 42.612868, -87.025669 42.531934, -87.021802 42.503671,

(I know the numbers are different, it is a different shape)

I want to query the data from BigQuery and use it in Jupyter notebooks as a JSON or GeoJSON data type, but I cannot convert it back into the JSON or GeoJSON data type. Is there any way to do this in Python without individually parsing the strings?


Solution

  • ST_ASGEOJSON() will do the job.

    https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_asgeojson

    ST_ASGEOJSON(geography_expression)

    Returns the RFC 7946 compliant GeoJSON representation of the input GEOGRAPHY.