1

I'm trying to produce a multipolygon feature in QGIS. I am producing filled contours with Matplotlib, taking the multipolygons representing each contour level, using shapely to produce WKT, and then trying to add geometry to my QGIS features with QgsGeometry.fromWKT. Even though shapely produces a valid geometry, and the WKT can be read by online tools such as Wicket, QgsGeometry.fromWKT returns None (indicating that there's some issue with my input).

Here's a code sample:

# shapely.geometry.multipolygon.MultiPolygon()
p = MultiPolygon(polygon)
# shapely.wkt.dumps
WKT = dumps(p)
if not p.is_valid: # p is always valid
    raise Exception("There is an invalid multipolygon")
QGSGeom = QgsGeometry.fromWkt(WKT) # Always returns None
feat.setGeometry(QGSGeom)

Exception:

TypeError: QgsFeature.setGeometry(QgsGeometry): argument 1 has unexpected type 'NoneType'

The first multipolygon produced in my code is below. You can paste that into Wicket and it is read correctly. The only problem I see with it is that it has duplicate sequential vertices. However if that were a problem, I'd imagine that the call to is_valid would have flagged that.

MULTIPOLYGON(((174.4422222222213 -38.5,174.49111111111017 -38.5,174.53999999999905 -38.5,174.53999999999905 -38.46611111111111,174.53999999999905 -38.43222222222222,174.53999999999905 -38.39833333333333,174.53999999999905 -38.364444444444445,174.53999999999905 -38.330555555555556,174.53999999999905 -38.29666666666667,174.58888888888794 -38.29666666666667,174.63777777777682 -38.29666666666667,174.63777777777682 -38.26277777777778,174.63777777777682 -38.22888888888889,174.63777777777682 -38.195,174.63777777777682 -38.16111111111111,174.63777777777682 -38.12722222222222,174.63777777777682 -38.093333333333334,174.63777777777682 -38.059444444444445,174.6866666666657 -38.059444444444445,174.6866666666657 -38.025555555555556,174.6866666666657 -37.99166666666667,174.6866666666657 -37.95777777777778,174.6866666666657 -37.92388888888889,174.6866666666657 -37.89,174.6866666666657 -37.85611111111111,174.73555555555458 -37.85611111111111,174.73555555555458 -37.82222222222222,174.6866666666657 -37.82222222222222,174.6866666666657 -37.788333333333334,174.6866666666657 -37.754444444444445,174.6866666666657 -37.720555555555556,174.6866666666657 -37.68666666666667,174.73555555555458 -37.68666666666667,174.73555555555458 -37.65277777777778,174.6866666666657 -37.65277777777778,174.6866666666657 -37.61888888888889,174.63777777777682 -37.61888888888889,174.63777777777682 -37.585,174.63777777777682 -37.55111111111111,174.63777777777682 -37.51722222222222,174.63777777777682 -37.483333333333334,174.63777777777682 -37.449444444444445,174.63777777777682 -37.41555555555556,174.63777777777682 -37.38166666666667,174.58888888888794 -37.38166666666667,174.58888888888794 -37.34777777777778,174.53999999999905 -37.34777777777778,174.53999999999905 -37.31388888888889,174.53999999999905 -37.28,174.53999999999905 -37.24611111111111,174.53999999999905 -37.21222222222222,174.49111111111017 -37.21222222222222,174.49111111111017 -37.178333333333335,174.4422222222213 -37.178333333333335,174.4422222222213 -37.144444444444446,174.4422222222213 -37.11055555555556,174.3933333333324 -37.11055555555556,174.3933333333324 -37.08084030041046,174.3918541180993 -37.07666666666667,174.3933333333324 -37.073563748483664,174.3933333333324 -37.04277777777778,174.3933333333324 -37.00888888888889,174.3933333333324 -36.975,174.3933333333324 -36.94111111111111,174.3933333333324 -36.907222222222224,174.3933333333324 -36.873333333333335,174.34444444444352 -36.873333333333335,174.29555555555464 -36.873333333333335,174.29555555555464 -36.839444444444446,174.29555555555464 -36.80555555555556,174.29555555555464 -36.77166666666667,174.24666666666576 -36.77166666666667,174.19777777777688 -36.77166666666667,174.19777777777688 -36.73777777777778,174.19777777777688 -36.70388888888889,174.19777777777688 -36.67,174.19777777777688 -36.63611111111111,174.19777777777688 -36.602222222222224,174.148888888888 -36.602222222222224,174.0999999999991 -36.602222222222224,174.07638009736746 -36.602222222222224,174.0783635985048 -36.63611111111111,174.08313907435038 -36.67,174.0945282251905 -36.70388888888889,174.0999999999991 -36.73597729247714,174.10119541117496 -36.73777777777778,174.13328583421037 -36.77166666666667,174.14374543646616 -36.80555555555556,174.148888888888 -36.83168881985557,174.15232756532507 -36.839444444444446,174.17476842624663 -36.873333333333335,174.1847075954181 -36.907222222222224,174.1956525713804 -36.94111111111111,174.19777777777688 -36.948316948136956,174.21202794606387 -36.975,174.22200422378174 -37.00888888888889,174.23281078508953 -37.04277777777778,174.2362527105547 -37.07666666666667,174.2443993175849 -37.11055555555556,174.24666666666576 -37.126506253934686,174.2553837712646 -37.144444444444446,174.26893886132717 -37.178333333333335,174.28608811503204 -37.21222222222222,174.29521386071525 -37.24611111111111,174.29555555555464 -37.24707296361483,174.30981910302373 -37.28,174.31985523780398 -37.31388888888889,174.32704467341563 -37.34777777777778,174.33320266579798 -37.38166666666667,174.33705269711575 -37.41555555555556,174.34043259488126 -37.449444444444445,174.34239999579125 -37.483333333333334,174.3434139886748 -37.51722222222222,174.34369589543317 -37.55111111111111,174.34277412563725 -37.585,174.34134753776272 -37.61888888888889,174.33842570444313 -37.65277777777778,174.33548194519963 -37.68666666666667,174.33168761737662 -37.720555555555556,174.3261546294196 -37.754444444444445,174.3201397488093 -37.788333333333334,174.30945948456915 -37.82222222222222,174.29582956616 -37.85611111111111,174.29555555555464 -37.8566856256242,174.2836244645519 -37.89,174.25137332154347 -37.92388888888889,174.24666666666576 -37.939789917181415,174.24568237633656 -37.95777777777778,174.23578037906807 -37.99166666666667,174.23094523800063 -38.025555555555556,174.21531880811364 -38.059444444444445,174.19777777777688 -38.07484898967532,174.1875235154153 -38.093333333333334,174.17925432310602 -38.12722222222222,174.148888888888 -38.153196039233286,174.14095602352862 -38.16111111111111,174.1252321775911 -38.195,174.0999999999991 -38.21365265825294,174.09474385546508 -38.22888888888889,174.05203184125423 -38.26277777777778,174.0999999999991 -38.277854908742455,174.1201063670163 -38.26277777777778,174.13390610093055 -38.22888888888889,174.148888888888 -38.219468649326835,174.19777777777688 -38.20859612970229,174.21995835962346 -38.195,174.23476922587 -38.16111111111111,174.24666666666576 -38.149179190442055,174.261704248229 -38.16111111111111,174.26281661298975 -38.195,174.26330478448233 -38.22888888888889,174.25832904507158 -38.26277777777778,174.24666666666576 -38.28860574460334,174.24032248600773 -38.29666666666667,174.2247539205459 -38.330555555555556,174.20611957106544 -38.364444444444445,174.19777777777688 -38.38089181715259,174.1882329880123 -38.39833333333333,174.1723973349145 -38.43222222222222,174.1581399702732 -38.46611111111111,174.19777777777688 -38.46611111111111,174.24666666666576 -38.46611111111111,174.29555555555464 -38.46611111111111,174.34444444444352 -38.46611111111111,174.3933333333324 -38.46611111111111,174.4422222222213 -38.46611111111111,174.4422222222213 -38.5,174.4422222222213 -38.5,174.4422222222213 -38.5)),((173.8555555555547 -36.27469467488416,173.8555555555547 -36.263333333333335,173.8555555555547 -36.22944444444445,173.8555555555547 -36.19555555555556,173.80666666666582 -36.19555555555556,173.80666666666582 -36.16166666666667,173.80666666666582 -36.12777777777778,173.75777777777694 -36.12777777777778,173.75777777777694 -36.09388888888889,173.7417959253454 -36.09388888888889,173.75557784459505 -36.12777777777778,173.75777777777694 -36.14911530091963,173.76325310347545 -36.16166666666667,173.80558726743018 -36.19555555555556,173.80666666666582 -36.197384850578814,173.82878975804974 -36.22944444444445,173.84944811561743 -36.263333333333335,173.8555555555547 -36.27469467488416,173.8555555555547 -36.27469467488416,173.8555555555547 -36.27469467488416)),((174.6866666666657 -36.12777777777778,174.73555555555458 -36.12777777777778,174.78444444444347 -36.12777777777778,174.78444444444347 -36.09388888888889,174.78444444444347 -36.06,174.78444444444347 -36.02611111111111,174.78444444444347 -35.992222222222225,174.78444444444347 -35.958333333333336,174.78444444444347 -35.92444444444445,174.78444444444347 -35.89055555555556,174.78444444444347 -35.87765747147396,174.7514938058022 -35.89055555555556,174.73555555555458 -35.89644654727934,174.68751545249424 -35.92444444444445,174.6866666666657 -35.92476578980626,174.6866666666657 -35.958333333333336,174.63777777777682 -35.958333333333336,174.63777777777682 -35.992222222222225,174.63777777777682 -36.02611111111111,174.63777777777682 -36.06,174.6866666666657 -36.06,174.6866666666657 -36.09388888888889,174.6866666666657 -36.12777777777778,174.6866666666657 -36.12777777777778,174.6866666666657 -36.12777777777778)),((173.70888888888805 -36.0377948322446,173.70888888888805 -36.02611111111111,173.70295771755846 -36.02611111111111,173.70888888888805 -36.0377948322446,173.70888888888805 -36.0377948322446,173.70888888888805 -36.0377948322446)),((173.65999999999917 -35.99243834496677,173.65999999999917 -35.992222222222225,173.65985904211445 -35.992222222222225,173.65999999999917 -35.99243834496677,173.65999999999917 -35.99243834496677,173.65999999999917 -35.99243834496677)),((173.2688888888881 -35.5186510460533,173.2688888888881 -35.51777777777778,173.26818353235572 -35.51777777777778,173.2688888888881 -35.5186510460533,173.2688888888881 -35.5186510460533,173.2688888888881 -35.5186510460533)),((172.97555555555482 -35.21739078513759,173.0244444444437 -35.23925169779804,173.0244444444437 -35.21277777777778,173.0244444444437 -35.17888888888889,173.0244444444437 -35.145,173.0244444444437 -35.111111111111114,173.0244444444437 -35.077222222222225,173.0244444444437 -35.04333333333334,172.97555555555482 -35.04333333333334,172.97555555555482 -35.00944444444445,172.97555555555482 -34.97555555555556,172.97555555555482 -34.94166666666667,172.97555555555482 -34.90777777777778,172.97555555555482 -34.87388888888889,172.92666666666594 -34.87388888888889,172.87777777777706 -34.87388888888889,172.87777777777706 -34.84,172.87777777777706 -34.806111111111115,172.87777777777706 -34.772222222222226,172.82888888888817 -34.772222222222226,172.7799999999993 -34.772222222222226,172.7799999999993 -34.73833333333334,172.7799999999993 -34.70444444444445,172.7311111111104 -34.70444444444445,172.7311111111104 -34.67055555555556,172.7311111111104 -34.63666666666667,172.7311111111104 -34.60277777777778,172.68222222222153 -34.60277777777778,172.63333333333264 -34.60277777777778,172.62600766948088 -34.60277777777778,172.62613979085938 -34.63666666666667,172.6294749774601 -34.67055555555556,172.63333333333264 -34.678486947011635,172.64573521644763 -34.70444444444445,172.6647644128248 -34.73833333333334,172.68222222222153 -34.768723960330995,172.68445886850148 -34.772222222222226,172.70490736277 -34.806111111111115,172.724997924181 -34.84,172.7311111111104 -34.8506897904488,172.7449245696022 -34.87388888888889,172.76321856150685 -34.90777777777778,172.7799999999993 -34.93852535629443,172.7818258971361 -34.94166666666667,172.79911908350337 -34.97555555555556,172.81436383530234 -35.00944444444445,172.82888888888817 -35.039007084410215,172.83197437321274 -35.04333333333334,172.85003248920535 -35.077222222222225,172.87777777777706 -35.10496647795794,172.88313700002374 -35.111111111111114,172.90780226210077 -35.145,172.92666666666594 -35.17519032942247,172.93087506556083 -35.17888888888889,172.97184333091403 -35.21277777777778,172.97555555555482 -35.21739078513759,172.97555555555482 -35.21739078513759,172.97555555555482 -35.21739078513759)),((173.80666666666582 -34.97555555555556,173.8555555555547 -34.97555555555556,173.88505076717465 -34.97555555555556,173.8555555555547 -34.95635582303564,173.81790508096478 -34.94166666666667,173.80666666666582 -34.93725493580909,173.80666666666582 -34.94166666666667,173.80666666666582 -34.97555555555556,173.80666666666582 -34.97555555555556,173.80666666666582 -34.97555555555556)),((173.5622222222214 -34.87388888888889,173.6111111111103 -34.87388888888889,173.6404955215007 -34.87388888888889,173.6137590343396 -34.84,173.6111111111103 -34.83897128340532,173.5622222222214 -34.82454559833585,173.5622222222214 -34.84,173.5622222222214 -34.87388888888889,173.5622222222214 -34.87388888888889,173.5622222222214 -34.87388888888889)),((173.21999999999923 -34.70444444444445,173.2688888888881 -34.70444444444445,173.30963094634106 -34.70444444444445,173.28493477815138 -34.67055555555556,173.2688888888881 -34.65386012980699,173.2482145460874 -34.63666666666667,173.21999999999923 -34.610049142157266,173.21104390378179 -34.60277777777778,173.17111111111035 -34.5706617923791,173.16892922600925 -34.56888888888889,173.13486746020118 -34.535000000000004,173.12222222222147 -34.52090845332938,173.12222222222147 -34.535000000000004,173.07333333333258 -34.535000000000004,173.07333333333258 -34.56888888888889,173.12222222222147 -34.56888888888889,173.12222222222147 -34.60277777777778,173.12222222222147 -34.63666666666667,173.17111111111035 -34.63666666666667,173.17111111111035 -34.67055555555556,173.21999999999923 -34.67055555555556,173.21999999999923 -34.70444444444445,173.21999999999923 -34.70444444444445,173.21999999999923 -34.70444444444445)))

Anticipating that I have issues such as bowties and other self-intersections, I have also tried performing a zero-distance buffer: p = p.buffer(0), as per a recommendation in the shapely manual. The same result comes out.

EDIT: pasting my geometry into a delimited text file also produces an error:

1 records discarded due to invalid geometry definitions
The following lines were not loaded into QGIS due to errors:
Invalid WKT at line 2

EDIT2: but PostGIS correctly parses it with ST_GeomFromText!

EDIT3: Orielo correctly reads my WKT.

EDIT4: The QGIS plugin QuickWKT cannot parse it.

So as far as I can see: I have a valid MultiPolygon, and I turn this into a correct WKT representation that can be read by web tools and PostGIS, but not by anything that uses pyqgis.

How can I produce a valid MultiPolygon for QGIS?

3
  • Strange. It works for me here in 2.8.2.
    – Nathan W
    Commented Jul 12, 2015 at 22:49
  • I'm using 2.11. Commented Jul 12, 2015 at 22:49
  • Also works fine for me (again, in 2.8.2). What operating system are you using? How did you install QGIS? Does QgsGeometry.fromWkt work with simpler MultiPolygon geometries? Commented Jul 13, 2015 at 8:06

1 Answer 1

1

Your version of QGIS (2.11 = QGIS dev) use a new geometry class (QGIS dev: New geometry classes merged and many other posts in this mailing list as Questions regarding new geometry engine, QGIS QgsGeometry.fromWkt not accepting polygons with Z values? , Potentially serious performance regression in new geometry - should 2.10 be delayed?, ...)

What is the date of your version (dev = one version per day), because in QGIS dev: New geometry classes merged, Marco Hugentobler write (May 25, 2015):

The changes contain only the core classes, not included are the changes in the rendering workflow and all the edit tools. They would be too risky for 2.10 and will be merged at a later time (most likely for 2.12).

At the moment, the compatibility class QgsGeometry should hide the differences to the old geometry. In practice, there will be a number of unexpected problems after the merge. If you encounter one, fill a ticket in the tracker and assign it to me (or drop a note to the devel list). My apologies beforehand for the trouble caused. There are a number of provider unit tests not passing at the moment (mostly because of wkt formating issues). I'll go through them in the next days.

Look also at Bug Report: WKT Parser can't handle multipolygon with spaces

2
  • Aha. Knew I shouldn't have gone bleeding edge. I'll investigate further tomorrow with QGIS 2.10 to ensure that this is the issue. Commented Jul 13, 2015 at 9:00
  • I stayed on QGIS dev; the issue was as is described in that final link (the bug report): support was broken for multipolygons with spaces. I simply adjusted WKT = p.dumps() to WKT = p.dumps().replace('), (','),(') and had no further issues. I thought I was going crazy, thanks. Commented Jul 14, 2015 at 4: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.