I have been able to connect my collection in MongoDB , write and store the collection to as .json file and load the GeoJSON file as a layer by QGIS's Python module unless there are mixed geometries. However, I have a problem when the collection in MongoDB has more than geometry type. It loads only one geometry to QGIS canvas. But when I drag and drop the file, I can load both geometries. So .json file, that has been created, is fine.

client = MongoClient()
client = MongoClient('localhost', 27019)
db = client['pb']
feature = QgsFeature()# create layer

os.chdir( "C:\Users\Public" )
feature =[]

for data in cursor:
    id  = (data['properties'])

    feature.append({"geometry":geom, 'properties':id})

json_str = json.dumps(feature)
final={"type": "FeatureCollection","features":feature}

with open('data_3.json', 'w') as f:
     json.dump(final, f)

vlayer = QgsVectorLayer('C:\Users\Public\data_3.json',"mygeojson","ogr")

Any advice on what I could do?


You should specify the geometry type in uri:

vlayer = QgsVectorLayer('C:\Users\Public\data_3.json|geometrytype=LineString',"mygeojson","ogr")

geometrytype can be "Point", "LineString" or "Polygon"

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.