Пример #1
0
def exportVectorLayer(layer):
    '''accepts a QgsVectorLayer'''
    settings = QtCore.QSettings()
    filename = unicode(layer.source())
    destFilename = unicode(layer.name())
    if not filename.lower().endswith("shp"):
        output = utils.tempFilenameInTempFolder(destFilename + ".shp")
        provider = layer.dataProvider()
        fields = layer.pendingFields()
        writer = QgsVectorFileWriter(output, "utf-8", fields,
                                     provider.geometryType(), layer.crs())
        for feat in layer.getFeatures():
            writer.addFeature(feat)
        del writer
        return output, "utf-8"
    else:
        # determine encoding
        # first look in .cpg file
        cpgPath = filename.replace('.shp', '.cpg')
        if os.path.exists(cpgPath):
            with open(cpgPath) as f:
                charset = f.readline().strip('\r\n')
        else:
            # try to get encoding from provider
            charset = self.layer.dataProvider().encoding()
            if charset.lower() == 'system':
                # get encoding from system
                charset = locale.getpreferredencoding()

        return filename, charset
Пример #2
0
def exportVectorLayerAddingId(layer, fid):
    '''accepts a QgsVectorLayer'''
    settings = QtCore.QSettings()
    destFilename = unicode(layer.name())
    output = utils.tempFilenameInTempFolder(destFilename + ".shp")
    provider = layer.dataProvider()
    fields = layer.pendingFields()
    fieldsInFid = re.findall("\[.*?\]", fid)
    fidattrs = {}
    for field in fieldsInFid:
        name = field[1:-1]
        idx = provider.fieldNameIndex(name)
        if idx == -1:
            raise Exception("Field %s not found in layer" % name)
        fidattrs[field] = idx
    fields.append(QgsField("geogigid", QtCore.QVariant.String))
    writer = QgsVectorFileWriter(output, "utf-8", fields,
                                 provider.geometryType(), layer.crs())
    outFeat = QgsFeature()
    for feat in layer.getFeatures():
        inGeom = feat.geometry()
        outFeat.setGeometry(inGeom)
        attrs = feat.attributes()
        if fid:
            geogitid = fid
            for k, v in fidattrs.iteritems():
                geogitid = geogitid.replace(k, unicode(attrs[v]))
        else:
            geogitid = str(hash(tuple(attrs)))
        attrs.append(geogitid)
        outFeat.setAttributes(attrs)
        writer.addFeature(outFeat)
        writer.addFeature(feat)
    del writer
    return output, "utf-8"
def exportVectorLayerAddingId(layer, fid):
    '''accepts a QgsVectorLayer'''
    settings = QtCore.QSettings()
    systemEncoding = settings.value("/UI/encoding", "System")
    destFilename = unicode(layer.name())
    output = utils.tempFilenameInTempFolder(destFilename + ".shp")
    provider = layer.dataProvider()
    fields = layer.pendingFields()
    fieldsInFid = re.findall("\[.*?\]", fid)
    fidattrs = {}
    for field in fieldsInFid:
        name = field[1:-1]
        idx = provider.fieldNameIndex(name)
        if idx == -1:
            raise Exception("Field %s not found in layer" % name)
        fidattrs[field] = idx
    fields.append(QgsField("geogigid", QtCore.QVariant.String))
    writer = QgsVectorFileWriter(output, systemEncoding, fields, provider.geometryType(), layer.crs())
    outFeat = QgsFeature()
    for feat in layer.getFeatures():
        inGeom = feat.geometry()
        outFeat.setGeometry(inGeom)
        attrs = feat.attributes()
        if fid:
            geogitid = fid
            for k, v in fidattrs.iteritems():
                geogitid = geogitid.replace(k, unicode(attrs[v]))
        else:
            geogitid = str(hash(tuple(attrs)))
        attrs.append(geogitid)
        outFeat.setAttributes(attrs)
        writer.addFeature(outFeat)
        writer.addFeature(feat)
    del writer
    return output
Пример #4
0
def exportVectorLayer(layer):
    '''accepts a QgsVectorLayer'''
    settings = QtCore.QSettings()
    filename = unicode(layer.source())
    destFilename = unicode(layer.name())
    if not filename.lower().endswith("shp"):
        output = utils.tempFilenameInTempFolder(destFilename + ".shp")
        provider = layer.dataProvider()
        fields = layer.pendingFields()
        writer = QgsVectorFileWriter(output, "utf-8", fields, provider.geometryType(), layer.crs())
        for feat in layer.getFeatures():
            writer.addFeature(feat)
        del writer
        return output, "utf-8"
    else:
        # determine encoding
        # first look in .cpg file
        cpgPath = filename.replace('.shp', '.cpg')
        if os.path.exists(cpgPath):
            with open(cpgPath) as f:
                charset = f.readline().strip('\r\n')
        else:
            # try to get encoding from provider
            charset = self.layer.dataProvider().encoding()
            if charset.lower() == 'system':
                # get encoding from system
                charset = locale.getpreferredencoding()

        return filename, charset
def exportVectorLayer(layer):
    '''accepts a QgsVectorLayer'''
    settings = QtCore.QSettings()
    systemEncoding = settings.value("/UI/encoding", "System")
    filename = unicode(layer.source())
    destFilename = unicode(layer.name())
    if not filename.lower().endswith("shp"):
        output = utils.tempFilenameInTempFolder(destFilename + ".shp")
        provider = layer.dataProvider()
        fields = layer.pendingFields()
        writer = QgsVectorFileWriter(output, systemEncoding, fields, provider.geometryType(), layer.crs())
        for feat in layer.getFeatures():
            writer.addFeature(feat)
        del writer
        return output
    else:
        return filename
Пример #6
0
 def createApp(self):
     appdef = self.createAppDefinition(False)
     appName = appdef["Settings"]["Title"].replace(" ", "")
     appdefFile = utils.tempFilenameInTempFolder(appName + ".json")
     folder = os.path.dirname(appdefFile)
     print folder
     exportLayers(appdef["Layers"], folder, self.progress, 3)
     saveAppdef(appdef, appdefFile)
     from ftplib import FTP
     url = self.g3mUrlBox.text()
     ftp = FTP(url, 'qgis', 'qgis')
     utils.chdir("ftp-box/g3m-qgis/%s" % appName, ftp)
     #ftp.cwd("ftp-box/g3m-qgis/%s" % appName)
     with open(appdefFile, 'rb') as f:
         ftp.storbinary('STOR ' + appName + ".json", f)
     utils.chdir("layers", ftp)
     for layer in appdef["Layers"]:
         layerName = utils.safeName(layer.layer)
         with open(folder + "/layers/lyr_%s.js" % layerName, 'rb') as f:
             ftp.storbinary('STOR ' + layerName + ".json", f)
Пример #7
0
 def createApp(self):
     appdef = self.createAppDefinition(False)
     appName = appdef["Settings"]["Title"].replace(" ", "")
     appdefFile = utils.tempFilenameInTempFolder(appName + ".json")
     folder = os.path.dirname(appdefFile)
     print folder
     exportLayers(appdef["Layers"], folder, self.progress, 3)
     saveAppdef(appdef, appdefFile)
     from ftplib import FTP
     url = self.g3mUrlBox.text()
     ftp = FTP(url, 'qgis', 'qgis')
     utils.chdir("ftp-box/g3m-qgis/%s" % appName, ftp)
     #ftp.cwd("ftp-box/g3m-qgis/%s" % appName)
     with open(appdefFile, 'rb') as f:
         ftp.storbinary('STOR ' + appName + ".json", f)
     utils.chdir("layers", ftp)
     for layer in appdef["Layers"]:
         layerName = utils.safeName(layer.layer)
         with open(folder + "/layers/lyr_%s.js" % layerName, 'rb') as f:
             ftp.storbinary('STOR ' + layerName + ".json", f)