How to automatically detect X / Y fields in CSV

I am developing a plugin in QGIS that needs to import a csv file at some point. For this I use a generic process:

uri = "file:///"+fileName+"?delimiter=%s&xField=%s&yField=%s" % (";", "X", "Y")
layer = QgsVectorLayer(uri, name, "delimitedtext")

      

The problem is that I don't know beforehand how the X and Y fields are written to the file (it could be "COORDX" or "COX" or something else), and I don't really want to ask the user to provide them manually.

Now I know that whenever you "manually" import a CSV file into QGIS, the latter can automatically detect the X and Y fields. I would like to know if there is a way to use this feature to detect them, or if I have to do it myself. (The bonus points to someone who can indicate if there is a way to automatically detect the delimiter!)

+3


source to share





All Articles