Inserir dados GeoJSON

Inserção por streaming na coluna GEOGRAFIA com dados GeoJSON.

Mais informações

Para ver a documentação detalhada que inclui este exemplo de código, consulte:

Exemplo de código

Ruby

Antes de testar esta amostra, siga as instruções de configuração do Ruby no Guia de início rápido do BigQuery: como usar bibliotecas de cliente. Para mais informações, consulte a documentação de referência da API BigQuery em Ruby.

Para autenticar no BigQuery, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

require "google/cloud/bigquery"
require "rgeo"
require "rgeo/geo_json"

def insert_geojson dataset_id = "your_dataset_id", table_id = "your_table_id"
  bigquery = Google::Cloud::Bigquery.new
  dataset  = bigquery.dataset dataset_id
  table    = dataset.table table_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_factory
  my_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.insert row_data

  if response.success?
    puts "Inserted GeoJSON row successfully"
  else
    puts "GeoJSON row insert failed: #{response.error_rows.first&.errors}"
  end
end

A seguir

Para pesquisar e filtrar exemplos de código de outros Google Cloud produtos, consulte a Google Cloud pesquisa de exemplos de código.