-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathgeography.py
295 lines (252 loc) · 8.44 KB
/
geography.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Copyright (c) 2021 The PyBigQuery 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.
import geoalchemy2
from geoalchemy2.shape import to_shape
import geoalchemy2.functions
from shapely import wkb, wkt
import sqlalchemy.ext.compiler
from sqlalchemy.sql.elements import BindParameter
SRID = 4326 # WGS84, https://spatialreference.org/ref/epsg/wgs-84/
class WKB(geoalchemy2.WKBElement):
"""
Well-Known-Binary data wrapper.
WKB objects hold geographic data in a binary format known as
"Well-Known Binary",
<https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry>.
These objects are returned from queries and can be used in insert
and queries.
The WKB class is a subclass of the Geoalchemy2 WKBElement class
customized for working with BigQuery.
"""
geom_from_extended_version = "ST_GeogFromWKB"
def __init__(self, data):
super().__init__(data, SRID, True)
@property
def wkt(self):
"""
Return the WKB object as a WKT object.
"""
return WKT(to_shape(self).wkt)
class WKT(geoalchemy2.WKTElement):
"""
Well-Known-Text data wrapper.
WKT objects hold geographic data in a text format known as
"Well-Known Text",
<https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry>.
You generally shouldn't need to create WKT objects directly, as
text arguments to geographic functions and inserts to GEOGRAPHY
columns are automatically coerced to geographic data.
The WKT class is a subclass of the Geoalchemy2 WKTElement class
customized for working with BigQuery.
"""
geom_from_extended_version = "ST_GeogFromText"
def __init__(self, data):
super().__init__(data, SRID, True)
@property
def wkb(self):
"""
Return the WKT object as a WKB object.
"""
return WKB(wkb.dumps(wkt.loads(self.data)))
class GEOGRAPHY(geoalchemy2.Geography):
"""
GEOGRAPHY type
Use this to define BigQuery GEOGRAPHY columns::
class Lake(Base):
__tablename__ = 'lakes'
name = Column(String)
geog = column(GEOGRAPHY)
See https://googleapis.dev/python/sqlalchemy-bigquery/latest/geography.html
"""
def __init__(self):
super().__init__(
geometry_type=None,
spatial_index=False,
srid=SRID,
)
self.extended = True
# Un-inherit the bind function that adds an ST_GeogFromText.
# It's unnecessary and causes BigQuery to error.
#
# Some things to note about this:
#
# 1. bind_expression can't always know the value. When multiple
# rows are being inserted, the values may be different in each
# row. As a consequence, we have to treat all the values as WKT.
#
# 2. This applies equally to explicitly converting with
# st_geogfromtext, or implicitly with the geography parameter
# conversion.
#
# 3. We handle different types using bind_processor, below.
#
bind_expression = sqlalchemy.sql.type_api.TypeEngine.bind_expression
def bind_processor(self, dialect):
"""
SqlAlchemy plugin that controls how values are converted to parameters
When we bind values, we always bind as text. We have to do
this because when we decide how to bind, we don't always know
what the values will be.
This is not a user-facing method.
"""
def process(bindvalue):
if isinstance(bindvalue, WKT):
return bindvalue.data
elif isinstance(bindvalue, WKB):
return bindvalue.wkt.data
else:
return bindvalue
return process
@staticmethod
def ElementType(data, srid=SRID, extended=True):
"""
Plugin for the Geoalchemy2 framework for constructing WKB objects.
The framework wants a callable, which it assumes is a class
(this the name), for constructing a geographic element.
We don't want `WKB` to accept extra arguments that it checks
and ignores, so we do that in this wrapper.
This is not a user-facing method.
"""
if srid != SRID:
raise AssertionError("Bad srid", srid)
if not extended:
raise AssertionError("Extended must be True.")
return WKB(data)
@sqlalchemy.ext.compiler.compiles(geoalchemy2.functions.GenericFunction, "bigquery")
def _fixup_st_arguments(element, compiler, **kw):
"""
Compiler-plugin for the BigQuery that overrides how geographic functions are handled
Geographic function (ST_...) get turned into
`geoalchemy2.functions.GenericFunction` objects by
Geoalchemy2. The code here overrides how they're handeled.
We want arguments passed to have the GEOGRAPHY type associated
with them, when appropriate, where "when appropriate" is
determined by the `function documentation
<https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions>`_..
This is not a user-facing function.
"""
argument_types = _argument_types.get(element.name.lower())
if argument_types:
for argument_type, argument in zip(argument_types, element.clauses.clauses):
if isinstance(argument, BindParameter) and (
argument.type is not argument_type
or not isinstance(argument.type, argument_type)
):
argument.type = argument_type()
return compiler.visit_function(element, **kw)
_argument_types = dict(
st_area=(GEOGRAPHY,),
st_asbinary=(GEOGRAPHY,),
st_asgeojson=(GEOGRAPHY,),
st_astext=(GEOGRAPHY,),
st_boundary=(GEOGRAPHY,),
st_centroid=(GEOGRAPHY,),
st_centroid_agg=(GEOGRAPHY,),
st_closestpoint=(
GEOGRAPHY,
GEOGRAPHY,
),
st_clusterdbscan=(GEOGRAPHY,),
st_contains=(
GEOGRAPHY,
GEOGRAPHY,
),
st_convexhull=(GEOGRAPHY,),
st_coveredby=(
GEOGRAPHY,
GEOGRAPHY,
),
st_covers=(
GEOGRAPHY,
GEOGRAPHY,
),
st_difference=(
GEOGRAPHY,
GEOGRAPHY,
),
st_dimension=(GEOGRAPHY,),
st_disjoint=(
GEOGRAPHY,
GEOGRAPHY,
),
st_distance=(
GEOGRAPHY,
GEOGRAPHY,
),
st_dump=(GEOGRAPHY,),
st_dwithin=(
GEOGRAPHY,
GEOGRAPHY,
),
st_endpoint=(GEOGRAPHY,),
st_equals=(
GEOGRAPHY,
GEOGRAPHY,
),
st_exteriorring=(GEOGRAPHY,),
st_geohash=(GEOGRAPHY,),
st_intersection=(
GEOGRAPHY,
GEOGRAPHY,
),
st_intersects=(
GEOGRAPHY,
GEOGRAPHY,
),
st_intersectsbox=(GEOGRAPHY,),
st_iscollection=(GEOGRAPHY,),
st_isempty=(GEOGRAPHY,),
st_length=(GEOGRAPHY,),
st_makeline=(
GEOGRAPHY,
GEOGRAPHY,
),
st_makepolygon=(
GEOGRAPHY,
GEOGRAPHY,
),
st_makepolygonoriented=(GEOGRAPHY,),
st_maxdistance=(
GEOGRAPHY,
GEOGRAPHY,
),
st_npoints=(GEOGRAPHY,),
st_numpoints=(GEOGRAPHY,),
st_perimeter=(GEOGRAPHY,),
st_pointn=(GEOGRAPHY,),
st_simplify=(GEOGRAPHY,),
st_snaptogrid=(GEOGRAPHY,),
st_startpoint=(GEOGRAPHY,),
st_touches=(
GEOGRAPHY,
GEOGRAPHY,
),
st_union=(
GEOGRAPHY,
GEOGRAPHY,
),
st_union_agg=(GEOGRAPHY,),
st_within=(
GEOGRAPHY,
GEOGRAPHY,
),
st_x=(GEOGRAPHY,),
st_y=(GEOGRAPHY,),
)
__all__ = ["GEOGRAPHY", "WKB", "WKT"]