[QGIS-Developer] Convert geojson geometry to QgsGeometry in python

Matthias Kuhn matthias at opengis.ch
Mon Jan 4 09:39:59 PST 2021


Hi Bo,

You can use the QgsJsonUtils.stringToFeatureList.
It adds a bit of overhead by roundtripping it through a QgsFeatureList but
works.

QgsJsonUtils.stringToFeatureList('''
{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

''')[0].geometry()

Matthias

On Mon, Jan 4, 2021 at 6:14 PM Bo Victor Thomsen <
bo.victor.thomsen at gmail.com> wrote:

> Hi list members:
>
> The subject says it : How to convert the geometry part from a geojson
> string to a QgsGeometry
>
> Some details:
>
> How can I convert the geojson "geometry" part to a QgsGeometry ? i.e. The
> part marked in *bold* ?
>
> {
>   "type": "Feature",
>   *"geometry"**:* *{*
>     *"type"**:* *"Point"**,*
>     *"coordinates"**:* *[**125.6**,* *10.1**]*
>   *}*,
>   "properties": {
>     "name": "Dinagat Islands"
>   }}
>
> It's not the only "point" type, but all the geometry types (minus
> "geometry collection"): linestring, polygon, multipoint, multilinestring
> and multipolygon.
>
> The json text is already converted to a python dict using json.dumps().
> AFAIK, it's trivial to convert from QgsGeometry to geojson, but I can't
> find an existing method to do the opposite.
>
> --
> Med venlig hilsen / Kind regards
>
> Bo Victor Thomsen
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210104/538427a5/attachment.html>


More information about the QGIS-Developer mailing list