Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I define the CRS for a memory layer from the QGIS project settings?

    vpoi = QgsVectorLayer("Point", "vectpoi", "memory")
    feature = QgsFeature()
    provider = vpoly.dataProvider()

And how can I define the CRS from another loaded layer?

share|improve this question
up vote 4 down vote accepted

The API docs state

Memory data providerType (memory)

The memory data provider is used to construct in memory data, for example scratch data or data generated from spatial operations such as contouring. There is no inherent persistent storage of the data. The data source uri is constructed. The url specifies the geometry type ("point", "linestring", "polygon", "multipoint","multilinestring","multipolygon"), optionally followed by url parameters as follows:

crs=definition Defines the coordinate reference system to use for the layer. definition is any string accepted by QgsCoordinateReferenceSystem::createFromString()

index=yes Specifies that the layer will be constructed with a spatial index

field=name:type(length,precision) Defines an attribute of the layer. Multiple field parameters can be added to the data provider definition. type is one of "integer", "double", "string".

An example url is "Point?crs=epsg:4326&field=id:integer&field=name:string(20)&index=yes"

So I guess it's

crs = # could also use .authid() instead of .toWkt()
vpoi = QgsVectorLayer("Point?crs=" + crs, "vectpoi", "memory")
feature = QgsFeature()
provider = vpoly.dataProvider()
share|improve this answer
Thanks a lot Matthias, this works well! – dassau Aug 30 '13 at 8:11
BTW. I also tested with QGIS 1.8 but that doesn't seem to work. e.g. vpoi = QgsVectorLayer("Point?crs=epsg:4326", "vectpoi", "memory") seems to be unrecognized. Although it is described here: – dassau Aug 30 '13 at 9:37
Passing the CRS as WKT may not be safe if the definition includes an ampersand (e.g. EPSG:32161). It's a fairly niche case, but for this reason I'd say it is preferable to pass the .authid() instead. However, the memory provider doesn't seem to recognise authids from user defined projections (i.e. those with a USER: prefix). – Snorfalorpagus Oct 31 '13 at 13:53

An alternate approach to that suggested by Matthias is to create the memory layer, then set the layer projection afterwards.

vpoi = QgsVectorLayer("Point?crs=EPSG:4326", "vectpoi", "memory") # create memory layer in WGS84
vpoi.setCrs( # change the coordinate reference system

If you don't specify any CRS initially (i.e. no "crs=") QGIS displays a warning to the user. This code creates a new memory layer using the WGS84 CRS, then changes the it to match the CRS from votherpoly. This avoids passing the CRS through the memory layer URI, which can have issues with some coordinate systems (such as those with an '&' in the name).

share|improve this answer

This works well for me

vl = QgsVectorLayer()
crs =

Edit: crs() method of vector layer return "QgsCoordinateReferenceSystem" (you can use alternatively class constructor like this


QgsCoordinateReferenceSystem has method to create crs from Id (alternatively you can use methods for create crs from postgis database, or some definitions of crs like wkt, or proj4 ( On last line I set creatd SRS to my layer.

share|improve this answer
Welcome to gis.stackexchange! Please note that a good question is expected to provide a bit more detail than just code lines. Please explain your approach. – underdark Dec 6 '15 at 17:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.