Skip to content

Commit dbc68b3

Browse files
authored
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
1 parent 5e266d8 commit dbc68b3

12 files changed

+502
-7
lines changed

noxfile.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ def snippets(session):
147147
# Skip tests in samples/snippets, as those are run in a different session
148148
# using the nox config from that directory.
149149
session.run("py.test", os.path.join("docs", "snippets.py"), *session.posargs)
150-
session.run("py.test", "samples", "--ignore=samples/snippets", *session.posargs)
150+
session.run(
151+
"py.test",
152+
"samples",
153+
"--ignore=samples/snippets",
154+
"--ignore=samples/geography",
155+
*session.posargs,
156+
)
151157

152158

153159
@nox.session(python="3.8")

samples/geography/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

samples/geography/conftest.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import uuid
17+
18+
from google.cloud import bigquery
19+
import pytest
20+
21+
22+
def temp_suffix():
23+
now = datetime.datetime.now()
24+
return f"{now.strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
25+
26+
27+
@pytest.fixture(scope="session")
28+
def bigquery_client():
29+
bigquery_client = bigquery.Client()
30+
return bigquery_client
31+
32+
33+
@pytest.fixture(scope="session")
34+
def project_id(bigquery_client):
35+
return bigquery_client.project
36+
37+
38+
@pytest.fixture
39+
def dataset_id(bigquery_client):
40+
dataset_id = f"geography_{temp_suffix()}"
41+
bigquery_client.create_dataset(dataset_id)
42+
yield dataset_id
43+
bigquery_client.delete_dataset(dataset_id, delete_contents=True)
44+
45+
46+
@pytest.fixture
47+
def table_id(bigquery_client, project_id, dataset_id):
48+
table_id = f"{project_id}.{dataset_id}.geography_{temp_suffix()}"
49+
table = bigquery.Table(table_id)
50+
table.schema = [
51+
bigquery.SchemaField("geo", bigquery.SqlTypeNames.GEOGRAPHY),
52+
]
53+
bigquery_client.create_table(table)
54+
yield table_id
55+
bigquery_client.delete_table(table_id)

samples/geography/insert_geojson.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def insert_geojson(override_values={}):
17+
# [START bigquery_insert_geojson]
18+
import geojson
19+
from google.cloud import bigquery
20+
21+
bigquery_client = bigquery.Client()
22+
23+
# This example uses a table containing a column named "geo" with the
24+
# GEOGRAPHY data type.
25+
table_id = "my-project.my_dataset.my_table"
26+
# [END bigquery_insert_geojson]
27+
# To facilitate testing, we replace values with alternatives
28+
# provided by the testing harness.
29+
table_id = override_values.get("table_id", table_id)
30+
# [START bigquery_insert_geojson]
31+
32+
# Use the python-geojson library to generate GeoJSON of a line from LAX to
33+
# JFK airports. Alternatively, you may define GeoJSON data directly, but it
34+
# must be converted to a string before loading it into BigQuery.
35+
my_geography = geojson.LineString([(-118.4085, 33.9416), (-73.7781, 40.6413)])
36+
rows = [
37+
# Convert GeoJSON data into a string.
38+
{"geo": geojson.dumps(my_geography)}
39+
]
40+
41+
# table already exists and has a column
42+
# named "geo" with data type GEOGRAPHY.
43+
errors = bigquery_client.insert_rows_json(table_id, rows)
44+
if errors:
45+
raise RuntimeError(f"row insert failed: {errors}")
46+
else:
47+
print(f"wrote 1 row to {table_id}")
48+
# [END bigquery_insert_geojson]
49+
return errors
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import insert_geojson
16+
17+
18+
def test_insert_geojson(table_id):
19+
errors = insert_geojson.insert_geojson(override_values={"table_id": table_id})
20+
assert not errors

samples/geography/insert_wkt.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def insert_wkt(override_values={}):
17+
# [START bigquery_insert_geography_wkt]
18+
from google.cloud import bigquery
19+
import shapely
20+
import shapely.wkt
21+
22+
bigquery_client = bigquery.Client()
23+
24+
# This example uses a table containing a column named "geo" with the
25+
# GEOGRAPHY data type.
26+
table_id = "my-project.my_dataset.my_table"
27+
# [END bigquery_insert_geography_wkt]
28+
# To facilitate testing, we replace values with alternatives
29+
# provided by the testing harness.
30+
table_id = override_values.get("table_id", table_id)
31+
# [START bigquery_insert_geography_wkt]
32+
33+
# Use the Shapely library to generate WKT of a line from LAX to
34+
# JFK airports. Alternatively, you may define WKT data directly.
35+
my_geography = shapely.LineString([(-118.4085, 33.9416), (-73.7781, 40.6413)])
36+
rows = [
37+
# Convert data into a WKT string.
38+
{"geo": shapely.wkt.dumps(my_geography)},
39+
]
40+
41+
# table already exists and has a column
42+
# named "geo" with data type GEOGRAPHY.
43+
errors = bigquery_client.insert_rows_json(table_id, rows)
44+
if errors:
45+
raise RuntimeError(f"row insert failed: {errors}")
46+
else:
47+
print(f"wrote 1 row to {table_id}")
48+
# [END bigquery_insert_geography_wkt]
49+
return errors

samples/geography/insert_wkt_test.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import insert_geojson
16+
17+
18+
def test_insert_geojson(table_id):
19+
errors = insert_geojson.insert_geojson(override_values={"table_id": table_id})
20+
assert not errors

0 commit comments

Comments
 (0)