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
1 Answer 1
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()
-
will try it now sir Jospeh. Thank you for replying. Godbless you. :)Conrado Domingo– Conrado Domingo2016年09月28日 04:04:40 +00:00Commented 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.Conrado Domingo– Conrado Domingo2016年09月28日 04:25:40 +00:00Commented Sep 28, 2016 at 4:25
-
No need to thank me for replying =). Does it work if you do it in the Python Console?Joseph– Joseph2016年09月28日 08:44:53 +00:00Commented Sep 28, 2016 at 8:44
-
1It 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 =)Joseph– Joseph2016年09月29日 10:15:38 +00:00Commented Sep 29, 2016 at 10:15
-
1well 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!Conrado Domingo– Conrado Domingo2016年09月29日 11:31:34 +00:00Commented Sep 29, 2016 at 11:31