Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
docs: add GEOGRAPHY data type code samples (#428)
* docs: add GEOGRAPHY data type code samples These are added to a separate directory in order to isolate the GeoJSON and WKT dependencies from the other code samples. * skip geography samples in snippets session
- Loading branch information
Showing
12 changed files
with
502 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import datetime | ||
import uuid | ||
|
||
from google.cloud import bigquery | ||
import pytest | ||
|
||
|
||
def temp_suffix(): | ||
now = datetime.datetime.now() | ||
return f"{now.strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}" | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def bigquery_client(): | ||
bigquery_client = bigquery.Client() | ||
return bigquery_client | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def project_id(bigquery_client): | ||
return bigquery_client.project | ||
|
||
|
||
@pytest.fixture | ||
def dataset_id(bigquery_client): | ||
dataset_id = f"geography_{temp_suffix()}" | ||
bigquery_client.create_dataset(dataset_id) | ||
yield dataset_id | ||
bigquery_client.delete_dataset(dataset_id, delete_contents=True) | ||
|
||
|
||
@pytest.fixture | ||
def table_id(bigquery_client, project_id, dataset_id): | ||
table_id = f"{project_id}.{dataset_id}.geography_{temp_suffix()}" | ||
table = bigquery.Table(table_id) | ||
table.schema = [ | ||
bigquery.SchemaField("geo", bigquery.SqlTypeNames.GEOGRAPHY), | ||
] | ||
bigquery_client.create_table(table) | ||
yield table_id | ||
bigquery_client.delete_table(table_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def insert_geojson(override_values={}): | ||
# [START bigquery_insert_geojson] | ||
import geojson | ||
from google.cloud import bigquery | ||
|
||
bigquery_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" | ||
# [END bigquery_insert_geojson] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
table_id = override_values.get("table_id", table_id) | ||
# [START bigquery_insert_geojson] | ||
|
||
# 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) | ||
if errors: | ||
raise RuntimeError(f"row insert failed: {errors}") | ||
else: | ||
print(f"wrote 1 row to {table_id}") | ||
# [END bigquery_insert_geojson] | ||
return errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from . import insert_geojson | ||
|
||
|
||
def test_insert_geojson(table_id): | ||
errors = insert_geojson.insert_geojson(override_values={"table_id": table_id}) | ||
assert not errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def insert_wkt(override_values={}): | ||
# [START bigquery_insert_geography_wkt] | ||
from google.cloud import bigquery | ||
import shapely | ||
import shapely.wkt | ||
|
||
bigquery_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" | ||
# [END bigquery_insert_geography_wkt] | ||
# To facilitate testing, we replace values with alternatives | ||
# provided by the testing harness. | ||
table_id = override_values.get("table_id", table_id) | ||
# [START bigquery_insert_geography_wkt] | ||
|
||
# Use the Shapely library to generate WKT of a line from LAX to | ||
# JFK airports. Alternatively, you may define WKT data directly. | ||
my_geography = shapely.LineString([(-118.4085, 33.9416), (-73.7781, 40.6413)]) | ||
rows = [ | ||
# Convert data into a WKT string. | ||
{"geo": shapely.wkt.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) | ||
if errors: | ||
raise RuntimeError(f"row insert failed: {errors}") | ||
else: | ||
print(f"wrote 1 row to {table_id}") | ||
# [END bigquery_insert_geography_wkt] | ||
return errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from . import insert_geojson | ||
|
||
|
||
def test_insert_geojson(table_id): | ||
errors = insert_geojson.insert_geojson(override_values={"table_id": table_id}) | ||
assert not errors |
Oops, something went wrong.