def createASCellFeature(attrs): feature = QgsFeature() feature.initAttributes(len(attrs) + 1) feature.setAttribute(0, str(uuid4()).replace('-', '')) for (index, attr) in enumerate(attrs): feature.setAttribute(index + 1, attr) return feature
def createABasicPointFeature(point, attrs): feature = QgsFeature() feature.initAttributes(len(attrs)) for (index, attr) in enumerate(attrs): feature.setAttribute(index, attr) geom = QgsGeometry.fromPoint(point) feature.setGeometry(geom) return feature
def createASiteFeature(point, attrs): feature = QgsFeature() feature.initAttributes(len(attrs) + 1) for (index, attr) in enumerate(attrs): feature.setAttribute(index + 1, attr) # 如果是小区要素,id为(RNC-BSC_SiteId_CellId) feature.setAttribute(0, str(uuid4()).replace('-', '')) geom = QgsGeometry.fromPoint(point) feature.setGeometry(geom) return feature
def createACellFeature(point, attrs): feature = QgsFeature() feature.initAttributes(len(attrs) + 1) for (index, attr) in enumerate(attrs): feature.setAttribute(index + 1, attr) # 如果是小区要素,id为(RNC-BSC_SiteId_CellId) feature.setAttribute( 0, str(attrs[6]) + '_' + str(attrs[0]) + '_' + str(attrs[2])) geom = QgsGeometry.fromPolygon([point]) feature.setGeometry(geom) return feature
def addPlaceMark(self, pos, name, description, category, timestamp): ''' adds a point to the layer :param pos: lat/lon position of the placemark :type pos: QgsPoint :param name: name of the placemark :type name: string :param description: extended text for the placemark :type description: string :param category: category to define the :type category: string :param timestamp: creation time of the placemark :type timestamp: string ''' if self.hasLayer: feat = QgsFeature(self.layer.pendingFields()) feat.setAttribute('name', name) feat.setAttribute('description', description) feat.setAttribute('class', category) feat.setAttribute('timestamp', timestamp) feat.setGeometry(QgsGeometry.fromPoint(pos)) (res, _) = self.layer.dataProvider().addFeatures([feat]) if res: self.layer.updateExtents() return res return False
def init_state(self): dirname = os.path.dirname(__file__) layer = QgsProject.instance().mapLayersByName('copy_energy_plant')[0] if layer.featureCount() < NUMB_ENERGY_PLANT: filename = os.path.join(dirname, 'dataset/global_power_plant_database.csv') layer.startEditing() with open(filename, 'r') as file: reader = csv.reader(file) for i, row in enumerate(reader): if i > 0 and row[7] == "Nuclear": pr = layer.dataProvider() # insert in attribute table poly = QgsFeature(layer.fields()) poly.setAttribute("Country", row[0]) poly.setAttribute("count_long", row[1]) poly.setAttribute("name", row[2]) poly.setAttribute("qppd_idnr", row[3]) poly.setAttribute("cap_mw", row[4]) poly.setAttribute("latitude", row[5]) poly.setAttribute("longitude", row[6]) poly.setAttribute("Radiation", random.randint(1, 200)) poly.setGeometry( QgsGeometry.fromPointXY( QgsPointXY(float(row[6]), float(row[5])))) pr.addFeatures([poly]) layer.updateExtents() layer.commitChanges() layer.reload()
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)