Welcome to the world of MicroStation Python! This wiki provides a Python code snippet that demonstrates how to import vector data stored in JSON/GEOJSON format. Discover how to leverage Python's capabilities to automate tasks and enhance your MicroStation workflows.
Let us dive into, creating and placing vector data stored in JSON/GEOJSON format. Refer to the Python Editor wiki to create and load a python project. Name your project as "ImportGJson.py" and save it to your preferred directory. Open the project in the editor for writing your Python script.
Here is the complete script.
from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * import json import tkinter as tk from tkinter import filedialog def get_uor_per_master(): ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() modelInfo = dgnModel.GetModelInfo() __, uorPerMast = modelInfo.StorageUnit.ConvertDistanceFrom(modelInfo.UorPerStorage, modelInfo.MasterUnit) return uorPerMast def create_shape_from_coords(coords, uorPerMast, dgnModel, height): # coords: list of [x, y] pairs points = DPoint3dArray() for xy in coords: pt = DPoint3d(xy[0], xy[1], 0.0) pt.Scale(uorPerMast) points.append(pt) eeh = EditElementHandle() status = ShapeHandler.CreateShapeElement(eeh, None, points, dgnModel.Is3d(), dgnModel) if height == 0: if status == BentleyStatus.eSUCCESS: eeh.AddToModel() return else: project_element = EditElementHandle() extrude_vector = DVec3d.From(0, 0, abs(height*uorPerMast)) status = SurfaceOrSolidHandler.CreateProjectionElement(project_element, None, eeh, points[0], extrude_vector, None, True, dgnModel) if status == BentleyStatus.eSUCCESS: project_element.AddToModel() return def open_geojson_file(): root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename( title="Select GeoJSON File", filetypes=[("GeoJSON files", "*.geojson *.json"), ("All files", "*.*")] ) return file_path def main(): file_path = open_geojson_file() if not file_path: print("No file selected.") return with open(file_path, "r") as f: geojson = json.load(f) ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() uorPerMast = get_uor_per_master() features = geojson.get("features", []) for feature in features: geom = feature.get("geometry", {}) props = feature.get("properties", []) parcel_height = props.get("parcel_height") if geom.get("type") == "Polygon": coords = geom.get("coordinates", []) if not coords: continue cord = coords[0] if not parcel_height: height = 0 else: height = parcel_height create_shape_from_coords(cord, uorPerMast, dgnModel, height) if __name__ == "__main__": main()
Load the project "ImportGJson.py" from the Python Editor dialog and Run/Execute the python script.
When prompted, select the following GEOJSON files: Parcels.zip
Boundary.geojson
parcels_2D.geojson
parcels_3D.geojson
The script will read these files and create the corresponding elements.
Dataset available in Introduction to GIS Programming - Dr. Qiusheng Wu
Vancouver Property Value
Contains information licensed under the Open Government Licence – Vancouver.
Source: City of Vancouver Open Data Portal
Video:
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs