1

I'm creating a custom Qt app that uses pyqgis for showing data on maps. I have successfully created a map canvas that shows a world map from a WMS server.

Now I'd like to overlay data on this map. However, it seems that my vector layer interprets all coordinates in meters instead of lon/lat.

Here's more or less what I do to setup my data layer:

my_layer = QgsVectorLayer('Point', 'My Layer', 'memory')
my_layer.setCrs(QgsCoordinateReferenceSystem(4326))
pr = my_layer.dataProvider()
pr.addAttributes([QgsField('name', QVariant.String),
                  QgsField('value', QVariant.Double)])

# add a point at lon=7.8, lat=47.5
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(7.8, 47.5)))
feature.setAttributes(['some name', 0.0])
pr.addFeatures([feature])

# ... add layer to registry and canvas

The point shows up nicely but in the wrong location. How can I get qgis to interpret my coordinates in lon/lat?

  • 1
    Obviously you need to reproject your coordinates from 4326 to the CRS that is used in your project. – SS_Rebelious Dec 11 '14 at 16:49
  • I use the qgis libraries in my own custom app so there is no such thing as a project. – Lukas Dec 12 '14 at 9:25
0

Ok, I think I figured it out. Apparently the map canvas has its own CRS and on the fly projection for layers which use a different CRS is disabled by default.

Enabling OTF projection with

my_canvas.setCrsTransformEnabled(True)

did the trick. (my_canvas being the QgsMapCanvas in my Qt window).

my_canvas and my_layer actually both had the EPSG:4326 CRS set which I assume is the default. But my background layer used meters, so I think it was the background layer that was projected wrongly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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