GIS data mapping using JSON in Dynamo

GIS data mapping using JSON in Dynamo

A query was raised in the previous post on examples of how JSON files could be used in Dynamo. Coincidentally, I was exploring on the topic of Geo-spatial Information Systems (GIS), where JSON files are commonly used to store useful information, in accordance with GeoJSON specifications, such as coordinates, polygons of boundaries, properties, etc. And I thought I’d share a fraction of what I was messing with in this post.

I was using data from data.gov.sg using JTC Corporation’s estate boundary data set as a test case, as I thought it would display interesting results (You could download the Files if you wish to test it out yourself). There’s probably a better way to achieve similar results in QGIS or similar, but we can think of dynamo as a platform to integrate data across different sources and perform further processes to achieve your original intent.

Import JSON into Dynamo

We begin by importing the GeoJSON test file to understand the structure of the data. This will help us know what are the available data, together with its ‘keys’ to retrieve values that we are interested in.

***NOTE: I am using Revit 2020, hence the error indicated in the previous post will not surface.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\DLLs')
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib') 
import json

jsonPath = IN[0]
output = []

#Load Json
with open(jsonPath, "r") as read_file:
    data = json.load(read_file)

OUT = data

Alternatively, if you are aware of how JSON files are structured, you can open the GeoJSON file in a text editor ( personally, I like Notepad++) to understand its structure. It should look something like this after you format it.

Retrieve Values from Keys

Now that we understand the structure of the dictionaries, we can retrieve those values through its keys. In this case, we will retrieve 1. Estate name and 2. Global coordinates representing corresponding boundaries as polygons, and group them in the same list for possible use in the future.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\DLLs')
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib') 
import json

jsonPath = IN[0]
output = []

#Load Json
with open(jsonPath, "r") as read_file:
    data = json.load(read_file)

feat = data["features"]
boundCrvs = []

for item in feat:
	name = item["properties"]["ESTATE_NAME"]
	type = item["geometry"]["type"]
	points = []
	
	if (type == "Polygon"):
		ptLst = item["geometry"]["coordinates"][0]
		
		for p in ptLst:
			p = Point.ByCoordinates(p[0] , p[1] , 0)
			points.append(p)
		
	elif (type == "Multipolygon"):
		polyLst = item["geometry"]["coordinates"]
		for polygon in polyLst:
			ptLst = polygon[0]
			
			for p in ptLst:
				p = Point.ByCoordinates(p[0] , p[1] , 0)
				points.append(p)
	
	output.append([name,points])
	
OUT = output

If you noticed the conditional statements of geometry types, i.e. item[“geometry”][“type”], it was included as the list nesting structures for “Polygon” and “Multipolygon” types are different.

Visualize Results

Voila! The results can be seen below! Results should come as no surprise to Singaporeans with JTC’s properties coverage resembling the profile of Singapore Island (Hehe).

To conclude, we can leverage on existing data (not only in JSON) to provide insights even in Dynamo.

In this case, I’d probably have to further process the data or even integrate more data sources from other agencies to provide more meaning to designer’s context.

I hope this was useful, or at least provided some ideas to you. And as always, Happy coding!

This Post Has 2 Comments

  1. Arno

    getting these errors:

    Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
    Traceback (most recent call last):
    File “”, line 15, in
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\__init__.py”, line 287, in load
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\__init__.py”, line 339, in loads
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\decoder.py”, line 364, in decode
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\decoder.py”, line 380, in raw_decode
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\scanner.py”, line 37, in _scan_once
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\decoder.py”, line 194, in JSONObject
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\scanner.py”, line 35, in _scan_once
    File “C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit\IronPython.StdLib.2.7.8\json\decoder.py”, line 123, in py_scanstring
    ValueError: Invalid \escape: ‘N’: line 3 column 26 (char 47)

    with the first example

    1. Han

      Hi Arno,

      There might be issues with the structure of the geoJSON from the link.

      Could you try this geoJSON file instead?

      Have updated the link in the original post as well. Thanks Arno!

      regards,
      Han

Leave a Reply