MCPcopy Create free account
hub / github.com/LAStools/LAStools / findLatestQGISInstallationPath

Function findLatestQGISInstallationPath

src/proj_loader.cpp:347–471  ·  view source on GitHub ↗

Finds the latest QGIS installation path by first checking the `QGIS_PREFIX_PATH` environment variable. If the environment variable is not set, it searches through default installation directories and selects the latest version based on directory names. appends "bin" to the path, where the proj lib is located

Source from the content-addressed store, hash-verified

345/// If the environment variable is not set, it searches through default installation directories and selects the latest version based on directory names.
346/// appends "bin" to the path, where the proj lib is located
347static std::string findLatestQGISInstallationPath(std::set<std::filesystem::path>& searchedPaths) {
348 // 1. Check the environment variable QGIS_PREFIX_PATH
349 const char* qgisPathEnv = getenv("QGIS_PREFIX_PATH");
350 std::string latestVersionPath;
351 std::string latestVersionName;
352
353#ifdef _WIN32
354 if (qgisPathEnv) {
355 try {
356 if (std::filesystem::exists(qgisPathEnv)) {
357 std::filesystem::path path(qgisPathEnv);
358 path /= "bin"; // Append "bin" to the path
359 if (std::filesystem::exists(path)) {
360 searchedPaths.insert(path);
361 LASMessage(LAS_VERY_VERBOSE, "PROJ library used via QGIS installation path: %s", path.string().c_str());
362 return path.string();
363 } else {
364 LASMessage(LAS_WARNING, "QGIS_PREFIX_PATH set but bin folder [%s] not found", path.string().c_str());
365 }
366 } else {
367 LASMessage(LAS_WARNING, "QGIS_PREFIX_PATH set [%s] but not found", qgisPathEnv);
368 }
369 } catch (const std::filesystem::filesystem_error&) {
370 }
371 }
372
373// 2. If the environment variable is not set, check default paths
374 size_t numPaths = 0;
375 const char** defaultPaths = getDefaultProgramPaths(numPaths);
376
377 // Checking the standard paths (e.g. "C:\Program Files\QGIS 3.36.3\bin\qgis-bin.exe")
378 for (size_t i = 0; i < numPaths; ++i) {
379 const char* pathCStr = defaultPaths[i];
380 if (!pathCStr) continue;
381
382 searchedPaths.insert(std::filesystem::path(pathCStr));
383 for (const auto& programEntry : std::filesystem::directory_iterator(pathCStr)) {
384 try {
385 if (std::filesystem::is_directory(programEntry.path())) {
386 std::string folderName = programEntry.path().filename().string();
387 // Check whether the name begins with QGIS
388 if (folderName.rfind("QGIS ") == 0) {
389 searchedPaths.insert(std::filesystem::path(programEntry.path()));
390
391 if (latestVersionName.empty() || compareVersions(folderName, latestVersionName)) {
392 latestVersionName = folderName;
393 std::filesystem::path path(programEntry.path());
394 path /= "bin"; // Append "bin" to the path
395 if (std::filesystem::exists(path)) {
396 searchedPaths.insert(path);
397 latestVersionPath = path.string();
398 }
399 }
400 }
401 }
402 }
403 catch (const std::filesystem::filesystem_error&) {
404 continue;

Callers 1

load_proj_libraryFunction · 0.85

Calls 8

LASMessageFunction · 0.85
getDefaultProgramPathsFunction · 0.85
compareVersionsFunction · 0.85
getHomeDirectoryFunction · 0.85
getProjLibraryPathFunction · 0.85
emptyMethod · 0.80
insertMethod · 0.45
stringMethod · 0.45

Tested by

no test coverage detected