3

I am new to QGIS and I am currently learning pyqgis. My problem is I tried to create a custom plugin to join csv and shapefile with 20,000+ features. I was successful in joining csv and shapefile via custom plugin that I created using this code.

from PyQt4.QtCore import *
import qgis
from qgis.core import QgsMapLayerRegistry, QgsVectorJoinInfo
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
 # Change to your shapefile name
 if layer.name() == "MyShapefile":
 qgis.utils.iface.setActiveLayer(layer)
 shp = qgis.utils.iface.activeLayer()
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
 # Change to your csv name
 if layer.name() == "MyDatabase":
 qgis.utils.iface.setActiveLayer(layer)
 csv = qgis.utils.iface.activeLayer()
# Set up join parameters
shpField='PIN'
csvField='UPIN'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = csv.id()
joinObject.joinFieldName = csvField
joinObject.targetFieldName = shpField
shp.addJoin(joinObject)

But when i try to open the attribute table it loads very slow and counting features by 8/10/15 at max. But when I join table using properties>join of shapefile and check the "cache data in virtual memory" my attributes loads perfectly fine even if I have 20,000 features.

My question is how can I make the loading of attribute table faster like in joining via properties>join using a custom plugin?

I am using QGIS 2.16

Midavalo
30k11 gold badges53 silver badges108 bronze badges
asked Sep 26, 2016 at 16:31

1 Answer 1

3

Try adding joinObject.memoryCache = True before you initiate the join:

from PyQt4.QtCore import *
import qgis
from qgis.core import QgsMapLayerRegistry, QgsVectorJoinInfo
shp = QgsMapLayerRegistry.instance().mapLayersByName( "MyShapefile" )[0]
csv = QgsMapLayerRegistry.instance().mapLayersByName( "MyDatabase" )[0]
# Set up join parameters
shpField='PIN'
csvField='UPIN'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = csv.id()
joinObject.joinFieldName = csvField
joinObject.targetFieldName = shpField
joinObject.memoryCache = True
shp.addJoin(joinObject)
# shp.createJoinCaches()
answered Sep 27, 2016 at 12:21
12
  • will try it now sir Jospeh. Thank you for replying. Godbless you. :) Commented Sep 28, 2016 at 4:04
  • Hi Sir Joseph, I tried adding joinObject.memoryCache = True but still with no luck.. the attribute table still loads very slow counting by 8/10/15. I tried using it thru Macro and my Custom Join Plugin. Thank you and more power. Commented Sep 28, 2016 at 4:25
  • No need to thank me for replying =). Does it work if you do it in the Python Console? Commented Sep 28, 2016 at 8:44
  • 1
    It takes a few seconds thanks to the memory cache. I asked a somewhat related question here where my layers did not load both visually and the attribute table. The code was used in a plugin. I wonder why it is still slow for you...I also simplified your code a bit =) Commented Sep 29, 2016 at 10:15
  • 1
    well i think I figured it out.. as you can see in my script UPIN is in all caps while in my csv it should be small letters upin i changed it in the code and now it loads in an instant. :) Thank you for the support! :) hope to hear from you soon! Commented Sep 29, 2016 at 11:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.