3

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

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
exodehm
  • 919
  • 6
  • 18

1 Answers1

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.

exodehm
  • 919
  • 6
  • 18
  • 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 07:43