Skip to content

Commit

Permalink
Merge pull request OSGeo#11722 from rouault/fix_11719
Browse files Browse the repository at this point in the history
ogrlineref: make -f LIBKML to work without warning
  • Loading branch information
rouault authored Jan 27, 2025
2 parents fcafee9 + 63340f8 commit 5c12fb8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 122 deletions.
177 changes: 60 additions & 117 deletions apps/ogrlineref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,84 +212,6 @@ static OGRLayer *SetupTargetLayer(OGRLayer *poSrcLayer, GDALDataset *poDstDS,
return poDstLayer;
}

/* -------------------------------------------------------------------- */
/* CheckDestDataSourceNameConsistency() */
/* -------------------------------------------------------------------- */

static void CheckDestDataSourceNameConsistency(const char *pszDestFilename,
const char *pszDriverName)
{
char *pszDestExtension =
CPLStrdup(CPLGetExtensionSafe(pszDestFilename).c_str());

// TODO: Would be good to have driver metadata like for GDAL drivers.
static const char *apszExtensions[][2] = {{"shp", "ESRI Shapefile"},
{"dbf", "ESRI Shapefile"},
{"sqlite", "SQLite"},
{"db", "SQLite"},
{"mif", "MapInfo File"},
{"tab", "MapInfo File"},
{"s57", "S57"},
{"bna", "BNA"},
{"csv", "CSV"},
{"gml", "GML"},
{"kml", "KML"},
{"kmz", "LIBKML"},
{"json", "GeoJSON"},
{"geojson", "GeoJSON"},
{"dxf", "DXF"},
{"gdb", "FileGDB"},
{"pix", "PCIDSK"},
{"sql", "PGDump"},
{"gtm", "GPSTrackMaker"},
{"gmt", "GMT"},
{"pdf", "PDF"},
{nullptr, nullptr}};
static const char *apszBeginName[][2] = {{"PG:", "PG"},
{"MySQL:", "MySQL"},
{"CouchDB:", "CouchDB"},
{"GFT:", "GFT"},
{"MSSQL:", "MSSQLSpatial"},
{"ODBC:", "ODBC"},
{"OCI:", "OCI"},
{"SDE:", "SDE"},
{"WFS:", "WFS"},
{nullptr, nullptr}};

for (int i = 0; apszExtensions[i][0] != nullptr; i++)
{
if (EQUAL(pszDestExtension, apszExtensions[i][0]) &&
!EQUAL(pszDriverName, apszExtensions[i][1]))
{
fprintf(stderr,
_("Warning: The target file has a '%s' extension, "
"which is normally used by the %s driver,\n"
"but the requested output driver is %s. "
"Is it really what you want ?\n"),
pszDestExtension, apszExtensions[i][1], pszDriverName);
break;
}
}

for (int i = 0; apszBeginName[i][0] != nullptr; i++)
{
if (EQUALN(pszDestFilename, apszBeginName[i][0],
strlen(apszBeginName[i][0])) &&
!EQUAL(pszDriverName, apszBeginName[i][1]))
{
fprintf(stderr,
_("Warning: The target file has a name which is normally "
"recognized by the %s driver,\n"
"but the requested output driver is %s. "
"Is it really what you want ?\n"),
apszBeginName[i][1], pszDriverName);
break;
}
}

CPLFree(pszDestExtension);
}

//------------------------------------------------------------------------
// AddFeature
//------------------------------------------------------------------------
Expand Down Expand Up @@ -1179,7 +1101,7 @@ struct OGRLineRefOptions
{
bool bQuiet = false;
bool bDisplayProgress = false;
std::string osFormat = "ESRI Shapefile";
std::string osFormat;

std::string osSrcLineDataSourceName;
std::string osSrcLineLayerName;
Expand Down Expand Up @@ -1386,6 +1308,63 @@ OGRLineRefAppOptionsGetParser(OGRLineRefOptions *psOptions)
return argParser;
}

/************************************************************************/
/* GetOutputDriver() */
/************************************************************************/

static GDALDriver *GetOutputDriver(OGRLineRefOptions &sOptions)
{
if (sOptions.osFormat.empty())
{
const auto aoDrivers = GetOutputDriversFor(
sOptions.osOutputDataSourceName.c_str(), GDAL_OF_VECTOR);
if (aoDrivers.empty())
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot guess driver for %s",
sOptions.osOutputDataSourceName.c_str());
return nullptr;
}
else
{
if (aoDrivers.size() > 1)
{
CPLError(
CE_Warning, CPLE_AppDefined,
"Several drivers matching %s extension. Using %s",
CPLGetExtensionSafe(sOptions.osOutputDataSourceName.c_str())
.c_str(),
aoDrivers[0].c_str());
}
sOptions.osFormat = aoDrivers[0];
}
}

GDALDriver *poDriver =
GetGDALDriverManager()->GetDriverByName(sOptions.osFormat.c_str());
if (poDriver == nullptr)
{
fprintf(stderr, _("Unable to find driver `%s'.\n"),
sOptions.osFormat.c_str());
fprintf(stderr, _("The following drivers are available:\n"));

GDALDriverManager *poDM = GetGDALDriverManager();
for (int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++)
{
GDALDriver *poIter = poDM->GetDriver(iDriver);
char **papszDriverMD = poIter->GetMetadata();
if (CPLTestBool(CSLFetchNameValueDef(papszDriverMD,
GDAL_DCAP_VECTOR, "FALSE")) &&
CPLTestBool(CSLFetchNameValueDef(papszDriverMD,
GDAL_DCAP_CREATE, "FALSE")))
{
fprintf(stderr, " -> `%s'\n", poIter->GetDescription());
}
}
}

return poDriver;
}

/************************************************************************/
/* main() */
/************************************************************************/
Expand Down Expand Up @@ -1581,27 +1560,9 @@ MAIN_START(argc, argv)
/* Find the output driver. */
/* ----------------------------------------------------------------- */

if (!psOptions.bQuiet)
CheckDestDataSourceNameConsistency(
psOptions.osOutputDataSourceName.c_str(),
psOptions.osFormat.c_str());

OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();

GDALDriver *poDriver =
poR->GetDriverByName(psOptions.osFormat.c_str());
GDALDriver *poDriver = GetOutputDriver(psOptions);
if (poDriver == nullptr)
{
fprintf(stderr, _("Unable to find driver `%s'.\n"),
psOptions.osFormat.c_str());
fprintf(stderr, _("The following drivers are available:\n"));

for (int iDriver = 0; iDriver < poR->GetDriverCount();
iDriver++)
{
fprintf(stderr, " -> `%s'\n",
poR->GetDriver(iDriver)->GetDescription());
}
exit(1);
}

Expand Down Expand Up @@ -1883,27 +1844,9 @@ MAIN_START(argc, argv)
}

// Find the output driver.
if (!psOptions.bQuiet)
CheckDestDataSourceNameConsistency(
psOptions.osOutputDataSourceName.c_str(),
psOptions.osFormat.c_str());

OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();

GDALDriver *poDriver =
poR->GetDriverByName(psOptions.osFormat.c_str());
GDALDriver *poDriver = GetOutputDriver(psOptions);
if (poDriver == nullptr)
{
fprintf(stderr, _("Unable to find driver `%s'.\n"),
psOptions.osFormat.c_str());
fprintf(stderr, _("The following drivers are available:\n"));

for (int iDriver = 0; iDriver < poR->GetDriverCount();
iDriver++)
{
fprintf(stderr, " -> `%s'\n",
poR->GetDriver(iDriver)->GetDescription());
}
exit(1);
}

Expand Down
6 changes: 2 additions & 4 deletions apps/ogrtindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,11 @@ MAIN_START(nArgc, papszArgv)
GDALDriverH hDriver = GDALGetDriverByName(osFormat.c_str());
if (hDriver == nullptr)
{
fprintf(stderr, "Unable to find driver `%s'.\n", osFormat.c_str());
fprintf(stderr, "The following drivers are available:\n");
GDALDriverManager *poDM = GetGDALDriverManager();
for (int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++)
{
fprintf(stderr, "Unable to find driver `%s'.\n",
osFormat.c_str());
fprintf(stderr, "The following drivers are available:\n");

GDALDriver *poIter = poDM->GetDriver(iDriver);
char **papszDriverMD = poIter->GetMetadata();
if (CPLTestBool(CSLFetchNameValueDef(
Expand Down
3 changes: 2 additions & 1 deletion autotest/utilities/test_ogrlineref.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_ogrlineref_4(ogrlineref_path, parts_shp, tmp_path):


@pytest.mark.require_driver("KML")
def test_ogrlineref_5(ogrlineref_path, tmp_path):
def test_ogrlineref_kml(ogrlineref_path, tmp_path):

parts_kml = str(tmp_path / "parts.kml")

Expand All @@ -117,3 +117,4 @@ def test_ogrlineref_5(ogrlineref_path, tmp_path):
)

assert os.path.exists(parts_kml)
assert "KML" in ogr.Open(parts_kml).GetDriver().GetDescription()

0 comments on commit 5c12fb8

Please sign in to comment.