PyQGIS – How to Delete Created Custom CRS

coordinate systemdeletepyqgis

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

Best Answer

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()
Related Question