def on_geometry_changed(self, fid: int, geometry: QgsGeometry) -> None: """ Slot activated, if a geometry changed in an edit session of the currently selected active layer :param fid: FeatureID of the changed geometry :param geometry: changed QgsGeometry object :return: Nothing """ # noinspection PyCallByClass,PyArgumentList,PyTypeChecker QgsMessageLog.logMessage("on_geometry_changed [{}]: {}".format( fid, str(geometry.asJson())), level=0) if self.__line_construct.active_feature_id == fid: # -> last part of self._parse_selection self.__line_construct.active_geometry = geometry if self.__line_construct.active_geometry.isEmpty(): self.iface.messageBar().pushWarning( "Warning", "Selected an empty geometry!") self.__line_construct.reset() return if self.__line_construct.active_geometry.isMultipart(): self.__line_construct.active_geometry = self.__line_construct.active_geometry.asGeometryCollection( )[0] line = self.__line_construct.active_geometry.asPolyline() if len(line) < 2: self.iface.messageBar().pushWarning( "Warning", "Selected line has less than two points. Cannot use it.") self.__line_construct.reset() return self.__line_construct.active_line = line
def set_draw_aoi(self, aoi): # noinspection PyArgumentList transform = QgsCoordinateTransform( QgsProject.instance().crs(), QgsCoordinateReferenceSystem("EPSG:4326"), QgsProject.instance()) aoi_json = None if isinstance(aoi, QgsRectangle): aoi_geom = QgsGeometry().fromRect(aoi) self._aoi_box.setToGeometry(aoi_geom) aoi_geom.transform(transform) aoi_json = aoi_geom.asJson(precision=6) if isinstance(aoi, QgsGeometry): if aoi.isMultipart(): aoi = QgsGeometry.fromPolygonXY(aoi.asMultiPolygon()[0]) self._aoi_box.setToGeometry(aoi) # TODO: validate geom is less than 500 vertices aoi.transform(transform) aoi_json = aoi.asJson(precision=6) if aoi_json: self.leAOI.setText(aoi_json) # noinspection PyUnresolvedReferences self._show_message('AOI set to drawn figure') self.zoom_to_aoi() if self._cur_maptool is not None: # Restore previously used maptool self._canvas.setMapTool(self._cur_maptool) self._cur_maptool = None else: # Fallback to activating pan tool self._iface.actionPan().trigger() else: # noinspection PyUnresolvedReferences self._show_message('AOI unable to be set', level=Qgis.Warning, duration=10)
def QgsGeometrytoGeoJSON(geom: QgsGeometry) -> str: ret = geom.asJson() return ret