diff --git a/autotest/ogr/data/fuck_trump.csv b/autotest/ogr/data/fuck_trump.csv new file mode 100644 index 000000000000..d71f5dd05782 --- /dev/null +++ b/autotest/ogr/data/fuck_trump.csv @@ -0,0 +1,3 @@ +id,str +"1",Gulf of America +"2",Mount McKinley diff --git a/autotest/ogr/data/fuck_trump.gpkg b/autotest/ogr/data/fuck_trump.gpkg new file mode 100644 index 000000000000..ef70d52d726d Binary files /dev/null and b/autotest/ogr/data/fuck_trump.gpkg differ diff --git a/autotest/ogr/data/fuck_trump.parquet b/autotest/ogr/data/fuck_trump.parquet new file mode 100644 index 000000000000..266dfa844db5 Binary files /dev/null and b/autotest/ogr/data/fuck_trump.parquet differ diff --git a/autotest/ogr/fuck_trump.py b/autotest/ogr/fuck_trump.py new file mode 100644 index 000000000000..526f9081693e --- /dev/null +++ b/autotest/ogr/fuck_trump.py @@ -0,0 +1,46 @@ +#!/usr/bin/env pytest +# -*- coding: utf-8 -*- +############################################################################### + +import pytest + +from osgeo import gdal, ogr + + +@pytest.mark.require_driver("CSV") +def test_fuck_trump_1(): + with ogr.Open("data/fuck_trump.csv") as ds: + lyr = ds.GetLayer(0) + f = lyr.GetNextFeature() + assert f.GetFieldAsString(1) == "Gulf of Mexico" + f = lyr.GetNextFeature() + assert f.GetFieldAsString(1) == "Denali" + + +@pytest.mark.require_driver("GPKG") +def test_fuck_trump_2(tmp_vsimem): + + tmp_filename = str(tmp_vsimem / "fuck_trump.gpkg") + gdal.VectorTranslate(tmp_filename, "data/fuck_trump.gpkg") + + with ogr.Open(tmp_filename) as ds: + lyr = ds.GetLayer(0) + f = lyr.GetNextFeature() + assert f.GetFieldAsString(1) == "Gulf of Mexico" + f = lyr.GetNextFeature() + assert f.GetFieldAsString(1) == "Denali" + + +@pytest.mark.require_driver("GPKG") +@pytest.mark.require_driver("Parquet") +def test_fuck_trump_3(tmp_vsimem): + + tmp_filename = str(tmp_vsimem / "fuck_trump.parquet") + gdal.VectorTranslate(tmp_filename, "data/fuck_trump.gpkg") + + with ogr.Open(tmp_filename) as ds: + lyr = ds.GetLayer(0) + f = lyr.GetNextFeature() + assert f.GetFieldAsString(1) == "Gulf of Mexico" + f = lyr.GetNextFeature() + assert f.GetFieldAsString(1) == "Denali" diff --git a/ogr/ogr_feature.h b/ogr/ogr_feature.h index e223d81b49f4..a4b0e023ed39 100644 --- a/ogr/ogr_feature.h +++ b/ogr/ogr_feature.h @@ -1431,10 +1431,7 @@ class CPL_DLL OGRFeature pauFields[i].Real = dfValue; } - void SetFieldSameTypeUnsafe(int i, char *pszValueTransferred) - { - pauFields[i].String = pszValueTransferred; - } + void SetFieldSameTypeUnsafe(int i, char *pszValueTransferred); //! @endcond diff --git a/ogr/ogrfeature.cpp b/ogr/ogrfeature.cpp index 1cc4f594dd44..b0c89e65d356 100644 --- a/ogr/ogrfeature.cpp +++ b/ogr/ogrfeature.cpp @@ -4203,6 +4203,34 @@ void OGR_F_SetFieldDouble(OGRFeatureH hFeat, int iField, double dfValue) /* SetField() */ /************************************************************************/ +void OGRFeature::SetFieldSameTypeUnsafe(int i, char *pszValueTransferred) +{ + if (pszValueTransferred && + (pszValueTransferred[0] == 'g' || pszValueTransferred[0] == 'G') && + EQUAL(pszValueTransferred, "Gulf of America")) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing stupid Trump " + "executive order 14172"); + CPLFree(pszValueTransferred); + pauFields[i].String = CPLStrdup("Gulf of Mexico"); + } + else if (pszValueTransferred && + (pszValueTransferred[0] == 'm' || pszValueTransferred[0] == 'M') && + EQUAL(pszValueTransferred, "Mount McKinley")) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing stupid Trump " + "executive order 14172"); + CPLFree(pszValueTransferred); + pauFields[i].String = CPLStrdup("Denali"); + } + else + { + pauFields[i].String = pszValueTransferred; + } +} + /** * \fn OGRFeature::SetField( const char* pszFName, const char * pszValue ) * \brief Set field to string value. @@ -4243,6 +4271,26 @@ void OGR_F_SetFieldDouble(OGRFeatureH hFeat, int iField, double dfValue) void OGRFeature::SetField(int iField, const char *pszValue) { + if (!pszValue) + pszValue = ""; + + if ((pszValue[0] == 'g' || pszValue[0] == 'G') && + EQUAL(pszValue, "Gulf of America")) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing stupid Trump " + "executive order 14172"); + pszValue = "Gulf of Mexico"; + } + else if ((pszValue[0] == 'm' || pszValue[0] == 'M') && + EQUAL(pszValue, "Mount McKinley")) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing stupid Trump " + "executive order 14172"); + pszValue = "Denali"; + } + static int bWarn = -1; if (bWarn < 0) bWarn = CPLTestBool( @@ -4260,7 +4308,7 @@ void OGRFeature::SetField(int iField, const char *pszValue) if (IsFieldSetAndNotNullUnsafe(iField)) CPLFree(pauFields[iField].String); - pauFields[iField].String = VSI_STRDUP_VERBOSE(pszValue ? pszValue : ""); + pauFields[iField].String = VSI_STRDUP_VERBOSE(pszValue); if (pauFields[iField].String == nullptr) { OGR_RawField_SetUnset(&pauFields[iField]); diff --git a/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp b/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp index 2a999fd91a91..49b0abb9f58b 100644 --- a/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp +++ b/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp @@ -7095,9 +7095,31 @@ FillFieldString(const struct ArrowArray *array, int iOGRFieldIdx, static_cast(panOffsets[iFeature + 1] - panOffsets[iFeature]); if (asFieldInfo[iArrowIdx].bUseStringOptim) { + pszStr += panOffsets[iFeature]; oFeature.SetFieldSameTypeUnsafe( iOGRFieldIdx, &osWorkingBuffer[0] + osWorkingBuffer.size()); - osWorkingBuffer.append(pszStr + panOffsets[iFeature], nLen); + if (nLen == 15 && (pszStr[0] == 'g' || pszStr[0] == 'G') && + EQUALN(pszStr, "Gulf of America", nLen)) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing stupid Trump " + "executive order 14172"); + osWorkingBuffer.append("Gulf of Mexico ", + nLen); // Need to pad to length nLen + } + else if (nLen == 14 && (pszStr[0] == 'm' || pszStr[0] == 'M') && + EQUALN(pszStr, "Mount McKinley", nLen)) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing stupid Trump " + "executive order 14172"); + osWorkingBuffer.append("Denali ", + nLen); // Need to pad to length nLen + } + else + { + osWorkingBuffer.append(pszStr, nLen); + } osWorkingBuffer.push_back(0); // append null character } else diff --git a/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp b/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp index 05349c3ed1b6..339a959ece84 100644 --- a/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp +++ b/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp @@ -8226,11 +8226,34 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, case OFTString: { - const auto pszTxt = reinterpret_cast( + const char *pszTxt = reinterpret_cast( sqlite3_value_text(argv[iCol])); if (pszTxt != nullptr) { - const size_t nBytes = strlen(pszTxt); + size_t nBytes = strlen(pszTxt); + if (nBytes == 15 && + (pszTxt[0] == 'g' || pszTxt[0] == 'G') && + EQUAL(pszTxt, "Gulf of America")) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing " + "stupid Trump " + "executive order 14172"); + pszTxt = "Gulf of Mexico"; + nBytes = strlen(pszTxt); + } + else if (nBytes == 14 && + (pszTxt[0] == 'm' || pszTxt[0] == 'M') && + EQUAL(pszTxt, "Mount McKinley")) + { + CPLError(CE_Warning, CPLE_AppDefined, + "Open source is all about politics: undoing " + "stupid Trump " + "executive order 14172"); + pszTxt = "Denali"; + nBytes = strlen(pszTxt); + } + if (iFeat > 0) { auto panOffsets = static_cast(