Last active
October 16, 2018 16:10
-
-
Save yasushisakai/2316d0ff2996b3d42f916df519c83886 to your computer and use it in GitHub Desktop.
rhinopython script to convert curves to a geojson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rhinoscriptsyntax as rs | |
import Rhino.Geometry.Point3d as Point | |
import json | |
# creates network from curves | |
# curves that are not ```Line```s will be divided | |
CURVE_DIVISION = 4 | |
# points that are separted less then this value will | |
# be treated as one point | |
TOLERANCE = 1; | |
# bbox = rs.GetRectangle(1) | |
# minx = 100000 | |
# maxx = -100000 | |
# miny = 100000 | |
# maxy = -100000 | |
# for p in bbox: | |
# minx = min(minx, p.X) | |
# maxx = max(maxx, p.X) | |
# miny = min(miny, p.Y) | |
# maxy = max(maxy, p.Y) | |
minx = 0 | |
maxx = 2128 | |
miny = 0 | |
maxy = 1330 | |
l = rs.GetLayer() | |
isoneway = rs.GetBoolean("is it?", ("Oneway", "no", "yes"), (False) | |
)[0] | |
crvs = list(filter(lambda x: rs.IsCurve(x), rs.ObjectsByLayer(l))) | |
points = [] | |
# returns the index of the point, | |
# addes the point to the array if it's unique | |
def appendIfUnique(pts, p): | |
for testP in pts: | |
if testP.DistanceTo(p) < TOLERANCE: | |
return (pts, testP) | |
pts.append(p) | |
return (pts, p) | |
def normalizePoint(p, minx=minx, maxx=maxx, miny=miny, maxy=maxy): | |
nx = (p.X - minx) / (maxx - minx) | |
ny = (p.Y - miny) / (maxy - miny) | |
return Point(nx, ny, 0.0) | |
geojson = {} | |
geojson["type"] = "Feature Collection" | |
geojson["features"] = [] | |
for c in crvs : | |
jsonobj = {} | |
jsonobj["type"] = "Feature" | |
jsonobj["properties"] = {} | |
jsonobj["properties"]["length"] = rs.CurveLength(c) | |
jsonobj["geometry"] = {} | |
jsonobj["geometry"]["type"] = "LineString" | |
if isoneway : | |
jsonobj["properties"]["oneway"] = True | |
else : | |
jsonobj["properties"]["oneway"] = False | |
if rs.IsLine(c): | |
points, st = appendIfUnique(points, rs.CurveStartPoint(c)) | |
points, en = appendIfUnique(points, rs.CurveEndPoint(c)) | |
st, en = [normalizePoint(e) for e in [st, en]] | |
jsonobj["geometry"]["coordinates"] = [ | |
[st.X, st.Y], | |
[en.X, en.Y] | |
] | |
else: | |
# it is not a line curve object | |
curvepoints = rs.DivideCurve(c, CURVE_DIVISION) | |
jsonobj["geometry"]["coordinates"] = [] | |
for p in curvepoints: | |
points, p = appendIfUnique(points, p) | |
p = normalizePoint(p) | |
jsonobj["geometry"]["coordinates"].append([p.X, p.Y]) | |
geojson["features"].append(jsonobj) | |
jsonstr = json.dumps(geojson, indent = 4) | |
f = open("{}.geojson".format(l), "w") | |
f.writelines(jsonstr) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment