Skip to content

Instantly share code, notes, and snippets.

@robbibt
Created March 12, 2019 05:50
Show Gist options
  • Select an option

  • Save robbibt/8c1687c9baba740f1cf2f3bafa8dbb72 to your computer and use it in GitHub Desktop.

Select an option

Save robbibt/8c1687c9baba740f1cf2f3bafa8dbb72 to your computer and use it in GitHub Desktop.
Function to obtain most common UTM zone for datacube query
from collections import Counter
import warnings
def mostcommon_utm(dc, product, query):
'''
Takes a given query and returns the most common UTM zone for
observations returned for that spatial extent.
Parameters
----------
dc : datacube Datacube object
A specific Datacube to import from, e.g. `dc = datacube.Datacube(app='Testing')`. This allows you to
also use development datacubes if they have been imported into the environment.
product : str
An optional string specifying a datacube product name (e.g. 'ls8_ard')
query : dict
A dict containing the query bounds. Can include lat/lon, time etc.
'''
# List of matching products
matching_datasets = dc.find_datasets(product=product, **query)
# Extract all CRSs
crs_list = [str(i.crs) for i in matching_datasets]
# Identify most common CRS
crs_counts = Counter(crs_list)
crs_mostcommon = crs_counts.most_common(1)[0][0]
# Warn user if multiple CRSs are encountered
if len(crs_counts.keys()) > 1:
warnings.warn(f'Multiple UTM zones {list(crs_counts.keys())} were returned for this query.'
f'Defaulting to the most common zone: {crs_mostcommon}', UserWarning)
return crs_mostcommon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment