0

I have been working in a data set that uses EPSG:25832 - its coordinate system means points are defined as large numbers - an example coordinate in Denmark would be X: 547250 Y 6260848. When I use these large coordinates in my code, QGIS seems to get upset, and renders the line inconsistently (it dissapears at most zoom levels, or if the camera is in certain places). For an example of what I mean I will provide a couple screenshots.

The code I am using is as follows:

vl = QgsVectorLayer("MultiLineString", "temporary_points1", "memory")
pr = vl.dataProvider()
vl.startEditing()
vl.updateFields()
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPolylineXY([QgsPointXY(1, 1), QgsPointXY(546750, 
                                                              6260849)]))
pr.addFeatures([fet])
vl.commitChanges()
vl.updateExtents()
QgsProject.instance().addMapLayer(vl)

Here you can see I have added two lines both starting from the coordinate 1,1. One goes to 20,20, and the other to the point in Denmark I mentioned before. A picture of qgis showing two lines

Now here is a picture with the camera zoomed out slightly and nothing else changed A similar picture, with only one line visible The line going to the large coordinate dissapeared

QGIS Version is 3.20.1-Odense - the latest release as of the question being asked

Has anyone else encountered this and is there a way to fix this?

1 Answer 1

0

So it turns out it is possible to specify a CRS when the layer is created. While large coordinates still are erratic, this at least solved the issue because QGIS behind the scenes converts the layer to lat-long or something similar.

The amended code would be something like:

vl = QgsVectorLayer("MultiLineString?crs=EPSG:25832", "temporary_points1", "memory")
pr = vl.dataProvider()
vl.startEditing()
vl.updateFields()
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPolylineXY([QgsPointXY(1, 1), 
                                            QgsPointXY(546750, 6260849)]))
pr.addFeatures([fet])
vl.commitChanges()
vl.updateExtents()
QgsProject.instance().addMapLayer(vl)

For whatever reason this works inside of QGIS but not in a qgis standalone application, but thats a separate question

2
  • 2
    Its not converting the layer to lat-long - you didn't specify a CRS in your code so it assumes the coordinate numbers are lat-long (epsg 4326) and those large coordinates aren't valid lat-long numbers.
    – Spacedman
    Commented Aug 9, 2021 at 11:23
  • @Spacedman I meant that because it knows the transform now it is converting the coordinates to some smaller value (possibly lat-long) for display on the canvas
    – Rugnir
    Commented Aug 9, 2021 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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