Skip to content
Snippets Groups Projects
Commit 6819c3a5 authored by Rico Possienka's avatar Rico Possienka
Browse files

digest geojson string

parent ac1499a1
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using SimpleJSON;
public class BrowserBindings : MonoBehaviour
{
......@@ -11,7 +12,9 @@ public class BrowserBindings : MonoBehaviour
public Dictionary<string, object> current;
public Stack<Dictionary<string, object>> jsonNest = new Stack<Dictionary<string, object>>();
public Stack<object> array = null;
public GeoJson geojson;
public JSONNode geoJsonNode;
public string jsonstring = "<UNSET>";
//Browser part
[DllImport("__Internal")]
......@@ -32,7 +35,40 @@ public class BrowserBindings : MonoBehaviour
[DllImport("__Internal")]
private static extern void BindWebGLTexture(int texture);
[System.Serializable]
public class Coordinate {
public double[] point = new double[] { };
}
[System.Serializable]
public class Geometry {
public string type = "";
public Coordinate[] coordinates = null;
}
//Unity part
[System.Serializable]
public class GeoJson {
public string type = "";
public GeoJson[] features = new GeoJson[0] {};
public Geometry geometry = null;
public Coordinate[] coordinates = null;
}
public void DigestJSON(string json) {
try
{
geojson = JsonUtility.FromJson<GeoJson>(json);
geoJsonNode = SimpleJSON.JSON.Parse(json);
jsonstring = json;
}
catch (System.Exception e)
{
jsonstring = e.Message;
}
}
public void JSONBegin()
{
current = new Dictionary<string, object>();
......@@ -72,7 +108,7 @@ public class BrowserBindings : MonoBehaviour
}
//Native functions
private void Awake()
public void Awake()
{
GameObject.DontDestroyOnLoad(this);
Instance = this;
......
......@@ -209,7 +209,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &967804446
GameObject:
......@@ -293,7 +293,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &967804450
MonoBehaviour:
......@@ -307,3 +307,49 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c185da79663a1e24db115f93b64504d2, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1523336119
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1523336121}
- component: {fileID: 1523336120}
m_Layer: 0
m_Name: BrowserBindings
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1523336120
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1523336119}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f7aa69c5178bf5d4c873d0b9f1acb59c, type: 3}
m_Name:
m_EditorClassIdentifier:
geojson:
type:
teststring: dsfdsfdsfsfs
--- !u!4 &1523336121
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1523336119}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
......@@ -294,49 +294,6 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1523336119
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1523336121}
- component: {fileID: 1523336120}
m_Layer: 0
m_Name: BrowserBindings
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1523336120
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1523336119}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f7aa69c5178bf5d4c873d0b9f1acb59c, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1523336121
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1523336119}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2041792447
GameObject:
m_ObjectHideFlags: 0
......
using System;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;
public class JsonTest : MonoBehaviour
{
private int x;
private int y;
private string bigjson;
public void Start()
{
......@@ -32,32 +34,130 @@ public class JsonTest : MonoBehaviour
public void OnGUI()
{
x = y = 100;
GUI.Label(new Rect(x, y, 100, 20), "JSON:");
GUI.Label(new Rect(x, y, 1000, 20), "JSON: " + Time.time);
y += 20;
GUI.Label(new Rect(x, y, 1000, 20), "test JSON: " + BrowserBindings.Instance.jsonstring.Substring(0, Mathf.Min(BrowserBindings.Instance.jsonstring.Length, 40)));
y += 20;
bigjson = GUI.TextArea(new Rect(x, y, 1000, 20), bigjson);
y += 20;
if (GUI.Button(new Rect(x, y, 100, 40), "digest json")) {
BrowserBindings.Instance.DigestJSON(bigjson);
}
y += 45;
//ShowJSON(BrowserBindings.Instance.geojson);
ShowSimpleJson(BrowserBindings.Instance.geoJsonNode);
}
ShowJSON(BrowserBindings.Instance.current);
string StripQuotes(string s) {
s = s.Trim();
if (s.StartsWith("\"")) return StripQuotes(s.Substring(1));
if (s.EndsWith("\"")) return StripQuotes(s.Substring(0, s.Length-1));
return s;
}
private void ShowJSON(Dictionary<string, object> json)
private void ShowSimpleJson(JSONNode j)
{
if (j == null) return;
if (j.HasKey("type"))
{
var type = j["type"];
if (type != null && StripQuotes(type.ToString()) == "FeatureCollection")
{
var features = j["features"].AsArray;
for (int i = 0; i != features.Count; ++i) {
var feature = features[i];
ShowSimpleJson(feature);
}
}
if (type != null && StripQuotes(type.ToString()) == "Feature")
{
var geometry = j["geometry"];
ShowSimpleJson(geometry);
}
}
if (j.HasKey("coordinates")) {
var array = j["coordinates"].AsArray;
for (int i = 0; i != array.Count; ++i) {
var coord = array[i];
var _x = coord[0].AsFloat;
var _y = coord[1].AsFloat;
x += 20;
GUI.Label(new Rect(x, y, 1000, 20), (new Vector2(_x, _y).ToString()));
y += 20;
}
}
}
private void ShowGeometry(BrowserBindings.Geometry g)
{
x += 20;
foreach (var o in json)
GUI.Label(new Rect(x, y, 1000, 20), g.type);
y += 20;
if (g.coordinates != null && g.coordinates.Length > 0)
{
var key = o.Key;
var val = o.Value;
x += 20;
for (int i = 0; i != g.coordinates.Length; ++i)
{
GUI.Label(new Rect(x, y, 1000, 20), g.coordinates[i].ToString());
y += 20;
}
x -= 20;
}
x -= 20;
}
private void ShowJSON(BrowserBindings.GeoJson g)
{
if (val is string || val is float || val is bool) {
GUI.Label(new Rect(x, y, 100, 20), key + " = " + val.ToString());
x += 20;
GUI.Label(new Rect(x, y, 1000, 20), g.type);
y += 20;
//if (g.coordinates != null && g.coordinates.Length > 0)
{
x += 20;
//for (int i = 0; i != g.coordinates.Length; ++i)
{
//GUI.Label(new Rect(x, y, 1000, 20), g.coordinates[i].ToString());
y += 20;
}
else {
var nested = val as Dictionary<string, object>;
if (nested != null) {
ShowJSON(nested);
}
x -= 20;
}
if (g.features != null) {
foreach (var feature in g.features)
{
ShowJSON(feature);
}
}
y += 20;
if (g.geometry != null) {
ShowGeometry(g.geometry);
}
if (g.coordinates != null && g.coordinates.Length > 0)
{
x += 20;
for (int i = 0; i != g.coordinates.Length; ++i)
{
GUI.Label(new Rect(x, y, 1000, 20), g.coordinates[i].ToString());
y += 20;
}
x -= 20;
}
x -= 20;
}
}
......@@ -5,13 +5,13 @@ EditorBuildSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/_app/Scenes/JsonTest.unity
guid: 3b2ea00127a0ae14eacd1a85d6afbf0f
- enabled: 1
path: Assets/_app/Scenes/Menu.unity
guid: dcde3a9c9eeecd741b2ed1da035a8509
- enabled: 1
path: Assets/_app/Scenes/Picking.unity
guid: 3aa5e051ae465864aaa5114b868d30ae
- enabled: 1
path: Assets/_app/Scenes/JsonTest.unity
guid: 3b2ea00127a0ae14eacd1a85d6afbf0f
m_configObjects: {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment