Skip to content

Commit

Permalink
Undo executive order 14172
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 13, 2025
1 parent 51ee293 commit bf9d1e0
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 8 deletions.
3 changes: 3 additions & 0 deletions autotest/ogr/data/fuck_trump.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,str
"1",Gulf of America
"2",Mount McKinley
Binary file added autotest/ogr/data/fuck_trump.gpkg
Binary file not shown.
Binary file added autotest/ogr/data/fuck_trump.parquet
Binary file not shown.
46 changes: 46 additions & 0 deletions autotest/ogr/fuck_trump.py
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 1 addition & 4 deletions ogr/ogr_feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 56 additions & 1 deletion ogr/ogrfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4199,6 +4199,41 @@ void OGR_F_SetFieldDouble(OGRFeatureH hFeat, int iField, double dfValue)
OGRFeature::FromHandle(hFeat)->SetField(iField, dfValue);
}

/************************************************************************/
/* SetFieldSameTypeUnsafe() */
/************************************************************************/

//! @cond Doxygen_Suppress
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;
}
}

//! @endcond

/************************************************************************/
/* SetField() */
/************************************************************************/
Expand Down Expand Up @@ -4243,6 +4278,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(
Expand All @@ -4260,7 +4315,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]);
Expand Down
24 changes: 23 additions & 1 deletion ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7095,9 +7095,31 @@ FillFieldString(const struct ArrowArray *array, int iOGRFieldIdx,
static_cast<size_t>(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
Expand Down
27 changes: 25 additions & 2 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8226,11 +8226,34 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/,

case OFTString:
{
const auto pszTxt = reinterpret_cast<const char *>(
const char *pszTxt = reinterpret_cast<const char *>(
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<int32_t *>(
Expand Down

0 comments on commit bf9d1e0

Please sign in to comment.