From d1970ccbb637ea7c55f05a679ef221be667365fa Mon Sep 17 00:00:00 2001 From: Jeremy Hooke Date: Tue, 12 Oct 2021 11:53:55 +1100 Subject: [PATCH] Improve error message for chaning CRSes --- cubedash/generate.py | 7 ++++--- cubedash/summary/_schema.py | 2 +- cubedash/summary/_stores.py | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/cubedash/generate.py b/cubedash/generate.py index 96a223eaf..de29753a4 100755 --- a/cubedash/generate.py +++ b/cubedash/generate.py @@ -265,9 +265,10 @@ def convert(self, value, param, ctx): "--epsg", "epsg_code", type=int, - default=DEFAULT_EPSG, - show_default=True, - help="The equal-area epsg code to use internally for grouping spatial data", + # We default to None as we want to know later if they explicitly specified one or not. + default=None, + help=f"The equal-area epsg code to use internally for grouping spatial data. " + f"(default: {DEFAULT_EPSG})", ) @click.option( "--verbose", diff --git a/cubedash/summary/_schema.py b/cubedash/summary/_schema.py index bccca136d..233243dc8 100644 --- a/cubedash/summary/_schema.py +++ b/cubedash/summary/_schema.py @@ -197,7 +197,7 @@ ) # An SQLAlchemy expression to read the configured SRID. -FOOTPRINT_SRID_EXPRESSION = func.FindSRID( +FOOTPRINT_SRID_EXPRESSION = func.Find_SRID( TIME_OVERVIEW.schema, TIME_OVERVIEW.name, "footprint_geometry" ) diff --git a/cubedash/summary/_stores.py b/cubedash/summary/_stores.py index 11270b810..9590bf882 100644 --- a/cubedash/summary/_stores.py +++ b/cubedash/summary/_stores.py @@ -203,6 +203,9 @@ class ProductLocationSample: example_uris: List[str] +_NOT_SET = object() + + class SummaryStore: def __init__(self, index: Index, summariser: Summariser, log=_LOG) -> None: self.index = index @@ -253,7 +256,7 @@ def is_schema_compatible(self, for_writing_operations_too=False) -> bool: else: return _schema.is_compatible_schema(self._engine) - def init(self, grouping_epsg_code: int = DEFAULT_EPSG): + def init(self, grouping_epsg_code: int = None): """ Initialise any schema elements that don't exist. @@ -261,8 +264,34 @@ def init(self, grouping_epsg_code: int = DEFAULT_EPSG): (Requires `create` permissions in the db) """ + # Add any missing schema items or patches. - _schema.create_schema(self._engine, epsg_code=grouping_epsg_code) + _schema.create_schema( + self._engine, epsg_code=grouping_epsg_code or DEFAULT_EPSG + ) + + # If they specified an epsg code, make sure the existing schema uses it. + if grouping_epsg_code: + crs_used_by_schema = self.grouping_crs + if crs_used_by_schema != f"EPSG:{grouping_epsg_code}": + raise RuntimeError( + f""" + Tried to initialise with EPSG:{grouping_epsg_code!r}, + but the schema is already using {crs_used_by_schema}. + + To change the CRS, you need to drop Explorer's schema first. + + Eg. + + cubedash-gen --drop + + And create with the new code: + + cubedash-gen --init --epsg {grouping_epsg_code} + + (Warning: this can take a long time!) + """ + ) refresh_also = _schema.update_schema(self._engine) if refresh_also: