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

If unused #83

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/OsmSharp/Changesets/OsmChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ public partial class OsmChange
/// Gets or sets the delete change array.
/// </summary>
public OsmGeo[] Delete { get; set; }

/// <summary>
/// Gets or sets the deleteIfUnused change array.
/// </summary>
public OsmGeo[] DeleteIfUnused { get; set; }
}
}
67 changes: 42 additions & 25 deletions src/OsmSharp/IO/Xml/Changesets/OsmChange.Xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Linq;

namespace OsmSharp.Changesets
{
Expand All @@ -50,19 +51,20 @@ void IXmlSerializable.ReadXml(XmlReader reader)
this.Attribution = reader.GetAttribute("attribution");
this.License = reader.GetAttribute("license");

List<OsmGeo> creates = new List<OsmGeo>(); ;
List<OsmGeo> modifies = new List<OsmGeo>(); ;
List<OsmGeo> deletes = new List<OsmGeo>(); ;
List<OsmGeo> creates = new List<OsmGeo>();
List<OsmGeo> modifies = new List<OsmGeo>();
List<OsmGeo> deletes = new List<OsmGeo>();
List<OsmGeo> deletesIfUnused = new List<OsmGeo>();

reader.GetElements(
new Tuple<string, Action>(
"create", () =>
{
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
reader.Read();
while ((reader.Name == "node" ||
reader.Name == "way" ||
Expand All @@ -81,11 +83,11 @@ void IXmlSerializable.ReadXml(XmlReader reader)
new Tuple<string, Action>(
"modify", () =>
{
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
reader.Read();
while ((reader.Name == "node" ||
reader.Name == "way" ||
Expand All @@ -104,17 +106,18 @@ void IXmlSerializable.ReadXml(XmlReader reader)
new Tuple<string, Action>(
"delete", () =>
{
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
var ifUnused = reader.GetAttribute("if-unused") != null;
reader.Read();
while ((reader.Name == "node" ||
reader.Name == "way" ||
reader.Name == "relation"))
{
deletes.Add(OsmChange.ReadOsmGeo(reader));
(ifUnused ? deletesIfUnused : deletes).Add(OsmChange.ReadOsmGeo(reader));
if (reader.NodeType == XmlNodeType.EndElement && (reader.Name == "node" ||
reader.Name == "way" ||
reader.Name == "relation"))
Expand All @@ -128,6 +131,7 @@ void IXmlSerializable.ReadXml(XmlReader reader)
this.Create = creates.ToArray();
this.Modify = modifies.ToArray();
this.Delete = deletes.ToArray();
this.DeleteIfUnused = deletesIfUnused.ToArray();
}

private static OsmGeo ReadOsmGeo(XmlReader reader)
Expand Down Expand Up @@ -160,27 +164,40 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
if (this.Create != null)
{
writer.WriteStartElement("create");
for (var i = 0; i < this.Create.Length; i++)
// Add in order: nodes, ways, relations
foreach (var OsmGeo in this.Create.OrderBy(g => g.Type))
{
OsmChange.WriteOsmGeo(writer, this.Create[i]);
OsmChange.WriteOsmGeo(writer, OsmGeo);
}
writer.WriteEndElement();
}
if (this.Modify != null)
{
writer.WriteStartElement("modify");
for (var i = 0; i < this.Modify.Length; i++)
foreach (var OsmGeo in this.Modify)
{
OsmChange.WriteOsmGeo(writer, this.Modify[i]);
OsmChange.WriteOsmGeo(writer, OsmGeo);
}
writer.WriteEndElement();
}
if (this.Delete != null)
{
writer.WriteStartElement("delete");
for (var i = 0; i < this.Delete.Length; i++)
// Delete elements in this order: relations, ways, nodes
foreach (var OsmGeo in this.Delete.OrderByDescending(g => g.Type))
{
OsmChange.WriteOsmGeo(writer, OsmGeo);
}
writer.WriteEndElement();
}
if (this.DeleteIfUnused != null)
{
writer.WriteStartElement("delete");
writer.WriteAttribute("if-unused", "true");
// Delete elements in this order: relations, ways, nodes
foreach (var OsmGeo in this.DeleteIfUnused.OrderByDescending(g => g.Type))
{
OsmChange.WriteOsmGeo(writer, this.Delete[i]);
OsmChange.WriteOsmGeo(writer, OsmGeo);
}
writer.WriteEndElement();
}
Expand Down
30 changes: 26 additions & 4 deletions test/OsmSharp.Test/IO/Xml/Changesets/OsmChangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,26 @@ public void TestSerialize()
{
Id = 300
}
},
DeleteIfUnused = new OsmGeo[]
{
new Node()
{
Id = 31
},
new Way()
{
Id = 301
},
new Relation()
{
Id = 3001
}
}
};

var result = osmChange.SerializeToXml();
Assert.AreEqual("<osmChange generator=\"OsmSharp\" version=\"0.6\"><create><node id=\"1\" /><way id=\"10\" /><relation id=\"100\" /></create><modify><node id=\"2\" /><way id=\"20\" /><relation id=\"200\" /></modify><delete><node id=\"3\" /><way id=\"30\" /><relation id=\"300\" /></delete></osmChange>",
Assert.AreEqual("<osmChange generator=\"OsmSharp\" version=\"0.6\"><create><node id=\"1\" /><way id=\"10\" /><relation id=\"100\" /></create><modify><node id=\"2\" /><way id=\"20\" /><relation id=\"200\" /></modify><delete><relation id=\"300\" /><way id=\"30\" /><node id=\"3\" /></delete><delete if-unused=\"true\"><relation id=\"3001\" /><way id=\"301\" /><node id=\"31\" /></delete></osmChange>",
result);
}

Expand All @@ -109,6 +124,7 @@ public void TestDeserialize_Empty()
Assert.IsNotNull(osmChange);
Assert.AreEqual(0, osmChange.Create.Length);
Assert.AreEqual(0, osmChange.Delete.Length);
Assert.AreEqual(0, osmChange.DeleteIfUnused.Length);
Assert.AreEqual(0, osmChange.Modify.Length);
Assert.AreEqual(0.6, osmChange.Version);
Assert.IsNull(osmChange.Generator);
Expand All @@ -126,6 +142,7 @@ public void TestDeserialize_WithAttributes()
Assert.IsNotNull(osmChange);
Assert.AreEqual(0, osmChange.Create.Length);
Assert.AreEqual(0, osmChange.Delete.Length);
Assert.AreEqual(0, osmChange.DeleteIfUnused.Length);
Assert.AreEqual(0, osmChange.Modify.Length);
Assert.AreEqual(0.6, osmChange.Version);
Assert.AreEqual("OsmSharp", osmChange.Generator);
Expand Down Expand Up @@ -170,7 +187,10 @@ public void TestDeserialize_Full()
Assert.AreEqual(300, osmChange.Delete[2].Id);
Assert.AreEqual(OsmGeoType.Relation, osmChange.Delete[2].Type);

Assert.AreEqual(0.6, osmChange.Version);
Assert.IsNotNull(osmChange.DeleteIfUnused);
Assert.AreEqual(0, osmChange.DeleteIfUnused.Length);

Assert.AreEqual(0.6, osmChange.Version);
Assert.AreEqual("OsmSharp", osmChange.Generator);

}
Expand All @@ -181,15 +201,17 @@ public void TestDeserialize_IfUnused()
var serializer = new XmlSerializer(typeof(OsmChange));

var osmChange = serializer.Deserialize(
new StringReader("<osmChange version=\"0.6\" generator=\"iD\"><create/><modify><node id=\"1014872736\" lon=\"4.793814787696839\" lat=\"51.26403992993145\" version=\"1470\" changeset=\"2\"/></modify><delete if-unused=\"true\"/></osmChange>")) as OsmChange;
new StringReader("<osmChange version=\"0.6\" generator=\"iD\"><create/><modify><node id=\"1014872736\" lon=\"4.793814787696839\" lat=\"51.26403992993145\" version=\"1470\" changeset=\"2\"/></modify><delete if-unused=\"true\"><node id=\"3\" /></delete></osmChange>")) as OsmChange;
Assert.IsNotNull(osmChange);

Assert.AreEqual(0 ,osmChange.Create.Length);
Assert.AreEqual(0, osmChange.Create.Length);
Assert.IsNotNull(osmChange.Modify);
Assert.AreEqual(1, osmChange.Modify.Length);
Assert.AreEqual(1014872736, osmChange.Modify[0].Id);
Assert.AreEqual(OsmGeoType.Node, osmChange.Modify[0].Type);
Assert.AreEqual(0, osmChange.Delete.Length);
Assert.AreEqual(1, osmChange.DeleteIfUnused.Length);
Assert.AreEqual(3, osmChange.DeleteIfUnused[0].Id);
Assert.AreEqual(0.6, osmChange.Version);
Assert.AreEqual("iD", osmChange.Generator);
}
Expand Down