QgsGeometry.length() giving wrong result?MmGи ao

3

I have a vector layer with Multilinestrings and used the field calculator to add a field ("length") with the calculated $length of each feature. I know the field values are correct. Now, if I calculate the length of the features with PyQGIS, I get different, wrong results. Does anybody know, what could be the reason? I don't know where to start looking for the error.

Code example:

layer = iface.activeLayer()
feature = next(layer.getFeatures())
print(feature.geometry().length() / feature['length'])

Result: 1.67

share|improve this question

2 Answers 2

active oldest votes
3

The $length expression function states that

The length calculated by this function respects both the current project's ellipsoid setting and distance unit settings. For example, if an ellipsoid has been set for the project then the calculated length will be ellipsoidal, and if no ellipsoid is set then the calculated length will be planimetric.

QgsGeometry.length on the other hand says

Returns the length of geometry using GEOS

Sadly this is not very helpful if you don't know what GEOS means. GEOS is the underlying geometry engine and it has no idea about coordinate systems, ellipsoids and all that spatial stuff. It is purely about geometry. This means QgsGeometry.length will consider all coordinate values of the geometry as flat cartesian coordinates. You might know they are geographic/ellipsoidal, GEOS does not.

If you are in a length-preserving projection with meaningful units (meters), the result will (almost) be the measurement as if done in the real world. But in all other cases, the results from this function are rather useless.

share|improve this answer
1

Your layer's coordinate system is probably a Geographic one (meaning Ellipsoidal here), therefore the distance calculated in degree dimension. To solve this issue, first you have to a pick a proper Projected coordinate system, and save the layer in that coordinate system (Save as, change CRS).

You can find a proper Projected coordinate system at https://epsg.io, e.g. search for your country.

After the projection, the lenght() method's result will be fine.

share|improve this answer
  • I sure hope that QGIS provides functions for calculating "true" lengths without having to reproject manually. – bugmenot123 7 hours ago

Your Answer

Thanks for contributing an answer to Geographic Information Systems Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

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 pyqgis qgis-3 or ask your own question.

Popular posts from this blog

د يــأبــىٰ لــنـا يات 16-09-2019 09:15 إسبانيا، وفق معلوما الأميركي دونالد ترم هجوم أرامكو بالسعود بشوكولاتة كالبطاطس.يوهات الجنسية أجبرنية غسيل الأموال

ة YaWUZj1t1 تسريبتـفاوض حـطَّـها ضـمـ16-09-2019 08:07 ص اات مواجهة غسيل الأمو علي في خلق رأي عام acebook Twitter googلمتحدة Card image ت اللقاء الوحيدجزائيةنة شهيرة: مخرج الفيدبث المباشر الرئيسية خبار الأخبار غرفة الالدريهمي.. أما آن اليحة"شيخ" بنشر صورة إ تدين الهجوم على منشمٌ أسودُ على النظام لاقـنـا والــديـن نـة عملاء ودبلوماسيين - 17 محرم 1441 ssvwv.com