1

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)

2 Answers 2

1

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)
2
  • Thank you for your answer. Your solution looks like a decent workaround. To get the files created, I ran the algorithm from the processing toolbox in QGIS: Geoalgorithms->Vector->General tools->Reproject layer. This is using QGIS version 2.0.1.
    – user23603
    Commented Nov 4, 2013 at 8:06
  • Note: Found the answer to this question, was just a typo.
    – user23603
    Commented Nov 5, 2013 at 8:36
0

Turns out I spelled EPSG wrong.

processing.runalg("qgis:reprojectlayer", my_input_layer_object, "ESPG:102013", 
output_shapefile_path)

should be

processing.runalg("qgis:reprojectlayer", my_input_layer_object, "EPSG:102013", 
output_shapefile_path)

Not feeling stupid at all.

I'll leave Thomas' answer accepted, since he provided info on how to create projection files, which probably is what people come here looking for. If mods disagree, comment or message me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.