4

I´m trying to build a polygon from a list of coordinates. I have the coordinates in different ways as:

pointsfloat
[(583899.8, 8177297.19), (583900.23, 8177262.52), (583896.49, 8177262.47), (583895.96, 8177297.29), (583899.8, 8177297.19)]

or

coordinates
[583899.8, 8177297.19, 583900.23, 8177262.52, 583896.49, 8177262.47, 583895.96, 8177297.29, 583899.8, 8177297.19]

When I try to convert it to a QgsPoint I always lose the decimals digits, it's converting the numbers to integer automatically.

How can I generate the polygon feature from those vertices?

1
  • 2
    Can you post the code that creates the polygon feature so we can evaluate it?
    – artwork21
    Commented Feb 20, 2017 at 0:55

1 Answer 1

5

QGIS v3.x

You can do it in this way:

coords = [(583899.8, 8177297.19), (583900.23, 8177262.52), (583896.49, 8177262.47), (583895.96, 8177297.29), (583899.8, 8177297.19)]   
polygon = QgsGeometry.fromPolygonXY( [[ QgsPointXY( pair[0], pair[1] ) for pair in coords ]] ) 

Now you have a polygon. To see its coordinates:

polygon.asWkt(2)
# Prints:
# 'Polygon ((583899.8 8177297.19, 583900.23 8177262.52, 583896.49 8177262.47, 583895.96 8177297.29, 583899.8 8177297.19))'

As you see, you aren't losing decimals.

If you want to quickly visualize that polygon, you could use the QuickWKT plugin and paste the WKT representation you just got.

QGIS v2.x

You can do it in this way:

coords = [(583899.8, 8177297.19), (583900.23, 8177262.52), (583896.49, 8177262.47), (583895.96, 8177297.29), (583899.8, 8177297.19)]   
polygon = QgsGeometry.fromPolygon( [[ QgsPoint( pair[0], pair[1] ) for pair in coords ]] ) 

Now you have a polygon. To see its coordinates:

polygon.exportToWkt(2)
# Prints:
# u'Polygon ((583899.8 8177297.19, 583900.23 8177262.52, 583896.49 8177262.47, 583895.96 8177297.29, 583899.8 8177297.19))'

As you see, you aren't losing decimals.

If you want to quickly visualize that polygon, you could use the QuickWKT plugin and paste the WKT representation you just got.

1
  • Thanks German!!! I have been trying things like QgsGeometry.fromPolygon([[QgsPoint(x1,y1),QgsPoint(x2,y2), QgsPoint(x3,y3),QgsPoint(x4,y4), QgsPoint(x1,y1)]]) or converting between types and i cant. Your solution is great. Regards
    – pacuga
    Commented Feb 20, 2017 at 15:16

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.