7

I am using QGIS version 2.18.14.

When I add a vector layer from a file geodatabase, I choose the OpenFileGdb driver and then pick the layers I would like to have on the map.

enter image description here

However, the layers are added with the name following the format <%DB_NAME% %LAYER_NAME%>. This makes it impossible to use the DBManager SQL editor directly for the added layers as I need to rename the layers first. This is because the database table cannot contain a white space. So, I need to rename "NYC streets" into "streets" or "NYC_streets".

Is there any setting in QGIS that can let me disable prefixing the layers I add with the database name from which they come?

enter image description here

1

If you are comfortable with a bit of pyqgis magic I can recommend you a solution @Joseph showed me once. You can find his answer here.

I've tried with the sample DB from ArcGIS which was linked by @Steven Kay and the result was the names of files without whitespaces:

import os

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    basename = os.path.splitext(os.path.basename(layer.source()))[0]
    layer.setLayerName(basename)

To extent this, you could add even more security by using something like this:

basename = basename.replace(" ", "_")

before you use the setLayerName function.


edit:

as correctly pointed out from OP the above approach just yields the name of the Database itself without the filename. I changed a bit of the code to tackle this. It looks like this now:

import os

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    basename = layer.name()
    layer.setLayerName(basename.replace(" ", "_"))
  • Thanks, it's helpful to be able to rename the layers in batch using Python after they have been added. The basename variable however would return just the database name, so it doesn't make sense to set all the layers' names to be the database names. But I got the idea, thanks anyway. – Alex Tereshenkov Jan 10 '18 at 9:20
  • @AlexTereshenkov ah you are right. I missed it while quickly trying this approach, my bad. I will leave the answer still as a reference if you dont mind – LaughU Jan 10 '18 at 9:29
  • no worries! Always good to know how to use Python for scripting in QGIS, helpful concepts. – Alex Tereshenkov Jan 10 '18 at 9:40
  • 1
    @AlexTereshenkov I've tried a bit around and found a solution which just replaces whitespaces with underscores ( see edit) – LaughU Jan 10 '18 at 9:41
3

Just tried this on a sample file geodatabase from ESRI - I see the same thing (except in that case the prefix is "RH_SampleData "). I'm using 2.18.13, GDAL 2.1.3

I don't know of any way to set the layer prefix when you load, but there is a workaround.

In Db Manager SQL queries (using Virtual Layers) you can double-quote table / layer / column names to allow for spaces. This is also useful for when field names match SQL reserved words

e.g.

select * from "RH_SampleData AADT"

The space does stop the auto-complete from working properly, though. So this is probably no faster or easier than renaming your layers :/

The OpenFileGDB driver seems to be read-only, so renaming your layers should be safe as you won't be able to edit them anyway.

  • Thanks, trick with quotes is helpful. Let's see if someone knows a setting to disable prefixing the layer with the database name. What's worse is that in DBManager > Virtual Layers > QGIS layers you cannot rename the layers either (the changes are discarded and the name remains the same). Too bad. – Alex Tereshenkov Jan 10 '18 at 5:59
2

You can use Command Bar Plugin which enables you to rename all layers at once. Using QGIS Command Bar you can write:

rename-layers NYC./

then press enter, and it will remove DB name from all layers at once. Note the dot (.) after NYC because it uses regex and the dot means remove everything including the space.

In the following example, I have a File GDB "RH_SampleData.gdb" and when loading the data into QGIS RH_SampleData was added before every layer. I used QGIS Command Bar and rename-layers command to remove RH_SampleData as you can see in the following gif:

enter image description here

  • 1
    Thanks for showing yet another way how to automate layer renaming :) Joseph shown that this can be done in Python, so no need for an extra plugin. I am still wondering whether it's possible to disable prefixing the layers names with the database name as I've stated in the question. Thanks anyway! – Alex Tereshenkov Jan 10 '18 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.