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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
midfield99
  • 679
  • 7
  • 17

1 Answers1

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

Riccardo
  • 2,648
  • 18
  • 31
  • 1
    you should try self.layer.setCoordinateSystem(XXX) where XXX is defined like above using QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) – Riccardo Apr 24 '14 at 10:19
  • Thanks, this looks like what I need. I'll give it a try next week. – midfield99 Apr 26 '14 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 May 12 '14 at 16:28