JTS3DMap / app.py
JanuaryDesk's picture
init
052a6c6
raw
history blame
6.76 kB
import gradio as gr
from pythonnet import load
load("coreclr", runtime_config="lib\JTSParser.runtimeconfig.json")
import clr
from System import Reflection
import os
lib_path = os.path.abspath("lib\JTSParser.dll")
Reflection.Assembly.LoadFile(lib_path)
import YYZ.JTS.NB
import numpy as np
import plotly.graph_objects as go
import math
"""
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
"""
def test():
fig = go.Figure()
fig.add_trace(go.Scatter3d(
x=["2017-01-01", "2017-02-10", "2017-03-20"],
y=["A", "B", "C"],
z=[1, 1000, 100000],
name="z",
))
fig.update_layout(
scene=dict(
xaxis=dict(type="date"),
yaxis=dict(type="category"),
zaxis=dict(type="log"),
annotations=[
dict(
showarrow=False,
x="2017-01-01",
y="A",
z=0,
text="Point 1",
xanchor="left",
xshift=10,
opacity=0.7),
dict(
x="2017-02-10",
y="B",
z=4,
text="Point 2",
textangle=0,
ax=0,
ay=-75,
font=dict(
color="black",
size=12
),
arrowcolor="black",
arrowsize=3,
arrowwidth=1,
arrowhead=1),
dict(
x="2017-03-20",
y="C",
z=5,
ax=50,
ay=0,
text="Point 3",
arrowhead=1,
xanchor="left",
yanchor="bottom"
)]
),
)
return fig
def dock(x, limit):
x1 = math.floor(x)
yield x1
x2 = min(math.ceil(x), limit)
if x2 != x1:
yield x2
default_path = "JTS_assets/ridge.map"
with gr.Blocks(analytics_enabled=False) as demo:
with gr.Row():
with gr.Column(scale=1):
file_input = gr.File(default_path, label="Map File (NB or CWB)", file_types=[".map"])
labels_checkbox = gr.Checkbox(True, label="Labels")
labels_size_threshold_number = gr.Number(0, label="Label Size Threshold", info="1 => Tactical, 2 => Normal, 3 => Important")
# with gr.Row():
with gr.Accordion("Roads"):
with gr.Row():
path_checkbox = gr.Checkbox(label="Path")
road_checkbox = gr.Checkbox(label="Road")
pike_checkbox = gr.Checkbox(True, label="Pike")
railway_checkbox = gr.Checkbox(True, label="Railway")
with gr.Row():
road_offset_number = gr.Number(0.3, label="Road Offset")
elevation_scale_number = gr.Number(0.1, label="Elevation Scale")
plot_button = gr.Button("Plot")
with gr.Column(scale=2):
output_plot = gr.Plot()
def plot(data):
with open(data[file_input].name) as f:
map_str = f.read()
map_file = YYZ.JTS.NB.MapFile.Parse(map_str)
graph = YYZ.JTS.NB.InfantryColumnGraph.FromMapFile(map_file)
height_mat = np.empty([map_file.Height, map_file.Width])
for i in range(map_file.Height):
for j in range(map_file.Width):
height_mat[i,j] = graph.HexMat[i, j].Height
surface = go.Surface(
x = np.arange(map_file.Width),
y = np.arange(map_file.Height),
z = height_mat
)
gl = []
road_offset = data[road_offset_number]
road_items = [
(YYZ.JTS.NB.RoadType.Path, path_checkbox, 'gray'),
(YYZ.JTS.NB.RoadType.Road, road_checkbox, 'green'),
(YYZ.JTS.NB.RoadType.Pike, pike_checkbox, 'pink'),
(YYZ.JTS.NB.RoadType.Railway, railway_checkbox, 'black')
]
for road_type, checkbox, color in road_items:
if data[checkbox]:
for road in graph.SimplifyRoad(road_type):
x_line = []
y_line = []
z_line = []
for node in road:
x_line.append(node.X)
y_line.append(node.Y)
z_line.append(node.Height + road_offset)
g = go.Scatter3d(
x=x_line, y=y_line, z=z_line,
line=dict(
color=color,
width=2
)
)
gl.append(g)
fig = go.Figure([surface] + gl)
scene = {
"aspectratio": {"x": 1, "y": 1, "z": data[elevation_scale_number]},
'yaxis': {'autorange': 'reversed'},
}
if data[labels_checkbox]:
labels_x = []
labels_y = []
labels_z = []
labels_text = []
for label in map_file.Labels:
if label.Size >= data[labels_size_threshold_number]:
max_height = -1
x = label.X
y = label.Y
for dx in dock(x, map_file.Width-1):
for dy in dock(y, map_file.Height-1):
max_height = max(max_height, map_file.HeightMap[dy, dx])
labels_x.append(x)
labels_y.append(y)
labels_z.append(max_height)
labels_text.append(label.Name)
scene["annotations"] = [
dict(
showarrow=False,
x=x,
y=y,
z=z,
text=text,
xanchor="left",
xshift=10,
opacity=0.7,
bgcolor="white"
)
for x, y, z, text in zip(labels_x, labels_y, labels_z, labels_text)
]
fig.update_layout(scene=scene, showlegend=False)
return {output_plot: fig}
plot_button.click(plot, {file_input,
labels_checkbox, labels_size_threshold_number,
path_checkbox, road_checkbox, pike_checkbox, railway_checkbox,
elevation_scale_number, road_offset_number}, {output_plot})
# plot_button.click(lambda data: {output_img: test()}, {file_input, path_checkbox, road_checkbox, pike_checkbox, plot_button}, {output_img})
if __name__ == "__main__":
demo.launch()