Inserire dati GeoJSON
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Inserimento in streaming nella colonna GEOGRAPHY con dati GeoJSON.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to stream insert GeoJSON data into a GEOGRAPHY column in a BigQuery table using both Python and Ruby client libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe examples use a line string defined by the coordinates of LAX and JFK airports, but users can define their own GeoJSON data for insertion.\u003c/p\u003e\n"],["\u003cp\u003eThe GeoJSON data, regardless of how it is created, must be converted into a string before it can be loaded into BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eBefore inserting data into BigQuery, you must authenticate and have a pre-existing table with a column using the GEOGRAPHY data type.\u003c/p\u003e\n"]]],[],null,["Streaming insert into GEOGRAPHY column with GeoJSON data.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Working with geospatial data](/bigquery/docs/geospatial-data)\n\nCode sample \n\nPython\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import geojson\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n bigquery_client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()\n\n # This example uses a table containing a column named \"geo\" with the\n # GEOGRAPHY data type.\n table_id = \"my-project.my_dataset.my_table\"\n\n # Use the python-geojson library to generate GeoJSON of a line from LAX to\n # JFK airports. Alternatively, you may define GeoJSON data directly, but it\n # must be converted to a string before loading it into BigQuery.\n my_geography = geojson.LineString([(-118.4085, 33.9416), (-73.7781, 40.6413)])\n rows = [\n # Convert GeoJSON data into a string.\n {\"geo\": geojson.dumps(my_geography)}\n ]\n\n # table already exists and has a column\n # named \"geo\" with data type GEOGRAPHY.\n errors = bigquery_client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_insert_rows_json(table_id, rows)\n if errors:\n raise RuntimeError(f\"row insert failed: {errors}\")\n else:\n print(f\"wrote 1 row to {table_id}\")\n\nRuby\n\n\nBefore trying this sample, follow the Ruby setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-bigquery/latest/Google/Cloud/Bigquery.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n require \"google/cloud/bigquery\"\n require \"rgeo\"\n require \"rgeo/geo_json\"\n\n def insert_geojson dataset_id = \"your_dataset_id\", table_id = \"your_table_id\"\n bigquery = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery-analytics_hub/latest/Google-Cloud-Bigquery.html.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery.html\n dataset = bigquery.dataset dataset_id\n table = dataset.table table_id\n\n # Use the RGeo library to generate GeoJSON of a line from LAX to\n # JFK airports. Alternatively, you may define GeoJSON data directly, but it\n # must be converted to a string before loading it into BigQuery.\n factory = RGeo::Geographic.spherical_factory\n my_line = factory.line_string([factory.point(-118.4085, 33.9416), factory.point(-73.7781, 40.6413)])\n row_data = [\n # Convert GeoJSON data into a string.\n { geo: RGeo::GeoJSON.encode(my_line).to_json }\n ]\n\n # Table already exists and has a column named \"geo\" with data type GEOGRAPHY.\n response = table.insert row_data\n\n if response.success?\n puts \"Inserted GeoJSON row successfully\"\n else\n puts \"GeoJSON row insert failed: #{response.error_rows.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery-reservation-v1/latest/Google-Cloud-Bigquery-Reservation-V1-SplitCapacityCommitmentResponse.html&.errors}\"\n end\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]