Beispiel #1
0
    def readVrt(self):
        if self.dataSource is None:
            return False

        vrtPath = self.vrtPath()
        if not os.path.exists(vrtPath):
            return False

        file = QtCore.QFile(vrtPath)
        if not file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text):
            self.warning(u"Impossible to open VRT file {}".format(vrtPath))
            return False

        self.geometryBox.setChecked(False)

        try:
            self.readVrtStream(file)
        except Exception:
            self.warning("An error occurs during existing VRT file loading")
            return False

        finally:
            file.close()

        # self.info("Existing VRT file has been loaded")
        return True
Beispiel #2
0
    def writeVrt(self):
        content = self.prepareVrt()

        vrtPath = self.vrtPath()
        file = QtCore.QFile(vrtPath)
        if file.exists():
            if file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text):
                oldContent = file.readAll()
                file.close()
                if content == oldContent:
                    return True

            msgBox = QtWidgets.QMessageBox()
            msgBox.setText(u"The file {} already exist.".format(vrtPath))
            msgBox.setInformativeText(u"Do you want to overwrite ?")
            msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok
                                      | QtWidgets.QMessageBox.Cancel)
            msgBox.setDefaultButton(QtWidgets.QMessageBox.Cancel)
            ret = msgBox.exec_()
            if ret == QtWidgets.QMessageBox.Cancel:
                return False
            QtCore.QFile.remove(vrtPath)

        if not file.open(QtCore.QIODevice.ReadWrite | QtCore.QIODevice.Text):
            self.warning(u"Impossible to open VRT file {}".format(vrtPath))
            return False

        file.write(content)
        file.close()
        return True
Beispiel #3
0
    def writeSampleVrt(self, without_fields=False):
        content = self.prepareVrt(sample=True, without_fields=without_fields)

        vrtPath = self.samplePath()
        file = QtCore.QFile(vrtPath)
        if file.exists():
            QtCore.QFile.remove(vrtPath)

        if not file.open(QtCore.QIODevice.ReadWrite | QtCore.QIODevice.Text):
            self.warning(u"Impossible to open VRT file {}".format(vrtPath))
            return False

        file.write(content)
        file.close()
        return True
Beispiel #4
0
 def _prepare_multipart(self,
                        source_path: Path,
                        sld_path: typing.Optional[Path] = None
                        ) -> QtNetwork.QHttpMultiPart:
     main_file = QtCore.QFile(str(source_path))
     main_file.open(QtCore.QIODevice.ReadOnly)
     sidecar_files = []
     if sld_path is not None:
         sld_file = QtCore.QFile(str(sld_path))
         sld_file.open(QtCore.QIODevice.ReadOnly)
         sidecar_files.append(("sld_file", sld_file))
     if self.layer.type() == qgis.core.QgsMapLayerType.VectorLayer:
         dbf_file = QtCore.QFile(
             str(source_path.parent / f"{source_path.stem}.dbf"))
         dbf_file.open(QtCore.QIODevice.ReadOnly)
         sidecar_files.append(("dbf_file", dbf_file))
         prj_file = QtCore.QFile(
             str(source_path.parent / f"{source_path.stem}.prj"))
         prj_file.open(QtCore.QIODevice.ReadOnly)
         sidecar_files.append(("prj_file", prj_file))
         shx_file = QtCore.QFile(
             str(source_path.parent / f"{source_path.stem}.shx"))
         shx_file.open(QtCore.QIODevice.ReadOnly)
         sidecar_files.append(("shx_file", shx_file))
     elif self.layer.type() == qgis.core.QgsMapLayerType.RasterLayer:
         # when uploading tif files GeoNode seems to want the same file be uploaded
         # twice - one under the `base_file` form field and another under the
         # `tif_file` form field. This seems like a bug in GeoNode though
         tif_file = QtCore.QFile(str(source_path))
         tif_file.open(QtCore.QIODevice.ReadOnly)
         sidecar_files.append(("tif_file", tif_file))
     permissions = {
         "users": {},
         "groups": {},
     }
     if self.allow_public_access:
         permissions["users"]["AnonymousUser"] = [
             "view_resourcebase",
             "download_resourcebase",
         ]
     multipart = build_multipart(self.layer.metadata(),
                                 permissions,
                                 main_file,
                                 sidecar_files=sidecar_files)
     # below we set all QFiles as children of the multipart object and later we
     # also make the multipart object a children on the network reply object. This is
     # done in order to ensure deletion of resources at the correct time, as
     # recommended by the Qt documentation at:
     # https://doc.qt.io/qt-5/qhttppart.html#details
     main_file.setParent(multipart)
     for _, qt_file in sidecar_files:
         qt_file.setParent(multipart)
     return multipart