Thursday, 17 March 2016

python - No prj file output from qgis:reproject



I'm trying to reproject a vector layer using the 'qgis:reproject' algorithm in the processing/sextante module of QGIS, from Python.


This kind of works, but the .prj and .qpj files created after running the tool are empty, so when importing the resulting shapefile as a layer I get the specify CRS dialog.


When running the algorithm from the toolbox (Geoalgorithms->Vector->General tools->Reproject layer), both a .prj and a .qpj file is created and filled with data. According to processing_qgis.log, I am running the same exact processing algorithm. (Note: Not true, see my answer)


What is it the toolbox version does that I'm not doing? Should I manually create the .prj/.qpj files after reprojecting?


# Reprojecting - does not create proper projection files
processing.runalg("qgis:reprojectlayer", my_input_layer_object, "ESPG:102013",
output_shapefile_path)

# Importing layer
output_layer = QgsVectorLayer(output_shapefile_path, "My Layer Name", "ogr")

QgsMapLayerRegistry.instance().addMapLayer(output_layer)

Answer



I didn't see in the function qgis:reprojectlayer you are using (also called from the sextante UI) where the prj file and qpj files are created.


You can maybe create the prj using gdal to avoid manual job (but clearly not the best way)


from osgeo import gdal, osr

# Declare a new SpatialReference
srs = osr.SpatialReference()
# Import the EPSG code into the new object srs
srs.ImportFromEPSG(102013)


# For learning purpose, print the result before transformation to ESRI WKT (not useful in final code)
srs.ExportToWkt()

# Transform your WKT definition to ESRI
srs.MorphToESRI()

# For learning purpose, print the result after transformation to ESRI WKT (not useful in final code)
srs.ExportToWkt()


# Export the WKT with the ESRI WKT notation in a file
esri_output = srs.ExportToWkt()
with open('/yourpath/yourfilename.prj', 'a') as prj_file:
prj_file.write(esri_output)

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...