[PROJ] How to find the active metadata search path in PROJ 6
Nyall Dawson
nyall.dawson at gmail.com
Sat Sep 7 16:38:36 PDT 2019
On Sat, 7 Sep 2019 at 23:20, Roger Bivand <Roger.Bivand at nhh.no> wrote:
xample
> holding grid files downloaded on the fly or caches from previous sessions,
> and where the default data/ directory is not user-writable,
Good move, I think all proj based end-user applications should also
implement this logic.
> I think I need
> to build the ":" or ";" separated list to update the search path. To avoid
> overwriting it, I need to prepend the active search path to the local
> user-writable directory.
>
> Hope this is just something I've missed, a function returning a string, or
> equivalent mechanism.
Here's how we do it in QGIS. Excuse the mix of pure c++/proj/qt api (a
necessary by-product of working in QGIS code):
on startup:
// append local user-writable folder as a proj search path
QStringList currentProjSearchPaths = QgsProjUtils::searchPaths();
currentProjSearchPaths.append( qgisSettingsDirPath() + "proj" );
char **newPaths = new char *[currentProjSearchPaths.length()];
for ( int i = 0; i < currentProjSearchPaths.count(); ++i )
{
newPaths[i] = CPLStrdup( currentProjSearchPaths.at( i
).toUtf8().constData() );
}
proj_context_set_search_paths( nullptr,
currentProjSearchPaths.count(), newPaths );
for ( int i = 0; i < currentProjSearchPaths.count(); ++i )
{
CPLFree( newPaths[i] );
}
delete [] newPaths;
where QgsProjUtils::searchPaths() is:
QStringList QgsProjUtils::searchPaths()
{
const QString path( proj_info().searchpath );
QStringList paths;
#if PROJ_VERSION_MINOR==1 && PROJ_VERSION_PATCH==0
// -- see https://github.com/OSGeo/proj.4/pull/1497
paths = path.split( ';' );
#else
#ifdef Q_OS_WIN
paths = path.split( ';' );
#else
paths = path.split( ':' );
#endif
#endif
QSet<QString> existing;
// thin out duplicates from paths -- see
https://github.com/OSGeo/proj.4/pull/1498
QStringList res;
res.reserve( paths.count() );
for ( const QString &p : qgis::as_const( paths ) )
{
if ( existing.contains( p ) )
continue;
existing.insert( p );
res << p;
}
return res;
}
There's quite a bit of logic here which is in place to work around
earlier PROJ 6 release behavior, see the links commented in the code
above.
Nyall
>
> Roger
>
> --
> Roger Bivand
> Department of Economics, Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
> https://orcid.org/0000-0003-2392-6140
> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
> _______________________________________________
> PROJ mailing list
> PROJ at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/proj
More information about the PROJ
mailing list