def create_fb(self): """ Create a new empty fieldbook from template and add to layer list. Layer/file name changed to start with 'fb\_' if neccessary. """ ofname = QFileDialog.getSaveFileName( self.iface.mainWindow(), tr('New fieldbook'), filter=tr('Fieldbook file (*.dbf)')) if not ofname: return if QRegExp('fb_').indexIn(QFileInfo(ofname).baseName()): ofname = QDir.cleanPath( QFileInfo(ofname).absolutePath() + QDir().separator() + 'fb_' + QFileInfo(ofname).fileName()) ofbase = QDir.cleanPath( QFileInfo(ofname).absolutePath() + QDir().separator() + QFileInfo(ofname).baseName()) tempbase = QDir.cleanPath(self.plugin_dir + QDir().separator() + 'template' + QDir().separator() + 'fb_template') for ext in ['.dbf']: QFile(tempbase + ext).copy(ofbase + ext) fb = QgsVectorLayer(ofbase + '.dbf', QFileInfo(ofbase).baseName(), "ogr") if fb.isValid(): QgsMapLayerRegistry.instance().addMapLayer(fb)
def canvasPressEvent(self, e): """ handler to handle left button down, start rubberband line :param e: event """ al = self.iface.activeLayer() if al is None or al.type() != QgsMapLayer.VectorLayer or \ al.geometryType() != 2: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("Actual layer contains no polygons")) return if len(al.selectedFeatures()) != 1: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("Not a single polygon is selected in active layer")) return self.layer = al self.startPoint = self.toMapCoordinates(e.pos()) # snap to point on active layer line_tolerance = float(QSettings().value( "SurveyingCalculation/line_tolerance", config.line_tolerance)) self.layer.snapPoint(self.startPoint, line_tolerance) self.endPoint = self.startPoint self.isEmittingPoint = True self.showLine()
def about(self): """ About box of the plugin """ QMessageBox.information( self.iface.mainWindow(), tr('About'), tr('Surveying Calculation Plugin\n\n (c) DigiKom Ltd 2014- http://digikom.hu, mail (at) digikom.hu\nVersion 0.2' ))
def addp(self): """ Add point(s) to coordinate list entering coordinates """ if get_coordlist() is None: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("A coordinate list must be opened!")) return # show the dialog self.newp_dlg.show() self.newp_dlg.activateWindow() # Run the dialog event loop result = self.newp_dlg.exec_()
def onContinueButton(self): """ Check input and accept dialog """ try: w = float(self.ui.StartEast.text()) w = float(self.ui.StartNorth.text()) w = float(self.ui.EndEast.text()) w = float(self.ui.EndNorth.text()) except ValueError: QMessageBox.warning(self, tr("Warning"), tr("Invalid coordinate value")) return self.accept()
def polygon_division(self): """ accept a line from the user to divide the selected polygon on active layer """ al = self.iface.activeLayer() if al is None or al.type() != QgsMapLayer.VectorLayer or \ al.geometryType() != QGis.Polygon: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("Actual layer contains no polygons")) return if len(al.selectedFeatures()) != 1: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("Not a single polygon is selected in active layer")) return self.iface.mapCanvas().setMapTool(self.tool_pdiv)
def batch_plotting(self): """ Batch plots selected geometry items using the selected template and scale. """ #check if there are polygon layers in the project polygon_layers = get_vector_layers_by_type(QGis.Polygon) if polygon_layers is None: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("This utility needs at least one polygon type layer!")) return # show the dialog self.batchplotting_dlg.show() self.batchplotting_dlg.activateWindow() # Run the dialog event loop result = self.batchplotting_dlg.exec_()
def polygon_division(self): """ accept a line from the user to divide the selected polygon on active layer """ al = self.iface.activeLayer() if al is None or al.type() != QgsMapLayer.VectorLayer or \ al.geometryType() != QGis.Polygon: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("Actual layer contains no polygons")) return if len(al.selectedFeatures()) != 1: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("Not a single polygon is selected in active layer")) return self.iface.mapCanvas().setMapTool(self.tool_pdiv)
def batch_plotting(self): """ Batch plots selected geometry items using the selected template and scale. """ #check if there are polygon layers in the project polygon_layers = get_vector_layers_by_type(QGis.Polygon) if polygon_layers is None: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("This utility needs at least one polygon type layer!")) return # show the dialog self.batchplotting_dlg.show() self.batchplotting_dlg.activateWindow() # Run the dialog event loop result = self.batchplotting_dlg.exec_()
def fillLayersCombo(self): """ Fill the polygon layers combobox. """ oldSelectedLayer = self.ui.LayersComboBox.itemText( self.ui.LayersComboBox.currentIndex()) self.ui.LayersComboBox.clear() # if batch plotting is false only map canvas will be in the list if not self.batch_plotting: self.ui.LayersComboBox.addItem(tr("<Map view>")) self.ui.LayersComboBox.setCurrentIndex(0) return # if batch plotting is true fill layers combo polygon_layers = get_vector_layers_by_type(QGis.Polygon) if polygon_layers is None: return for layer in polygon_layers: self.ui.LayersComboBox.addItem(layer.name(), layer) # get current layer name try: actlayer_name = self.iface.activeLayer().name() except (AttributeError): actlayer_name = "" if self.ui.LayersComboBox.findText(oldSelectedLayer) == -1: self.ui.LayersComboBox.setCurrentIndex( self.ui.LayersComboBox.findText(actlayer_name)) else: self.ui.LayersComboBox.setCurrentIndex( self.ui.LayersComboBox.findText(oldSelectedLayer))
def __init__(self, iface, batch_plotting): """ Initialize dialog data and event handlers """ super(BatchPlottingDialog, self).__init__() self.ui = Ui_BatchPlottingDialog() self.ui.setupUi(self) self.iface = iface self.ui.OutputTab.setCurrentIndex(2) # if batch_plotting is True -> plotting by selected polygons # False -> plot map canvas self.batch_plotting = batch_plotting if not self.batch_plotting: self.setWindowTitle(tr("Plot by Template")) self.ui.OutputTab.setTabEnabled(0,False) self.ui.OutputTab.setTabEnabled(1,False) # event handlers self.ui.PlotButton.clicked.connect(self.onPlotButton) self.ui.TempDirButton.clicked.connect(self.onTempDirButton) self.ui.CloseButton.clicked.connect(self.onCloseButton) self.ui.TemplateList.setSortingEnabled(True) # set paths self.pdfpath = "" if self.batch_plotting: self.ui.OutputPDFEdit.setText( QgsAtlasComposition(None).filenamePattern() ) self.ui.SingleFileCheckbox.stateChanged.connect(self.changedSingleFileCheckbox) else: # set scale to map canvas scale self.ui.ScaleCombo.insertItem(0,"%d"%round(self.iface.mapCanvas().scale())) self.ui.ScaleCombo.insertItem(0,"<extent>") self.ui.ScaleCombo.setCurrentIndex(0) self.printer = None
def fillLayersCombo(self): """ Fill the polygon layers combobox. """ oldSelectedLayer = self.ui.LayersComboBox.itemText( self.ui.LayersComboBox.currentIndex() ) self.ui.LayersComboBox.clear() # if batch plotting is false only map canvas will be in the list if not self.batch_plotting: self.ui.LayersComboBox.addItem(tr("<Map view>")) self.ui.LayersComboBox.setCurrentIndex(0) return # if batch plotting is true fill layers combo polygon_layers = get_vector_layers_by_type(QGis.Polygon) if polygon_layers is None: return for layer in polygon_layers: self.ui.LayersComboBox.addItem(layer.name(),layer) # get current layer name try: actlayer_name = self.iface.activeLayer().name() except (AttributeError): actlayer_name = "" if self.ui.LayersComboBox.findText(oldSelectedLayer) == -1: self.ui.LayersComboBox.setCurrentIndex( self.ui.LayersComboBox.findText(actlayer_name) ) else: self.ui.LayersComboBox.setCurrentIndex( self.ui.LayersComboBox.findText(oldSelectedLayer) )
def onGamaPathButton(self): """ Change the directory of the gama-local executable. """ path = QFileDialog.getOpenFileName( self, tr("Select Path to GNU Gama Executable"), self.ui.GamaPathEdit.text()) if path != "": self.ui.GamaPathEdit.setText(path)
def onLogPathButton(self): """ Change the directory of the log file. """ path = QFileDialog.getSaveFileName(self, tr("Select Log File Path"), self.ui.LogPathEdit.text(), "", QFileDialog.DontConfirmOverwrite) if path != "": self.ui.LogPathEdit.setText(path)
def onGamaPathButton(self): """ Change the directory of the gama-local executable. """ path = QFileDialog.getOpenFileName(self, tr("Select Path to GNU Gama Executable"), self.ui.GamaPathEdit.text()) if path!="": self.ui.GamaPathEdit.setText(path)
def onPlotTemplateDirButton(self): """ Change the directory of the plot template files. """ path = QFileDialog.getExistingDirectory( self, tr("Select Plot Template Directory"), self.ui.PlotTemplateDirEdit.text(), QFileDialog.ShowDirsOnly) if path != "": self.ui.PlotTemplateDirEdit.setText(path)
def unload(self): """ Removes the plugin menu item and icon from QGIS GUI. """ for action in self.actions: self.iface.removePluginMenu(tr(u'&SurveyingCalculation'), action) self.iface.removeToolBarIcon(action) del self.menu del self.toolbar
def onHomeDirButton(self): """ Change the home directory where fieldbooks are stored. """ path = QFileDialog.getExistingDirectory(self, tr("Select Home Directory"), self.ui.HomeDirEdit.text(), QFileDialog.ShowDirsOnly) if path != "": self.ui.HomeDirEdit.setText(path)
def onPlotTemplateDirButton(self): """ Change the directory of the plot template files. """ path = QFileDialog.getExistingDirectory(self, tr("Select Plot Template Directory"), self.ui.PlotTemplateDirEdit.text(), QFileDialog.ShowDirsOnly) if path!="": self.ui.PlotTemplateDirEdit.setText(path)
def onLogPathButton(self): """ Change the directory of the log file. """ path = QFileDialog.getSaveFileName(self, tr("Select Log File Path"), self.ui.LogPathEdit.text(), "", QFileDialog.DontConfirmOverwrite) if path!="": self.ui.LogPathEdit.setText(path)
def onHomeDirButton(self): """ Change the home directory where fieldbooks are stored. """ path = QFileDialog.getExistingDirectory(self, tr("Select Home Directory"), self.ui.HomeDirEdit.text(), QFileDialog.ShowDirsOnly) if path!="": self.ui.HomeDirEdit.setText(path)
def unload(self): """ Removes the plugin menu item and icon from QGIS GUI. """ for action in self.actions: self.iface.removePluginMenu( tr(u'&SurveyingCalculation'), action) self.iface.removeToolBarIcon(action) del self.menu del self.toolbar
def create_coordlist(self): """ Create a new coordinate list from template and add to layer list. Layer/file name changed to start with 'coord\_' if neccessary. """ ofname = QFileDialog.getSaveFileName(self.iface.mainWindow(), tr('QGIS co-ordinate list'), filter = tr('Shape file (*.shp)')) if not ofname: return if QRegExp('coord_').indexIn(QFileInfo(ofname).baseName()): ofname = QDir.cleanPath(QFileInfo(ofname).absolutePath() + QDir().separator() + 'coord_' + QFileInfo(ofname).fileName()) ofbase = QDir.cleanPath(QFileInfo(ofname).absolutePath() + QDir().separator() + QFileInfo(ofname).baseName()) tempbase = QDir.cleanPath(self.plugin_dir + QDir().separator() + 'template' + QDir().separator() + 'coord_template') for ext in ['.shp', '.shx', '.dbf']: QFile(tempbase+ext).copy(ofbase+ext) coord = QgsVectorLayer(ofbase+'.shp', QFileInfo(ofbase).baseName(), "ogr") if coord.isValid(): QgsMapLayerRegistry.instance().addMapLayer(coord)
def create_fb(self): """ Create a new empty fieldbook from template and add to layer list. Layer/file name changed to start with 'fb\_' if neccessary. """ ofname = QFileDialog.getSaveFileName(self.iface.mainWindow(), tr('New fieldbook'), filter = tr('Fieldbook file (*.dbf)')) if not ofname: return if QRegExp('fb_').indexIn(QFileInfo(ofname).baseName()): ofname = QDir.cleanPath(QFileInfo(ofname).absolutePath() + QDir().separator() + 'fb_' + QFileInfo(ofname).fileName()) ofbase = QDir.cleanPath(QFileInfo(ofname).absolutePath() + QDir().separator() + QFileInfo(ofname).baseName()) tempbase = QDir.cleanPath(self.plugin_dir + QDir().separator() + 'template' + QDir().separator() + 'fb_template') for ext in ['.dbf']: QFile(tempbase+ext).copy(ofbase+ext) fb = QgsVectorLayer(ofbase+'.dbf', QFileInfo(ofbase).baseName(), "ogr") if fb.isValid(): QgsMapLayerRegistry.instance().addMapLayer(fb)
def onTempDirButton(self): """ Change the directory that contains print composer templates. """ templatepath = QFileDialog.getExistingDirectory(self, tr("Select Directory"), self.templatepath, QFileDialog.ShowDirsOnly) if templatepath!="": self.templatepath = templatepath QSettings().setValue("SurveyingCalculation/template_dir",templatepath) QSettings().sync() self.fillTemplateList()
def onTempDirButton(self): """ Change the directory that contains print composer templates. """ templatepath = QFileDialog.getExistingDirectory( self, tr("Select Directory"), self.templatepath, QFileDialog.ShowDirsOnly) if templatepath != "": self.templatepath = templatepath QSettings().setValue("SurveyingCalculation/template_dir", templatepath) QSettings().sync() self.fillTemplateList()
def create_coordlist(self): """ Create a new coordinate list from template and add to layer list. Layer/file name changed to start with 'coord\_' if neccessary. """ ofname = QFileDialog.getSaveFileName(self.iface.mainWindow(), tr('QGIS co-ordinate list'), filter=tr('Shape file (*.shp)')) if not ofname: return if QRegExp('coord_').indexIn(QFileInfo(ofname).baseName()): ofname = QDir.cleanPath( QFileInfo(ofname).absolutePath() + QDir().separator() + 'coord_' + QFileInfo(ofname).fileName()) ofbase = QDir.cleanPath( QFileInfo(ofname).absolutePath() + QDir().separator() + QFileInfo(ofname).baseName()) tempbase = QDir.cleanPath(self.plugin_dir + QDir().separator() + 'template' + QDir().separator() + 'coord_template') for ext in ['.shp', '.shx', '.dbf']: QFile(tempbase + ext).copy(ofbase + ext) coord = QgsVectorLayer(ofbase + '.shp', QFileInfo(ofbase).baseName(), "ogr") if coord.isValid(): QgsMapLayerRegistry.instance().addMapLayer(coord)
def onDivideButton(self): """ Check input and accept dialog """ try: a = float(self.ui.AreaLineEdit.text()) except ValueError: QMessageBox.warning(self, tr("Warning"), tr("Invalid area value")) return if a <= 0: QMessageBox.warning(self, tr("Warning"), tr("Invalid area value")) return if not self.ui.OnePointRadio.isChecked() and not self.ui.TwoPointRadio.isChecked(): QMessageBox.warning(self, tr("Warning"), tr("Select division method")) return self.accept()
def __init__(self, iface, batch_plotting): """ Initialize dialog data and event handlers """ super(BatchPlottingDialog, self).__init__() self.ui = Ui_BatchPlottingDialog() self.ui.setupUi(self) self.iface = iface self.ui.OutputTab.setCurrentIndex(2) # if batch_plotting is True -> plotting by selected polygons # False -> plot map canvas self.batch_plotting = batch_plotting if not self.batch_plotting: self.setWindowTitle(tr("Plot by Template")) self.ui.OutputTab.setTabEnabled(0, False) self.ui.OutputTab.setTabEnabled(1, False) # event handlers self.ui.PlotButton.clicked.connect(self.onPlotButton) self.ui.TempDirButton.clicked.connect(self.onTempDirButton) self.ui.CloseButton.clicked.connect(self.onCloseButton) self.ui.TemplateList.setSortingEnabled(True) # set paths self.pdfpath = "" if self.batch_plotting: self.ui.OutputPDFEdit.setText( QgsAtlasComposition(None).filenamePattern()) self.ui.SingleFileCheckbox.stateChanged.connect( self.changedSingleFileCheckbox) else: # set scale to map canvas scale self.ui.ScaleCombo.insertItem( 0, "%d" % round(self.iface.mapCanvas().scale())) self.ui.ScaleCombo.insertItem(0, "<extent>") self.ui.ScaleCombo.setCurrentIndex(0) self.printer = None
def divide(self): """ Divide the selected polygon. :param area: area to divide (float) :param rotate: rotate/offset True/False (bool) """ selection = self.layer.selectedFeatures() if len(selection) != 1: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("Not a single polygon is selected in active layer")) return feat = selection[0] # feature to divide geom = feat.geometry() if geom is None: return save_geom = QgsGeometry(geom) # save original geometry # change to layer coordinates point1 = self.toLayerCoordinates( self.layer, QgsPoint(self.startPoint.x(), self.startPoint.y())) point2 = self.toLayerCoordinates( self.layer, QgsPoint(self.endPoint.x(), self.endPoint.y())) # show coordinates to change coord_dlg = CoordDialog(point1, point2) if not coord_dlg.exec_(): return point1 = QgsPoint(float(coord_dlg.ui.StartEast.text()), \ float(coord_dlg.ui.StartNorth.text())) point2 = QgsPoint(float(coord_dlg.ui.EndEast.text()), \ float(coord_dlg.ui.EndNorth.text())) # center of rotation point0 = point1 rotate = True while geom.contains(point1): # extend line outside polygon point1 = QgsPoint(point1.x() - (point2.x() - point1.x()) * 10, \ point1.y() - (point2.y() - point1.y()) * 10) while geom.contains(point2): # extend line outside polygon point2 = QgsPoint(point2.x() + (point2.x() - point1.x()) * 10, \ point2.y() + (point2.y() - point1.y()) * 10) geom_line = QgsGeometry.fromPolyline([point1, point2]) # divider if not geom.intersects(geom_line): if QMessageBox.question(self.iface.mainWindow(), \ tr("Question"), tr("Line does not intersects polygon, line will be shifted into the polygon. Do you want to continue?"), \ QMessageBox.Yes, QMessageBox.No) == QMessageBox.No: return # find an internal point if QGis.QGIS_VERSION > '2.4': cp = geom.pointOnSurface().vertexAt(0) else: cp = geom.centroid().vertexAt(0) # offset line to go through cp dx = point2.x() - point1.x() dy = point2.y() - point1.y() point0 = QgsPoint(cp.x(), cp.y()) point1 = QgsPoint(cp.x() - 1000.0 * dx, cp.y() - 1000.0 * dy) point2 = QgsPoint(cp.x() + 1000.0 * dx, cp.y() + 1000.0 * dy) rotate = False # divide polygon result, new_geoms, test_points = geom.splitGeometry([point1, point2], True) if result != 0: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("Area division failed ") + str(result)) return # open modal dialog area_dlg = AreaDialog(save_geom.area(), geom.area(), rotate) if not area_dlg.exec_(): return area = float(area_dlg.ui.AreaLineEdit.text()) rotate = area_dlg.ui.OnePointRadio.isChecked() if save_geom.area() <= area: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("Area of polygon is smaller then requested area")) return area_tolerance = float(QSettings().value( "SurveyingCalculation/area_tolerance", config.area_tolerance)) max_iteration = int(QSettings().value( "SurveyingCalculation/max_iteration", config.max_iteration)) i = 0 #l = ((point2.x() - point1.x())**2 + (point2.y() - point1.y())**2)**0.5 while True: da = geom.area() - area if fabs(da) <= area_tolerance: break # area OK exit loop # length of intersection geom_line = QgsGeometry.fromPolyline([point1, point2]) section = save_geom.intersection(geom_line) l = section.length() # section length dir = atan2(point2.x() - point0.x(), point2.y() - point0.y()) if rotate: # change line direction b = da * 2.0 / l dir += atan(b / l) point1 = QgsPoint(point0.x() + sin(dir + pi) * 1000.0, \ point0.y() + cos(dir + pi) * 1000.0) point2 = QgsPoint(point0.x() + sin(dir) * 1000.0, \ point0.y() + cos(dir) * 1000.0) else: # offset line # perpendicular direction to line b = da / l # approximate offset dir += pi / 2.0 # perpendicular dir point1 = QgsPoint(point1.x() + sin(dir) * b, \ point1.y() + cos(dir) * b) point2 = QgsPoint(point2.x() + sin(dir) * b, \ point2.y() + cos(dir) * b) i += 1 if i > max_iteration: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("Area division not finished after max iteration") + str(result)) return geom = QgsGeometry(save_geom) # continue from original geomerty while geom.contains(point1): # extend line outside polygon point1 = QgsPoint(point1.x() - (point2.x() - point1.x()) * 10, \ point1.y() - (point2.y() - point1.y()) * 10) while geom.contains(point2): # extend line outside polygon point2 = QgsPoint(point2.x() + (point2.x() - point1.x()) * 10, \ point2.y() + (point2.y() - point1.y()) * 10) result, new_geoms, test_points = geom.splitGeometry( [point1, point2], True) # refresh old geometry fid = feat.id() self.layer.dataProvider().changeGeometryValues({fid: geom}) # add new feature feat_new = QgsFeature() fields = self.layer.dataProvider().fields() feat_new.setFields(fields, True) # combine new_geoms to single geometry new_geom = QgsGeometry(new_geoms[0]) for new_g in new_geoms[1:]: new_geom = QgsGeometry(new_geom.combine(new_g)) feat_new.setGeometry(new_geom) self.layer.dataProvider().addFeatures([feat_new]) # refresh canvas self.canvas.refresh()
def load_fieldbook(self): """ Import an electric fieldbook from file (GSI, JOB/ARE, ...) """ if get_coordlist() is None: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"), tr("No coordinate list is opened, coordinates will be lost from the fieldbook")) homedir = QSettings().value("SurveyingCalculation/homedir",config.homedir) fname = QFileDialog.getOpenFileName(self.iface.mainWindow(), \ tr('Electric fieldbook'), homedir, \ filter = tr('Leica GSI (*.gsi);;Leica IDX (*.idx);;Geodimeter JOB/ARE (*.job *.are);;Sokkia CRD (*.crd);;SurvCE RW5 (*.rw5);;STONEX DAT (*.dat);;Text dump (*.dmp)')) if fname: # file selected # make a copy of dbf template if not are is loaded if QRegExp('\.are$', Qt.CaseInsensitive).indexIn(fname) == -1: # ask for table name ofname = QFileDialog.getSaveFileName(self.iface.mainWindow(), tr('QGIS fieldbook'), QFileInfo(fname).absolutePath(), filter = tr('DBF file (*.dbf)')) if not ofname: return # remember last input dir QSettings().setValue("SurveyingCalculation/homedir",QFileInfo(fname).absolutePath()) QSettings().sync() # start with 'fb_'? if QRegExp('fb_').indexIn(QFileInfo(ofname).baseName()): ofname = QDir.cleanPath(QFileInfo(ofname).absolutePath() + QDir().separator() + 'fb_' + QFileInfo(ofname).fileName()) # extension is .dbf? if QRegExp('\.dbf$', Qt.CaseInsensitive).indexIn(ofname) == -1: ofname += '.dbf' tempname = QDir.cleanPath(self.plugin_dir + QDir().separator() + 'template' + QDir().separator() + 'fb_template.dbf') if not QFile(tempname).copy(ofname): QMessageBox.warning(self.iface.mainWindow(), tr('File warning'), tr('Error copying fieldbook template, target file exists?'), tr('OK')) return fb_dbf = QgsVectorLayer(ofname, QFileInfo(ofname).baseName(), "ogr") if not fb_dbf or not fb_dbf.isValid(): QMessageBox.warning(self.iface.mainWindow(), tr('File warning'), tr('Fieldbook loading error'), tr('OK')) return QgsMapLayerRegistry.instance().addMapLayer(fb_dbf) if QRegExp('\.gsi$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = LeicaGsi(fname) elif QRegExp('\.job$', Qt.CaseInsensitive).indexIn(fname) > -1 or \ QRegExp('\.are$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = JobAre(fname) elif QRegExp('\.crd$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = Sdr(fname) elif QRegExp('\.rw5$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = SurvCE(fname) elif QRegExp('\.dat$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = Stonex(fname) elif QRegExp('\.dmp$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = Dump(fname) elif QRegExp('\.idx$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = Idex(fname) else: QMessageBox.warning(self.iface.mainWindow(), tr('File warning'), tr('Unknown fieldbook type'), tr('OK')) return i = 10 # ordinal number for fieldbook records #fb_dbf.startEditing() fb.open() n_fb = 0 # fieldbook records stored n_co = 0 # points stored in coordinate list while True: # get next observation/station data from fieldbook r = fb.parse_next() if r is None: break # end of file if 'station' in r: # add row to fieldbook table record = QgsFeature() # add & initialize attributes record.setFields(fb_dbf.pendingFields(), True) j = fb_dbf.dataProvider().fieldNameIndex('id') if j != -1: record.setAttribute(j, i) for key in r: j = fb_dbf.dataProvider().fieldNameIndex(key) if j != -1: record.setAttribute(j, r[key]) (xxx, yyy) = fb_dbf.dataProvider().addFeatures([record]) if not xxx: QMessageBox.warning(self.iface.mainWindow(), tr('File warning'), tr('Fieldbook record creation error'), tr('OK')) return n_fb += 1 if 'station_e' in r or 'station_z' in r: # store station coordinates too dimension = 0 if 'station_z' in r: dimension += 1 else: r['station_z'] = None if 'station_e' in r and 'station_n' in r: dimension += 2 else: r['station_e'] = None r['station_n'] = None if not 'pc' in r: r['pc'] = None p = Point(r['point_id'], r['station_e'], r['station_n'], r['station_z'], r['pc']) qp = ScPoint(p) qp.store_coord(dimension) n_co += 1 if 'e' in r or 'z' in r: # store coordinates too dimension = 0 if 'z' in r: dimension += 1 else: r['z'] = None if 'e' in r and 'n' in r: dimension += 2 else: r['e'] = None r['n'] = None if not 'pc' in r: r['pc'] = None p = Point(r['point_id'], r['e'], r['n'], r['z'], r['pc']) qp = ScPoint(p) qp.store_coord(dimension) n_co += 1 i += 10 #fb_dbf.commitChanges() if QRegExp('\.are$', Qt.CaseInsensitive).indexIn(fname) == -1: if n_fb == 0: # no observations QgsMapLayerRegistry.instance().removeMapLayer(fb_dbf.id()) # remove empty file QFile(ofname).remove() if n_co == 0: # no coordinates QMessageBox.warning(self.iface.mainWindow(), tr("Warning"),\ tr("Neither coordinates nor observations found")) else: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"),\ tr("No observations found")) self.log.write() self.log.write_log(tr("Fieldbook loaded: ") + fname) self.log.write(" %d observations, %d coordinates" % (n_fb, n_co)) return
def about(self): """ About box of the plugin """ QMessageBox.information(self.iface.mainWindow(), tr('About'), tr('Surveying Calculation Plugin\n\n (c) DigiKom Ltd 2014- http://digikom.hu, mail (at) digikom.hu\nVersion 0.2'))
def onPlotButton(self): """ Batch plots selected geometry items using the selected template and scale. """ # check if one layer is selected if self.ui.LayersComboBox.currentIndex() == -1: QMessageBox.warning(self, tr("Warning"), tr("Select a layer!")) self.ui.LayersComboBox.setFocus() return # check if one composition template is selected if self.ui.TemplateList.selectedItems() == []: QMessageBox.warning(self, tr("Warning"), tr("Select a composer template!")) self.ui.TemplateList.setFocus() return template_filename = QDir(self.templatepath).absoluteFilePath( self.ui.TemplateList.currentItem().text()) # get the scale if self.ui.ScaleCombo.currentText() == "<extent>": scale = -1 else: try: scale = int(self.ui.ScaleCombo.currentText()) except (ValueError): QMessageBox.warning( self, tr("Warning"), tr("Scale must be a positive integer value!")) self.ui.ScaleCombo.setFocus() return if scale <= 0: QMessageBox.warning( self, tr("Warning"), tr("Scale must be a positive integer value!")) self.ui.ScaleCombo.setFocus() return # get composer name composer_name = self.ui.ComposerEdit.text() #check if there are selected items on polygon layers if self.batch_plotting: selected_layer = self.ui.LayersComboBox.itemData( self.ui.LayersComboBox.currentIndex()) selected_polygons = get_features(selected_layer.name(), QGis.Polygon, True) if selected_polygons is None: QMessageBox.warning( self, tr("Warning"), tr("Select at least one polygon on layer '%s'!" % selected_layer.name())) return # check output setting if self.ui.OutputTab.currentIndex() == 0: # to PDF if not self.ui.SingleFileCheckbox.checkState(): if len(self.ui.OutputPDFEdit.text()) == 0: res = QMessageBox.warning( self, tr("Warning"), tr("The filename pattern is empty. A default one will be used." ), QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) if res == QMessageBox.Cancel: return self.ui.OutputPDFEdit.setText( QgsAtlasComposition(None).filenamePattern()) elif self.ui.OutputTab.currentIndex() == 1: # to Printer # no need for checking pass elif self.ui.OutputTab.currentIndex() == 2: # to Composer View # no need for checking yet pass # get map renderer of map canvas renderer = self.iface.mapCanvas().mapRenderer() self.composition = QgsComposition(renderer) # if plot to Composer View the composition must be set # before loading the template # otherwise composer's item properties doesn't appear if self.ui.OutputTab.currentIndex() == 2: # to Composer View if len(composer_name) == 0: composer = self.iface.createNewComposer() else: composer = self.iface.createNewComposer(composer_name) composer.setComposition(self.composition) # read template file and add to composition template_file = QFile(template_filename) template_file.open(QIODevice.ReadOnly | QIODevice.Text) template_content = template_file.readAll() template_file.close() document = QDomDocument() document.setContent(template_content) self.composition.loadFromTemplate(document) # if batch_plotting is True create an atlas composition if self.batch_plotting: # get composer map item and set new scale and the grid cmap = self.composition.getComposerMapById(0) cmap.setNewScale(scale) cmap.setGridIntervalX(scale / 10) cmap.setGridIntervalY(scale / 10) cmap.setAtlasDriven(True) cmap.setAtlasScalingMode(QgsComposerMap.Fixed) # set atlas composition parameters atlas = self.composition.atlasComposition() atlas.setEnabled(True) atlas.setCoverageLayer(selected_layer) atlas.setHideCoverage(False) atlas.setFilenamePattern(self.ui.OutputPDFEdit.text()) atlas.setSingleFile(self.ui.SingleFileCheckbox.checkState()) atlas.setSortFeatures(False) atlas.setFilterFeatures(True) selected_ids = [f.id() for f in selected_layer.selectedFeatures()] filter_id_string = ','.join([str(sid) for sid in selected_ids]) atlas.setFeatureFilter("$id in (" + filter_id_string + ")") # print the complete atlas composition if self.ui.OutputTab.currentIndex() == 0: # to PDF self.composition.setAtlasMode(QgsComposition.ExportAtlas) if self.pdfpath == "": self.pdfpath = QgsProject.instance().homePath().encode( sys.getfilesystemencoding()) if self.ui.SingleFileCheckbox.checkState(): #print to single pdf (multi-page) outputFileName = QDir( self.pdfpath).absoluteFilePath("qgis.pdf") outputFileName = QFileDialog.getSaveFileName( self, tr("Choose a file name to save the map as"), outputFileName, tr("PDF Format") + " (*.pdf *.PDF)") if not outputFileName: return if not outputFileName.lower().endswith(".pdf"): outputFileName += ".pdf" self.pdfpath = QDir(outputFileName).absolutePath() else: #print to more pdf outputDir = QFileDialog.getExistingDirectory( self, tr("Directory where to save PDF files"), self.pdfpath, QFileDialog.ShowDirsOnly) if not outputDir: return # test directory (if it exists and is writable) if not QDir(outputDir).exists() or not QFileInfo( outputDir).isWritable(): QMessageBox.warning( self, tr("Unable to write into the directory"), tr("The given output directory is not writable. Cancelling." )) return self.pdfpath = outputDir printer = QPrinter() painter = QPainter() if not len(atlas.featureFilterErrorString()) == 0: QMessageBox.warning( self, tr("Atlas processing error"), tr("Feature filter parser error: %s" % atlas.featureFilterErrorString())) return atlas.beginRender() if self.ui.SingleFileCheckbox.checkState(): #prepare for first feature, so that we know paper size to begin with atlas.prepareForFeature(0) self.composition.beginPrintAsPDF(printer, outputFileName) # set the correct resolution self.composition.beginPrint(printer) printReady = painter.begin(printer) if not printReady: QMessageBox.warning( self, tr("Atlas processing error"), tr("Error creating %s." % outputFileName)) return progress = QProgressDialog(tr("Rendering maps..."), tr("Abort"), 0, atlas.numFeatures(), self) QApplication.setOverrideCursor(Qt.BusyCursor) for featureI in range(0, atlas.numFeatures()): progress.setValue(featureI + 1) # process input events in order to allow aborting QCoreApplication.processEvents() if progress.wasCanceled(): atlas.endRender() break if not atlas.prepareForFeature(featureI): QMessageBox.warning(self, tr("Atlas processing error"), tr("Atlas processing error")) progress.cancel() QApplication.restoreOverrideCursor() return if not self.ui.SingleFileCheckbox.checkState(): multiFilePrinter = QPrinter() outputFileName = QDir(outputDir).filePath( atlas.currentFilename()) + ".pdf" self.composition.beginPrintAsPDF( multiFilePrinter, outputFileName) # set the correct resolution self.composition.beginPrint(multiFilePrinter) printReady = painter.begin(multiFilePrinter) if not printReady: QMessageBox.warning( self, tr("Atlas processing error"), tr("Error creating %s." % outputFileName)) progress.cancel() QApplication.restoreOverrideCursor() return self.composition.doPrint(multiFilePrinter, painter) painter.end() else: # start print on a new page if we're not on the first feature if featureI > 0: printer.newPage() self.composition.doPrint(printer, painter) atlas.endRender() if self.ui.SingleFileCheckbox.checkState(): painter.end() QApplication.restoreOverrideCursor() elif self.ui.OutputTab.currentIndex() == 1: # to Printer # if To Printer is selected set the printer # setting up printer if self.printer is None: self.printer = QPrinter() self.printer.setFullPage(True) self.printer.setColorMode(QPrinter.Color) # open printer setting dialog pdlg = QPrintDialog(self.printer, self) pdlg.setModal(True) pdlg.setOptions(QAbstractPrintDialog.None) if not pdlg.exec_() == QDialog.Accepted: return QApplication.setOverrideCursor(Qt.BusyCursor) #prepare for first feature, so that we know paper size to begin with self.composition.setAtlasMode(QgsComposition.ExportAtlas) atlas.prepareForFeature(0) # set orientation if self.composition.paperWidth( ) > self.composition.paperHeight(): self.printer.setOrientation(QPrinter.Landscape) self.printer.setPaperSize( QSizeF(self.composition.paperHeight(), self.composition.paperWidth()), QPrinter.Millimeter) else: self.printer.setOrientation(QPrinter.Portrait) self.printer.setPaperSize( QSizeF(self.composition.paperWidth(), self.composition.paperHeight()), QPrinter.Millimeter) self.printer.setResolution(self.composition.printResolution()) self.composition.beginPrint(self.printer) painter = QPainter(self.printer) if not len(atlas.featureFilterErrorString()) == 0: QMessageBox.warning( self, tr("Atlas processing error"), tr("Feature filter parser error: %s" % atlas.featureFilterErrorString())) QApplication.restoreOverrideCursor() return atlas.beginRender() progress = QProgressDialog(tr("Rendering maps..."), tr("Abort"), 0, atlas.numFeatures(), self) for featureI in range(0, atlas.numFeatures()): progress.setValue(featureI + 1) # process input events in order to allow cancelling QCoreApplication.processEvents() if progress.wasCanceled(): atlas.endRender() break if not atlas.prepareForFeature(featureI): QMessageBox.warning(self, tr("Atlas processing error"), tr("Atlas processing error")) progress.cancel() QApplication.restoreOverrideCursor() return # start print on a new page if we're not on the first feature if featureI > 0: self.printer.newPage() self.composition.doPrint(self.printer, painter) atlas.endRender() painter.end() QApplication.restoreOverrideCursor() elif self.ui.OutputTab.currentIndex() == 2: # to Composer View # create new composer self.composition.setAtlasMode(QgsComposition.PreviewAtlas) composer.composerWindow().on_mActionAtlasPreview_triggered( True) atlas.parameterChanged.emit() # Increase the reference count of the composer object # for not being garbage collected. # If not doing this composer would lost reference and qgis would crash # when referring to this composer object or at quit. ctypes.c_long.from_address(id(composer)).value += 1 else: # if batch_plotting is False open a QgsComposerView with current map canvas cmap = self.composition.getComposerMapById(0) # set the new extent of composer map item newextent = self.iface.mapCanvas().mapRenderer().extent() currentextent = cmap.extent() canvas_ratio = newextent.width() / newextent.height() map_ratio = currentextent.width() / currentextent.height() if map_ratio < canvas_ratio: dh = newextent.width() / map_ratio - newextent.height() newextent.setYMinimum(newextent.yMinimum() - dh / 2) newextent.setYMaximum(newextent.yMaximum() + dh / 2) else: dw = map_ratio * newextent.height() - newextent.width() newextent.setXMinimum(newextent.xMinimum() - dw / 2) newextent.setXMaximum(newextent.xMaximum() + dw / 2) cmap.setNewExtent(newextent) # set the new scale of composer map item if scale > 0: cmap.setNewScale(scale) sc = cmap.scale() # set the grid interval according to the scale cmap.setGridIntervalX(sc / 10) cmap.setGridIntervalY(sc / 10) # Increase the reference count of the composer object # for not being garbage collected. # If not doing this composer would lost reference and qgis would crash # when referring to this composer object or at quit. ctypes.c_long.from_address(id(composer)).value += 1 self.accept()
def load_fieldbook(self): """ Import an electric fieldbook from file (GSI, JOB/ARE, ...) """ if get_coordlist() is None: QMessageBox.warning( self.iface.mainWindow(), tr("Warning"), tr("No coordinate list is opened, coordinates will be lost from the fieldbook" )) homedir = QSettings().value("SurveyingCalculation/homedir", config.homedir) fname = QFileDialog.getOpenFileName(self.iface.mainWindow(), \ tr('Electric fieldbook'), homedir, \ filter = tr('Leica GSI (*.gsi);;Geodimeter JOB/ARE (*.job *.are);;Sokkia CRD (*.crd);;SurvCE RW5 (*.rw5);;STONEX DAT (*.dat)')) if fname: # file selected # make a copy of dbf template if not are is loaded if QRegExp('\.are$', Qt.CaseInsensitive).indexIn(fname) == -1: # ask for table name ofname = QFileDialog.getSaveFileName( self.iface.mainWindow(), tr('QGIS fieldbook'), QFileInfo(fname).absolutePath(), filter=tr('DBF file (*.dbf)')) if not ofname: return # remember last input dir QSettings().setValue("SurveyingCalculation/homedir", QFileInfo(fname).absolutePath()) QSettings().sync() if QRegExp('fb_').indexIn(QFileInfo(ofname).baseName()): ofname = QDir.cleanPath( QFileInfo(ofname).absolutePath() + QDir().separator() + 'fb_' + QFileInfo(ofname).fileName()) tempname = QDir.cleanPath(self.plugin_dir + QDir().separator() + 'template' + QDir().separator() + 'fb_template.dbf') QFile(tempname).copy(ofname) fb_dbf = QgsVectorLayer(ofname, QFileInfo(ofname).baseName(), "ogr") QgsMapLayerRegistry.instance().addMapLayer(fb_dbf) if QRegExp('\.gsi$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = LeicaGsi(fname) elif QRegExp('\.job$', Qt.CaseInsensitive).indexIn(fname) > -1 or \ QRegExp('\.are$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = JobAre(fname) elif QRegExp('\.crd$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = Sdr(fname) elif QRegExp('\.rw5$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = SurvCE(fname) elif QRegExp('\.dat$', Qt.CaseInsensitive).indexIn(fname) > -1: fb = Stonex(fname) else: QMessageBox.warning(self.iface.mainWindow(), tr('File warning'), tr('Unknown fieldbook type'), tr('OK')) return i = 10 # ordinal number for fieldbook records #fb_dbf.startEditing() fb.open() n_fb = 0 # fieldbook records stored n_co = 0 # points stored in coordinate list while True: # get next observation/station data from fieldbook r = fb.parse_next() if r is None: break # end of file if 'station' in r: # add row to fieldbook table record = QgsFeature() # add & initialize attributes record.setFields(fb_dbf.pendingFields(), True) j = fb_dbf.dataProvider().fieldNameIndex('id') if j != -1: record.setAttribute(j, i) for key in r: j = fb_dbf.dataProvider().fieldNameIndex(key) if j != -1: record.setAttribute(j, r[key]) fb_dbf.dataProvider().addFeatures([record]) n_fb += 1 if 'station_e' in r or 'station_z' in r: # store station coordinates too dimension = 0 if 'station_z' in r: dimension += 1 else: r['station_z'] = None if 'station_e' in r and 'station_n' in r: dimension += 2 else: r['station_e'] = None r['station_n'] = None if not 'pc' in r: r['pc'] = None p = Point(r['point_id'], r['station_e'], r['station_n'], r['station_z'], r['pc']) qp = ScPoint(p) qp.store_coord(dimension) n_co += 1 if 'e' in r or 'z' in r: # store coordinates too dimension = 0 if 'z' in r: dimension += 1 else: r['z'] = None if 'e' in r and 'n' in r: dimension += 2 else: r['e'] = None r['n'] = None if not 'pc' in r: r['pc'] = None p = Point(r['point_id'], r['e'], r['n'], r['z'], r['pc']) qp = ScPoint(p) qp.store_coord(dimension) n_co += 1 i += 10 #fb_dbf.commitChanges() if QRegExp('\.are$', Qt.CaseInsensitive).indexIn(fname) == -1: if n_fb == 0: # no observations QgsMapLayerRegistry.instance().removeMapLayer(fb_dbf.id()) # remove empty file QFile(ofname).remove() if n_co == 0: # no coordinates QMessageBox.warning(self.iface.mainWindow(), tr("Warning"),\ tr("Neither coordinates nor observations found")) else: QMessageBox.warning(self.iface.mainWindow(), tr("Warning"),\ tr("No observations found")) self.log.write() self.log.write_log(tr("Fieldbook loaded: ") + fname) self.log.write(" %d observations, %d coordinates" % (n_fb, n_co)) return
def onOKButton(self): """ Close dialog. The changes will be saved. """ # check values in widgets try: line_tolerance = float(self.ui.LineToleranceEdit.text()) except (ValueError): QMessageBox.warning(self, tr("Warning"), tr("Snap tolerance must be a positive float value in layer units!")) self.ui.LineToleranceEdit.setFocus() return if line_tolerance<=0.0: QMessageBox.warning(self, tr("Warning"), tr("Snap tolerance must be a positive float value in layer units!")) self.ui.LineToleranceEdit.setFocus() return try: area_tolerance = float(self.ui.AreaToleranceEdit.text()) except (ValueError): QMessageBox.warning(self, tr("Warning"), tr("Area tolerance must be a positive float value in layer units!")) self.ui.AreaToleranceEdit.setFocus() return if area_tolerance<=0.0: QMessageBox.warning(self, tr("Warning"), tr("Area tolerance must be a positive float value in layer units!")) self.ui.AreaToleranceEdit.setFocus() return try: max_iteration = int(self.ui.MaxIterationEdit.text()) except (ValueError): QMessageBox.warning(self, tr("Warning"), tr("Maximum iteration must be a positive integer value!")) self.ui.MaxIterationEdit.setFocus() return if max_iteration<=0: QMessageBox.warning(self, tr("Warning"), tr("Maximum iteration must be a positive integer value!")) self.ui.MaxIterationEdit.setFocus() return # store settings # TODO store font setting from widgets QSettings().setValue("SurveyingCalculation/fontname",self.ui.FontNameCombo.currentText()) QSettings().setValue("SurveyingCalculation/fontsize",self.ui.FontSizeCombo.currentText()) QSettings().setValue("SurveyingCalculation/homedir",self.ui.HomeDirEdit.text()) QSettings().setValue("SurveyingCalculation/log_path",self.ui.LogPathEdit.text()) QSettings().setValue("SurveyingCalculation/gama_path",self.ui.GamaPathEdit.text()) QSettings().setValue("SurveyingCalculation/template_dir",self.ui.PlotTemplateDirEdit.text()) QSettings().setValue("SurveyingCalculation/line_tolerance",line_tolerance) QSettings().setValue("SurveyingCalculation/area_tolerance",area_tolerance) QSettings().setValue("SurveyingCalculation/max_iteration",max_iteration) QSettings().sync() self.accept()
def initGui(self): """Create the menu entries and toolbar icons inside the QGIS GUI.""" icon_path = ':/plugins/SurveyingCalculation/icon.png' icon_dir = QDir.cleanPath( self.plugin_dir + QDir.separator() + 'icons') # build menu self.actions = [] self.menu = QMenu() self.menu.setTitle(tr(u'&SurveyingCalculation')) self.sc_coord = QAction(QIcon(QDir(icon_dir).absoluteFilePath('new_coord.png')), tr("New coordinate list ..."), self.iface.mainWindow()) self.sc_fb = QAction(QIcon(QDir(icon_dir).absoluteFilePath('new_fb.png')),tr("New fieldbook ..."), self.iface.mainWindow()) self.sc_load = QAction(QIcon(QDir(icon_dir).absoluteFilePath('import_fieldbook.png')), tr("Import fieldbook ..."), self.iface.mainWindow()) self.sc_addp = QAction(QIcon(QDir(icon_dir).absoluteFilePath('addp.png')), tr("Add new point ..."), self.iface.mainWindow()) self.sc_calc = QAction(QIcon(QDir(icon_dir).absoluteFilePath('single_calc.png')), tr("Single point calculations ..."), self.iface.mainWindow()) self.sc_trav = QAction(QIcon(QDir(icon_dir).absoluteFilePath('traverse_calc.png')), tr("Traverse calculations ..."), self.iface.mainWindow()) self.sc_netw = QAction(QIcon(QDir(icon_dir).absoluteFilePath('network_calc.png')), tr("Network adjustment ..."), self.iface.mainWindow()) self.sc_tran = QAction(QIcon(QDir(icon_dir).absoluteFilePath('coord_calc.png')), tr("Coordinate transformation ..."), self.iface.mainWindow()) self.sc_pdiv = QAction(QIcon(QDir(icon_dir).absoluteFilePath('poly_div.png')), tr("Polygon division ..."), self.iface.mainWindow()) self.sc_plot = QAction(QIcon(QDir(icon_dir).absoluteFilePath('plot.png')), tr("Plot by template ..."), self.iface.mainWindow()) self.sc_batchplot = QAction(QIcon(QDir(icon_dir).absoluteFilePath('batch_plot.png')), tr("Batch plotting ..."), self.iface.mainWindow()) self.sc_settings = QAction(tr("Settings ..."), self.iface.mainWindow()) self.sc_help = QAction(tr("Help"), self.iface.mainWindow()) self.sc_about = QAction(tr("About"), self.iface.mainWindow()) self.menu.addActions([self.sc_coord, self.sc_fb, self.sc_load, self.sc_addp, self.sc_calc, self.sc_trav, self.sc_netw, self.sc_tran, self.sc_plot, self.sc_batchplot, self.sc_settings, self.sc_help, self.sc_about]) self.menu.insertSeparator(self.sc_calc) self.menu.insertSeparator(self.sc_plot) self.menu.insertSeparator(self.sc_settings) self.menu.insertSeparator(self.sc_help) menu_bar = self.iface.mainWindow().menuBar() actions = menu_bar.actions() lastAction = actions[len(actions) - 1] menu_bar.insertMenu(lastAction, self.menu) self.sc_coord.triggered.connect(self.create_coordlist) self.sc_fb.triggered.connect(self.create_fb) self.sc_load.triggered.connect(self.load_fieldbook) self.sc_addp.triggered.connect(self.addp) self.sc_calc.triggered.connect(self.calculations) self.sc_trav.triggered.connect(self.traverses) self.sc_netw.triggered.connect(self.networks) self.sc_tran.triggered.connect(self.transformation) self.sc_pdiv.setCheckable(True) self.tool_pdiv = LineMapTool(self.iface) self.tool_pdiv.setAction(self.sc_pdiv) self.sc_pdiv.triggered.connect(self.polygon_division) self.sc_plot.triggered.connect(self.plot_by_temp) self.sc_batchplot.triggered.connect(self.batch_plotting) self.sc_settings.triggered.connect(self.settings) self.sc_about.triggered.connect(self.about) self.sc_help.triggered.connect(self.help) # add icons to toolbar self.toolbar = self.iface.addToolBar(u'SurveyingCalculation') self.toolbar.setObjectName(u'SurveyingCalculation') self.toolbar.addActions([self.sc_load, self.sc_addp, self.sc_calc, self.sc_trav, self.sc_netw, self.sc_tran, self.sc_pdiv, self.sc_plot, self.sc_batchplot]) self.toolbar.insertSeparator(self.sc_calc) self.toolbar.insertSeparator(self.sc_plot)
def initGui(self): """Create the menu entries and toolbar icons inside the QGIS GUI.""" icon_path = ':/plugins/SurveyingCalculation/icon.png' icon_dir = QDir.cleanPath(self.plugin_dir + QDir.separator() + 'icons') # build menu self.actions = [] self.menu = QMenu() self.menu.setTitle(tr(u'&SurveyingCalculation')) self.sc_coord = QAction( QIcon(QDir(icon_dir).absoluteFilePath('new_coord.png')), tr("New coordinate list ..."), self.iface.mainWindow()) self.sc_fb = QAction( QIcon(QDir(icon_dir).absoluteFilePath('new_fb.png')), tr("New fieldbook ..."), self.iface.mainWindow()) self.sc_load = QAction( QIcon(QDir(icon_dir).absoluteFilePath('import_fieldbook.png')), tr("Import fieldbook ..."), self.iface.mainWindow()) self.sc_addp = QAction( QIcon(QDir(icon_dir).absoluteFilePath('addp.png')), tr("Add new point ..."), self.iface.mainWindow()) self.sc_calc = QAction( QIcon(QDir(icon_dir).absoluteFilePath('single_calc.png')), tr("Single point calculations ..."), self.iface.mainWindow()) self.sc_trav = QAction( QIcon(QDir(icon_dir).absoluteFilePath('traverse_calc.png')), tr("Traverse calculations ..."), self.iface.mainWindow()) self.sc_netw = QAction( QIcon(QDir(icon_dir).absoluteFilePath('network_calc.png')), tr("Network adjustment ..."), self.iface.mainWindow()) self.sc_tran = QAction( QIcon(QDir(icon_dir).absoluteFilePath('coord_calc.png')), tr("Coordinate transformation ..."), self.iface.mainWindow()) self.sc_pdiv = QAction( QIcon(QDir(icon_dir).absoluteFilePath('poly_div.png')), tr("Polygon division ..."), self.iface.mainWindow()) self.sc_plot = QAction( QIcon(QDir(icon_dir).absoluteFilePath('plot.png')), tr("Plot by template ..."), self.iface.mainWindow()) self.sc_batchplot = QAction( QIcon(QDir(icon_dir).absoluteFilePath('batch_plot.png')), tr("Batch plotting ..."), self.iface.mainWindow()) self.sc_settings = QAction(tr("Settings ..."), self.iface.mainWindow()) self.sc_help = QAction(tr("Help"), self.iface.mainWindow()) self.sc_about = QAction(tr("About"), self.iface.mainWindow()) self.menu.addActions([ self.sc_coord, self.sc_fb, self.sc_load, self.sc_addp, self.sc_calc, self.sc_trav, self.sc_netw, self.sc_tran, self.sc_plot, self.sc_batchplot, self.sc_settings, self.sc_help, self.sc_about ]) self.menu.insertSeparator(self.sc_calc) self.menu.insertSeparator(self.sc_plot) self.menu.insertSeparator(self.sc_settings) self.menu.insertSeparator(self.sc_help) menu_bar = self.iface.mainWindow().menuBar() actions = menu_bar.actions() lastAction = actions[len(actions) - 1] menu_bar.insertMenu(lastAction, self.menu) self.sc_coord.triggered.connect(self.create_coordlist) self.sc_fb.triggered.connect(self.create_fb) self.sc_load.triggered.connect(self.load_fieldbook) self.sc_addp.triggered.connect(self.addp) self.sc_calc.triggered.connect(self.calculations) self.sc_trav.triggered.connect(self.traverses) self.sc_netw.triggered.connect(self.networks) self.sc_tran.triggered.connect(self.transformation) self.sc_pdiv.setCheckable(True) self.tool_pdiv = LineMapTool(self.iface) self.tool_pdiv.setAction(self.sc_pdiv) self.sc_pdiv.triggered.connect(self.polygon_division) self.sc_plot.triggered.connect(self.plot_by_temp) self.sc_batchplot.triggered.connect(self.batch_plotting) self.sc_settings.triggered.connect(self.settings) self.sc_about.triggered.connect(self.about) self.sc_help.triggered.connect(self.help) # add icons to toolbar self.toolbar = self.iface.addToolBar(u'SurveyingCalculation') self.toolbar.setObjectName(u'SurveyingCalculation') self.toolbar.addActions([ self.sc_load, self.sc_addp, self.sc_calc, self.sc_trav, self.sc_netw, self.sc_tran, self.sc_pdiv, self.sc_plot, self.sc_batchplot ]) self.toolbar.insertSeparator(self.sc_calc) self.toolbar.insertSeparator(self.sc_plot)
def onOKButton(self): """ Close dialog. The changes will be saved. """ # check values in widgets try: line_tolerance = float(self.ui.LineToleranceEdit.text()) except (ValueError): QMessageBox.warning( self, tr("Warning"), tr("Snap tolerance must be a positive float value in layer units!" )) self.ui.LineToleranceEdit.setFocus() return if line_tolerance <= 0.0: QMessageBox.warning( self, tr("Warning"), tr("Snap tolerance must be a positive float value in layer units!" )) self.ui.LineToleranceEdit.setFocus() return try: area_tolerance = float(self.ui.AreaToleranceEdit.text()) except (ValueError): QMessageBox.warning( self, tr("Warning"), tr("Area tolerance must be a positive float value in layer units!" )) self.ui.AreaToleranceEdit.setFocus() return if area_tolerance <= 0.0: QMessageBox.warning( self, tr("Warning"), tr("Area tolerance must be a positive float value in layer units!" )) self.ui.AreaToleranceEdit.setFocus() return try: max_iteration = int(self.ui.MaxIterationEdit.text()) except (ValueError): QMessageBox.warning( self, tr("Warning"), tr("Maximum iteration must be a positive integer value!")) self.ui.MaxIterationEdit.setFocus() return if max_iteration <= 0: QMessageBox.warning( self, tr("Warning"), tr("Maximum iteration must be a positive integer value!")) self.ui.MaxIterationEdit.setFocus() return # store settings # TODO store font setting from widgets QSettings().setValue("SurveyingCalculation/fontname", self.ui.FontNameCombo.currentText()) QSettings().setValue("SurveyingCalculation/fontsize", self.ui.FontSizeCombo.currentText()) QSettings().setValue("SurveyingCalculation/homedir", self.ui.HomeDirEdit.text()) QSettings().setValue("SurveyingCalculation/log_path", self.ui.LogPathEdit.text()) QSettings().setValue("SurveyingCalculation/gama_path", self.ui.GamaPathEdit.text()) QSettings().setValue("SurveyingCalculation/template_dir", self.ui.PlotTemplateDirEdit.text()) QSettings().setValue("SurveyingCalculation/line_tolerance", line_tolerance) QSettings().setValue("SurveyingCalculation/area_tolerance", area_tolerance) QSettings().setValue("SurveyingCalculation/max_iteration", max_iteration) QSettings().sync() self.accept()
def onPlotButton(self): """ Batch plots selected geometry items using the selected template and scale. """ # check if one layer is selected if self.ui.LayersComboBox.currentIndex() == -1: QMessageBox.warning(self, tr("Warning"), tr("Select a layer!")) self.ui.LayersComboBox.setFocus() return # check if one composition template is selected if self.ui.TemplateList.selectedItems() == []: QMessageBox.warning(self, tr("Warning"), tr("Select a composer template!")) self.ui.TemplateList.setFocus() return template_filename = QDir(self.templatepath).absoluteFilePath( self.ui.TemplateList.currentItem().text()) # get the scale if self.ui.ScaleCombo.currentText()=="<extent>": scale = -1 else: try: scale = int(self.ui.ScaleCombo.currentText()) except (ValueError): QMessageBox.warning(self, tr("Warning"), tr("Scale must be a positive integer value!")) self.ui.ScaleCombo.setFocus() return if scale<=0: QMessageBox.warning(self, tr("Warning"), tr("Scale must be a positive integer value!")) self.ui.ScaleCombo.setFocus() return # get composer name composer_name = self.ui.ComposerEdit.text() #check if there are selected items on polygon layers if self.batch_plotting: selected_layer = self.ui.LayersComboBox.itemData(self.ui.LayersComboBox.currentIndex()) selected_polygons = get_features(selected_layer.name(),QGis.Polygon,True) if selected_polygons is None: QMessageBox.warning(self, tr("Warning"), tr("Select at least one polygon on layer '%s'!"%selected_layer.name())) return # check output setting if self.ui.OutputTab.currentIndex() == 0: # to PDF if not self.ui.SingleFileCheckbox.checkState(): if len( self.ui.OutputPDFEdit.text() ) == 0: res = QMessageBox.warning(self, tr("Warning"), tr("The filename pattern is empty. A default one will be used."), QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) if res == QMessageBox.Cancel: return self.ui.OutputPDFEdit.setText( QgsAtlasComposition(None).filenamePattern() ) elif self.ui.OutputTab.currentIndex() == 1: # to Printer # no need for checking pass elif self.ui.OutputTab.currentIndex() == 2: # to Composer View # no need for checking yet pass # get map renderer of map canvas renderer = self.iface.mapCanvas().mapRenderer() self.composition = QgsComposition(renderer) # if plot to Composer View the composition must be set # before loading the template # otherwise composer's item properties doesn't appear if self.ui.OutputTab.currentIndex() == 2: # to Composer View if len(composer_name)==0: composer = self.iface.createNewComposer() else: composer = self.iface.createNewComposer(composer_name) composer.setComposition(self.composition) # read template file and add to composition template_file = QFile( template_filename ) template_file.open(QIODevice.ReadOnly | QIODevice.Text) template_content = template_file.readAll() template_file.close() document = QDomDocument() document.setContent(template_content) self.composition.loadFromTemplate(document) # if batch_plotting is True create an atlas composition if self.batch_plotting: # get composer map item and set new scale and the grid cmap = self.composition.getComposerMapById(0) cmap.setNewScale(scale) cmap.setGridIntervalX(scale/10) cmap.setGridIntervalY(scale/10) cmap.setAtlasDriven(True) cmap.setAtlasScalingMode( QgsComposerMap.Fixed ) # set atlas composition parameters atlas = self.composition.atlasComposition() atlas.setEnabled(True) atlas.setCoverageLayer( selected_layer ) atlas.setHideCoverage(False) atlas.setFilenamePattern( self.ui.OutputPDFEdit.text() ) atlas.setSingleFile( self.ui.SingleFileCheckbox.checkState() ) atlas.setSortFeatures(False) atlas.setFilterFeatures(True) selected_ids = [f.id() for f in selected_layer.selectedFeatures()] filter_id_string = ','.join([str(sid) for sid in selected_ids]) atlas.setFeatureFilter("$id in (" + filter_id_string + ")") # print the complete atlas composition if self.ui.OutputTab.currentIndex() == 0: # to PDF self.composition.setAtlasMode( QgsComposition.ExportAtlas ) if self.pdfpath=="": self.pdfpath = QgsProject.instance().homePath().encode(sys.getfilesystemencoding()) if self.ui.SingleFileCheckbox.checkState(): #print to single pdf (multi-page) outputFileName = QDir(self.pdfpath).absoluteFilePath("qgis.pdf") outputFileName = QFileDialog.getSaveFileName(self, tr( "Choose a file name to save the map as" ), outputFileName, tr( "PDF Format" ) + " (*.pdf *.PDF)" ) if not outputFileName: return if not outputFileName.lower().endswith(".pdf"): outputFileName += ".pdf" self.pdfpath = QDir(outputFileName).absolutePath() else: #print to more pdf outputDir = QFileDialog.getExistingDirectory( self, tr( "Directory where to save PDF files" ), self.pdfpath, QFileDialog.ShowDirsOnly ) if not outputDir: return # test directory (if it exists and is writable) if not QDir(outputDir).exists() or not QFileInfo(outputDir).isWritable(): QMessageBox.warning( self, tr( "Unable to write into the directory" ), tr( "The given output directory is not writable. Cancelling." ) ) return self.pdfpath = outputDir printer = QPrinter() painter = QPainter() if not len(atlas.featureFilterErrorString()) == 0: QMessageBox.warning( self, tr( "Atlas processing error" ), tr( "Feature filter parser error: %s" % atlas.featureFilterErrorString() ) ) return atlas.beginRender() if self.ui.SingleFileCheckbox.checkState(): #prepare for first feature, so that we know paper size to begin with atlas.prepareForFeature(0) self.composition.beginPrintAsPDF(printer, outputFileName) # set the correct resolution self.composition.beginPrint(printer) printReady = painter.begin(printer) if not printReady: QMessageBox.warning( self, tr( "Atlas processing error" ), tr( "Error creating %s." % outputFileName ) ) return progress = QProgressDialog( tr( "Rendering maps..." ), tr( "Abort" ), 0, atlas.numFeatures(), self ) QApplication.setOverrideCursor( Qt.BusyCursor ) for featureI in range(0, atlas.numFeatures()): progress.setValue( featureI+1 ) # process input events in order to allow aborting QCoreApplication.processEvents() if progress.wasCanceled(): atlas.endRender() break if not atlas.prepareForFeature( featureI ): QMessageBox.warning( self, tr( "Atlas processing error" ), tr( "Atlas processing error" ) ) progress.cancel() QApplication.restoreOverrideCursor() return if not self.ui.SingleFileCheckbox.checkState(): multiFilePrinter = QPrinter() outputFileName = QDir( outputDir ).filePath( atlas.currentFilename() ) + ".pdf" self.composition.beginPrintAsPDF( multiFilePrinter, outputFileName ) # set the correct resolution self.composition.beginPrint( multiFilePrinter ) printReady = painter.begin( multiFilePrinter ) if not printReady: QMessageBox.warning( self, tr( "Atlas processing error" ), tr( "Error creating %s." % outputFileName ) ) progress.cancel() QApplication.restoreOverrideCursor() return self.composition.doPrint( multiFilePrinter, painter ) painter.end() else: # start print on a new page if we're not on the first feature if featureI > 0: printer.newPage() self.composition.doPrint( printer, painter ) atlas.endRender() if self.ui.SingleFileCheckbox.checkState(): painter.end() QApplication.restoreOverrideCursor() elif self.ui.OutputTab.currentIndex() == 1: # to Printer # if To Printer is selected set the printer # setting up printer if self.printer is None: self.printer = QPrinter() self.printer.setFullPage(True) self.printer.setColorMode(QPrinter.Color) # open printer setting dialog pdlg = QPrintDialog(self.printer,self) pdlg.setModal(True) pdlg.setOptions(QAbstractPrintDialog.None) if not pdlg.exec_() == QDialog.Accepted: return QApplication.setOverrideCursor(Qt.BusyCursor) #prepare for first feature, so that we know paper size to begin with self.composition.setAtlasMode( QgsComposition.ExportAtlas ) atlas.prepareForFeature(0) # set orientation if self.composition.paperWidth() > self.composition.paperHeight(): self.printer.setOrientation(QPrinter.Landscape) self.printer.setPaperSize( QSizeF(self.composition.paperHeight(), self.composition.paperWidth()), QPrinter.Millimeter) else: self.printer.setOrientation(QPrinter.Portrait) self.printer.setPaperSize( QSizeF(self.composition.paperWidth(), self.composition.paperHeight()), QPrinter.Millimeter) self.printer.setResolution(self.composition.printResolution()) self.composition.beginPrint( self.printer ) painter = QPainter(self.printer) if not len(atlas.featureFilterErrorString()) == 0: QMessageBox.warning( self, tr( "Atlas processing error" ), tr( "Feature filter parser error: %s" % atlas.featureFilterErrorString() ) ) QApplication.restoreOverrideCursor() return atlas.beginRender() progress = QProgressDialog( tr( "Rendering maps..." ), tr( "Abort" ), 0, atlas.numFeatures(), self ) for featureI in range(0, atlas.numFeatures()): progress.setValue( featureI+1 ) # process input events in order to allow cancelling QCoreApplication.processEvents() if progress.wasCanceled(): atlas.endRender() break if not atlas.prepareForFeature( featureI ): QMessageBox.warning( self, tr( "Atlas processing error" ), tr( "Atlas processing error" ) ) progress.cancel() QApplication.restoreOverrideCursor() return # start print on a new page if we're not on the first feature if featureI > 0: self.printer.newPage() self.composition.doPrint( self.printer, painter ) atlas.endRender() painter.end() QApplication.restoreOverrideCursor() elif self.ui.OutputTab.currentIndex() == 2: # to Composer View # create new composer self.composition.setAtlasMode( QgsComposition.PreviewAtlas ) composer.composerWindow().on_mActionAtlasPreview_triggered(True) atlas.parameterChanged.emit() # Increase the reference count of the composer object # for not being garbage collected. # If not doing this composer would lost reference and qgis would crash # when referring to this composer object or at quit. ctypes.c_long.from_address( id(composer) ).value += 1 else: # if batch_plotting is False open a QgsComposerView with current map canvas cmap = self.composition.getComposerMapById(0) # set the new extent of composer map item newextent = self.iface.mapCanvas().mapRenderer().extent() currentextent = cmap.extent() canvas_ratio = newextent.width()/newextent.height() map_ratio = currentextent.width()/currentextent.height() if map_ratio < canvas_ratio: dh = newextent.width() / map_ratio - newextent.height() newextent.setYMinimum( newextent.yMinimum() - dh / 2 ) newextent.setYMaximum( newextent.yMaximum() + dh / 2 ) else: dw = map_ratio * newextent.height() - newextent.width() newextent.setXMinimum( newextent.xMinimum() - dw / 2 ) newextent.setXMaximum( newextent.xMaximum() + dw / 2 ) cmap.setNewExtent(newextent) # set the new scale of composer map item if scale>0: cmap.setNewScale(scale) sc = cmap.scale() # set the grid interval according to the scale cmap.setGridIntervalX(sc/10) cmap.setGridIntervalY(sc/10) # Increase the reference count of the composer object # for not being garbage collected. # If not doing this composer would lost reference and qgis would crash # when referring to this composer object or at quit. ctypes.c_long.from_address( id(composer) ).value += 1 self.accept()