I used a script from the PyQGISDeveloperCookbook and another StackExchange post to render an image from a specific coordinate. The problem I have is that I am unsure how to input the correct coordinates for a custom CRS. The CRS I am using in my project is ETRS89 / GK25FIN, And the coordinate I want to render is 25500381,6677153. When I attempt to render, I only output garbage similar to the result Leolo got in the previously mentioned post.

I have tried using a smaller number but the odds of me brute forcing the correct location is practically zero. Being a noob I am unsure what the given CRS actually means, and what coordinates it is supposed to use. I have tried using this site to translate the coordinates with no luck.

Here is the my slightly modified script from the previous post:

from PyQt4.QtGui import QImage, QPainter
from PyQt4.QtCore import QSize
import os

# radius for image in meters
radius = 512
# center point 
p = [25499771,6677153] # <- I don't know what to write here in this CRS
# bounding box
bbox = QgsRectangle(p[0]-radius, p[1]-radius, p[0]+radius, p[1]+radius)
# image
img = QImage(QSize(200,200), QImage.Format_ARGB32_Premultiplied)
## create painter
painter = QPainter()
render = QgsMapRenderer()
## set layer set
layers = QgsMapLayerRegistry.instance().mapLayers()
lst = layers.keys()

# Set destination CRS to match the CRS of the first layer
# Enable OTF reprojection

## set extent
## set output size
render.setOutputSize(img.size(), img.logicalDpiX())
## do the rendering
## save image
img.save("K:\\QgisOutput\\TestHeight.png", "png")
  • 1
    Have you tried the coordinates in X, Y order (East, North)? – AndreJ Jul 9 '17 at 11:49
  • I have tried it just now. With the same result; just static. The previous post mentioned that this is what you get when you render outside your layers. So the point is just somewhere in space. – Školstvo Jul 9 '17 at 11:58
  • I actually linked to that post in my question. This is a separate issue but it is related, as I have stated. Is this a bot? – Školstvo Jul 10 '17 at 7:39
  • @Školstvo I thought your issue was very close to the possible duplicate question (the code is identical and it was misleading). So, your issue was a problem with the loading of layers? The last part of your answer seems related to the other question. – mgri Jul 10 '17 at 7:43

Your code works for me in the Python console, with the first layer in GK25FIN, and the second an Openstreetmap background:

enter image description here

Note that the image takes the CRS of the first layer in the table of content, and there should be something to be seen in the map within 512 meters around the point.

  • I tried importing a OSM layer from the OpenLayers plugin and I got an a correct image out, but now the layers I wanted to actually render are in outer space. Thank you for testing this, but I think the underlying issue is that I still don't know what coordinates work with what CRS or why. I will try to experiment with this at work tomorrow, but I think the correct solution is just to re-import everything until the coordinates returned by the cursor coordinate display match real life. Some CRS just don't match with a Lat Lon coordinate system. I will edit this post tomorrow. – Školstvo Jul 9 '17 at 15:05
  • Try the QuickMapServices plugin instead of OpenLayers. I have put your coordinate in a text file, and added it as delimited text as the first layer, choosing GK25FIN, then added the OSM background, and set the project CRS to GK25FIN too. Maybe there is something wrong with the CRS of the layers you have. I think this is worth a new question, adding more details about them (extent etc). – AndreJ Jul 9 '17 at 15:31
  • Thank you for testing this for me. I answered my own question, since I was the only one that could know the issue since I caused it myself. – Školstvo Jul 10 '17 at 7:10
  • -you can answer the question if you wish, since you were so helpful. – Školstvo Jul 10 '17 at 7:16

The solution is to re-import the needed layers and make sure no layers are missing.

In my specific case the problem arose with the use of layers that weren't linked to their sources anymore. I created the project and used the script on two different computers, where in the latter case I had missing sources which probably messed up the data in some way. The coordinates visible in the tab from the mouse cursor location "should" always match to the ones written in the console or a script, unless something went wrong like in my case.

I hope someone finds this useful if they encounter the same issue.

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.