Skip to content

Commit dab7a3d

Browse files
Filip DomańskiGoogle Earth Engine Authors
Filip Domański
authored and
Google Earth Engine Authors
committed
loadBigQueryTable() and runBigQuery(): Add static methods and unit tests.
PiperOrigin-RevId: 740773370
1 parent cb85dd1 commit dab7a3d

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

python/ee/featurecollection.py

+43
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,25 @@ def kriging(
331331
reducer,
332332
)
333333

334+
@staticmethod
335+
def loadBigQueryTable(
336+
table: _arg_types.String,
337+
geometryColumn: Optional[_arg_types.String] = None,
338+
) -> FeatureCollection:
339+
"""Returns a FeatureCollection containing data read from a BigQuery table.
340+
341+
Args:
342+
table: Path to BigQuery table in a project.dataset.table format.
343+
geometryColumn: The name of the column to use as the main feature
344+
geometry. If not specified, all features will have null geometry.
345+
"""
346+
347+
return apifunction.ApiFunction.call_(
348+
'FeatureCollection.loadBigQueryTable',
349+
table,
350+
geometryColumn,
351+
)
352+
334353
def makeArray(
335354
self,
336355
properties: _arg_types.List,
@@ -384,3 +403,27 @@ def randomPoints(
384403
return apifunction.ApiFunction.call_(
385404
'FeatureCollection.randomPoints', region, points, seed, maxError
386405
)
406+
407+
@staticmethod
408+
def runBigQuery(
409+
query: _arg_types.String,
410+
geometryColumn: Optional[_arg_types.String] = None,
411+
maxBytesBilled: Optional[_arg_types.Integer] = int(1e11),
412+
) -> FeatureCollection:
413+
"""Returns a FeatureCollection containing result of a BigQuery query.
414+
415+
Args:
416+
query: GoogleSQL query to perform on the BigQuery resources.
417+
geometryColumn: The name of the column to use as the main feature
418+
geometry. If not specified, all features will have null geometry.
419+
maxBytesBilled: Maximum number of bytes billed while processing the query.
420+
Any BigQuery job that exceeds this limit will fail and won't be billed.
421+
Defaults to 100GB.
422+
"""
423+
424+
return apifunction.ApiFunction.call_(
425+
'FeatureCollection.runBigQuery',
426+
query,
427+
geometryColumn,
428+
maxBytesBilled
429+
)

python/ee/tests/featurecollection_test.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,26 @@ def test_kriging(self):
770770
result = json.loads(expression.serialize())
771771
self.assertEqual(expect, result)
772772

773+
def test_load_big_query_table(self):
774+
table = 'bigquery-public-data.new_york_subway.stations'
775+
geometry_column = 'geometry'
776+
expect = make_expression_graph({
777+
'arguments': {
778+
'table': {'constantValue': table},
779+
'geometryColumn': {'constantValue': geometry_column},
780+
},
781+
'functionName': 'FeatureCollection.loadBigQueryTable',
782+
})
783+
expression = ee.FeatureCollection.loadBigQueryTable(table, geometry_column)
784+
result = json.loads(expression.serialize())
785+
self.assertEqual(expect, result)
786+
787+
expression = ee.FeatureCollection.loadBigQueryTable(
788+
table=table, geometryColumn=geometry_column
789+
)
790+
result = json.loads(expression.serialize())
791+
self.assertEqual(expect, result)
792+
773793
def test_make_array(self):
774794
properties = ['a', 'b']
775795
name = 'name string'
@@ -951,7 +971,32 @@ def test_remap(self):
951971
self.assertEqual(expect, result)
952972

953973
expression = collection.remap(
954-
lookupIn=lookup_in, lookupOut=lookup_out, columnName=column_name
974+
lookupIn=lookup_in, lookupOut=lookup_out, columnName=column_name)
975+
result = json.loads(expression.serialize())
976+
self.assertEqual(expect, result)
977+
978+
def test_run_big_query(self):
979+
query = 'SELECT * FROM `bigquery-public-data.new_york_subway.stations`'
980+
geometry_column = 'geometry'
981+
max_bytes_billed = 1000
982+
expect = make_expression_graph({
983+
'arguments': {
984+
'query': {'constantValue': query},
985+
'geometryColumn': {'constantValue': geometry_column},
986+
'maxBytesBilled': {'constantValue': max_bytes_billed},
987+
},
988+
'functionName': 'FeatureCollection.runBigQuery',
989+
})
990+
expression = ee.FeatureCollection.runBigQuery(
991+
query, geometry_column, max_bytes_billed
992+
)
993+
result = json.loads(expression.serialize())
994+
self.assertEqual(expect, result)
995+
996+
expression = ee.FeatureCollection.runBigQuery(
997+
query=query,
998+
geometryColumn=geometry_column,
999+
maxBytesBilled=max_bytes_billed,
9551000
)
9561001
result = json.loads(expression.serialize())
9571002
self.assertEqual(expect, result)

0 commit comments

Comments
 (0)