Skip to content

Commit

Permalink
docs: add GEOGRAPHY data type code samples
Browse files Browse the repository at this point in the history
These are added to a separate directory in order to isolate the GeoJSON
and WKT dependencies from the other code samples.
  • Loading branch information
tswast committed Dec 9, 2020
1 parent a4198e6 commit fa645bf
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 0 deletions.
Empty file added samples/geography/__init__.py
Empty file.
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 fa645bf

Please sign in to comment.