0

I'm trying to write a mini QGIS plugin to test a few things. It has the bare minimum components such as the metadata file, definition of the class QGISPlugin, and a docket with a couple of tabs. It also loads a tiled map. However, what is not clicking is how to programmatically change the CRS. Everything works fine if done via the GUI (i.e. definition of the new CRS and manual selection of the CRS), but I cannot get the change of CRS to work from my code. The class defining the dock reads:

class TabulaDock(QDockWidget):
    def __init__(self, iface: QgisInterface, dock_widget: QDockWidget) -> None:   
        super().__init__('TABULA')
        tabs = QTabWidget()
        tabs.setStyleSheet('''QTabBar::tab {font-size: 10pt; font-family: Verdana; font-weight: bold; color: #004F00; height: 40px; width: 140px;}''')
        self.tab1 = QWidget()
        tabs.addTab(self.tab1,"TAB 1")
        self.tab2 = QWidget()
        tabs.addTab(self.tab2,"TAB 2")
        self.setWidget(tabs)
        self.tabs = tabs
        self.add_tiled_basemap()
        self.crs = QgsCoordinateReferenceSystem()
        self.crs.createFromProj4("+proj=lcc +lat_1=33.0 +lat_2=60.0 +lat_0=40.0 +lon_0=-90.0 +datum=WGS84 +no_defs")
        self.crs.saveAsUserCrs("TABULA CRS")
        QApplication.processEvents()

This creates the tabs and loads the tiled basemap, but it doesn't refresh the map canvas. [NOTE: the boolean associated with self.crs.createFromProj4 returns True). If I click on EPSG in the status bar and select TABULA_CRS, it does refresh the map canvas. However, the python script didn't complete this step for some reason that I cannot figure out.

2
  • You are trying to update the project CRS?
    – Matt
    Commented Nov 17, 2022 at 18:43
  • I'm not really touching the project per se. I simply open the plugin and need the map canvas to be updated with the custom projection. Commented Nov 17, 2022 at 18:48

1 Answer 1

2

You should be able to achieve what you want with:

QgsProject.instance().setCrs(self.crs)
1
  • I tried yesterday but didn't work. Today I have been reading up some of the documentation. However, it's still not working for me when I try programmatically and I have to do it manually. Commented Nov 18, 2022 at 22:58

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.