1

How can I delete custom CRS by PyQGIS?

My not working solution is:

my_crs = QgsCoordinateReferenceSystem()
my_crs.createFromProj4("+proj=aea +lat_1=21 +lat_2=49 +lat_0=37 +lon_0=87 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")
my_crs.saveAsUserCrs("my custom CRS")

Object QgsCoordinateReferenceSystem not contains any method for deleting created CRS:

dir(my_crs)
['CrsType', 'EpsgCrsId', 'Format', 'FormatProj', 'FormatWkt', 'FullString', 'IdentifierType', 'InternalCrsId', 'MediumString', 'PostgisCrsId', 'ShortString', 'WKT1_ESRI', 'WKT1_GDAL', 'WKT2_2015', 'WKT2_2015_SIMPLIFIED', 'WKT2_2018', 'WKT2_2018_SIMPLIFIED', 'WKT2_2019', 'WKT2_2019_SIMPLIFIED', 'WKT_PREFERRED', 'WKT_PREFERRED_GDAL', 'WKT_PREFERRED_SIMPLIFIED', 'WktVariant', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'authid', 'bounds', 'createFromId', 'createFromOgcWmsCrs', 'createFromProj', 'createFromProj4', 'createFromSrid', 'createFromSrsId', 'createFromString', 'createFromUserInput', 'createFromWkt', 'description', 'ellipsoidAcronym', 'findMatchingProj', 'fromEpsgId', 'fromOgcWmsCrs', 'fromProj', 'fromProj4', 'fromSrsId', 'fromWkt', 'geographicCrsAuthId', 'hasAxisInverted', 'invalidateCache', 'isGeographic', 'isValid', 'mapUnits', 'postgisSrid', 'projectionAcronym', 'pushRecentCoordinateReferenceSystem', 'readXml', 'recentCoordinateReferenceSystems', 'recentProjections', 'saveAsUserCrs', 'setValidationHint', 'setupESRIWktFix', 'srsid', 'staticMetaObject', 'syncDatabase', 'toProj', 'toProj4', 'toWkt', 'userFriendlyIdentifier', 'validSrsIds', 'validate', 'validationHint', 'writeXml']

In QGIS is deleting very simple:

enter image description here

2
  • 1
    I could assume that this task could be done somehow through the iface, but this is just an assumption. I think he knows: @BenW
    – Taras
    Nov 4 at 9:16
  • 1
    @Taras, I see you tagged me :-) Lucky I was able to make an answer!
    – Ben W
    Nov 5 at 11:12
3

For versions >= QGIS 3.18:

This was made simpler in 3.18 with the introduction of the QgsCoordinateReferenceSystemRegistry class. Once you access this class instance, you can remove a custom user CRS by its id with the following code:

my_crs = QgsCoordinateReferenceSystem()
my_crs.createFromProj4("+proj=aea +lat_1=21 +lat_2=49 +lat_0=37 +lon_0=87 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")
my_crs.saveAsUserCrs("my custom CRS")
#retrieve id of your custom CRS
srs_id = my_crs.srsid()
#print(srs_id)
# Remove custom user CRS from registry
registry = QgsApplication.coordinateReferenceSystemRegistry()
registry.removeUserCrs(srs_id)

Links to Python and C++ API docs for the QgsCoordinateReferenceSystemRegistry class below:

C++ API

PyQGIS API

For versions < 3.18

Prior to 3.18, it was not quite as straightforward as in recent versions but you can remove a custom user crs directly from the sqlite3 database using the crs id with the following code.

Note: I don't claim to be sql or database expert but I have tested this in 3.16 and it worked for me.

import sqlite3

my_crs = QgsCoordinateReferenceSystem()
my_crs.createFromProj4("+proj=aea +lat_1=21 +lat_2=49 +lat_0=37 +lon_0=87 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")
my_crs.saveAsUserCrs("my custom CRS")
srs_id = my_crs.srsid()

# After saving custom user CRS with code above
db_path = QgsApplication.qgisUserDatabaseFilePath()
con = sqlite3.connect(db_path)
cur = con.cursor()
sql = "DELETE FROM tbl_srs where srs_id=?"
cur.execute(sql, (srs_id,))
con.commit()
con.close()
8
  • 1
    I knew that you could manage that. Thank you for your answers !!! Always a pleasure to inspect them and learn something new
    – Taras
    Nov 5 at 11:13
  • 1
    @Taras, TBH I learn a lot myself from answering questions here (and sometimes from my mistakes) not to mention reading all the other great answers on this site, including yours!
    – Ben W
    Nov 5 at 11:20
  • 1
    Thank you for your kind words! BTW we are doing great mutual job :)
    – Taras
    Nov 5 at 11:25
  • Thank you for your anwer, i will try your solution in monday
    – Wenceslauw
    Nov 6 at 20:49
  • @BenW your solution does not work: AttributeError: type object 'QgsApplication' has no attribute 'coordinateReferenceSystemRegistry'
    – Wenceslauw
    Nov 9 at 7:42

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.