internal static ChangeSet ConvertToSimple(Osm.Xml.v0_6.delete delete)
        {
            // create change set record.
            OsmSharp.Osm.ChangeSet change_set = new OsmSharp.Osm.ChangeSet();

            // create change record.
            OsmSharp.Osm.Change change = new OsmSharp.Osm.Change();
            change.Type   = OsmSharp.Osm.ChangeType.Delete;
            change.OsmGeo = new List <OsmGeo>();

            // add all relations to the list.
            if (delete.relation != null)
            {
                foreach (Osm.Xml.v0_6.relation osm_geo in delete.relation)
                {
                    change.OsmGeo.Add(XmlSimpleConverter.ConvertToSimple(osm_geo));
                }
            }

            // add all ways to the list.
            if (delete.way != null)
            {
                foreach (Osm.Xml.v0_6.way osm_geo in delete.way)
                {
                    change.OsmGeo.Add(XmlSimpleConverter.ConvertToSimple(osm_geo));
                }
            }

            // add all nodes to the list.
            if (delete.node != null)
            {
                foreach (Osm.Xml.v0_6.node osm_geo in delete.node)
                {
                    change.OsmGeo.Add(XmlSimpleConverter.ConvertToSimple(osm_geo));
                }
            }


            // add change to changeset
            change_set.Changes = new List <Change>();
            change_set.Changes.Add(change);

            return(change_set);
        }
 /// <summary>
 /// Apply the given changeset.
 /// </summary>
 /// <param name="change_set"></param>
 public override void ApplyChange(ChangeSet change_set)
 {
     if (change_set != null && change_set.Changes != null)
     {
         foreach (Change change in change_set.Changes)
         {
             switch (change.Type)
             {
                 case ChangeType.Create:
                     foreach (OsmGeo geo in change.OsmGeo)
                     {
                         // start applying the simplechange.
                         OracleTransaction trans = _connection.BeginTransaction();
                         try
                         {
                             if (geo is Node)
                             {
                                 this.Create(geo as Node);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "+(n:{0})", geo.Id.Value);
                             }
                             else if (geo is Way)
                             {
                                 this.Create(geo as Way);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "+(w:{0})", geo.Id.Value);
                             }
                             else if (geo is Relation)
                             {
                                 this.Create(geo as Relation);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "+(r:{0})", geo.Id.Value);
                             }
                             trans.Commit();
                         }
                         catch (OracleException ex)
                         {
                             trans.Rollback();
                             if (!_pragmatic)
                             {
                                 throw ex;
                             }
                             else
                             {
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "+(E:{0}-{1})", geo.Id.Value, geo.Type.ToString());
                             }
                         }
                     }
                     break;
                 case ChangeType.Delete:
                     foreach (OsmGeo geo in change.OsmGeo)
                     {
                         // start applying the simplechange.
                         OracleTransaction trans = _connection.BeginTransaction();
                         try
                         {
                             if (geo is Node)
                             {
                                 this.Delete(geo as Node);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "-(n:{0})", geo.Id.Value);
                             }
                             else if (geo is Way)
                             {
                                 this.Delete(geo as Way);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "-(w:{0})", geo.Id.Value);
                             }
                             else if (geo is Relation)
                             {
                                 this.Delete(geo as Relation);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "-(r:{0})", geo.Id.Value);
                             }
                             trans.Commit();
                         }
                         catch (OracleException ex)
                         {
                             trans.Rollback();
                             if (!_pragmatic)
                             {
                                 throw ex;
                             }
                             else
                             {
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "-(E:{0}-{1})", geo.Id.Value, geo.Type.ToString());
                             }
                         }
                     }
                     break;
                 case ChangeType.Modify:
                     foreach (OsmGeo geo in change.OsmGeo)
                     {
                         // start applying the simplechange.
                         OracleTransaction trans = _connection.BeginTransaction();
                         try
                         {
                             if (geo is Node)
                             {
                                 this.Modify(geo as Node);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "/(n:{0})", geo.Id.Value);
                             }
                             else if (geo is Way)
                             {
                                 this.Modify(geo as Way);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "/(w:{0})", geo.Id.Value);
                             }
                             else if (geo is Relation)
                             {
                                 this.Modify(geo as Relation);
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "/(r:{0})", geo.Id.Value);
                             }
                             trans.Commit();
                         }
                         catch (OracleException ex)
                         {
                             trans.Rollback();
                             if (!_pragmatic)
                             {
                                 throw ex;
                             }
                             else
                             {
                                 OsmSharp.Logging.Log.TraceEvent("OsmSharp.Data.Oracle.Osm.Streams.OracleOsmChangesetStreamTarget", System.Diagnostics.TraceEventType.Information,
                                     "/(E:{0}-{1})", geo.Id.Value, geo.Type.ToString());
                             }
                         }
                     }
                     break;
             }
         }
     }
 }
示例#3
0
        /// <summary>
        /// Applies the given changeset to the data in this datasource.
        /// </summary>
        /// <param name="change_set"></param>
        public void ApplyChangeSet(ChangeSet change_set)
        {
            // test if the changeset was not already applied.
            if (_closed_change_set.Contains(change_set.Id))
            {
                throw new InvalidOperationException("Cannot apply an already closed changeset!");
            }

            // change the objects in the changeset.
            foreach (Change change in change_set.Changes)
            {
                switch (change.Type)
                {
                    case ChangeType.Create:
                        // set the changeset and version field.
                        change.Object.ChangeSetId = change_set.Id;
                        change.Object.Version = 0;

                        switch (change.Object.Type)
                        {
                            case OsmType.Node:
                                this.AddNode(change.Object as Node);
                                break;
                            case OsmType.Relation:
                                this.AddRelation(change.Object as Relation);
                                break;
                            case OsmType.Way:
                                this.AddWay(change.Object as Way);
                                break;
                        }
                        break;
                    case ChangeType.Delete:

                        switch (change.Object.Type)
                        {
                            case OsmType.Node:
                                this.RemoveNode(change.Object as Node);
                                break;
                            case OsmType.Relation:
                                this.RemoveRelation(change.Object as Relation);
                                break;
                            case OsmType.Way:
                                this.RemoveWay(change.Object as Way);
                                break;
                        }
                        this.RegisterChangeSetId(change_set.Id);
                        break;
                    case ChangeType.Modify:

                        // update the changeset field and the version field.
                        change.Object.ChangeSetId = change_set.Id;
                        if (change.Object.Version.HasValue)
                        {
                            change.Object.Version =  change.Object.Version.Value + 1;
                        }

                        switch (change.Object.Type)
                        {
                            case OsmType.Node:
                                _nodes[change.Object.Id] = (change.Object as Node);
                                break;
                            case OsmType.Relation:
                                _relations[change.Object.Id] = (change.Object as Relation);

                                // update the relations data.
                                IList<long> old_members_to_remove = new List<long>();
                                foreach (KeyValuePair<long, List<long>> pair in _relations_per_member)
                                {
                                    if (pair.Value.Contains(change.Object.Id))
                                    {
                                        pair.Value.Remove(change.Object.Id);
                                        // remove the old members that are only used in this relation.
                                        if (pair.Value.Count == 0)
                                        {
                                            old_members_to_remove.Add(pair.Key);
                                        }
                                    }
                                }
                                foreach (int old_member in old_members_to_remove)
                                {
                                    _relations_per_member.Remove(old_member);
                                }

                                // re-index the relation.
                                this.RegisterRelationMemberRelation(change.Object as Relation);
                                break;
                            case OsmType.Way:
                                _ways[change.Object.Id] = (change.Object as Way);

                                // update the way-node relation data.
                                IList<long> old_nodes_to_remove = new List<long>();
                                foreach (KeyValuePair<long, List<long>> pair in _ways_per_node)
                                {
                                    if (pair.Value.Contains(change.Object.Id))
                                    {
                                        pair.Value.Remove(change.Object.Id);

                                        // remove the old nodes that are only used in this way.
                                        if (pair.Value.Count == 0)
                                        {
                                            old_nodes_to_remove.Add(pair.Key);
                                        }
                                    }
                                }
                                foreach (long old_node in old_nodes_to_remove)
                                {
                                    _ways_per_node.Remove(old_node);
                                }

                                // re-index the relation.
                                this.RegisterNodeWayRelation(change.Object as Way);

                                // remove unused nodes.
                                // => nodes that are not re-indexed!
                                foreach (long old_node in old_nodes_to_remove)
                                {
                                    if (!_ways_per_node.ContainsKey(old_node))
                                    {
                                        this.RemoveNode(this.GetNode(old_node));
                                    }
                                    _ways_per_node.Remove(old_node);
                                }
                                break;
                        }
                        this.RegisterChangeSetId(change_set.Id);
                        break;
                }
            }
        }
示例#4
0
        /// <summary>
        /// Converts an API v6 xml osmChange object to a SimpleChange object.
        /// </summary>
        /// <param name="osm_change"></param>
        /// <returns></returns>
        private ChangeSet Convertv6XmlChanges(OsmSharp.Osm.Xml.v0_6.osmChange osm_change)
        {
            List<Change> changes = new List<Change>();

            if (osm_change.create != null)
            {
                for (int idx = 0; idx < osm_change.create.Length; idx++)
                {
                    OsmSharp.Osm.Xml.v0_6.create create = osm_change.create[idx];

                    List<OsmGeo> changed_objects = new List<OsmGeo>();

                    if (create.node != null)
                    { // change represents a change in a node.
                        for (int node_idx = 0; node_idx < create.node.Length; node_idx++)
                        {
                            changed_objects.Add(this.Convertv6XmlNode(create.node[node_idx]));
                        }
                    }
                    if (create.way != null)
                    { // change represents a change in a way.
                        for (int way_idx = 0; way_idx < create.way.Length; way_idx++)
                        {
                            changed_objects.Add(this.Convertv6XmlWay(create.way[way_idx]));
                        }
                    }
                    if (create.relation != null)
                    { // change represents a change in a relation.
                        for (int relation_idx = 0; relation_idx < create.relation.Length; relation_idx++)
                        {
                            changed_objects.Add(this.Convertv6XmlRelation(create.relation[relation_idx]));
                        }
                    }

                    if (changed_objects.Count > 0)
                    { // there are actually changed objects.
                        changes.Add(new OsmSharp.Osm.Change()
                        {
                            OsmGeo = changed_objects,
                            Type = OsmSharp.Osm.ChangeType.Create
                        });
                    }
                }
            }

            ChangeSet simple_change_set = new ChangeSet();
            simple_change_set.Changes = changes;

            return simple_change_set;
        }
 /// <summary>
 /// Applies a change to the target.
 /// </summary>
 /// <param name="change"></param>
 public abstract void ApplyChange(ChangeSet change);