Insert GeoJSON data Stay organized with collections Save and categorize content based on your preferences.
Streaming insert into GEOGRAPHY column with GeoJSON data.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Python
Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
importgeojsonfromgoogle.cloudimportbigquerybigquery_client=bigquery.Client()# This example uses a table containing a column named "geo" with the# GEOGRAPHY data type.table_id="my-project.my_dataset.my_table"# Use the python-geojson library to generate GeoJSON of a line from LAX to# JFK airports. Alternatively, you may define GeoJSON data directly, but it# must be converted to a string before loading it into BigQuery.my_geography=geojson.LineString([(-118.4085,33.9416),(-73.7781,40.6413)])rows=[# Convert GeoJSON data into a string.{"geo":geojson.dumps(my_geography)}]# table already exists and has a column# named "geo" with data type GEOGRAPHY.errors=bigquery_client.insert_rows_json(table_id,rows)iferrors:raiseRuntimeError(f"row insert failed:{errors}")else:print(f"wrote 1 row to{table_id}")Ruby
Before trying this sample, follow theRuby setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryRuby API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
require"google/cloud/bigquery"require"rgeo"require"rgeo/geo_json"definsert_geojsondataset_id="your_dataset_id",table_id="your_table_id"bigquery=Google::Cloud::Bigquery.newdataset=bigquery.datasetdataset_idtable=dataset.tabletable_id# Use the RGeo library to generate GeoJSON of a line from LAX to# JFK airports. Alternatively, you may define GeoJSON data directly, but it# must be converted to a string before loading it into BigQuery.factory=RGeo::Geographic.spherical_factorymy_line=factory.line_string([factory.point(-118.4085,33.9416),factory.point(-73.7781,40.6413)])row_data=[# Convert GeoJSON data into a string.{geo:RGeo::GeoJSON.encode(my_line).to_json}]# Table already exists and has a column named "geo" with data type GEOGRAPHY.response=table.insertrow_dataifresponse.success?puts"Inserted GeoJSON row successfully"elseputs"GeoJSON row insert failed:#{response.error_rows.first&.errors}"endendWhat's next
To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.