Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 dbc68b3
Show file tree
Hide file tree
Showing 12 changed files with 502 additions and 7 deletions.
8 changes: 7 additions & 1 deletion noxfile.py
Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions samples/geography/__init__.py
@@ -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.
55 changes: 55 additions & 0 deletions samples/geography/conftest.py
@@ -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)
49 changes: 49 additions & 0 deletions samples/geography/insert_geojson.py
@@ -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
20 changes: 20 additions & 0 deletions samples/geography/insert_geojson_test.py
@@ -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
49 changes: 49 additions & 0 deletions samples/geography/insert_wkt.py
@@ -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
20 changes: 20 additions & 0 deletions samples/geography/insert_wkt_test.py
@@ -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 comments on commit dbc68b3

Please sign in to comment.