3

I'm currently having trouble accessing and defining CRS information in a standalone application. I've read the API documentation and have tried it in the python console in QGIS 2.2.0. The python console works fine. I'm able to get the information I need. But the layers in my stand alone application do not have have valid CRS's. What is the reason for this?

Code:

import qgis
from qgis.core import *
from qgis.utils import *
from qgis.gui import *

import pdb

QgsApplication.setPrefixPath( r"C:\OSGeo4W64\apps\qgis", True )
QgsApplication.initQgis()
app = QgsApplication([], True)  

print "Testing Json crs:"
layer = QgsVectorLayer("E:/GIS_Data/GeoJsonRendering_Test/test.json" ,  'musym', "ogr")
QgsMapLayerRegistry.instance().addMapLayer(layer)
print layer.isValid()
print layer.crs().isValid()
print layer.crs().authid()

print "Testing QgsCoordinateReferenceSystem:"

t = QgsCoordinateTransform()
projSrc = QgsCoordinateReferenceSystem(3857, QgsCoordinateReferenceSystem.EpsgCrsId)
projDest = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)

print projSrc.isValid()
print projDest.isValid()

print "Testing memory layer CRS:"
vTemp = QgsVectorLayer("Point?crs=EPSG:3857", "temporary_points", "memory")
print vTemp.isValid()
print vTemp.crs().isValid()

Output:

Testing Json crs:
True
False

Testing QgsCoordinateReferenceSystem:
False
False
Testing memory layer CRS:
True
False

So I have two valid layers without valid projections.

Here is the output for that code in the python console in QGIS:

Testing Json crs:
True
True
EPSG:3857
Testing QgsCoordinateReferenceSystem:
True
True
Testing memory layer CRS:
True
True

So my code only works in the QGIS python console. I'm assuming that I am not defining settings that need to be defined, but I"m not quite sure what I'm missing.

  • Could you provide us with your sample json file? – Germán Carrillo Jan 20 '15 at 22:37
  • I would like to, but I'm not currently working on that problem. So I don't know if I could currently replicate the exact bug. – midfield99 Jan 22 '15 at 17:40
2

I experienced the same problem, and I have finally solved it by changing the order of the first three lines.

Try with this:

QgsApplication.initQgis()
app = QgsApplication([], True)
QgsApplication.setPrefixPath( r"C:\OSGeo4W64\apps\qgis", True )
...

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.