def paint_extent(self, rec): self.roi_x_max = rec.xMaximum() self.ui.XMaxLineEdit.setText(str(round(rec.xMaximum(), 3))) self.roi_y_min = rec.yMinimum() self.ui.YMinLineEdit.setText(str(round(rec.yMinimum(), 3))) self.roi_x_min = rec.xMinimum() self.ui.XMinLineEdit.setText(str(round(rec.xMinimum(), 3))) self.roi_y_max = rec.yMaximum() self.ui.YMaxLineEdit.setText(str(round(rec.yMaximum(), 3))) if self.extent: self.canvas.scene().removeItem(self.extent) self.extent = None self.extent = QgsRubberBand(self.canvas, True) points = [ QgsPoint(self.roi_x_max, self.roi_y_min), QgsPoint(self.roi_x_max, self.roi_y_max), QgsPoint(self.roi_x_min, self.roi_y_max), QgsPoint(self.roi_x_min, self.roi_y_min), QgsPoint(self.roi_x_max, self.roi_y_min) ] self.extent.setToGeometry(QgsGeometry.fromPolyline(points), None) self.extent.setColor(QColor(227, 26, 28, 255)) self.extent.setWidth(5) self.extent.setLineStyle(Qt.PenStyle(Qt.DashLine)) self.canvas.refresh()
def exit_dialog(self, points, crs): self.dialog.close() layer = QgsVectorLayer('LineString', self.name_tracado, "memory") pr = layer.dataProvider() print points anterior = QgsPoint(points[0]) fets = [] for p in points[1:]: fet = QgsFeature(layer.pendingFields()) fet.setGeometry(QgsGeometry.fromPolyline([anterior, QgsPoint(p)])) fets.append(fet) anterior = QgsPoint(p) pr.addFeatures(fets) self.crs = crs layer.setCrs(QgsCoordinateReferenceSystem(int(crs), 0)) layer.updateExtents() QgsMapLayerRegistry.instance().addMapLayer(layer)
def paint(self, p, *args): super(RubberBand, self).paint(p) # p.drawRect(self.boundingRect()) if not roam.config.settings.get("draw_distance", True): return offset = QPointF(5, 5) nodescount = self.numberOfVertices() for index in range(nodescount, -1, -1): if index == 0: return qgspoint = self.getPoint(0, index) qgspointbefore = self.getPoint(0, index - 1) # No point before means we are the first index and there is nothing # before us. if not qgspointbefore: return if qgspoint and qgspointbefore: distance = self.distancearea.measureLine( qgspoint, qgspointbefore) if int(distance) == 0: continue text = QgsDistanceArea.formatDistance(distance, 3, self.unit, keepBaseUnit=False) linegeom = QgsGeometry.fromPolyline( [QgsPoint(qgspoint), QgsPoint(qgspointbefore)]) midpoint = linegeom.centroid().asPoint() midpoint = self.toCanvasCoordinates(midpoint) - self.pos() midpoint += offset path = QPainterPath() path.addText(midpoint, self.font, text) p.setPen(self.blackpen) p.setRenderHints(QPainter.Antialiasing) p.setFont(self.font) p.setBrush(self.whitebrush) p.drawPath(path)
def create_line(self, p1, p2, name): layer = QgsVectorLayer('LineString?crs=%s' % int(self.crs), name, "memory") mycrs = QgsCoordinateReferenceSystem(int(self.crs), 0) self.reprojectgeographic = QgsCoordinateTransform( self.iface.mapCanvas().mapRenderer().destinationCrs(), mycrs) pr = layer.dataProvider() line = QgsFeature() line.setGeometry( QgsGeometry.fromPolyline([ self.reprojectgeographic.transform(p1), self.reprojectgeographic.transform(p2) ])) pr.addFeatures([line]) #layer.setCrs(QgsCoordinateReferenceSystem(int(self.crs), 0)) layer.updateExtents() QgsMapLayerRegistry.instance().addMapLayer(layer) return p1, p2
def paint_extent(self, rec): self.roi_x_max = rec.xMaximum() self.ui.XMaxLineEdit.setText(str(round(rec.xMaximum(), 3))) self.roi_y_min = rec.yMinimum() self.ui.YMinLineEdit.setText(str(round(rec.yMinimum(), 3))) self.roi_x_min = rec.xMinimum() self.ui.XMinLineEdit.setText(str(round(rec.xMinimum(), 3))) self.roi_y_max = rec.yMaximum() self.ui.YMaxLineEdit.setText(str(round(rec.yMaximum(), 3))) if self.extent: self.canvas.scene().removeItem(self.extent) self.extent = None self.extent = QgsRubberBand(self.canvas, True) points = [QgsPoint(self.roi_x_max, self.roi_y_min), QgsPoint(self.roi_x_max, self.roi_y_max), QgsPoint(self.roi_x_min, self.roi_y_max), QgsPoint(self.roi_x_min, self.roi_y_min), QgsPoint(self.roi_x_max, self.roi_y_min)] self.extent.setToGeometry(QgsGeometry.fromPolyline(points), None) self.extent.setColor(QColor(227, 26, 28, 255)) self.extent.setWidth(5) self.extent.setLineStyle(Qt.PenStyle(Qt.DashLine)) self.canvas.refresh()
def createPart(self, layer, ring): c = len(ring) curr = 1 pointLayer = self.getLayerByName(u'Точки') if pointLayer is None: idx = -1 else: idx = pointLayer.fieldNameIndex('name') cadastreLayer = self.getLayerByName(u'Кадастр') for point in ring: if curr < c: point1 = point point2 = ring[curr] isEqual = False pt1stst = False pt2stst = False curr += 1 #print point1, point2 line_geometry=QgsGeometry.fromPolyline([QgsPoint(point1.x(), point1.y()), QgsPoint(point2.x(), point2.y())]) # find point for pointfeature in pointLayer.getFeatures(): if pointfeature.geometry().equals(QgsGeometry.fromPoint(QgsPoint(point1.x(), point1.y()))): name = unicode(pointfeature.attribute(u'name')) if name[0] == u'н': pt1stst = True if pointfeature.geometry().equals(QgsGeometry.fromPoint(QgsPoint(point2.x(), point2.y()))): name = unicode(pointfeature.attribute(u'name')) if name[0] == u'н': pt2stst = True # check for identity features = layer.getFeatures() for f in features: if line_geometry.equals(f.geometry()): self.iface.messageBar().pushMessage(u'Найдена дублирующая часть границы, пропущена', level=QgsMessageBar.INFO) isEqual = True break #check for cadastre if not pt1stst and not pt2stst: findInCadastre = False print 'cadastre check' if cadastreLayer is not None and not findInCadastre: cadObjs = cadastreLayer.getFeatures() for cadObj in cadObjs: #print 'cadastre check iteration' if cadObj.geometry().isMultipart(): for cpoly in cadObj.geometry().asMultiPolygon(): if not findInCadastre: for cring in cpoly: cadCurr = 1 if not findInCadastre: cc = len(cring) for cpoint in cring: if cadCurr < cc: cpoint1 = cpoint cpoint2 = cring[cadCurr] cadCurr += 1 cadLine=QgsGeometry.fromPolyline([QgsPoint(cpoint1.x(), cpoint1.y()), QgsPoint(cpoint2.x(), cpoint2.y())]) #print 'checking line' if line_geometry.within(cadLine.buffer(0.000001, 16)): print 'find in cadastre' findInCadastre = True break else: findInCadastre = False else: if not findInCadastre: for cring in cadObj.geometry().asPolygon(): cadCurr = 1 if not findInCadastre: cc = len(cring) for cpoint in cring: if cadCurr < cc: cpoint1 = cpoint cpoint2 = cring[cadCurr] cadCurr += 1 cadLine=QgsGeometry.fromPolyline([QgsPoint(cpoint1.x(), cpoint1.y()), QgsPoint(cpoint2.x(), cpoint2.y())]) #print 'checking line' if line_geometry.within(cadLine.buffer(0.000001, 16)): print 'finded in cadastre' findInCadastre = True break else: findInCadastre = False if not findInCadastre: pt2stst = True if not isEqual: feat = QgsFeature() feat.setGeometry(line_geometry) typeidx = layer.fieldNameIndex('type') feat.initAttributes(2) if pt1stst or pt2stst: feat.setAttribute(typeidx, 2) else: feat.setAttribute(typeidx, 0) layer.dataProvider().addFeatures([feat])
def showCellFeature(self): # 清空元素选择 self.cellLayer.selectAll() self.cellLayer.invertSelection() self.scellLayer.selectAll() self.scellLayer.invertSelection() ncell_dict = self.getNCellList() # 提取所有相邻小区数据 '''变量声明''' showNCellList = [] showNCellIdDict = {} # key 为 NCell, value 为 NType showCellList = [] distanceList = {} t_switchTimes_list = [] s_switchTime_list = [] s_switchRate_list = [] count1 = 0 count2 = 0 '''清空涂色''' if len(self.HL_NCell_O) > 0: for h in self.HL_NCell_O: self.iface.mapCanvas().scene().removeItem(h) if len(self.HL_NCell_D) > 0: for h in self.HL_NCell_D: self.iface.mapCanvas().scene().removeItem(h) '''补全所选中的服务小区的相邻小区信息''' update_dict = {} # 要更新的features dict updateGemo_dict = {} # 要更新的features gemotry dict #featureUtils = FeatureUtils(u'相邻小区', self.iface) allNCells = self.scellLayer.getFeatures() for NCell in allNCells: feature_id = None # 需要更新的相邻小区的features_id if NCell['SCell'] == self.SCell: if NCell.geometry() == None: # 相邻小区的 geometry 不存在才更新信息 feature_id = NCell.id() scell_point = None # 服务小区经纬度 scell_siteId = None # 服务小区所属基站 ncell_point = None # 相邻小区经纬度 ncell_siteId = None # 相邻小区所属基站 allCells = self.cellLayer.getFeatures() for Cell in allCells: if Cell['id'] == NCell['SCell']: scell_point = QgsPoint(Cell[u"经度"], Cell[u"纬度"]) if Cell['id'] == NCell['NCell']: ncell_point = QgsPoint(Cell[u"经度"], Cell[u"纬度"]) if scell_point != None and ncell_point != None: break # 更新相邻小区信息 if feature_id != None and scell_point != None and ncell_point != None: features_value = {} # 需要更新的 features 的值得dict # 判断相邻小区类型 if not ncell_dict.has_key( (NCell['NCell'], NCell['SCell'])): features_value[self.scellLayer.fieldNameIndex( 'NType')] = 1 else: features_value[self.scellLayer.fieldNameIndex( 'NType')] = 2 # 计算距离 d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(scell_point, ncell_point), 2, 0, False)[0] features_value[self.scellLayer.fieldNameIndex( 'Distance')] = distance update_dict[feature_id] = features_value updateGemo_dict[feature_id] = QgsGeometry.fromPolyline( [scell_point, ncell_point]) if len(update_dict) > 0 and len(updateGemo_dict) > 0: modifyFeatures(self.scellLayer, update_dict) modifyFeaturesGeom(self.scellLayer, updateGemo_dict) else: pass '''获得相邻小区表中服务小区的相邻小区id,计算各类型相邻小区的数量,并保存其距离到 distanceList 中''' allNCells = self.scellLayer.getFeatures() for NCell in allNCells: if NCell['SCell'] == self.SCell: # 在相邻小区表中找出 SCell 为所设置的服务小区的项 showNCellIdDict[NCell['NCell']] = NCell['NType'] showNCellList.append(NCell.id()) if NCell['NType'] == '1': count1 = count1 + 1 if NCell['NType'] == '2': count2 = count2 + 1 distanceList[(NCell['SCell'], NCell['NCell'])] = NCell['Distance'] t_switchTimes_list.append(str(NCell['HOAttempt'])) s_switchTime_list.append(str(NCell['HOSucc'])) s_switchRate_list.append(str(NCell['HOSuccRate'])) else: pass '''在小区图层中选中相邻小区,并找到服务小区的名字''' allCells = self.cellLayer.getFeatures() for cell in allCells: for (NCell, NType) in showNCellIdDict.items(): if cell[u'RNC-BSC'] + '_' + (cell[u'基站ID']) + '_' + ( cell[u'小区ID']) == NCell: showCellList.append(cell.id()) # 涂色 self.iface.actionZoomFullExtent().trigger( ) # 涂色前将画布缩放到全图显示 if NType == u"1": # 单向小区涂粉色 h = QgsHighlight(self.iface.mapCanvas(), cell.geometry(), self.cellLayer) h.setFillColor(QColor('Pink')) self.HL_NCell_O.append(h) else: h = QgsHighlight(self.iface.mapCanvas(), cell.geometry(), self.cellLayer) h.setFillColor(QColor('Blue')) self.HL_NCell_D.append(h) if cell[u'RNC-BSC'] + '_' + (cell[u'基站ID']) + '_' + ( cell[u'小区ID']) == self.SCell: scell_name = cell[u'小区名'] '''获得相邻小区的名字,并显示服务小区与相邻小区的距离''' i = 0 allCells = self.cellLayer.getFeatures() info_list = [] for cell2 in allCells: for ncell, distance in distanceList.items(): if ncell[1] == (cell2[u'RNC-BSC'] + '_' + (cell2[u'基站ID']) + '_' + (cell2[u'小区ID'])): ncell_name = cell2[u"小区名"] try: # 显示服务小区与相邻小区的相关信息 info = unicode(scell_name) + u' <----> ' + unicode( ncell_name) + u'之间的距离为: ' + unicode( str(distance)) + u'米' info_list.append(info) i = i + 1 except UnicodeEncodeError: pass '''显示相邻小区''' if len(showCellList) == 0: return False else: self.cellLayer.setSelectedFeatures(showCellList) self.scellLayer.setSelectedFeatures(showNCellList) self.iface.actionZoomToSelected().trigger() self.scellwin = SCellInfoUI(info_list, scell_name, count1, count2) self.scellwin.show() self.scellwin.exec_() return (scell_name, count1, count2)
def addCellFeature2(self): if self.__dataprovider.capabilities( ) & QgsVectorDataProvider.AddFeatures: cellfeatures = [] ncells = {} ncell_dict = self.getNCellList() allCells = self.cellLayer.getFeatures() update_dict = {} # 要更新的features dict updateGemo_dict = {} # 要更新的features gemotry dict '''获得该小区的相邻小区数''' count = {} count = self.countCell(self.SCell, count) '''获得服务小区的SiteId''' scell = self.getCell() scell_SiteId = (scell[u"基站ID"]) scell_point = QgsPoint(scell[u'经度'], scell[u'纬度']) '''添加小区到相邻小区表中''' for ncell in self.selections: ncell_id = ncell[u'RNC-BSC'] + '_' + (ncell[u"基站ID"]) + '_' + ( ncell[u"小区ID"]) ncells[ncell_id] = str(ncell[u"基站ID"]) '''计算ncell的相邻小区个数''' count = self.countCell(ncell_id, count) '''若服务小区与选中的小区重叠,返回 1 ''' if ncell_id == self.SCell: return 1 if not ncell_dict.has_key((self.SCell, ncell_id)): '''若相邻小区表中不存在要添加的相邻小区信息,则添加''' ncell_point = QgsPoint(ncell[u'经度'], ncell[u'纬度']) if (scell_point is not None) and (ncell_point is not None): d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(scell_point, ncell_point), 2, 0, False)[0] cellfaeture = QgsFeature() cellfaeture.initAttributes(8) cellfaeture.setAttribute(0, str(uuid4()).replace( '-', '')) # 生成唯一标识id cellfaeture.setAttribute(1, self.SCell) cellfaeture.setAttribute(2, ncell_id) # 把 NType 置为 2 (双向) cellfaeture.setAttribute(3, '2') if distance >= 0 and (distance is not None): cellfaeture.setAttribute(7, distance) cellfaeture.setGeometry( QgsGeometry.fromPolyline( [scell_point, ncell_point])) cellfeatures.append(cellfaeture) if count.has_key(self.SCell): count[self.SCell] = count[self.SCell] + 1 else: count[self.SCell] = 1 else: # 若已存在则更新信息 update_data_id = ncell_dict[(self.SCell, ncell_id)] update_data_value = {} # 需要更新的 features 的值得dict # 计算距离 ncell_point = QgsPoint(ncell[u'经度'], ncell[u'纬度']) if (scell_point is not None) and (ncell_point is not None): d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(scell_point, ncell_point), 2, 0, False)[0] update_data_value[self.scellLayer.fieldNameIndex( 'Distance')] = distance # NType 设为 2(双向) update_data_value[self.scellLayer.fieldNameIndex( 'NType')] = 2 update_dict[update_data_id] = update_data_value updateGemo_dict[update_data_id] = QgsGeometry.fromPolyline( [scell_point, ncell_point]) '''判断原先是否存在对称相邻小区''' if not ncell_dict.has_key((ncell_id, self.SCell)): # 如果不存在则添加对称相邻小区信息 ncell_point = QgsPoint(ncell[u'经度'], ncell[u'纬度']) if (scell_point is not None) and (ncell_point is not None): d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(ncell_point, scell_point), 2, 0, False)[0] cellfaeture = QgsFeature() cellfaeture.initAttributes(8) cellfaeture.setAttribute(0, str(uuid4()).replace( '-', '')) # 生成唯一标识id cellfaeture.setAttribute(1, ncell_id) cellfaeture.setAttribute(2, self.SCell) # NType 置为 2 (双向) cellfaeture.setAttribute(3, '2') if distance >= 0 and (distance is not None): cellfaeture.setAttribute(7, distance) cellfaeture.setGeometry( QgsGeometry.fromPolyline( [ncell_point, scell_point])) cellfeatures.append(cellfaeture) '''再次计算服务小区的相邻小区数''' if count.has_key(ncell_id): count[ncell_id] = count[ncell_id] + 1 else: count[ncell_id] = 1 else: # 若已存在则对称小区的更新信息 # 把要更新的对称小区的 NType 信息加入到 update_dict 中 update_data_id = ncell_dict[(ncell_id, self.SCell)] update_data_value = {} # 需要更新的 features 的值得dict # 计算距离 ncell_point = QgsPoint(ncell[u'经度'], ncell[u'纬度']) if (scell_point is not None) and (ncell_point is not None): d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(scell_point, ncell_point), 2, 0, False)[0] update_data_value[self.scellLayer.fieldNameIndex( 'Distance')] = distance # NType 设为 2 (双向) update_data_value[self.scellLayer.fieldNameIndex( 'NType')] = 2 update_dict[update_data_id] = update_data_value '''若服务小区的相邻小区数大于31则不添加,并返回 2 ''' if count[self.SCell] > 31: return 2 if count[ncell_id] > 31: return 3 '''添加''' self.__dataprovider.addFeatures(cellfeatures) # 更新相邻小区表内数据 if len(update_dict) > 0: modifyFeatures(self.scellLayer, update_dict) if len(updateGemo_dict) > 0: modifyFeaturesGeom(self.scellLayer, updateGemo_dict) return 0
def addCellFeature1(self): if self.__dataprovider.capabilities( ) & QgsVectorDataProvider.AddFeatures: cellfeatures = [] ncells = {} ncell_dict = self.getNCellList() allCells = self.cellLayer.getFeatures() update_dict = {} # 要更新的features dict updateGemo_dict = {} # 要更新的features gemotry dict '''获得该小区的相邻小区数''' count = {} count = self.countCell(self.SCell, count) '''获得服务小区的基站ID''' scell = self.getCell() scell_SiteId = (scell[u'基站ID']) scell_point = QgsPoint(scell[u"经度"], scell[u"纬度"]) '''添加小区到相邻小区表中''' for ncell in self.selections: ncell_id = ncell[u'RNC-BSC'] + '_' + (ncell[u'基站ID']) + '_' + ( ncell[u'小区ID']) ncells[ncell_id] = str(ncell[u'基站ID']) '''若服务小区与选中的小区重叠,返回 1 ''' if ncell_id == self.SCell: return 1 '''检查相邻小区表中是否已存在要添加的相邻小区信息,若否则添加''' if not ncell_dict.has_key((self.SCell, ncell_id)): ncell_point = QgsPoint(ncell[u"经度"], ncell[u"纬度"]) if (scell_point is not None) and (ncell_point is not None): d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(scell_point, ncell_point), 2, 0, False)[0] cellfaeture = QgsFeature() cellfaeture.initAttributes(8) cellfaeture.setAttribute(0, str(uuid4()).replace( '-', '')) # 生成唯一标识id cellfaeture.setAttribute(1, self.SCell) cellfaeture.setAttribute(2, ncell_id) # 判断是否存在对称相邻小区 if ncell_dict.has_key((ncell_id, self.SCell)): # 如果存在对称小区,则新添加的小区为双向邻区 cellfaeture.setAttribute(3, '2') # 把要更新的对称小区的 NType 信息加入到 update_dict 中 update_data_id = ncell_dict[(ncell_id, self.SCell)] update_data_value = { self.scellLayer.fieldNameIndex('NType'): 2 } update_dict[update_data_id] = update_data_value else: # 如是单向相邻小区 NType 置为 1 cellfaeture.setAttribute(3, '1') if distance >= 0 and (distance is not None): cellfaeture.setAttribute(7, distance) cellfaeture.setGeometry( QgsGeometry.fromPolyline( [scell_point, ncell_point])) cellfeatures.append(cellfaeture) '''再次计算服务小区的相邻小区数''' if count.has_key(self.SCell): count[self.SCell] = count[self.SCell] + 1 else: count[self.SCell] = 1 else: # 若已存在则更新信息 update_data_id = ncell_dict[(self.SCell, ncell_id)] update_data_value = {} # 需要更新的 features 的值得dict # 计算距离 ncell_point = QgsPoint(ncell[u"经度"], ncell[u"纬度"]) if (scell_point is not None) and (ncell_point is not None): d = QgsDistanceArea() distance = d.convertMeasurement( d.measureLine(scell_point, ncell_point), 2, 0, False)[0] update_data_value[self.scellLayer.fieldNameIndex( 'Distance')] = distance # 判断是否存在对称相邻小区 if ncell_dict.has_key((ncell_id, self.SCell)): # 若存在对称小区则 NType 设为 2 update_data_value[self.scellLayer.fieldNameIndex( 'NType')] = 2 else: # 反之,NType 设为 1 update_data_value[self.scellLayer.fieldNameIndex( 'NType')] = 1 update_dict[update_data_id] = update_data_value updateGemo_dict[update_data_id] = QgsGeometry.fromPolyline( [scell_point, ncell_point]) '''若服务小区的相邻小区数大于31则不添加,并返回 2 ''' if count[self.SCell] > 31: return 2 '''添加''' self.__dataprovider.addFeatures(cellfeatures) # 更新相邻小区表内数据 if len(update_dict) > 0: modifyFeatures(self.scellLayer, update_dict) if len(updateGemo_dict) > 0: modifyFeaturesGeom(self.scellLayer, updateGemo_dict) return 0 else: # 图层不能够添加新的Feature,提示错误消息给用户 self.iface.messageBar().pushMessage(u'错误', u'添加相邻小区失败', QgsMessageBar.CRITICAL, 3)
def split_line(self, p1, p2, pos, pipeline): geo = QgsGeometry.fromPolyline([p1, p2]) center = geo.centroid() pipeline.insert(pos, center.asPoint())