Add simply GeoJSON dataset from QGIS
Recipe 1: add QGIS built-in data
You can reuse the QGIS contributors map dataset directly from QGIS without any downloading using PyQGIS. It's a GeoJSON file available within your installed program.
Just run in QGIS Python console the following
import os
from qgis.core import QgsApplication
geojson_contributors = os.path.join(
os.path.dirname(QgsApplication.developersMapFilePath()),
'contributors.json'
)
print geojson_contributors
iface.addVectorLayer(geojson_contributors, 'QGIS contributors', 'ogr')
Recipe 2: add remote GeoJSON
This is quite handy to get the remote data. You need to be aware that it's a good solution when remote content change or for demo. However, it's not always a good idea for production where your data can stop being available.
- Try to do it by going to the website geojson.xyz to get an URL data source.
The data are coming from Natural Earth Data, the main advantage here is the fact GeoJSON file are delivered via a CDN (Content Delivery Network) whose goal is according to Wikipedia definition
to serve content to end-users with high availability and high performance
- Choose a layer source
- Copy it URL
Now, you've got an URL to a remote GeoJSON, you can add the GeoJSON from the UI or via PyQGIS.
From the GUI
The following screencast illustrates the way to add remote GeoJSON from GUI
You can't choose the layer name when you load the data source: it's the default one.
From the QGIS Python console
The solution throught the QGIS Python console is just this one liner. You just need to change the URL.
iface.addVectorLayer('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_populated_places.geojson', 'Populated places', 'ogr')
Comments