1

I'm trying to load some points through a vectorlayer on my QMainWindow, part of my pyqgis application. The code is:

    uri = 'file:///C:/Users/.../data/Targets_com.csv?delimiter=,&crs=epsg:4326&xField=x&yField=y'
    # uri = 'file:///C:/Users/.../data/Targets_wkt.csv?delimiter=,&crs=epsg:4326'#&wktField=wkt'
    # Loading the layer and check it:
    self.target_layer = QgsVectorLayer(uri, 'Targets' , 'delimitedtext')
    if self.target_layer.isValid():
        QgsMapLayerRegistry.instance().addMapLayer(self.target_layer)
        print('Layer OKKK')
    else:
        print('Layer Fail')
    self.target_layer.updateExtents()

When I run my application with that, I get "Layer Fail", so for sure there is something wrong in my "uri" definition, isn't it?

If I change uri like:

uri = 'file:///C:/Users/.../data/Targets_com.csv?delimiter=,&crs=epsg:4326'

that is, cutting the geometry definition (on both cases: (x,y) and wkt), I get the "Layer OKKK", but I can't see my points on the map.

I think the .csv files are well written, cause I can load them on QGIS. Anyway, this are my files.csv (and I tried with one more using ";" like delimiter):

enter image description here I have read all the posts I found about csv files and syntax with qgis, but I don't know how to solve this, anyone have any idea? i'm using the OSGeo4W package (QGIS 2.16, its own python interpreter, PyQt4,...) and using eclipse-PyDev for development.

0

Try using the following for your X,Y data format:

uri = "file:///C:/Users/.../data/Targets_com.csv?delimiter=,&crs=epsg:4326&xField=%s&yField=%s" % (",", "LON", "LAT")

where you can specify the field names at the end.


The following worked for me from the Python Console:

uri = "file:///C:/Users/.../data/Targets_com.csv?delimiter=,&crs=epsg:4326&xField=%s&yField=%s" % (",", "LON", "LAT")
vlayer = QgsVectorLayer(uri, 'Point example', "delimitedtext")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

Result

  • 1
    Wou, thank you, I already tried to specify fields at the end. My fault was that the names were incorrect: (",", "x", "y") instead of (",", "LON", "LAT")... – Nacho F. Sep 12 '16 at 11:44
  • @NachoF. - Most welcome and don't worry, that is a very easy mistake to make ;) – Joseph Sep 12 '16 at 11:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.