4

I want to add new attributes to a new and empty shapefile layer. The problem is, that QGIS 3.10 takes attributes from the shapefile layer which was used before when loading it to the GUI. So there are attributes in the table where no attributes are not yet added.

The next problem is, that when I want to edit another layer, for example a point layer and I edited a polygon layer before, QGIS loads the new point layer as polygon.

Is there a possibility to clear the cache of QGIS before editing a new layer?

Please find below my code.

processing.run("native:reprojectlayer", {'INPUT':'C:/shp_P.shp',
    'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:32633'), 
    'OUTPUT':'C:/edit_N.shp'})

#load layer
P_n =  QgsVectorLayer('D:/SFT_editierbare_Layer','edit_N','ogr') 
P_n.setCrs(QgsCoordinateReferenceSystem(32633))
QgsProject.instance().addMapLayer(P_n, True)

P_n.startEditing()
P_n.dataProvider().addAttributes([
    QgsField ('obj_id',QVariant.Int, 'integer', 10),
    QgsField ('DKM_bh', QVariant.String, 'character', 200),
    QgsField ('DKM_kg', QVariant.Int, 'integer', 10),
    QgsField ('DKM_gnr',QVariant.String, 'character', 20),
    QgsField ('FK_rev_nr',QVariant.String, 'character', 200),
    QgsField ('FK_abt_nr',QVariant.Int, 'integer', 20),
    QgsField ('FK_uab_tx',QVariant.String, 'character', 20),
    QgsField ('ALS_seeh', QVariant.Int, 'integer', 8),
    QgsField ('ALS_neigpr', QVariant.Int, 'integer', 8),
    QgsField ('ALS_exptxt',QVariant.String, 'character', 20),
    #QgsField ('ALS_exint',QVariant.Int, 'integer', 8),
    QgsField ('oberh', QVariant.Double, 'double', 3, 1),
    #QgsField ('FK_alter', QVariant.Int, 'integer', 3), 
    #QgsField ('nhpro', QVariant.Int, 'integer', 3),
    #QgsField ('lhpro', QVariant.Int, 'integer', 3), 
    #QgsField ('vha_nh', QVariant.Double, 'double', 10,3),
    #QgsField ('vha_lh', QVariant.Double, 'double', 10,3),
    #QgsField ('vha_sum', QVariant.Double, 'double', 10,3),
    QgsField ('x_coord', QVariant.Double, 'double', 10,2), 
    QgsField ('y_coord', QVariant.Double, 'double', 10,2),
    QgsField ('baumart',QVariant.String, 'character', 254),
    QgsField ('anz_baeume',QVariant.Int, 'integer', 10),
    QgsField ('efm_schatz', QVariant.Double, 'double', 10,3),
    QgsField ('bemerkung',QVariant.String, 'character', 254),
    QgsField ('data_img',QVariant.String, 'character', 254),
    QgsField ('data_doc',QVariant.String, 'character', 254),
    #QgsField ('row_privat', QVariant.String, 'character', 4),
    QgsField ('user_ins',QVariant.String, 'character', 20),
    QgsField ('user_mod',QVariant.String, 'character', 20),
    QgsField ('date_ins',QVariant.Date),
    QgsField ('date_mod',QVariant.Date)])
P_n.updateFields()
P_n.commitChanges()
1
  • Change D:/SFT_editierbare_Layer to D:/SFT_editierbare_Layer/yourfile.shp otherwise, QGIS will "automagically" open the 1st spatial file found in the directory.
    – ThomasG77
    Commented May 19, 2021 at 12:42

1 Answer 1

2

You can find instructions on how to do this manually here: http://blog.discoverthat.co.uk/2019/09/qgis-clear-cache.html

It's about clicking through Settings -> Options, clicking the network tab and pressing the "delete" button for the cache.

I used the information gathered by following the above instruction to prefix relevant scripts of mine with the following:

from shutil import rmtree                                                              
try: 
    rmtree(r"c:\Users\[USERNAME HERE]\AppData\Local\QGIS\QGIS3\cache", ignore_errors=True)
except: print("Could not delete cache")  

Your Answer

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

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