Bug report #7491

PyQGIS - Add delimited Table as QGsVectorLayer behaviour on Windows systems

Added by Martin Jung over 11 years ago. Updated over 11 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Python plugins
Affected QGIS version:master Regression?:No
Operating System:Windows Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:Yes Copied to github as #:16442

Description

Hi, i tried to add a comma-separated file (csv) as table to QGIS.
The following example works without issues on Linux systems (QGIS dev):

  fileName = vectorPath # the path to the csv
  fileInfo = QFileInfo(fileName)
  baseName = fileInfo.baseName()
  uri = fileName+"?delimiter=%s" % (";") # ; as seperator
  vlayer = QgsVectorLayer(uri, baseName, "delimitedtext")
  if not vlayer.isValid():
    QMessageBox.warning(QDialog(),"Warning","Failed to add the Layer to QGis")
  QgsMapLayerRegistry.instance().addMapLayer(vlayer)

However using QGIS dev on Windows XP always results in an invalid layer ("Failed to add the Layer to QGIS"), while QGIS 1.8 simply crashes ("Error Message: QGIS.bin has to be closed").
Did i stumble upon a bug or is the procedure to add tables to QGIS different on Windows systems?

History

#1 Updated by Jürgen Fischer over 11 years ago

Did you enable the delimited text plugin?

#2 Updated by Martin Jung over 11 years ago

No, i didn't before.
I just tested it and it has no influence on the errors (QGIS 1.8 still crashing on WIN XP -> qgis.exe has found an Error | QGIS dev returns the messagebox that the layer is not valid.)

#3 Updated by Jürgen Fischer over 11 years ago

Martin Jung wrote:

No, i didn't before.
I just tested it and it has no influence on the errors (QGIS 1.8 still crashing on WIN XP -> qgis.exe has found an Error | QGIS dev returns the messagebox that the layer is not valid.)

use uri = "file:/"+fileName+"?delimiter=%s" % (";") # ; as seperator

#4 Updated by Martin Jung over 11 years ago

  • Status changed from Open to Closed

Ahh, that fixed it. Thanks!

#5 Updated by Chris Crook over 11 years ago

One safe way of building the url for the delimited text plugin is to use QUrl...

  url = QUrl.fromLocalFile(filename)
  url.addQueryItem('delimiter',';')
  url.addQueryItem('xField','longitude')
  url.addQueryItem('yField','latitude')
  layer_uri=QString.fromAscii(url.toEncoded())
  vlayer=QgsVectorLayer(layer_uri,"My layer","delimitedtext")

Also available in: Atom PDF