Export data to QGIS

If you want to use spatial data that you created in nodegoat in a geographic information system (GIS) application like QGIS, you can export your data as a CSV file or query the nodegoat API. In both cases, you end up with one or multiple files that you then can process and work with in a GIS application.

In this guide we will describe a more dynamic approach: use the Python console in QGIS to pull in data from your nodegoat environment. This approach allows you to use nodegoat as your primary data store and instantly update your QGIS project whenever you want to load in the most recent version of your data.

To make this work, first configure the API of your research environment in such a way that you can run queries to fetch the spatial data you want to use in your GIS application. You should have enabled at least one project in the API settings, and you should have configured at least one active client and one active user who has access to the enabled project.

The identifiers of Objects, Object Types, Scopes, or Filters (and any other nodegoat element) are shown under the name of the element when you edit or view the element in nodegoat. You will use these identifiers to construct the API queries.

If you do not have access to the API module that can be accessed via 'Management', please contact the administrator of your nodegoat environment or send a message to support@nodegoat.net.

Before you start querying the API from your GIS application, it is recommended to first run your query as a cURL command to confirm that the requested data is correctly returned. For example:

curl https://nodegoat.io/data/type/[Your Object Type ID]/object?limit=1 -H 'Authorization: Bearer [Your Passkey]'

Connect QGIS to nodegoat  

To use the response data in QGIS: start a new project and go to 'Plugins > Python Console'. Click the 'Show Editor' button in order to be able to write and run a python script.

The first section of the script fetches your data from the nodegoat API:

import requests # Import the requests library to make HTTP requests 
query_url = 'https://nodegoat.io/data/type/[Your Object Type ID]/object' # The nodegoat API request URL 
passkey = '[Your Passkey]' # Your passkey
request = requests.get(query_url, headers={'Content-Type':'application/json', 'Authorization': 'Bearer '+passkey}) # Fetch the data
json_data = request.json() # Format the response as JSON
objects = json_data['data']['objects'] # Go to the position in the response data that contains the list of returned Objects

Next, create a point layer that can host the returned data:

point_layer = QgsVectorLayer('Point?crs=EPSG:4326', 'Point Layer', 'memory') # Create a point layer using the EPSG:4326 code
point_layer.dataProvider().addAttributes([QgsField('id',  QVariant.Int), QgsField('label', QVariant.String)]) # Add attributes for IDs and Labels
point_layer.updateFields() # Enable the newly created fields
QgsProject.instance().addMapLayer(point_layer) # Add the newly created layer to your current project

Finally, iterate over the returned Objects and Sub-Objects in order to add the geometry data to the newly created layer:

for object_id in objects: # Iterate over the returned Objects

  for object_sub_id in objects[object_id]['object_subs']: # Iterate over the Sub-Objects of each Object

    string_geometry = objects[object_id]['object_subs'][object_sub_id]['object_sub']['object_sub_location_geometry'] # Get the geometry data

    if string_geometry: # Check if the Sub-Object has geometry data

      geometry = QgsJsonUtils.stringToFeatureList(string_geometry, QgsFields(), None) # Create a feature list based on the returned GeoJSON data
      geometry[0].setAttributes([object_sub_id, objects[object_id]['object']['object_name']]) # Assign attributes to the newly created feature
      point_layer.dataProvider().addFeatures(geometry) # Add the feature to the point layer

point_layer.updateExtents() # update the layer

Paste these three sections of the script in the editor. Click the 'Run Script' button to fetch and display your nodegoat data in your QGIS project. Enable the 'Show Labels' option to display the returned Object Names as labels.

To include geometries of related Objects, you set a Scope that follows a reference to a related Object Type containing spatial data.   Follow the guide 'Manage your Visualisation' to learn more about the Scope functionality. Store this Scope and use its identifier in your query, e.g.: 'https://nodegoat.io/data/type/[Your Object Type ID]/scope/[Your Scope ID]/object'.

Update the part of the scripts that iterates over the Objects and Sub-Objects to this:

for object_id in objects: # Iterate over the returned Objects

  object = objects[object_id] 

  if 'cross_referencing' in object: # Check if Cross-Referencing Objects exist

    for cross_referencing_object_id in object['cross_referencing']:  # Iterate over the Cross-Referencing Objects

      cross_referencing_object = object['cross_referencing'][cross_referencing_object_id]

      for object_sub_id in cross_referencing_object['object_subs']: # Iterate over the Sub-Objects of each Cross-Referencing Object

        object_sub = cross_referencing_object['object_subs'][object_sub_id]
        string_geometry = object_sub['object_sub']['object_sub_location_geometry'] # Get the geometry data

        if string_geometry: # Check if the Sub-Object has geometry data

          geometry = QgsJsonUtils.stringToFeatureList(string_geometry, QgsFields(), None) # Create a feature list based on the returned GeoJSON data
          geometry[0].setAttributes([object_sub_id, object['object']['object_name']]) # Assign attributes to the newly created feature
          point_layer.dataProvider().addFeatures(geometry) # Add the feature to the point layer

point_layer.updateExtents() # Update the layer