[GIS] pyqgis: Add GeoJSON as editable Layer

pyqgisqgis

following GeoJSON should be added to QGIS as an editable Layer:

{"type":"MultiPolygon","coordinates":[[[[540776.621887207,5912792.64251709],[540773.4404907227,5912798.570678711],[540774.3524780273,5912833.500305176],[540773.7451171875,5912839.8787231445],[540781.3389282227,5912846.864685059],[540789.8435058594,5912855.673095703],[540800.170715332,5912865.088500977],[540805.637878418,5912877.541870117],[540806.8529052734,5912887.261291504],[540804.4237060547,5912900.322509766],[540796.8306884766,5912932.8220825195],[540795.3120727539,5912966.841308594],[540800.7799072266,5912984.76171875],[540803.6392822266,5912983.147521973],[540799.428527832,5912964.180908203],[540801.6641235352,5912931.578491211],[540808.3139038086,5912901.5205078125],[540812.0502929688,5912888.047912598],[540810.8626708984,5912876.851501465],[540805.3167114258,5912862.604125977],[540796.9296875,5912854.176513672],[540787.0308837891,5912844.229919434],[540777.3010864258,5912836.084289551],[540776.621887207,5912792.64251709]]]]}

Follows the GeoJSON can be added to the map.

vl = QgsVectorLayer(GeojsonAsString,"mygeojson","ogr")
QgsMapLayerRegistry.instance().addMapLayer(vl)

Now the layer is not editable because the GeoJSON needs to be converted into another format eg Shp. Another possibility would be to create an editable QgsFeature. How can I create from the GeoJSON such a QgsFeature?

A simple point can be created as follows:

feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))

In my case, a MultiPolygon (QgsGeometry.fromMultiPolygon) must be created. Unfortunately the GeoJSON is not in the required format. How can I convert the GeoJSON in the required format?

Another question:

By means of the OGR tools the GeoJSON can be converted to a Geometry.
Like:

geomOGR = ogr.CreateGeometryFromJson(GeojsonAsString)

How can this geometry be added to QGIS and would the feature be editable?

Best Answer

You can create a memory layer ( always editable) with your GeoJSON geometry. The problem is that PyQGIS does not know this format for that but there are other solutions.

1) Direct transformation of the GeoJSON coordinates to a QGIS polygon:

geojson= {"type":"MultiPolygon","coordinates":[[[[540776.621887207,5912792.64251709],[540773.4404907227,5912798.570678711],[540774.3524780273,5912833.500305176],[540773.7451171875,5912839.8787231445],[540781.3389282227,5912846.864685059],[540789.8435058594,5912855.673095703],[540800.170715332,5912865.088500977],[540805.637878418,5912877.541870117],[540806.8529052734,5912887.261291504],[540804.4237060547,5912900.322509766],[540796.8306884766,5912932.8220825195],[540795.3120727539,5912966.841308594],[540800.7799072266,5912984.76171875],[540803.6392822266,5912983.147521973],[540799.428527832,5912964.180908203],[540801.6641235352,5912931.578491211],[540808.3139038086,5912901.5205078125],[540812.0502929688,5912888.047912598],[540810.8626708984,5912876.851501465],[540805.3167114258,5912862.604125977],[540796.9296875,5912854.176513672],[540787.0308837891,5912844.229919434],[540777.3010864258,5912836.084289551],[540776.621887207,5912792.64251709]]]]}
geom = QgsGeometry.fromPolygon([[QgsPoint(pt[0],pt[1])  for pt in geojson['coordinates'][0] [0]]])

2) Use of other Python modules (transformation of the GeoJSON geometry to WKT (or WKB)):

With geomet:

from geomet import wkt 
geom = QgsGeometry.fromWkt(wkt.dumps(a, decimals=4))

With Shapely

from shapely.geometry import MultiPolygon, shape
geom = shape(geojson)
geom = QgsGeometry.fromWkt(geom.wkt)

With OGR:

from osgeo import ogr
import json
string = json.dumps(geojson)
geom = ogr.CreateGeometryFromJson(str)
geom = QgsGeometry.fromWkt(point.ExportToWkt())

3) after that you can create a memory layer (editable)

layer = QgsVectorLayer('Polygon','test','memory')
pr = layer.dataProvider()
elem= QgsFeature()
elem.setGeometry(geom)
pr.addFeatures( [ elem ] )
layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([layer])
Related Question