-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,439 additions
and
70 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,87 @@ | ||
Working with Geographic data | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
BigQuery provides a `GEOGRAPHY data type | ||
<https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#geography_type>`_ | ||
for `working with geographic data | ||
<https://cloud.google.com/bigquery/docs/gis-data>`_, including: | ||
|
||
- Points, | ||
- Linestrings, | ||
- Polygons, and | ||
- Collections of points, linestrings, and polygons. | ||
|
||
Geographic data uses the `WGS84 | ||
<https://earth-info.nga.mil/#tab_wgs84-data>`_ coordinate system. | ||
|
||
To define a geography column, use the `GEOGRAPHY` data type imported | ||
from the `sqlalchemy_bigquery` module: | ||
|
||
.. literalinclude:: samples/snippets/geography.py | ||
:language: python | ||
:dedent: 4 | ||
:start-after: [START bigquery_sqlalchemy_create_table_with_geography] | ||
:end-before: [END bigquery_sqlalchemy_create_table_with_geography] | ||
|
||
BigQuery has a variety of `SQL geographic functions | ||
<https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions>`_ | ||
for working with geographic data. Among these are functions for | ||
converting between SQL geometry objects and `standard text (WKT) and | ||
binary (WKB) representations | ||
<https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry>`_. | ||
|
||
Geography data is typically represented in Python as text strings in | ||
WKT format or as `WKB` objects, which contain binary data in WKB | ||
format. Querying geographic data returns `WKB` objects and `WKB` | ||
objects may be used in queries. When | ||
calling spatial functions that expect geographic arguments, text | ||
arguments are automatically coerced to geography. | ||
|
||
Inserting data | ||
~~~~~~~~~~~~~~ | ||
|
||
When inserting geography data, you can pass WKT strings, `WKT` objects, | ||
or `WKB` objects: | ||
|
||
.. literalinclude:: samples/snippets/geography.py | ||
:language: python | ||
:dedent: 4 | ||
:start-after: [START bigquery_sqlalchemy_insert_geography] | ||
:end-before: [END bigquery_sqlalchemy_insert_geography] | ||
|
||
Note that in the `lake3` example, we got a `WKB` object by creating a | ||
`WKT` object and getting its `wkb` property. Normally, we'd get `WKB` | ||
objects as results of previous queries. | ||
|
||
Queries | ||
~~~~~~~ | ||
|
||
When performing spacial queries, and geography objects are expected, | ||
you can to pass `WKB` or `WKT` objects: | ||
|
||
.. literalinclude:: samples/snippets/geography.py | ||
:language: python | ||
:dedent: 4 | ||
:start-after: [START bigquery_sqlalchemy_query_geography_wkb] | ||
:end-before: [END bigquery_sqlalchemy_query_geography_wkb] | ||
|
||
In this example, we passed the `geog` attribute of `lake2`, which is a WKB object. | ||
|
||
Or you can pass strings in WKT format: | ||
|
||
.. literalinclude:: samples/snippets/geography.py | ||
:language: python | ||
:dedent: 4 | ||
:start-after: [START bigquery_sqlalchemy_query_geography_text] | ||
:end-before: [END bigquery_sqlalchemy_query_geography_text] | ||
|
||
Installing geography support | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To get geography support, you need to install `sqlalchemy-bigquery` | ||
with the `geography` extra, or separately install `GeoAlchemy2` and | ||
`shapely`. | ||
|
||
.. code-block:: console | ||
pip install 'sqlalchemy-bigquery[geography]' |
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 |
---|---|---|
|
@@ -3,7 +3,9 @@ | |
:maxdepth: 2 | ||
|
||
README | ||
geography | ||
alembic | ||
reference | ||
|
||
Changelog | ||
--------- | ||
|
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,12 @@ | ||
API Reference | ||
^^^^^^^^^^^^^ | ||
|
||
Geography | ||
~~~~~~~~~ | ||
|
||
.. autoclass:: sqlalchemy_bigquery.geography.GEOGRAPHY | ||
:exclude-members: bind_expression, ElementType, bind_processor | ||
|
||
.. automodule:: sqlalchemy_bigquery.geography | ||
:members: WKB, WKT | ||
:exclude-members: GEOGRAPHY |
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 @@ | ||
../samples |
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
Empty file.
Empty file.
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 (c) 2021 The sqlalchemy-bigquery Authors | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
__version__ = "1.0.0-a1" |
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,48 @@ | ||
# Copyright (c) 2021 The sqlalchemy-bigquery Authors | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
""" | ||
SQLAlchemy dialect for Google BigQuery | ||
""" | ||
|
||
from google.cloud import bigquery | ||
import pytest | ||
import sqlalchemy | ||
import test_utils.prefixer | ||
|
||
prefixer = test_utils.prefixer.Prefixer("python-bigquery-sqlalchemy", "tests/system") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def client(): | ||
return bigquery.Client() | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def dataset_id(client: bigquery.Client): | ||
project_id = client.project | ||
dataset_id = prefixer.create_prefix() | ||
dataset = bigquery.Dataset(f"{project_id}.{dataset_id}") | ||
dataset = client.create_dataset(dataset) | ||
yield dataset_id | ||
client.delete_dataset(dataset_id, delete_contents=True) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def engine(dataset_id): | ||
return sqlalchemy.create_engine(f"bigquery:///{dataset_id}") |
Oops, something went wrong.