1

I have the following problem: I have two vector layers, foobar and clipLayer. About the python console I call the following command:

processing.runalg ('qgis:clip', 'foobar', 'clipLayer', None)

As a result, I get the temporary path to a Shp-file. That is so far correct. But I need the result not as a Shp-file but in a GeoJSON format.

How can I convert the shapefile to the GeoJSON format? There is a convert function within the GDAL tools.

processing.runalg('gdalogr:convertformat', input_layer, format, options, output_layer)

As a result, I get the path to a file .shp.geojson. but I want to get a GeoJSON string as a result.

Why do I need this? Within a plugin the clip function is used. The result must be further processed as a Qgsfeature. In this case I can not use a shp file.

2 Answers 2

1

You could try using something like the following which was tested in the Python console where you can call the output of the clip algorithm as an input to the convertformat algorithm:

clip_layer= processing.runalg("qgis:clip", layer2, layer1, None)
processing.runandload('gdalogr:convertformat', clip_layer["OUTPUT"], 1, "", None)
2
  • I have updated my question. Thanks for the answer.
    – eftas
    Apr 4, 2016 at 14:01
  • @eftas - Most welcome but I would suggest you post your Update as an answer instead of editing it into the question =)
    – Joseph
    Apr 4, 2016 at 14:03
1

Update

Sometimes the solution is simpler than you think. First, I proceed as described by Joseph. After carrying out the converter function I get a .geojson file. This can be further processed as follows:

json.load(open(pathGeoJSON))

From the generated JSON file the coordinates can be extracted and a QgsFeature generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.