QgsGeometry.length() giving wrong result?Is it possible to programmatically add calculated fields?Calculate...

Is "stainless" a bulk or a surface property of stainless steel?

Levenshtein Neighbours

Unsolved Problems due to Lack of Computational Power

What happened after the end of the Truman Show?

Why don't sharp and flat root note chords seem to be present in much guitar music?

How do slats reduce stall speed?

Why should I pay for an SSL certificate?

Repurpose telephone line to ethernet

Could sidesticks be linked?

Have only girls been born for a long time in this village?

Why didn’t Doctor Strange stay in the original winning timeline?

Is it allowable to use an organization's name to publish a paper in a conference, even after I graduate from it?

insert several equation in one frame in beamer

Are there any legal requirements concerning airline pilots and their watches?

How to avoid using System.String with Rfc2898DeriveBytes in C#

Does the Green Flame-Blade cantrip work with the Zephyr Strike spell?

Why doesn't the Falcon-9 first stage use three legs to land?

Are there categories whose internal hom is somewhat 'exotic'?

How to translate 脑袋短路 into English?

Nuclear decay triggers

Can I submit a paper to a computer science conference using an alias if using my real name can cause legal trouble in my original country

Use of vor in this sentence

In xXx, is Xander Cage's 10th vehicle a specific reference to another franchise?

How could Tony Stark wield the Infinity Nano Gauntlet - at all?



QgsGeometry.length() giving wrong result?


Is it possible to programmatically add calculated fields?Calculate line lengths with Python in QGISWhy can't I multiply values from column named 'length' using field calculator via PyQGIS?Why would having a reference string “boh@10” cause a crash?new vector layer's field based on other layers attributes - “for”cycle errorCalculate Expression with Python?how to get first and last point of MultiLineString-objects in qgis3Adding new field with expression in pyqgis






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







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

































    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





























      3












      3








      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
















      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







      pyqgis qgis-3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      PolyGeo

      54.9k17 gold badges86 silver badges258 bronze badges




      54.9k17 gold badges86 silver badges258 bronze badges










      asked Aug 16 at 10:53









      lueholueho

      1189 bronze badges




      1189 bronze badges

























          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

































            0














            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
              Aug 16 at 13:19











            • Maybe try it...

              – pnz
              23 hours ago














            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "79"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f332179%2fqgsgeometry-length-giving-wrong-result%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            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






























              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




























                3












                3








                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













                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












                share|improve this answer



                share|improve this answer










                answered Aug 16 at 11:05









                bugmenot123bugmenot123

                4,45214 silver badges43 bronze badges




                4,45214 silver badges43 bronze badges




























                    0














                    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
                      Aug 16 at 13:19











                    • Maybe try it...

                      – pnz
                      23 hours ago
















                    0














                    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
                      Aug 16 at 13:19











                    • Maybe try it...

                      – pnz
                      23 hours ago














                    0












                    0








                    0







                    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













                    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












                    share|improve this answer



                    share|improve this answer










                    answered Aug 16 at 11:17









                    pnzpnz

                    9064 silver badges17 bronze badges




                    9064 silver badges17 bronze badges
















                    • I sure hope that QGIS provides functions for calculating "true" lengths without having to reproject manually.

                      – bugmenot123
                      Aug 16 at 13:19











                    • Maybe try it...

                      – pnz
                      23 hours ago



















                    • I sure hope that QGIS provides functions for calculating "true" lengths without having to reproject manually.

                      – bugmenot123
                      Aug 16 at 13:19











                    • Maybe try it...

                      – pnz
                      23 hours ago

















                    I sure hope that QGIS provides functions for calculating "true" lengths without having to reproject manually.

                    – bugmenot123
                    Aug 16 at 13:19





                    I sure hope that QGIS provides functions for calculating "true" lengths without having to reproject manually.

                    – bugmenot123
                    Aug 16 at 13:19













                    Maybe try it...

                    – pnz
                    23 hours ago





                    Maybe try it...

                    – pnz
                    23 hours ago


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f332179%2fqgsgeometry-length-giving-wrong-result%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m