1

I have a problem. I'm currently trying to get the extent of a vector layer in latitude and longitude coordinates in python. The layer is currently in EPSG:3857, so .fullExtent() is not returning what I want. I have tried reprojecting the layer using this:

self.layer= QgsVectorLayer(self.input, self.featureName, "ogr")
self.layer.setCoordinateSystem("crs=epsg:4326")

But the second line introduces a type error when loading the vector layer.

So what would be the best way of getting the bounds in latitude and longitude coordinates? It would be nice if I could get the coordinates from the pyqgis API, but I would be happy if there was another way tool that would do it, such as gdal.

1 Answer 1

5

my solution was:

canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
exp_crs = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
for i in allLayers: 

    if i.type() != 0 :
        print(i.name() + " skipped as it is not a vector layer")  
    if i.type() == 0 :
        #exp_crs = QgsCoordinateReferenceSystem(4326,QgsCoordinateReferenceSystem.PostgisCrsId)
        print i.crs()
        print exp_crs
        qgis.core.QgsVectorFileWriter.writeAsVectorFormat(i, i.name() + '.js', 'utf-8', exp_crs, 'GeoJSON')

this saves all layers in a new projection

3
  • 1
    you should try self.layer.setCoordinateSystem(XXX) where XXX is defined like above using QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
    – Riccardo
    Commented Apr 24, 2014 at 10:19
  • Thanks, this looks like what I need. I'll give it a try next week.
    – midfield99
    Commented Apr 26, 2014 at 15:57
  • I'm still looking into the code. Right now the trouble is that I'm unable to get code related to a layer crs working. I think it has to do with the fact that it is in a standalone app. But it looks like that is for another question.
    – midfield99
    Commented May 12, 2014 at 16:28

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.