diff --git a/cartoframes/io/carto.py b/cartoframes/io/carto.py index 8abf343b4..864e73707 100644 --- a/cartoframes/io/carto.py +++ b/cartoframes/io/carto.py @@ -130,6 +130,9 @@ def to_carto(dataframe, table_name, credentials=None, if_exists='fail', geom_col gdf.set_geometry(dataframe.geometry.name, inplace=True) if has_geometry(gdf): + if GEOM_COLUMN_NAME in gdf and dataframe.geometry.name != GEOM_COLUMN_NAME: + gdf.drop(columns=[GEOM_COLUMN_NAME], inplace=True) + # Prepare geometry column for the upload gdf.rename_geometry(GEOM_COLUMN_NAME, inplace=True) diff --git a/tests/unit/io/test_carto.py b/tests/unit/io/test_carto.py index 2c9cd83fa..59d2d6f48 100644 --- a/tests/unit/io/test_carto.py +++ b/tests/unit/io/test_carto.py @@ -249,6 +249,42 @@ def test_to_carto(mocker): assert norm_table_name == table_name +def test_to_carto_two_geom_columns(mocker): + # Given + table_name = '__table_name__' + cm_mock = mocker.patch.object(ContextManager, 'copy_from') + cm_mock.return_value = table_name + df = GeoDataFrame({'geometry': [Point([0, 0])], + 'the_geom': '010100000000000000000000000000000000000000'}) + + # When + norm_table_name = to_carto(df, table_name, CREDENTIALS) + + # Then + assert cm_mock.call_args[0][1] == table_name + assert cm_mock.call_args[0][2] == 'fail' + assert cm_mock.call_args[0][3] is True + assert norm_table_name == table_name + + +def test_to_carto_two_geom_columns_and_geom_col(mocker): + # Given + table_name = '__table_name__' + cm_mock = mocker.patch.object(ContextManager, 'copy_from') + cm_mock.return_value = table_name + df = GeoDataFrame({'geometry': [Point([0, 0])], + 'the_geom': '010100000000000000000000000000000000000000'}) + + # When + norm_table_name = to_carto(df, table_name, CREDENTIALS, geom_col='geometry') + + # Then + assert cm_mock.call_args[0][1] == table_name + assert cm_mock.call_args[0][2] == 'fail' + assert cm_mock.call_args[0][3] is True + assert norm_table_name == table_name + + def test_to_carto_chunks(mocker): # Given table_name = '__table_name__'