1

I am writing a QGIS standalone Python command line tool. I check that all input shapefiles have a valid CRS with layer.crs().isValid() and then I reproject any files that are not already in ESPG:4326. One input file is originally in EPSG:3338. So, I call:

processing.runalg('qgis:reprojectlayer', input.shp, 'ESPG:4326', reprojectOut.shp)

the output 'reprojectOut.shp' has the correct CRS (4326) and correct .prj file and also displays properly even with 'On The Fly' disabled. So all seems well... until I try to extract a few features from the reprojected shapefile with:

processing.runalg('qgis:extractbyexpression', reprojectOut.shp, expression, extractOut.shp)

and somehow this output 'extractOut.shp' contains the correct features but has gone back to the original 'EPSG:3338' CRS. Is there something I am missing? Is there a reference to the original input file hiding somewhere? I have verified the filepaths are correct and tried passing both direct filepaths and also QgsVectorLayer() objects as input parameters. I am running QGIS 2.18.13 on Mac OSX High Sierra.

  • Can you confirm you used the correct terminology in your code (i.e. it is "EPSG" and not "ESPG")? – Joseph Nov 6 '17 at 10:41
  • @Joseph I (sort of) found the issue but I'd be curious if you know what happened. I called layer=QgsVectorLayer('/path/to/fileA.shp','input',"ogr") and then processing.runalg('qgis:reprojectlayer',layer,crs.authid(), fileB.shp) and then to save space I deleted fileA.shp. I then renamed using os.rename fileB.shp to fileA.shp (and .dbf .qpj etc.) and reset data source with layer.setDataSource('fileA.shp'..). Did I cut a reference with the renaming? Proceeding with name 'fileB.shp' works fine. Renaming back to 'fileA.shp' causes incorrect spatial reference on any future process outputs. – Luke Nov 6 '17 at 23:21
  • I would guess that some data from layer is still stored in memory. It's probably cleaner to redefine layer again (i.e. use layer=QgsVectorLayer('/path/to/fileA.shp','input',"ogr") after you renamed the shapefile). – Joseph Nov 7 '17 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.