Monday, 2 October 2017

gdal - Reprojecting layer in PyQGIS?


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.



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...