Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenStreetMap_Engine: Update ElementContainer support HTTP Adapter for requests #27

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 115 additions & 62 deletions OpenStreetMap_Engine/Convert/ToElementContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
using BH.oM.OpenStreetMap;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ComponentModel;
using BH.oM.Reflection.Attributes;
using BH.oM.Base;
using BH.Engine.Serialiser;

namespace BH.Engine.OpenStreetMap
{
Expand All @@ -36,78 +34,84 @@ public static partial class Convert
/***************************************************/
/**** Public Methods ****/
/***************************************************/
/***************************************************/
[Description("Create an OpenStreetMap_oM ElementContainer from JSON formatted query result")]
[Input("openStreetMapQueryJSONResult", "string formatted as JSON")]
[Output("elementContainer", "ElementContainer containing the objects defined in the JSON formatted query result")]
public static ElementContainer ToElementContainer(string openStreetMapQueryJSONResult)
{
return ToElementContainer(new List<CustomObject> { (CustomObject)Serialiser.Convert.FromJson(openStreetMapQueryJSONResult) });
}
/***************************************************/
[Description("Create an OpenStreetMap_oM ElementContainer from HTTP Pull results")]
[Input("queryResults", "results from the HTTP_Adapter")]
[Output("elementContainer", "ElementContainer containing the objects defined in the results from the HTTP_Adapter")]
public static ElementContainer ToElementContainer(List<CustomObject> queryResults)
{
List<Way> ways = new List<Way>();
List<Node> nodes = new List<Node>();
if (openStreetMapQueryJSONResult == null) return new ElementContainer();
JObject data;
using (JsonTextReader reader = new JsonTextReader(new StringReader(openStreetMapQueryJSONResult)))
List<Relation> relations = new List<Relation>();
foreach (CustomObject result in queryResults)
{
data = (JObject)JToken.ReadFrom(reader);
}
var ele = data.SelectToken("elements");
if (ele is JArray)
{
foreach (JObject g in ele)
if (result.CustomData.ContainsKey("elements"))
{
if ((string)g.SelectToken("type") == "node")
List<object> elements = (List<object>)result.CustomData["elements"];// .Cast<CustomObject>();
CustomObject tags = new CustomObject();
foreach (object ele in elements)
{
double latitude = 0;
double longitude = 0;
long id = 0;
double.TryParse((string)g.SelectToken("lat"), out latitude);
double.TryParse((string)g.SelectToken("lon"), out longitude);
long.TryParse((string)g.SelectToken("id"), out id);
Node node = new Node()
try
{
Latitude = latitude,
Longitude = longitude,
OsmID = id
};
//add key values to node
var tags = g.SelectToken("tags");
if (tags != null)
{
foreach (JProperty jp in tags)
CustomObject element = (CustomObject)ele;
long id = 0;
if (element.CustomData["id"] is int)
id = (int)element.CustomData["id"];
if (element.CustomData["id"] is long)
id = (long)element.CustomData["id"];
if (element.CustomData.ContainsKey("tags"))
tags = (CustomObject)element.CustomData["tags"];
if ((string)element.CustomData["type"] == "node")
{
node.KeyValues.Add(jp.Name, (string)jp.Value);
double latitude = (double)element.CustomData["lat"];
double longitude = (double)element.CustomData["lon"];
Node node = new Node()
{
Latitude = latitude,
Longitude = longitude,
OsmID = id
};
node.AddTags(tags);
nodes.Add(node);
}
}
nodes.Add(node);
}
if ((string)g.SelectToken("type") == "way")
{
List<Int64> ids = new List<Int64>();
long id = 0;
long.TryParse((string)g.SelectToken("id"), out id);
Way way = new Way()
{
NodeOsmIds = ids,
OsmID = id
};
var n = g.SelectToken("nodes");
if (n is JArray)
{
for (int i = 0; i < n.Count(); i++)
if ((string)element.CustomData["type"] == "way")
{
long.TryParse((string)n[i], out id);
ids.Add(id);
List<object> ids = new List<object>();
if (element.CustomData.ContainsKey("nodes"))
ids = (List<object>)element.CustomData["nodes"];
Way way = new Way()
{
OsmID = id
};
way.AddTags(tags);
way.AddNodeIds(ids);
ways.Add(way);
}
}
var tags = g.SelectToken("tags");
if (tags != null)
{
foreach (JProperty jp in tags)
if ((string)element.CustomData["type"] == "relation")
{
way.KeyValues.Add(jp.Name, (string)jp.Value);
List<object> members = new List<object>();
if (element.CustomData.ContainsKey("members"))
members = (List<object>)element.CustomData["members"];
Relation relation = new Relation()
{
OsmID = id
};
relation.AddTags(tags);
relation.AddMembers(members);
relations.Add(relation);
}
}
ways.Add(way);
catch (Exception e)
{
throw new ArgumentException("Unexpected data format from OpenStreetMap " + e.Message);
}
}
}
}
Expand All @@ -120,10 +124,59 @@ public static ElementContainer ToElementContainer(string openStreetMapQueryJSONR
return new ElementContainer()
{
Nodes = nodes,
Ways = ways
};
Ways = ways,
Relations = relations
};

}
/***************************************************/
private static void AddTags(this IOpenStreetMapElement element, CustomObject tags)
{
if (tags == null) return;
foreach (var kvp in tags.CustomData)
{
if (!element.KeyValues.ContainsKey(kvp.Key))
element.KeyValues.Add(kvp.Key, (string)kvp.Value);
}
}
/***************************************************/
private static void AddNodeIds(this Way way, List<object> ids)
{
if (ids == null) return;
foreach (object id in ids)
{
if (id is long)
way.NodeOsmIds.Add((long)id);
if (id is int)
way.NodeOsmIds.Add((int)id);
}
}
/***************************************************/
private static void AddMembers(this Relation relation, List<object> members)
{
if (members == null) return;
foreach (object member in members)
{
CustomObject element = (CustomObject)member;
long osmid = 0;
if (element.CustomData["ref"] is int)
osmid = (int)element.CustomData["ref"];
if (element.CustomData["ref"] is long)
osmid = (long)element.CustomData["ref"];
string role = "";
if (element.CustomData.ContainsKey("role"))
role = (string)element.CustomData["role"];
IOpenStreetMapElement openStreetMapElement = null;
if ((string)element.CustomData["type"] == "node")
openStreetMapElement = new Node() { OsmID = osmid };
if ((string)element.CustomData["type"] == "way")
openStreetMapElement = new Way() { OsmID = osmid };
if ((string)element.CustomData["type"] == "relation")
openStreetMapElement = new Relation() { OsmID = osmid };
if (role != "")
openStreetMapElement.KeyValues.Add("role", role);
relation.Members.Add(openStreetMapElement);
}
}
}
}

}
4 changes: 4 additions & 0 deletions OpenStreetMap_Engine/OpenStreetMap_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<HintPath>..\..\..\BHoM\Build\Reflection_oM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Serialiser_Engine">
<HintPath>..\..\BHoM_Engine\Build\Serialiser_Engine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
2 changes: 2 additions & 0 deletions OpenStreetMap_oM/Elements/ElementContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ElementContainer : BHoMObject

public virtual List<Node> Nodes { get; set; } = new List<Node>();
public virtual List<Way> Ways { get; set; } = new List<Way>();
public List<Relation> Relations { get; set; } = new List<Relation>();


/***************************************************/
}
Expand Down
1 change: 1 addition & 0 deletions OpenStreetMap_oM/Elements/Relation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Relation : BHoMObject, IOpenStreetMapElement

public virtual long OsmID { get; set; }
public virtual Dictionary<string, string> KeyValues { get; set; } = new Dictionary<string, string>();
public List<IOpenStreetMapElement> Members { get; set; } = new List<IOpenStreetMapElement>();

}
}
Binary file modified examples/M25.gh
Binary file not shown.
Binary file modified examples/SearchAlongLineString.gh
Binary file not shown.
Binary file modified examples/SearchBoundingBox.gh
Binary file not shown.
Binary file modified examples/SearchBoundingBox2.gh
Binary file not shown.
Binary file modified examples/SearchCentreRadius.gh
Binary file not shown.
Binary file modified examples/SearchInPolygon.gh
Binary file not shown.