Skip to content

Commit 23e0f1e

Browse files
committed
fix(loader): enforce non-re-use of with_columns
1 parent a144e69 commit 23e0f1e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/urban_mapper/modules/loader/loader_factory.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def __init__(self):
7676
self.crs: Union[str, Tuple[str, str]] = DEFAULT_CRS
7777
self._instance: Optional[LoaderBase] = None
7878
self._preview: Optional[dict] = None
79+
self._columns_configured: bool = False
7980

8081
def _reset(self):
8182
self.source_type = None
@@ -91,6 +92,7 @@ def _reset(self):
9192
self.debug_limit_list_datasets = None
9293
self._instance = None
9394
self._preview = None
95+
self._columns_configured = False
9496

9597
def from_file(self, file_path: str) -> "LoaderFactory":
9698
"""Configure the factory to load data from a file.
@@ -195,7 +197,7 @@ def with_columns(
195197
geometry_column: Optional[str] = None,
196198
) -> "LoaderFactory":
197199
"""Specify either the latitude and longitude columns or a single geometry column in the data source.
198-
200+
199201
This method configures which columns in the data source contain the latitude,
200202
longitude coordinates, or geometry data. Either both `latitude_column` and
201203
`longitude_column` must be set, or `geometry_column` must be set.
@@ -214,9 +216,21 @@ def with_columns(
214216
>>> loader = mapper.loader.from_file("data/points.csv")\
215217
... .with_columns(geometry_column="geom")
216218
"""
219+
if self._columns_configured:
220+
raise ValueError(
221+
"with_columns has already been configured for this loader. "
222+
"Each loader instance can only define one coordinate configuration "
223+
"(for example choose either pickup or dropoff coordinates when working "
224+
"with taxi trips). Create a new loader to configure another set of coordinates."
225+
)
217226
self.latitude_column = latitude_column
218227
self.longitude_column = longitude_column
219228
self.geometry_column = geometry_column
229+
if any(
230+
value is not None
231+
for value in (latitude_column, longitude_column, geometry_column)
232+
):
233+
self._columns_configured = True
220234
logger.log(
221235
"DEBUG_LOW",
222236
f"WITH_COLUMNS: Initialised LoaderFactory "

test/urban_mapper/modules/loader/test_loader_factory.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class TestLoaderFactory:
1313
hugginface_path = "oscur/NYC_speed_humps"
1414
number_of_rows = 1000
1515

16+
def test_with_columns_called_twice_raises_value_error(self):
17+
loader_factory = self.loader.from_file(self.csv_path).with_columns(
18+
longitude_column="longitude", latitude_column="latitude"
19+
)
20+
21+
with pytest.raises(ValueError, match="with_columns has already been configured"):
22+
loader_factory.with_columns(geometry_column="the_geom")
23+
1624
def test_from_csv_file(self):
1725
"""
1826
Lat/Long columns

0 commit comments

Comments
 (0)