Skip to content
Permalink
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
tswast committed Dec 9, 2020
1 parent 5e266d8 commit dbc68b3d1f325f80d24a2da5f028b0f653fb0317
@@ -147,7 +147,13 @@ def snippets(session):
# Skip tests in samples/snippets, as those are run in a different session
# using the nox config from that directory.
session.run("py.test", os.path.join("docs", "snippets.py"), *session.posargs)
session.run("py.test", "samples", "--ignore=samples/snippets", *session.posargs)
session.run(
"py.test",
"samples",
"--ignore=samples/snippets",
"--ignore=samples/geography",
*session.posargs,
)


@nox.session(python="3.8")
@@ -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.
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
Loading

0 comments on commit dbc68b3

Please sign in to comment.