def create_memory_layer(layer, new_name=''): """Return a memory copy of a layer :param layer: QgsVectorLayer that shall be copied to memory. :type layer: QgsVectorLayer :param new_name: The name of the copied layer. :type new_name: str :returns: An in-memory copy of a layer. :rtype: QgsMapLayer """ if new_name is '': new_name = layer.name() + ' TMP' if layer.type() == QgsMapLayer.VectorLayer: vector_type = layer.geometryType() if vector_type == QGis.Point: type_string = 'Point' elif vector_type == QGis.Line: type_string = 'Line' elif vector_type == QGis.Polygon: type_string = 'Polygon' else: raise MemoryLayerCreationError('Layer is whether Point nor ' 'Line nor Polygon') else: raise MemoryLayerCreationError('Layer is not a VectorLayer') crs = layer.crs().authid().lower() uuid_string = str(uuid.uuid4()) uri = '%s?crs=%s&index=yes&uuid=%s' % (type_string, crs, uuid_string) memory_layer = QgsVectorLayer(uri, new_name, 'memory') memory_provider = memory_layer.dataProvider() provider = layer.dataProvider() vector_fields = provider.fields() fields = [] for i in vector_fields: fields.append(i) memory_provider.addAttributes(fields) for ft in provider.getFeatures(): memory_provider.addFeatures([ft]) return memory_layer
def create_memory_layer(layer_name, geometry, coordinate_reference_system=None, fields=None): """Create a vector memory layer. :param layer_name: The name of the layer. :type layer_name: str :param geometry: The geometry of the layer. :rtype geometry: QgsWkbTypes (note: from C++ QgsWkbTypes::GeometryType enum) :param coordinate_reference_system: The CRS of the memory layer. :type coordinate_reference_system: QgsCoordinateReferenceSystem :param fields: Fields of the vector layer. Default to None. :type fields: QgsFields :return: The memory layer. :rtype: QgsVectorLayer """ if geometry == QgsWkbTypes.PointGeometry: wkb_type = QgsWkbTypes.MultiPoint elif geometry == QgsWkbTypes.LineGeometry: wkb_type = QgsWkbTypes.MultiLineString elif geometry == QgsWkbTypes.PolygonGeometry: wkb_type = QgsWkbTypes.MultiPolygon elif geometry == QgsWkbTypes.NullGeometry: wkb_type = QgsWkbTypes.NoGeometry else: raise MemoryLayerCreationError( 'Layer geometry must be one of: Point, Line, ' 'Polygon or Null, I got %s' % geometry) if coordinate_reference_system is None: coordinate_reference_system = QgsCoordinateReferenceSystem() if fields is None: fields = QgsFields() elif not isinstance(fields, QgsFields): # fields is a list new_fields = QgsFields() for f in fields: new_fields.append(f) fields = new_fields memory_layer = QgsMemoryProviderUtils. \ createMemoryLayer(name=layer_name, fields=fields, geometryType=wkb_type, crs=coordinate_reference_system) memory_layer.dataProvider().createSpatialIndex() memory_layer.keywords = {'inasafe_fields': {}} return memory_layer
def create_memory_layer(layer_name, geometry, coordinate_reference_system=None, fields=None): """Create a vector memory layer. :param layer_name: The name of the layer. :type layer_name: str :param geometry: The geometry of the layer. :rtype geometry: QGis.WkbType :param coordinate_reference_system: The CRS of the memory layer. :type coordinate_reference_system: QgsCoordinateReferenceSystem :param fields: Fields of the vector layer. Default to None. :type fields: QgsFields :return: The memory layer. :rtype: QgsVectorLayer """ if geometry == QGis.Point: type_string = 'MultiPoint' elif geometry == QGis.Line: type_string = 'MultiLineString' elif geometry == QGis.Polygon: type_string = 'MultiPolygon' elif geometry == QGis.NoGeometry: type_string = 'none' else: raise MemoryLayerCreationError( 'Layer is whether Point nor Line nor Polygon, I got %s' % geometry) uri = '%s?index=yes&uuid=%s' % (type_string, str(uuid4())) if coordinate_reference_system: crs = coordinate_reference_system.authid().lower() uri += '&crs=%s' % crs memory_layer = QgsVectorLayer(uri, layer_name, 'memory') memory_layer.keywords = {'inasafe_fields': {}} if fields: data_provider = memory_layer.dataProvider() data_provider.addAttributes(fields) memory_layer.updateFields() return memory_layer