Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to use 'inbuilt:'-uri with OGR provider #39175

Closed
wants to merge 1 commit into from
Closed

Make it possible to use 'inbuilt:'-uri with OGR provider #39175

wants to merge 1 commit into from

Conversation

rduivenvoorde
Copy link
Contributor

Not sure if this is still handy, but I was looking for some code to use the world map in the PyQGIS cookbook...

Loading the easter egg 'world' actually loads an inbuild gpkg from
the (install dir) resources directory.
It ends up in the project file with the eg following uri:
source="inbuilt:/data/world_map.gpkg|layername=countries"
This commit makes it possible to actually (re)use that uri in the
OGR provider.

So in pyqgis you can now do:

iface.addVectorLayer('inbuilt:/data/world_map.gpkg|layername=countries', 'Countries', 'ogr')

I do not think this breaks anything?
I'm aware this also works

iface.addVectorLayer(QgsApplication.pkgDataPath() + '/resources/data/world_map.gpkg|layername=Countries', '', 'ogr')

Loading the easter egg 'world' actually loads an inbuild gpkg from
the (install dir) resources directory.
It ends up in the project file with the eg following uri:
 source="inbuilt:/data/world_map.gpkg|layername=countries"
This commit makes it possible to actually (re)use that uri in the
OGR provider.
So in pyqgis you can now do:
 iface.addVectorLayer('inbuilt:/data/world_map.gpkg|layername=countries', 'Countries', 'ogr')
@github-actions github-actions bot added this to the 3.16.0 milestone Oct 4, 2020
Copy link
Collaborator

@nyalldawson nyalldawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be done in a different way -- it's not a provider level responsibility (and infact the OGR provider already works with it for layers coming from projects).

Rather you'd need to edit QgisApp::addVectorLayerPrivate to extract the filename from the vectorLayerPath argument (via QgsProviderRegistry::instance()->decodeUri() ) and then push the returned path value through QgsPathResolver().readPath() before updating the parsed uri map with the resolved value, and then reencode this uri for use in the rest of addVectorLayerPrivate.

Let me know if you'd like to tackle this or want me to...

@rduivenvoorde
Copy link
Contributor Author

rduivenvoorde commented Oct 5, 2020

Thanks @nyalldawson ! I will try this myself first, if in trouble I'll call for help :-)
Should I just use this branch, or should I just create a new one? What is easier for reviewers?

@nyalldawson
Copy link
Collaborator

Totally up to you and what git workflow you're comfortable with! :D

@arongergely
Copy link
Contributor

Maybe it's better to implement a convenient way to resolve the path of "inbuilt:" to absolute paths. Then the output could be used in the OGR provider call, the usual way.

Curently QgsPathResolver resolves the 'inbuilt:' paths when the project file is opened: #31396

By the way there have been talks before about elevating the world layer from 'easter egg' status.
Could this be the time?

It is already used as the base layer for the projection bounds overview. (Implemented in QgsCoordinateBoundsPreviewMapWidget)

@rduivenvoorde
Copy link
Contributor Author

@arongergely note that

iface.addVectorLayer(QgsApplication.pkgDataPath() + '/resources/data/world_map.gpkg|layername=Countries', '', 'ogr')

works...
So you can use as 'inbuilt:' : QgsApplication.pkgDataPath() + '/resources/data/' I think?

@arongergely
Copy link
Contributor

arongergely commented Oct 7, 2020

Yes that should work! It was a long while ago - but I remember I hit some issues when i was implementing code for multiplatform use - . Considering people may use the cookbook on windows.. UNIX / Windows file separators issue? the string literal contains the file separators.

@arongergely
Copy link
Contributor

But in any case one could just handle that via the os module:
iface.addVectorLayer(os.path.join(QgsApplication.pkgDataPath(),'resources','data','world_map.gpkg|layername=Countries'), '', 'ogr')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants