Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

how can I define the CRS for a memory layer from the qgis project settings

    vpoi = QgsVectorLayer("Point", "vectpoi", "memory")
    feature = QgsFeature()
    feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
    provider = vpoly.dataProvider()
    ...

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

thanks a lot

share|improve this question
add comment

2 Answers

up vote 2 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 = votherpoly.crs().toWkt() # could also use .authid() instead of .toWkt()
vpoi = QgsVectorLayer("Point?crs=" + crs, "vectpoi", "memory")
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
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: qgis.org/pyqgis-cookbook/vector.html#memory-provider. –  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
add comment

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(votherpoly.crs()) # 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
add comment

Your Answer

 
discard

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.