2

This question is very similar to How to define layer's CRS and avoid the CRS dialog in PyQGIS?, but it's doesn't work for me.

This is my try:

vlayer.setCrs(QgsCoordinateReferenceSystem(25830))
iface.mapCanvas().mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(25830))

But the dialog pop up asking for CRS to choose, and when I ask for the CRS of the layer, it show a diferent CRS:

print "CRS: " + vlayer.crs().geographicCRSAuthId()

It show CRS: EPSG:4258 and It must be 25830

3

Sorry. I was in a mistake:

The current CRS is get by authid() method, not geographicCRSAuthId()

On the other hand, I wanted to avoid the CRS Dialog when I load a vector layer from pyQGIS.

Similar questions are been asked here and here . I have got the results setting QGIS like: Setting>Options>CRS ->Use CRS of project

And I put my snippet of code to load a PostGIS view like a vector layer: (please, let me know if I am in a mistake)

mycrs = QgsCoordinateReferenceSystem(25830)#If not specified otherwise in second parameter, PostGIS SRID is used by default.
iface.mapCanvas().mapRenderer().setDestinationCrs(mycrs)# set CRS to canvas
uri = QgsDataSourceURI()
uri.setConnection('localhost', '5432', '-mydb-', '-myname-', '-mypasswd-')
uri.setDataSource('public', '-my table/view-', 'the_geom')
uri.setKeyColumn('id')# neccesary for postgres views who doesn't support primary key
vlayer = QgsVectorLayer(uri.uri(), '-nameofmyvectorlayer-', 'postgres')
vlayer.setCrs(mycrs,True)#set CRS to vector layer. I'm not sure True param. is neccesary
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

When I load the vector layer a Warning Message appears telling that: CRS is not defined, and will be used the CRS of the project...and it works.

|improve this answer|||||
  • 1
    I have the same issue. Although I set up crs, I see this warning message from qgis. I made a dirty warkaround and removed this warning: iface.messageBar().popWidget() – wawka Jul 24 '17 at 7:43

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.