6

I have a list of polygons' points like

[[x1,y1],[x2,y2],[x3,y3],[x4,y4]]   [[x5,y5],[x6,y6],[x7,y7].[x8,y8]]

now I want to create multipolygon from it by using

feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromMultiPolygon([polygon_points]))

but it returns an error:

TypeError: QgsGeometry.fromMultiPolygon(): argument 1 has unexpected type 'list'

So how to make it right type for QgsGeometry.fromMultiPolygon()?

or maybe I am wrong with something?

======================Newly Update======================================

following is my point list

Points_list

My code read this list by

read_polygon=open('shapes_point.txt','rb')
polygons=[]
polygon_lines=read_polygon.readlines()  #read all polygons
for line in polygon_lines:              #read per polygon in polygons
  for points in line:                   #read per point in polygon
        x=points[0]
        y=points[1]
        Qgs_point=QgsPoint(float(x),float(y))
        polygons.append(Qgs_point)
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromMultiPolygon((QgsGeometry.fromMultiPolygon([polygons for polygon in polygons]))))

and returns me the error above

=====================================================

I will try to make it more details

I want to read a shapefile in a dialog box, after some calculate progress, I will put values in each polygon features. The following is the main problem I have now!

I have the shapefile, and I want to fill it with color by values, but the method I only know is make a new polygon shapefile and append value to it

I don't know how to append the value in the selected polygon shapefile

So I use

feature = QgsFeature()
feature.setGeometry(QgsGeometry.Polygon([polygon_points]))

but it's always crash in multipolygons

so I want to know how to make multipolygons function need?

  • You can't parse the text file with that code: polygon_lines[0][0][0] is [, not the x coordinate of the first point, as you assume. I think it would be best if you could edit your question to explain what you're actually trying to achieve (it seems like you're trying to exchange polygon data between two programs, so maybe there's a more elegant way to do that than "manually" writing the file and reading it in QGIS). – Jake Oct 11 '13 at 7:13
  • I still don't understand your workflow. QGIS can read and display shapeless natively (that's basically what it's for). – Jake Oct 11 '13 at 7:30
  • @Jake I have edit my post please take a look~ thanks for help! – blueman010112 Oct 11 '13 at 8:54
  • Where does shapes_point.txt come from? – Jake Oct 11 '13 at 9:33
  • after select shapefile I record shpae[i].points into shapes_points.txt – blueman010112 Oct 11 '13 at 9:45
4

QgsGeometry.fromMultiPolygon() expects a list of polygons. A polygon, in turn, is a list of rings: An outer ring, and a number of inner rings. A ring, in turn, is a list of QgsPoint objects.

A valid MultiPolygon could thus look like this:

[ # The list of polygons
 [ # The list of rings for the first polygon
  [ # The list of QgsCoordinates
    QgsPoint(0,0), QgsPoint(1,0), QgsPoint(0,1)
  ]
 ],
 [ # The list of rings for the second polygon
  [ # The list of QgsCoordinates for the first polygon
    QgsPoint(2,2), QgsPoint(3,2), QgsPoint(2,3)
  ]
 ]
]

You can transform your list of coordinates into that structure using

[[[QgsPoint(point[0],point[1]) for point in polygon ] for polygon in polygon_points ]]

So to create the feature, you could use

feature.setGeometry(QgsGeometry.fromMultiPolygon([[[QgsPoint(point[0],point[1]) for point in polygon ] for polygon in polygon_points ]]))
  • I still have problem here, I use feature.setGeometry(QgsGeometry.fromMultiPolygon([polygons for polygon in polygons])) where polygons is a list of QgsPoints, but it's still give me same error before, so it's not a list of polygon? – blueman010112 Oct 11 '13 at 2:12
  • @blueman010112: You're using the list comprehension wrong: [polygons for polygon in polygons] just repeats the polygons list as many times as there are coordinates in that list. If polygons is already a list of QgsPoints, all you have to do is QgsGeometry.fromMultiPolygon([[polygons]]). I don't understand why you want to use a MultiPolygon for this, though, since you only seem to have one polygon. Maybe it's best if you edit your question with an example of your data. – Jake Oct 11 '13 at 5:59
  • I have add my pointlist and code in my post thanks for help! – blueman010112 Oct 11 '13 at 7:03

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.