2

I will want create "Hello World" project at PyQgis. I will want to show map on form.ui from .shp-file, but it does not download and show. But it is not loaded and does not appear. It`s value variable "layer" after load .shp enter image description here

    file = QFileDialog.getOpenFileName(self,
                   "Open Shapefile", ".", "Shapefiles (*.shp)")
    fileInfo = QFileInfo(file)

    # Add the layer
    layer = QgsVectorLayer(file, fileInfo.fileName(), "ogr")

    if not layer.isValid():
      return

    # Add layer to the registry
    QgsMapLayerRegistry.instance().addMapLayer(layer);

    # Set extent to the extent of our layer
    self.canvas.setExtent(layer.extent())

    # Set up the map canvas layer set
    cl = QgsMapCanvasLayer(layer)
    layers = [cl]
    self.canvas.setLayerSet(layers)

layer.isValid() = false

map files have extensions: .cpg .dbf .prj .qix .shp .shx

enter image description here enter image description here

enter image description here

I tried to change. Nothing changed

  • You have a semi-colon at the end of this statement, what happens if you remove it, QgsMapLayerRegistry.instance().addMapLayer(layer); – artwork21 Jun 13 '16 at 12:23
  • nothing, it is after layer.isValid() = false. – Ivan Jun 13 '16 at 15:54
  • Something must be wrong with the file variable path format or the shp layer. Does the shp layer show if you manually add it into QGIS? Try hard coding the file path to see if that works. If that works, then add a print statement on the file variable to see if it is formatted correctly. – artwork21 Jun 13 '16 at 16:54
  • I tried to change. Nothing changed. – Ivan Jun 14 '16 at 5:15
  • screens attached to the end of the answer – Ivan Jun 14 '16 at 5:16

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.