def get_temp_filename(self): tmpFile = QTemporaryFile() tmpFile.open() # fileName is no available until open tmpName = tmpFile.fileName() tmpFile.close() tmpFile.setAutoRemove(False) return tmpName
def tmpPath(): f = QTemporaryFile() f.open() f.close() os.remove(f.fileName()) return f.fileName().replace('.', '_')
def fetch_online_directories(self): """Fetch online directory of repositories.""" downloader = NetworkManager(self.DIRECTORY_URL) status, _ = downloader.fetch() if status: directory_file = QTemporaryFile() if directory_file.open(): directory_file.write(downloader.content) directory_file.close() with open(directory_file.fileName()) as csv_file: reader = csv.DictReader(csv_file, fieldnames=('name', 'url')) for row in reader: self._online_directories[row['name']] = row['url'].strip() # Save it to cache settings = QSettings() settings.beginGroup(repo_settings_group()) settings.setValue('online_directories', self._online_directories) settings.endGroup() else: # Just use cache from previous use settings = QSettings() settings.beginGroup(repo_settings_group()) self._online_directories = settings.value('online_directories', {}) settings.endGroup()
def write(self, theRasterName): print(theRasterName) path = "%s/%s" % (self.testDataDir, theRasterName) rasterLayer = QgsRasterLayer(path, "test") if not rasterLayer.isValid(): return False provider = rasterLayer.dataProvider() tmpFile = QTemporaryFile() tmpFile.open() # fileName is no avialable until open tmpName = tmpFile.fileName() tmpFile.close() # do not remove when class is destroyed so that we can read # the file and see difference tmpFile.setAutoRemove(False) fileWriter = QgsRasterFileWriter(tmpName) pipe = QgsRasterPipe() if not pipe.set(provider.clone()): print("Cannot set pipe provider") return False projector = QgsRasterProjector() projector.setCrs(provider.crs(), provider.crs()) if not pipe.insert(2, projector): print("Cannot set pipe projector") return False fileWriter.writeRaster( pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs()) checker = QgsRasterChecker() ok = checker.runTest("gdal", tmpName, "gdal", path) self.report += checker.report() # All OK, we can delete the file tmpFile.setAutoRemove(ok) return ok
def test_zip_file_yet_exist(self): zip = QTemporaryFile() zip.open() zip.close() os.remove(zip.fileName()) f0 = os.path.join(unitTestDataPath(), 'multipoint.shp') f1 = os.path.join(unitTestDataPath(), 'lines.shp') f2 = os.path.join(unitTestDataPath(), 'joins.qgs') rc = QgsZipUtils.zip(zip.fileName(), [f0, f1, f2]) self.assertTrue(rc) rc = QgsZipUtils.zip(zip.fileName(), [f0, f1, f2]) self.assertFalse(rc)
def render_plot(self, tight_layout=False): """ Saves the pyplot instance as an image in the OS's temp folder then refers the absolute path of the image to the QgsComposerPicture item. """ chart_tmp = QTemporaryFile() if chart_tmp.open(): tmp_file_name = chart_tmp.fileName() if tight_layout: plt.tight_layout() plt.savefig(tmp_file_name, format="tif", dpi=200) # Set path of picture item self.composer_item().setPicturePath(tmp_file_name) else: raise RuntimeError("Chart item could not be rendered")
def __init__(self, url: str, convert: bool = False): """Constructor of query. :param url:Full URL of OverPass Query with the query encoded in it. :type url:str """ super().__init__(url) if convert: temporary = QTemporaryFile( os.path.join(QDir.tempPath(), 'request-XXXXXX.txt')) else: temporary = QTemporaryFile( os.path.join(QDir.tempPath(), 'request-XXXXXX.osm')) temporary.open() self.result_path = temporary.fileName() temporary.close()
def testSaveRestoreFromProject(self): p = QgsProject() provider = ProjectProvider(p) # add some algorithms alg = QgsProcessingModelAlgorithm('test name', 'test group') provider.add_model(alg) alg2 = QgsProcessingModelAlgorithm('test name2', 'test group2') provider.add_model(alg2) self.assertEqual(len(provider.algorithms()), 2) tmp_file = QTemporaryFile() tmp_file.open() # fileName is no available until open temp_path = tmp_file.fileName() tmp_file.close() self.assertTrue(p.write(temp_path)) # restore project p2 = QgsProject() provider2 = ProjectProvider(p2) self.assertTrue(p2.read(temp_path)) self.assertEqual(len(provider2.model_definitions), 2) self.assertEqual(len(provider2.algorithms()), 2) self.assertEqual(provider2.algorithms()[0].name(), 'test name') self.assertEqual(provider2.algorithms()[0].group(), 'test group') self.assertEqual(provider2.algorithms()[1].name(), 'test name2') self.assertEqual(provider2.algorithms()[1].group(), 'test group2') # clear project should remove algorithms p2.clear() self.assertFalse(provider2.algorithms())
def generate_code(self): """ Generates the barcode image and sets the image in the picture item. :return: Returns True if the code was successfully generated, else False. :rtype: bool """ status = False if not self.computed_value(): self.setPicturePath('') return status svg_file = QTemporaryFile() try: if svg_file.open(): svg_path = '{0}.svg'.format(svg_file.fileName()) self._gen_image(svg_path) self.setPicturePath(svg_path) SvgFileTracker.instance().add_file(svg_path) status = True except BarcodeException as bc_ex: # Set error image self.set_error_image() QgsMessageLog.logMessage(repr(bc_ex), 'QRBarcodeItem', level=Qgis.Critical) finally: svg_file.close() return status
def __init__(self, url: str = None): """Constructor. :param url: URL of Nominatim server. :type url: basestring """ super().__init__(url) temporary = QTemporaryFile( os.path.join(QDir.tempPath(), 'request-XXXXXX.json')) temporary.open() self.result_path = temporary.fileName() temporary.close()
def __init__(self, url): """Constructor of query. :param url:Full URL of OverPass Query with the query encoded in it. :type url:str """ self._url = QUrl(url) temporary = QTemporaryFile( os.path.join(QDir.tempPath(), 'request-XXXXXX.osm')) temporary.open() self.result_path = temporary.fileName() temporary.close()
def __init__(self, db, sql, parent): super().__init__(db, sql, parent) tf = QTemporaryFile() tf.open() path = tf.fileName() tf.close() df = QgsVirtualLayerDefinition() df.setFilePath(path) df.setQuery(sql) self.subtask = QgsVirtualLayerTask(df) self.addSubTask(self.subtask, [], QgsTask.ParentDependsOnSubTask)
def __init__(self, url=None): """Constructor. :param url: URL of Nominatim server. :type url: basestring """ if url is None: url = 'https://nominatim.openstreetmap.org/search?format=json' self.__url = url temporary = QTemporaryFile( os.path.join(QDir.tempPath(), 'request-XXXXXX.json')) temporary.open() self.result_path = temporary.fileName() temporary.close()
def testTagging(self): self.assertFalse(QgsExifTools.geoTagImage('', QgsPointXY(1, 2))) self.assertFalse( QgsExifTools.geoTagImage('not a path', QgsPointXY(1, 2))) src_photo = os.path.join(TEST_DATA_DIR, 'photos', 'notags.JPG') tmpFile = QTemporaryFile() tmpFile.open() tmpName = tmpFile.fileName() tmpFile.close() shutil.copy(src_photo, tmpName) self.assertTrue(QgsExifTools.geoTagImage(tmpName, QgsPointXY(1.1, 3.3))) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'Point (1.1 3.3)') os.remove(tmpName) shutil.copy(src_photo, tmpName) self.assertTrue( QgsExifTools.geoTagImage(tmpName, QgsPointXY(-1.1, -3.3))) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'Point (-1.1 -3.3)') os.remove(tmpName) shutil.copy(src_photo, tmpName) deets = QgsExifTools.GeoTagDetails() deets.elevation = 110.1 self.assertTrue( QgsExifTools.geoTagImage(tmpName, QgsPointXY(1.1, 3.3), deets)) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'PointZ (1.1 3.3 110.1)') os.remove(tmpName) shutil.copy(src_photo, tmpName) deets = QgsExifTools.GeoTagDetails() deets.elevation = -110.1 self.assertTrue( QgsExifTools.geoTagImage(tmpName, QgsPointXY(1.1, 3.3), deets)) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'PointZ (1.1 3.3 -110.1)') os.remove(tmpName)
def _get_cursor_columns(self, c): tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() df = QgsVirtualLayerDefinition() df.setFilePath(tmp) df.setQuery(c.sql) p = QgsVectorLayer(df.toString(), "vv", "virtual") if not p.isValid(): return [] f = [f.name() for f in p.fields()] if p.geometryType() != QgsWkbTypes.NullGeometry: gn = getQueryGeometryName(tmp) if gn: f += [gn] return f
def _get_cursor_columns(self, c): tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() q = QUrl.toPercentEncoding(c.sql) p = QgsVectorLayer( "%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") if not p.isValid(): return [] f = [f.name() for f in p.fields()] if p.geometryType() != QgsWkbTypes.NullGeometry: gn = getQueryGeometryName(tmp) if gn: f += [gn] return f
def fetch_online_directories(self): """Fetch online directory of repositories.""" downloader = NetworkManager(self.DIRECTORY_URL) status, _ = downloader.fetch() if status: directory_file = QTemporaryFile() if directory_file.open(): directory_file.write(downloader.content) directory_file.close() with open(directory_file.fileName()) as csv_file: reader = csv.DictReader(csv_file, fieldnames=("name", "url")) for row in reader: repName = row["name"] repUrl = row["url"] # Check name and URL for None before stripping and adding if repName is not None and repUrl is not None: self._online_directories[row["name"]] = repUrl.strip() else: if repName is None: # No name LOGGER.warning("Missing name for repository" " - not added") else: # No URL LOGGER.warning( "Missing URL for repository" + str(row["name"]) + " - not added" ) # Save to settings settings = QgsSettings() settings.beginGroup(repo_settings_group()) settings.setValue("online_directories", self._online_directories) settings.endGroup() else: # Use settings settings = QgsSettings() settings.beginGroup(repo_settings_group()) self._online_directories = settings.value("online_directories", {}) settings.endGroup()
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results q = QUrl.toPercentEncoding(sql) t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() p = QgsVectorLayer( "%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") self._secs = t.elapsed() / 1000.0 if not p.isValid(): data = [] header = [] raise DbError(p.dataProvider().error().summary(), sql) else: header = [f.name() for f in p.fields()] has_geometry = False if p.geometryType() != QgsWkbTypes.NullGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.hasGeometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)
def __init__(self, db, sql, parent=None, layer=None, path=None): t = QTime() t.start() if not layer: tf = QTemporaryFile() tf.open() path = tf.fileName() tf.close() df = QgsVirtualLayerDefinition() df.setFilePath(path) df.setQuery(sql) layer = QgsVectorLayer(df.toString(), "vv", "virtual") self._secs = t.elapsed() / 1000.0 data = [] header = [] if not layer.isValid(): raise DbError(layer.dataProvider().error().summary(), sql) else: header = [f.name() for f in layer.fields()] has_geometry = False if layer.geometryType() != QgsWkbTypes.NullGeometry: gn = getQueryGeometryName(path) if gn: has_geometry = True header += [gn] for f in layer.getFeatures(): a = f.attributes() if has_geometry: if f.hasGeometry(): a += [f.geometry().asWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)
def _get_cursor_columns(self, c): tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() q = QUrl.toPercentEncoding(c.sql) p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") if not p.isValid(): return [] f = [f.name() for f in p.fields()] if p.geometryType() != Qgis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: f += [gn] return f
def generate_qr_code(qr_content): """ Generates a QR code SVG item and saves it as a temporary file. :param qr_content: Content used to generate a QR code. :type qr_content: str :return: Returns a QTemporaryFile object containing the SVG file with the QR code. :rtype: QTemporaryFile """ tmpf = QTemporaryFile() if tmpf.open(): file_path = tmpf.fileName() qr_code = pyqrcode.create(qr_content) qr_code.svg(file_path, scale=6) tmpf.close() return tmpf
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() df = QgsVirtualLayerDefinition() df.setFilePath(tmp) df.setQuery(sql) p = QgsVectorLayer(df.toString(), "vv", "virtual") self._secs = t.elapsed() / 1000.0 if not p.isValid(): data = [] header = [] raise DbError(p.dataProvider().error().summary(), sql) else: header = [f.name() for f in p.fields()] has_geometry = False if p.geometryType() != QgsWkbTypes.NullGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.hasGeometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)
def testTagging(self): self.assertFalse(QgsExifTools.geoTagImage('', QgsPointXY(1, 2))) self.assertFalse(QgsExifTools.geoTagImage('not a path', QgsPointXY(1, 2))) src_photo = os.path.join(TEST_DATA_DIR, 'photos', 'notags.JPG') tmpFile = QTemporaryFile() tmpFile.open() tmpName = tmpFile.fileName() tmpFile.close() shutil.copy(src_photo, tmpName) self.assertTrue(QgsExifTools.geoTagImage(tmpName, QgsPointXY(1.1, 3.3))) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'Point (1.1 3.3)') os.remove(tmpName) shutil.copy(src_photo, tmpName) self.assertTrue(QgsExifTools.geoTagImage(tmpName, QgsPointXY(-1.1, -3.3))) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'Point (-1.1 -3.3)') os.remove(tmpName) shutil.copy(src_photo, tmpName) deets = QgsExifTools.GeoTagDetails() deets.elevation = 110.1 self.assertTrue(QgsExifTools.geoTagImage(tmpName, QgsPointXY(1.1, 3.3), deets)) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'PointZ (1.1 3.3 110.1)') os.remove(tmpName) shutil.copy(src_photo, tmpName) deets = QgsExifTools.GeoTagDetails() deets.elevation = -110.1 self.assertTrue(QgsExifTools.geoTagImage(tmpName, QgsPointXY(1.1, 3.3), deets)) tag, ok = QgsExifTools.getGeoTag(tmpName) self.assertTrue(ok) self.assertEqual(tag.asWkt(6), 'PointZ (1.1 3.3 -110.1)') os.remove(tmpName)
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results q = QUrl.toPercentEncoding(sql) t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") self._secs = t.elapsed() / 1000.0 if not p.isValid(): data = [] header = [] raise DbError(p.dataProvider().error().summary(), sql) else: header = [f.name() for f in p.fields()] has_geometry = False if p.geometryType() != QGis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.geometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)
def adjust(self): """ Export data to GNU Gama xml, adjust the network and read result :returns: result list of adjusment from GNU Gama """ # fix = 0 free network fix = 0 adj = 0 for p, s in self.points: if s == 'FIX': fix += 1 else: adj += 1 if adj == 0 or len(self.observations) == 0: # no unknowns or observations return None doc = QDomDocument() doc.appendChild( doc.createComment( 'Gama XML created by SurveyingCalculation plugin for QGIS')) gama_local = doc.createElement('gama-local') gama_local.setAttribute('version', '2.0') doc.appendChild(gama_local) network = doc.createElement('network') network.setAttribute('axes-xy', 'ne') network.setAttribute('angles', 'left-handed') gama_local.appendChild(network) description = doc.createElement('description') if self.dimension == 1: description.appendChild(doc.createTextNode('GNU Gama 1D network')) elif self.dimension == 2: description.appendChild(doc.createTextNode('GNU Gama 2D network')) elif self.dimension == 3: description.appendChild(doc.createTextNode('GNU Gama 3D network')) network.appendChild(description) parameters = doc.createElement('parameters') parameters.setAttribute('sigma-apr', '1') parameters.setAttribute('conf-pr', str(self.probability)) parameters.setAttribute('tol-abs', '1000') parameters.setAttribute('sigma-act', 'aposteriori') parameters.setAttribute('update-constrained-coordinates', 'yes') network.appendChild(parameters) points_observations = doc.createElement('points-observations') points_observations.setAttribute( 'distance-stdev', str(self.stdev_dist) + ' ' + str(self.stdev_dist1)) points_observations.setAttribute('direction-stdev', str(self.stdev_angle)) points_observations.setAttribute('angle-stdev', str(math.sqrt(self.stdev_angle * 2))) points_observations.setAttribute('zenith-angle-stdev', str(self.stdev_angle)) network.appendChild(points_observations) for p, s in self.points: if self.dimension == 1: tmp = doc.createElement('point') tmp.setAttribute('id', p.id) if p.z is not None: tmp.setAttribute('z', str(p.z)) if s == 'FIX': tmp.setAttribute('fix', 'z') else: if fix == 0: tmp.setAttribute('adj', 'Z') else: tmp.setAttribute('adj', 'z') points_observations.appendChild(tmp) elif self.dimension == 2: tmp = doc.createElement('point') tmp.setAttribute('id', p.id) if p.e is not None and p.n is not None: tmp.setAttribute('y', str(p.e)) tmp.setAttribute('x', str(p.n)) if s == 'FIX': tmp.setAttribute('fix', 'xy') else: if fix == 0: # free network tmp.setAttribute('adj', 'XY') else: tmp.setAttribute('adj', 'xy') points_observations.appendChild(tmp) elif self.dimension == 3: tmp = doc.createElement('point') tmp.setAttribute('id', p.id) if p.e is not None and p.n is not None: tmp.setAttribute('y', str(p.e)) tmp.setAttribute('x', str(p.n)) if p.z is not None: tmp.setAttribute('z', str(p.z)) if s == 'FIX': tmp.setAttribute('fix', 'xyz') else: if fix == 0: tmp.setAttribute('adj', 'XYZ') else: tmp.setAttribute('adj', 'xyz') points_observations.appendChild(tmp) if self.dimension == 1: hd = doc.createElement('height-differences') points_observations.appendChild(hd) for o in self.observations: if o.station == 'station': # station record st_id = o.point_id if o.th is None: ih = 0 else: ih = o.th if self.dimension in [2, 3]: sta = doc.createElement('obs') sta.setAttribute('from', o.point_id) points_observations.appendChild(sta) else: # observation if self.dimension == 2: # horizontal network if o.hz is not None: tmp = doc.createElement('direction') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(o.hz.get_angle('GON'))) sta.appendChild(tmp) if o.d is not None: # horizontal distance hd = o.horiz_dist() if hd is not None: tmp = doc.createElement('distance') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(hd)) sta.appendChild(tmp) elif self.dimension == 1: # elevations only 1d if o.th is None: th = 0 else: th = o.th if o.d is not None and o.v is not None: tmp = doc.createElement('dh') tmp.setAttribute('from', st_id) tmp.setAttribute('to', o.point_id) # TODO hibaterjedes tmp.setAttribute('stdev', '1') sz = math.sin(o.v.get_angle()) w = self.stdev_dist + self.stdev_dist1 * o.d.d / 1000 ro_cc = 200 * 100 * 100 / math.pi if o.d.mode == 'SD': cz = math.cos(o.v.get_angle()) tmp.setAttribute('val', str(o.d.d * cz + ih - th)) tmp.setAttribute( 'stdev', str( math.sqrt(cz**2 * w**2 + (o.d.d * 1000)**2 * sz**2 * (self.stdev_angle / RO_CC)**2))) else: tz = math.tan(o.v.get_angle()) tmp.setAttribute( 'val', str(o.d.d / math.tan(o.v.get_angle()) + ih - th)) tmp.setAttribute( 'stdev', str( math.sqrt((1 / tz)**2 * w**2 + (o.d.d * 1000)**2 * (o.d.d * 1000)**2 * (1 / sz**2)**2 * (self.stdev_angle / RO_CC)**2))) hd.appendChild(tmp) elif self.dimension == 3: # 3d if o.th is None: th = 0 else: th = o.th if o.hz is not None: tmp = doc.createElement('direction') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(o.hz.get_angle('GON'))) sta.appendChild(tmp) if o.d is not None: if o.d.mode == 'SD': tmp = doc.createElement('s-distance') tmp.setAttribute('val', str(o.d.d)) tmp.setAttribute('from_dh', str(ih)) tmp.setAttribute('to_dh', str(th)) else: tmp = doc.createElement('distance') tmp.setAttribute('val', str(o.d.d)) tmp.setAttribute('to', o.point_id) sta.appendChild(tmp) if o.v is not None: tmp = doc.createElement('z-angle') tmp.setAttribute('to', o.point_id) tmp.setAttribute('val', str(o.v.get_angle('GON'))) tmp.setAttribute('from_dh', str(ih)) tmp.setAttribute('to_dh', str(th)) sta.appendChild(tmp) else: # unknown dimension return None # generate temp file name tmpf = QTemporaryFile(QDir.temp().absoluteFilePath('w')) tmpf.open(QIODevice.WriteOnly) tmpf.close() tmp_name = tmpf.fileName() f = QFile(tmp_name + '.xml') if f.open(QIODevice.WriteOnly): f.write(doc.toByteArray()) f.close() # run gama-local if self.gama_prog is None: return None status = QProcess.execute(self.gama_prog, [ tmp_name + '.xml', '--angles', '360', '--text', tmp_name + '.txt', '--xml', tmp_name + 'out.xml' ]) if status != 0: return None xmlParser = QXmlSimpleReader() xmlFile = QFile(tmp_name + 'out.xml') xmlInputSource = QXmlInputSource(xmlFile) doc.setContent(xmlInputSource, xmlParser) f_txt = QFile(tmp_name + '.txt') f_txt.open(QIODevice.ReadOnly) res = f_txt.readAll().data() f_txt.close() # store coordinates adj_nodes = doc.elementsByTagName('adjusted') if adj_nodes.count() < 1: return res adj_node = adj_nodes.at(0) for i in range(len(adj_node.childNodes())): pp = adj_node.childNodes().at(i) if pp.nodeName() == 'point': for ii in range(len(pp.childNodes())): ppp = pp.childNodes().at(ii) if ppp.nodeName() == 'id': p = Point(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'Y' or ppp.nodeName() == 'y': p.e = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'X' or ppp.nodeName() == 'x': p.n = float(ppp.firstChild().nodeValue()) elif ppp.nodeName() == 'Z' or ppp.nodeName() == 'z': p.z = float(ppp.firstChild().nodeValue()) ScPoint(p).store_coord(self.dimension) # remove input xml and output xml tmpf.remove() f_txt.remove() f.remove() xmlFile.remove() return res
def _end_of_request(self): tf = QTemporaryFile(os.path.join(QDir.tempPath(), 'request-XXXXXX.osm')) tf.setAutoRemove(False) tf.open(QIODevice.WriteOnly | QIODevice.Text) tf.write(self.network_reply.readAll().simplified()) tf.close() self.result_path = tf.fileName() self.loop.quit()
class ZipWriter: def __init__(self, outputPath, rootDir): self.output = outputPath self.rootDir = rootDir self.zipFile = zipfile.ZipFile(self.output.absoluteFilePath(), 'w') self.tempFile = QTemporaryFile() self.tempFile.setAutoRemove(False) self.tempFile.open(QIODevice.WriteOnly) self.tempFileName = self.tempFile.fileName() self.tempFile.close() def writeTile(self, tile, image, fileFormat, quality): path = '%s/%s/%s' % (self.rootDir, tile.z, tile.x) image.save(self.tempFileName, fileFormat, quality) tilePath = '%s/%s.%s' % (path, tile.y, fileFormat.lower()) self.zipFile.write(self.tempFileName, tilePath) def finalize(self): self.tempFile.close() self.tempFile.remove() self.zipFile.close()
def send_output_file(self, handler): format_dict = WFSFormats[self.format] # read the GML gml_path = join(self.temp_dir, '{}.gml'.format(self.filename)) output_layer = QgsVectorLayer(gml_path, 'qgis_server_wfs_features', 'ogr') if not output_layer.isValid(): handler.appendBody(b'') self.logger.critical( 'Output layer {} is not valid.'.format(gml_path)) return False # Temporary file where to write the output temporary = QTemporaryFile( join( self.temp_dir, 'to-{}-XXXXXX.{}'.format(self.format, format_dict['filenameExt']))) temporary.open() output_file = temporary.fileName() temporary.remove() # Fix issue #18 try: # create save options options = QgsVectorFileWriter.SaveVectorOptions() # driver name options.driverName = format_dict['ogrProvider'] # file encoding options.fileEncoding = 'utf-8' # coordinate transformation if format_dict['forceCRS']: options.ct = QgsCoordinateTransform( output_layer.crs(), QgsCoordinateReferenceSystem(format_dict['forceCRS']), QgsProject.instance()) # datasource options if format_dict['ogrDatasourceOptions']: options.datasourceOptions = format_dict['ogrDatasourceOptions'] # write file # noinspection PyUnresolvedReferences if Qgis.QGIS_VERSION_INT >= 31003: write_result, error_message = QgsVectorFileWriter.writeAsVectorFormatV2( output_layer, output_file, QgsProject.instance().transformContext(), options) else: write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( output_layer, output_file, options) if write_result != QgsVectorFileWriter.NoError: handler.appendBody(b'') self.logger.critical(error_message) return False except Exception as e: handler.appendBody(b'') exc_type, _, exc_tb = exc_info() self.logger.critical(str(e)) self.logger.critical(exc_type) self.logger.critical('\n'.join(traceback.format_tb(exc_tb))) return False if format_dict['zip']: # compress files import zipfile try: import zlib # NOQA compression = zipfile.ZIP_DEFLATED except ImportError: compression = zipfile.ZIP_STORED # create the zip file base_filename = splitext(output_file)[0] zip_file_path = join(self.temp_dir, '{}.zip'.format(base_filename)) with zipfile.ZipFile(zip_file_path, 'w') as zf: # Add the main file arc_filename = '{}.{}'.format(self.typename, format_dict['filenameExt']) zf.write(output_file, compress_type=compression, arcname=arc_filename) for extension in format_dict['extToZip']: file_path = join(self.temp_dir, '{}.{}'.format(base_filename, extension)) if exists(file_path): arc_filename = '{}.{}'.format(self.typename, extension) zf.write(file_path, compress_type=compression, arcname=arc_filename) zf.close() f = QFile(zip_file_path) if f.open(QFile.ReadOnly): ba = f.readAll() handler.appendBody(ba) return True else: # return the file created without zip f = QFile(output_file) if f.open(QFile.ReadOnly): ba = f.readAll() handler.appendBody(ba) return True handler.appendBody(b'') self.logger.critical('Error no output file') return False
def sendOutputFile(self, handler): format_dict = WFSFormats[self.format] # read the GML gml_path = join(self.tempdir, '{}.gml'.format(self.filename)) output_layer = QgsVectorLayer(gml_path, 'qgis_server_wfs_features', 'ogr') # Temporary file where to write the output temporary = QTemporaryFile( join(QDir.tempPath(), 'request-WFS-XXXXXX.{}'.format(format_dict['filenameExt']))) temporary.open() output_file = temporary.fileName() temporary.close() if output_layer.isValid(): try: # create save options options = QgsVectorFileWriter.SaveVectorOptions() # driver name options.driverName = format_dict['ogrProvider'] # file encoding options.fileEncoding = 'utf-8' # coordinate transformation if format_dict['forceCRS']: options.ct = QgsCoordinateTransform( output_layer.crs(), QgsCoordinateReferenceSystem(format_dict['forceCRS']), QgsProject.instance()) # datasource options if format_dict['ogrDatasourceOptions']: options.datasourceOptions = format_dict[ 'ogrDatasourceOptions'] # write file if Qgis.QGIS_VERSION_INT >= 31003: write_result, error_message = QgsVectorFileWriter.writeAsVectorFormatV2( output_layer, output_file, QgsProject.instance().transformContext(), options) else: write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat( output_layer, output_file, options) if write_result != QgsVectorFileWriter.NoError: handler.appendBody(b'') self.logger.critical(error_message) return False except Exception as e: handler.appendBody(b'') self.logger.critical(str(e)) return False if format_dict['zip']: # compress files import zipfile # noinspection PyBroadException try: import zlib # NOQA compression = zipfile.ZIP_DEFLATED except Exception: compression = zipfile.ZIP_STORED # create the zip file zip_file_path = join(self.tempdir, '%s.zip' % self.filename) with zipfile.ZipFile(zip_file_path, 'w') as zf: # add all files zf.write(join( self.tempdir, '%s.%s' % (self.filename, format_dict['filenameExt'])), compress_type=compression, arcname='%s.%s' % (self.typename, format_dict['filenameExt'])) for e in format_dict['extToZip']: file_path = join(self.tempdir, '%s.%s' % (self.filename, e)) if exists(file_path): zf.write(file_path, compress_type=compression, arcname='%s.%s' % (self.typename, e)) zf.close() f = QFile(zip_file_path) if f.open(QFile.ReadOnly): ba = f.readAll() handler.appendBody(ba) return True else: # return the file created without zip f = QFile(output_file) if f.open(QFile.ReadOnly): ba = f.readAll() handler.appendBody(ba) return True handler.appendBody(b'') self.logger.critical('Error no output file') return False
def finalize(self): archive_info = { "cache_size_multiply": 0, "levels": [], 'max_level': max(self.levels.keys()), 'min_level': min(self.levels.keys()), "name": self.__layerName, "renderer_properties": { "alpha": 255, "antialias": True, "brightness": 0, "contrast": 1, "dither": True, "filterbitmap": True, "greyscale": False, "type": "tms_renderer" }, "tms_type": 2, "type": 32, "visible": True } for level, coords in self.levels.items(): level_json = { "level": level, "bbox_maxx": max(coords["x"]), "bbox_maxy": max(coords["y"]), "bbox_minx": min(coords["x"]), "bbox_miny": min(coords["y"]), } archive_info["levels"].append(level_json) tempFile = QTemporaryFile() tempFile.setAutoRemove(False) tempFile.open(QIODevice.WriteOnly) tempFile.write(json.dumps(archive_info)) tempFileName = tempFile.fileName() tempFile.close() self.zipFile.write(tempFileName, "%s.json" % self.rootDir) ZipWriter.finalize(self)