Exemplo n.º 1
0
        /// <summary>
        /// This function assembles a version of the logged relation action from the revision table in the OsmChange format http://wiki.openstreetmap.org/wiki/OsmChange. 
        /// </summary>
        /// <param name="relationTable"></param>
        /// <param name="action"></param>
        /// <param name="osmID"></param>
        /// <param name="changeSetID"></param>
        /// <param name="osmVersion"></param>
        /// <param name="nodeosmIDLookup"></param>
        /// <param name="wayosmIDLookup"></param>
        /// <param name="relationosmIDLookup"></param>
        /// <param name="extensionVersion"></param>
        /// <returns></returns>
        private ESRI.ArcGIS.OSM.OSMClassExtension.osm CreateOSMRelationRepresentation(ITable relationTable, string action, long osmID, string changeSetID, int osmVersion, Dictionary<long, long> nodeosmIDLookup, Dictionary<long, long> wayosmIDLookup, Dictionary<long, long> relationosmIDLookup, int extensionVersion)
        {
            ESRI.ArcGIS.OSM.OSMClassExtension.osm relationOSMPresentation = new ESRI.ArcGIS.OSM.OSMClassExtension.osm();

            relationOSMPresentation.Items = new object[] { CreateRelationRepresentation(relationTable, action, osmID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, extensionVersion) };

            return relationOSMPresentation;
        }
Exemplo n.º 2
0
        private ESRI.ArcGIS.OSM.OSMClassExtension.osm CreateOSMWayRepresentation(IFeatureClass featureClass, string action, long osmID, string changeSetID, int osmVersion, Dictionary<long, long> osmIDLookup, IFeatureClass pointFeatureClass, int pointOSMIDFieldIndex, int extensionVersion)
        {
            osm wayOSMPresentation = new osm();

            wayOSMPresentation.Items = new object[] { CreateWayRepresentation(featureClass, action, osmID, changeSetID, osmVersion, osmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, extensionVersion) };

            return wayOSMPresentation;
        }
Exemplo n.º 3
0
        private ESRI.ArcGIS.OSM.OSMClassExtension.osm CreateOSMNodeRepresentation(IFeatureClass pointFeatureClass, string action, long osmID, string osmChangeSetID, int osmVersion, IPoint deletePoint, int extensionVersion)
        {
            ESRI.ArcGIS.OSM.OSMClassExtension.osm nodeOSMPresentation = null;

            node simpleNode = CreateNodeRepresentation(pointFeatureClass, action, osmID, osmChangeSetID, osmVersion, deletePoint, extensionVersion);

            // if the node representation is unsuccessfull then return a null pointer
            if (simpleNode != null)
            {
                nodeOSMPresentation = new ESRI.ArcGIS.OSM.OSMClassExtension.osm();
                nodeOSMPresentation.Items = new object[] { simpleNode };
            }

            return nodeOSMPresentation;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Closes the current changeset and opens a new one
        /// </summary>
        /// <param name="message"></param>
        /// <param name="httpResponse"></param>
        /// <param name="httpClient"></param>
        /// <param name="changeSetID">returns the id of the new changeset</param>
        /// <param name="baseURLGPString"></param>
        /// <param name="featureUpdateCounter"></param>
        /// <param name="httpContent"></param>
        private void CreateNextChangeSet(ESRI.ArcGIS.Geodatabase.IGPMessages message, osm createChangeSetDocument, string authenticationHeader, int secondsToTimeout, ref string changeSetID, IGPString baseURLGPString, ref int featureUpdateCounter)
        {
            // close the existing changeset
            HttpWebResponse httpResponse = null;

            HttpWebRequest httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/" + changeSetID + "/close") as HttpWebRequest;
            httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
            httpClient.Method = "PUT";

            SetBasicAuthHeader(httpClient, authenticationHeader);

            httpClient.Timeout = secondsToTimeout * 1000;

            try
            {
                httpResponse = httpClient.GetResponse() as HttpWebResponse;
                message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPUpload_closeChangeSet"), changeSetID));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                if (httpResponse != null)
                {
                    foreach (var errorItem in httpResponse.Headers.GetValues("Error"))
                    {
                        message.AddError(120009, errorItem);
                    }
                    httpResponse.Close();
                }

                throw ex;
            }

            featureUpdateCounter = 1;

            // open/create a new changeset
            // ----------------------------------------
            try
            {
                string sData = OsmRest.SerializeUtils.CreateXmlSerializable(createChangeSetDocument, null, Encoding.ASCII, "text/xml");

                httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/create") as HttpWebRequest;
                httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                httpClient.Method = "PUT";
                SetBasicAuthHeader(httpClient, authenticationHeader);
                httpClient.Timeout = secondsToTimeout * 1000;

                Stream requestStream = httpClient.GetRequestStream();
                StreamWriter mywriter = new StreamWriter(requestStream);

                mywriter.Write(sData);
                mywriter.Close();

                httpResponse = httpClient.GetResponse() as HttpWebResponse;
            }
            catch (Exception ex)
            {
                if (httpResponse != null)
                {
                    if (httpResponse.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        foreach (var errorItem in httpResponse.Headers.GetValues("Error"))
                        {
                            message.AddError(120009, errorItem);
                        }

                        message.AddError(120009, httpResponse.StatusCode.ToString());
                    }
                    else
                    {
                        message.AddError(120009, ex.Message);
                    }
                    httpResponse.Close();
                }

                throw ex;
            }

            // updated changeset id
            Stream responseStreamclient = httpResponse.GetResponseStream();
            StreamReader myreader = new StreamReader(responseStreamclient);
            changeSetID = myreader.ReadToEnd();

            myreader.Close();
            httpResponse.Close();

            message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPUpload_openChangeSet"), changeSetID));
        }
Exemplo n.º 5
0
        public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message)
        {
            _message = message;

            // classes to carry out the basic client/server communication
            HttpWebResponse httpResponse = null;
            string changeSetID = "-1";
            IGPString baseURLGPString = new GPStringClass();
            ICursor searchCursor = null;

            IGPUtilities3 gpUtilities3 = new GPUtilitiesClass();

            if (TrackCancel == null)
            {
                TrackCancel = new CancelTrackerClass();
            }

            IGPParameter userNameParameter = paramvalues.get_Element(in_userNameNumber) as IGPParameter;
            IGPString userNameGPValue = gpUtilities3.UnpackGPValue(userNameParameter) as IGPString;

            IHttpBasicGPValue userCredentialGPValue = new HttpBasicGPValue();

            if (userNameGPValue != null)
            {
                userCredentialGPValue.UserName = userNameGPValue.Value;
            }
            else
            {
                return;
            }

            IGPParameter passwordParameter = paramvalues.get_Element(in_passwordNumber) as IGPParameter;
            IGPStringHidden passwordGPValue = gpUtilities3.UnpackGPValue(passwordParameter) as IGPStringHidden;

            if (passwordGPValue != null)
            {
                userCredentialGPValue.PassWord = passwordGPValue.Value;
            }
            else
            {
                return;
            }

            ITable revisionTable = null;
            int secondsToTimeout = 10;

            try
            {
                UpdateMessages(paramvalues, envMgr, message);

                if ((message.MaxSeverity == esriGPMessageSeverity.esriGPMessageSeverityAbort) ||
                    (message.MaxSeverity == esriGPMessageSeverity.esriGPMessageSeverityError))
                {
                    message.AddMessages(message);
                    return;
                }

                IGPParameter baseURLParameter = paramvalues.get_Element(in_uploadURLNumber) as IGPParameter;
                baseURLGPString = gpUtilities3.UnpackGPValue(baseURLParameter) as IGPString;

                IGPParameter commentParameter = paramvalues.get_Element(in_uploadCommentNumber) as IGPParameter;
                IGPString uploadCommentGPString = gpUtilities3.UnpackGPValue(commentParameter) as IGPString;

                ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass() as ISpatialReferenceFactory;
                m_wgs84 = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984) as ISpatialReference;

                System.Xml.Serialization.XmlSerializer serializer = null;
                serializer = new XmlSerializer(typeof(osm));

                osm createChangeSetOSM = new osm();
                string user_displayname = "";
                int userID = -1;

                // set the "default" value of the OSM server
                int maxElementsinChangeSet = 50000;

                HttpWebRequest httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/capabilities") as HttpWebRequest;
                httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                httpClient.Timeout = secondsToTimeout * 1000;

                createChangeSetOSM.generator = m_Generator;
                createChangeSetOSM.version = "0.6";

                changeset createChangeSet = new changeset();
                createChangeSet.id = "0";
                createChangeSet.open = changesetOpen.@false;

                List<tag> changeSetTags = new List<tag>();

                tag createdByTag = new tag();
                createdByTag.k = "created_by";
                createdByTag.v = "ArcGIS Editor for OpenStreetMap";
                changeSetTags.Add(createdByTag);

                tag commentTag = new tag();
                commentTag.k = "comment";
                commentTag.v = uploadCommentGPString.Value;
                changeSetTags.Add(commentTag);

                createChangeSet.tag = changeSetTags.ToArray();
                createChangeSetOSM.Items = new object[] { createChangeSet };

                api apiCapabilities = null;

                // retrieve some server settings

                try
                {
                    httpResponse = httpClient.GetResponse() as HttpWebResponse;

                    osm osmCapabilities = null;

                    Stream stream = httpResponse.GetResponseStream();

                    XmlTextReader xmlReader = new XmlTextReader(stream);
                    osmCapabilities = serializer.Deserialize(xmlReader) as osm;
                    xmlReader.Close();

                    apiCapabilities = osmCapabilities.Items[0] as api;
                    httpResponse.Close();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    message.AddWarning(ex.Message);
                }

                if (apiCapabilities != null)
                {
                    // read the timeout parameter
                    secondsToTimeout = Convert.ToInt32(apiCapabilities.timeout.seconds);
                    httpClient.Timeout = secondsToTimeout * 1000;

                    // update the setting of allowed features per changeset from the actual capabilities response
                    maxElementsinChangeSet = Convert.ToInt32(apiCapabilities.changesets.maximum_elements);
                }

                // retrieve some information about the user
                try
                {
                    httpClient = null;
                    httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/user/details") as HttpWebRequest;
                    httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                    SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);

                    httpResponse = httpClient.GetResponse() as HttpWebResponse;

                    osm osmCapabilities = null;

                    Stream stream = httpResponse.GetResponseStream();

                    XmlTextReader xmlReader = new XmlTextReader(stream);
                    osmCapabilities = serializer.Deserialize(xmlReader) as osm;
                    xmlReader.Close();
                    user userInformation = osmCapabilities.Items[0] as user;

                    if (userInformation != null)
                    {
                        user_displayname = userInformation.display_name;
                        userID = Convert.ToInt32(userInformation.id);
                    }
                }

                catch (ArgumentOutOfRangeException ex)
                {
                    message.AddError(120044, ex.Message);
                    return;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    message.AddWarning(ex.Message);
                }

                IGPParameter revisionTableParameter = paramvalues.get_Element(in_changesTablesNumber) as IGPParameter;
                int featureUpdateCounter = 0;

                IQueryFilter revisionTableQueryFilter = null;

                try
                {
                    gpUtilities3.DecodeTableView(gpUtilities3.UnpackGPValue(revisionTableParameter), out revisionTable, out revisionTableQueryFilter);
                }
                catch
                {
                    message.AddError(120045,resourceManager.GetString("GPTools_OSMGPUpload_missingRevisionTable"));
                    return;
                }

                int revChangeSetIDFieldIndex = revisionTable.Fields.FindField("osmchangeset");
                int revActionFieldIndex = revisionTable.Fields.FindField("osmaction");
                int revElementTypeFieldIndex = revisionTable.Fields.FindField("osmelementtype");
                int revVersionFieldIndex = revisionTable.Fields.FindField("osmversion");
                int revFCNameFieldIndex = revisionTable.Fields.FindField("sourcefcname");
                int revOldIDFieldIndex = revisionTable.Fields.FindField("osmoldid");
                int revNewIDFieldIndex = revisionTable.Fields.FindField("osmnewid");
                int revStatusFieldIndex = revisionTable.Fields.FindField("osmstatus");
                int revStatusCodeFieldIndex = revisionTable.Fields.FindField("osmstatuscode");
                int revErrorMessageFieldIndex = revisionTable.Fields.FindField("osmerrormessage");
                int revLongitudeFieldIndex = revisionTable.Fields.FindField("osmlon");
                int revLatitudeFieldIndex = revisionTable.Fields.FindField("osmlat");

                // let's find all the rows that have a different status than OK - meaning success
                IQueryFilter queryFilter = new QueryFilterClass();

                searchCursor = revisionTable.Search(queryFilter, false);
                IRow searchRowToUpdate = null;

                // lookup table to adjust all osm ID references if there are know entities
                Dictionary<long, long> nodeosmIDLookup = new Dictionary<long, long>();
                Dictionary<long, long> wayosmIDLookup = new Dictionary<long, long>();
                Dictionary<long, long> relationosmIDLookup = new Dictionary<long, long>();

                // let's pre-populate the lookup IDs with already know entities
                // it is necessary if the revision table is used more than once and in different sessions
                queryFilter.WhereClause = "NOT " + revisionTable.SqlIdentifier("osmnewid") + " IS NULL";

                using (ComReleaser comReleaser = new ComReleaser())
                {
                    ICursor searchIDCursor = revisionTable.Search(queryFilter, false);
                    comReleaser.ManageLifetime(searchIDCursor);

                    IRow searchRow = searchIDCursor.NextRow();
                    comReleaser.ManageLifetime(searchRow);

                    while (searchRow != null)
                    {
                        if (revOldIDFieldIndex > -1 && revNewIDFieldIndex > -1)
                        {
                            string elementType = Convert.ToString(searchRow.get_Value(revElementTypeFieldIndex));

                            switch (elementType)
                            {
                                case "node":
                                    if (nodeosmIDLookup.ContainsKey(Convert.ToInt64(searchRow.get_Value(revOldIDFieldIndex))) == false)
                                    {
                                        nodeosmIDLookup.Add(Convert.ToInt64(searchRow.get_Value(revOldIDFieldIndex)), Convert.ToInt64(searchRow.get_Value(revNewIDFieldIndex)));
                                    }
                                    break;
                                case "way":
                                    if (wayosmIDLookup.ContainsKey(Convert.ToInt64(searchRow.get_Value(revOldIDFieldIndex))) == false)
                                    {
                                        wayosmIDLookup.Add(Convert.ToInt64(searchRow.get_Value(revOldIDFieldIndex)), Convert.ToInt64(searchRow.get_Value(revNewIDFieldIndex)));
                                    }
                                    break;
                                case "relation":
                                    if (relationosmIDLookup.ContainsKey(Convert.ToInt64(searchRow.get_Value(revOldIDFieldIndex))) == false)
                                    {
                                        relationosmIDLookup.Add(Convert.ToInt64(searchRow.get_Value(revOldIDFieldIndex)), Convert.ToInt64(searchRow.get_Value(revNewIDFieldIndex)));
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        searchRow = searchIDCursor.NextRow();
                    }
                }

                IFeatureClass pointFeatureClass = null;
                int pointOSMIDFieldIndex = -1;
                IFeatureClass lineFeatureClass = null;
                IFeatureClass polygonFeatureClass = null;
                ITable relationTable = null;

                int osmDelimiterPosition = ((IDataset)revisionTable).Name.IndexOf("_osm_");
                string osmBaseName = ((IDataset)revisionTable).Name.Substring(0, osmDelimiterPosition);

                IFeatureWorkspace osmFeatureWorkspace = ((IDataset)revisionTable).Workspace as IFeatureWorkspace;

                if (osmFeatureWorkspace != null)
                {
                    pointFeatureClass = osmFeatureWorkspace.OpenFeatureClass(osmBaseName + "_osm_pt");
                    pointOSMIDFieldIndex = pointFeatureClass.FindField("OSMID");
                    lineFeatureClass = osmFeatureWorkspace.OpenFeatureClass(osmBaseName + "_osm_ln");
                    polygonFeatureClass = osmFeatureWorkspace.OpenFeatureClass(osmBaseName + "_osm_ply");
                    relationTable = osmFeatureWorkspace.OpenTable(osmBaseName + "_osm_relation");
                }

                // determine version of extension
                int internalExtensionVersion = pointFeatureClass.OSMExtensionVersion();

                string sData = OsmRest.SerializeUtils.CreateXmlSerializable(createChangeSetOSM, serializer, Encoding.ASCII, "text/xml");
                HttpWebRequest httpClient2 = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/create") as HttpWebRequest;
                httpClient2.Method = "PUT";
                httpClient2 = OSMGPDownload.AssignProxyandCredentials(httpClient2);
                SetBasicAuthHeader(httpClient2, userCredentialGPValue.EncodedUserNamePassWord);
                httpClient2.Timeout = secondsToTimeout * 1000;

                try
                {
                    Stream requestStream = httpClient2.GetRequestStream();
                    StreamWriter mywriter = new StreamWriter(requestStream);

                    mywriter.Write(sData);
                    mywriter.Close();

                    WebResponse clientResponse = httpClient2.GetResponse();
                    Stream readStream = clientResponse.GetResponseStream();
                    StreamReader streamReader = new StreamReader(readStream);
                    changeSetID = streamReader.ReadToEnd();
                    streamReader.Close();

                    message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPUpload_openChangeSet"), changeSetID));

                }
                catch (Exception ex)
                {
                    if (httpResponse != null)
                    {
                        if (httpResponse.StatusCode != System.Net.HttpStatusCode.OK)
                        {
                            foreach (var errorItem in httpResponse.Headers.GetValues("Error"))
                            {
                                message.AddError(120009, errorItem);
                            }

                            message.AddError(120009, httpResponse.StatusCode.ToString());
                            message.AddError(120009, ex.Message);
                        }
                    }
                    else
                    {
                        message.AddError(120047, ex.Message);
                    }

                    return;
                }

                IGPParameter uploadFormatParameter = paramvalues.get_Element(in_uploadFormatNumber) as IGPParameter;
                IGPBoolean useOSMChangeFormatGPValue = gpUtilities3.UnpackGPValue(uploadFormatParameter) as IGPBoolean;

                // Al Hack
                if (useOSMChangeFormatGPValue == null)
                {
                    useOSMChangeFormatGPValue = new GPBoolean();
                    useOSMChangeFormatGPValue.Value = false;
                }

                SQLFormatter sqlFormatter = new SQLFormatter(revisionTable);

                if (useOSMChangeFormatGPValue.Value == true)
                {
                    #region osmchange upload format

                    osmChange osmChangeDocument = new osmChange();
                    osmChangeDocument.generator = m_Generator;
                    osmChangeDocument.version = "0.6";

                    // xml elements to describe the changeset
                    create uploadCreates = null;
                    modify uploadModify = null;
                    delete uploadDelete = null;

                    // helper classes to keep track of elements entered into a changeset
                    List<object> listOfCreates = null;
                    List<object> listOfModifies = null;
                    List<object> listOfDeletes = null;

                    List<object> changeSetItems = new List<object>();

                    #region upload create actions
                    // loop through creates
                    queryFilter.WhereClause = "(" + sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'create'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        searchRowToUpdate = searchCursor.NextRow();
                        comReleaser.ManageLifetime(searchRowToUpdate);

                        // if we have at least one entry with a create action, then add the 'create' element to the changeset representation
                        if (searchRowToUpdate != null)
                        {
                            uploadCreates = new create();
                            listOfCreates = new List<object>();
                        }

                        while (searchRowToUpdate != null)
                        {
                            try
                            {
                                if (TrackCancel.Continue() == false)
                                {
                                    closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                    return;
                                }

                                string action = String.Empty;
                                if (revActionFieldIndex != -1)
                                {
                                    action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                }
                                string elementType = String.Empty;
                                if (revElementTypeFieldIndex != -1)
                                {
                                    elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                }

                                string sourceFCName = String.Empty;
                                if (revFCNameFieldIndex != -1)
                                {
                                    sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                }

                                long osmOldID = -1;
                                if (revOldIDFieldIndex != -1)
                                {
                                    osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                }

                                // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                                // into multiple sets
                                if (featureUpdateCounter > 0 & (featureUpdateCounter % maxElementsinChangeSet) == 0)
                                {
                                    // add any outstanding creations to the changeset items
                                    if (listOfCreates != null && uploadCreates != null)
                                    {
                                        uploadCreates.Items = listOfCreates.ToArray();
                                        // in case there are any creates let's add them to the changeset document
                                        changeSetItems.Add(uploadCreates);
                                    }

                                    // add all the changeset items to the changeset document
                                    osmChangeDocument.Items = changeSetItems.ToArray();

                                    // submit changeset
                                    try
                                    {
                                        httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/" + changeSetID + "/upload") as HttpWebRequest;
                                        httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                                        httpClient.Method = "POST";
                                        SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                                        httpClient.Timeout = secondsToTimeout * 1000;

                                        string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(osmChangeDocument, null, Encoding.UTF8, "text/xml");

                                        message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPUpload_featureSubmit"), featureUpdateCounter));

                                        OsmRest.HttpUtils.Post(httpClient, sContent);

                                        httpResponse = httpClient.GetResponse() as HttpWebResponse;
                                        diffResult diffResultResonse = OsmRest.HttpUtils.GetResponse(httpResponse);

                                        // parse changes locally and update local data sources
                                        if (diffResultResonse != null)
                                        {
                                            ParseResultDiff(diffResultResonse, revisionTable, pointFeatureClass, lineFeatureClass, polygonFeatureClass, relationTable, user_displayname, userID, changeSetID, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                        message.AddError(120009, ex.Message);

                                        if (ex is WebException)
                                        {
                                            WebException webException = ex as WebException;
                                            string serverErrorMessage = webException.Response.Headers["Error"];
                                            if (!String.IsNullOrEmpty(serverErrorMessage))
                                            {
                                                message.AddError(120009, serverErrorMessage);
                                            }
                                        }

                                        if (httpResponse != null)
                                        {
                                            httpResponse.Close();
                                        }
                                        return;
                                    }
                                    finally
                                    {
                                        // reset the list and containers of modifications for the next batch
                                        listOfCreates.Clear();
                                        changeSetItems.Clear();

                                        if (httpResponse != null)
                                        {
                                            httpResponse.Close();
                                        }
                                    }

                                    if (TrackCancel.Continue() == false)
                                    {
                                        closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                        return;
                                    }

                                    CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                }

                                switch (elementType)
                                {
                                    case "node":
                                        node createNode = CreateNodeRepresentation(pointFeatureClass, action, osmOldID, changeSetID, 1, null, internalExtensionVersion);
                                        listOfCreates.Add(createNode);
                                        break;
                                    case "way":
                                        way createWay = null;
                                        if (sourceFCName.Contains("_osm_ln"))
                                        {
                                            createWay = CreateWayRepresentation(lineFeatureClass, action, osmOldID, changeSetID, 1, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                        }
                                        else if (sourceFCName.Contains("_osm_ply"))
                                        {
                                            createWay = CreateWayRepresentation(polygonFeatureClass, action, osmOldID, changeSetID, 1, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                        }
                                        listOfCreates.Add(createWay);
                                        break;
                                    case "relation":
                                        relation createRelation = null;
                                        if (sourceFCName.Contains("_osm_ln"))
                                        {
                                            createRelation = CreateRelationRepresentation((ITable)lineFeatureClass, action, osmOldID, changeSetID, 1, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }
                                        else if (sourceFCName.Contains("_osm_ply"))
                                        {
                                            createRelation = CreateRelationRepresentation((ITable)polygonFeatureClass, action, osmOldID, changeSetID, 1, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }
                                        else if (sourceFCName.Contains("_osm_relation"))
                                        {
                                            createRelation = CreateRelationRepresentation(relationTable, action, osmOldID, changeSetID, 1, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }
                                        listOfCreates.Add(createRelation);
                                        break;
                                    default:
                                        break;
                                }
                                // increment the counter keeping track of the submitted changes
                                featureUpdateCounter = featureUpdateCounter + 1;
                            }
                            catch
                            {
                            }
                            searchRowToUpdate = searchCursor.NextRow();
                        }

                        if (listOfCreates != null && uploadCreates != null)
                        {
                            // sort the list of created elements in the order of nodes, ways, relations
                            listOfCreates.Sort(new OSMElementComparer());

                            uploadCreates.Items = listOfCreates.ToArray();
                            // in case there are any creates let's add them to the changeset document
                            changeSetItems.Add(uploadCreates);
                        }
                    }
                    #endregion

                    #region upload modify actions
                    // loop through modifies
                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        queryFilter.WhereClause = "(" +
                            sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                            + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                            + sqlFormatter.SqlIdentifier("osmaction") + " = 'modify'";

                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        searchRowToUpdate = searchCursor.NextRow();

                        if (searchRowToUpdate != null)
                        {
                            uploadModify = new modify();
                            listOfModifies = new List<object>();
                        }

                        while (searchRowToUpdate != null)
                        {
                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                return;
                            }

                            try
                            {
                                string action = String.Empty;
                                if (revActionFieldIndex != -1)
                                {
                                    action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                }

                                string elementType = String.Empty;
                                if (revElementTypeFieldIndex != -1)
                                {
                                    elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                }

                                string sourceFCName = String.Empty;
                                if (revFCNameFieldIndex != -1)
                                {
                                    sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                }

                                long osmOldID = -1;
                                if (revOldIDFieldIndex != -1)
                                {
                                    osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                }

                                // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                                long modifyID = -1;
                                if (revNewIDFieldIndex != -1)
                                {
                                    object osmIDValue = searchRowToUpdate.get_Value(revNewIDFieldIndex);

                                    if (osmIDValue == DBNull.Value)
                                    {
                                        osmIDValue = osmOldID;
                                    }

                                    try
                                    {
                                        modifyID = Convert.ToInt64(osmIDValue);
                                    }
                                    catch { }

                                    // modifies should only happen to osm IDs > 0
                                    // if that condition is not met let's skip this feature as something is not right
                                    if (modifyID < 0)
                                    {
                                        searchRowToUpdate = searchCursor.NextRow();
                                        continue;
                                    }
                                }

                                int osmVersion = -1;
                                if (revVersionFieldIndex != -1)
                                {
                                    osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                                }

                                // into multiple sets
                                if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                                {
                                    // add any outstanding modifications to the changeset items
                                    if (listOfModifies != null && uploadModify != null)
                                    {
                                        uploadModify.Items = listOfModifies.ToArray();
                                        // in case there are any creates let's add them to the changeset document
                                        changeSetItems.Add(uploadModify);
                                    }

                                    // add all the changeset items to the changeset document
                                    osmChangeDocument.Items = changeSetItems.ToArray();

                                    // submit changeset
                                    try
                                    {
                                        httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/" + changeSetID + "/upload") as HttpWebRequest;
                                        httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                                        httpClient.Method = "POST";
                                        SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                                        httpClient.Timeout = secondsToTimeout * 1000;

                                        string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(osmChangeDocument, null, Encoding.UTF8, "text/xml");
                                        OsmRest.HttpUtils.Post(httpClient, sContent);

                                        httpResponse = httpClient.GetResponse() as HttpWebResponse;
                                        diffResult diffResultResonse = OsmRest.HttpUtils.GetResponse(httpResponse);

                                        // parse changes locally and update local data sources
                                        if (diffResultResonse != null)
                                        {
                                            ParseResultDiff(diffResultResonse, revisionTable, pointFeatureClass, lineFeatureClass, polygonFeatureClass, relationTable, user_displayname, userID, changeSetID, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                        message.AddError(120009, ex.Message);

                                        if (ex is WebException)
                                        {
                                            WebException webException = ex as WebException;
                                            string serverErrorMessage = webException.Response.Headers["Error"];
                                            if (!String.IsNullOrEmpty(serverErrorMessage))
                                            {
                                                message.AddError(120009, serverErrorMessage);
                                            }

                                            if (httpResponse != null)
                                            {
                                                httpResponse.Close();
                                            }
                                        }
                                        return;
                                    }
                                    finally
                                    {
                                        // reset the list and containers of modifications for the next batch
                                        listOfModifies.Clear();
                                        changeSetItems.Clear();

                                        if (httpResponse != null)
                                        {
                                            httpResponse.Close();
                                        }
                                    }

                                    if (TrackCancel.Continue() == false)
                                    {
                                        closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                        return;
                                    }

                                    CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                }

                                switch (elementType)
                                {
                                    case "node":
                                        node updateNode = CreateNodeRepresentation(pointFeatureClass, action, modifyID, changeSetID, osmVersion, null, internalExtensionVersion);
                                        listOfModifies.Add(updateNode);
                                        break;
                                    case "way":
                                        way updateWay = null;
                                        if (sourceFCName.Contains("_osm_ln"))
                                        {
                                            updateWay = CreateWayRepresentation(lineFeatureClass, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                        }
                                        else if (sourceFCName.Contains("_osm_ply"))
                                        {
                                            updateWay = CreateWayRepresentation(polygonFeatureClass, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                        }
                                        listOfModifies.Add(updateWay);
                                        break;
                                    case "relation":
                                        relation updateRelation = null;
                                        if (sourceFCName.Contains("_osm_ln"))
                                        {
                                            updateRelation = CreateRelationRepresentation((ITable)lineFeatureClass, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }
                                        else if (sourceFCName.Contains("_osm_ply"))
                                        {
                                            updateRelation = CreateRelationRepresentation((ITable)polygonFeatureClass, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }
                                        else if (sourceFCName.Contains("_osm_relation"))
                                        {
                                            updateRelation = CreateRelationRepresentation(relationTable, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                        }
                                        listOfModifies.Add(updateRelation);
                                        break;
                                    default:
                                        break;
                                }

                                // track the update/sync requests against the server
                                featureUpdateCounter = featureUpdateCounter + 1;
                            }
                            catch
                            {
                            }

                            searchRowToUpdate = searchCursor.NextRow();
                        }

                        if (listOfModifies != null && uploadModify != null)
                        {
                            uploadModify.Items = listOfModifies.ToArray();
                            // in case there are any creates let's add them to the changeset document
                            changeSetItems.Add(uploadModify);
                        }

                    }
                    #endregion

                    #region upload delete actions
                    // loop through deletes in "reverse" - relation, then way, then node
                    string[] elementTypes = new string[] { "relation", "way", "node" };

                    foreach (string osmElementType in elementTypes)
                    {
                        using (ComReleaser comReleaser = new ComReleaser())
                        {
                            queryFilter.WhereClause = "(" +  sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                                + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                                + sqlFormatter.SqlIdentifier("osmaction") + " = 'delete' AND "
                                + sqlFormatter.SqlIdentifier("osmelementtype") + " = '" + osmElementType + "'";

                            searchCursor = revisionTable.Search(queryFilter, false);
                            comReleaser.ManageLifetime(searchCursor);

                            searchRowToUpdate = searchCursor.NextRow();

                            if (searchRowToUpdate != null)
                            {
                                if (TrackCancel.Continue() == false)
                                {
                                    closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                    return;
                                }

                                if (uploadDelete == null)
                                {
                                    uploadDelete = new delete();
                                    listOfDeletes = new List<object>();
                                }
                            }

                            while (searchRowToUpdate != null)
                            {
                                try
                                {
                                    string action = String.Empty;
                                    if (revActionFieldIndex != -1)
                                    {
                                        action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                    }

                                    string elementType = String.Empty;
                                    if (revElementTypeFieldIndex != -1)
                                    {
                                        elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                    }

                                    string sourceFCName = String.Empty;
                                    if (revFCNameFieldIndex != -1)
                                    {
                                        sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                    }

                                    long osmOldID = -1;
                                    if (revOldIDFieldIndex != -1)
                                    {
                                        osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                    }

                                    int osmVersion = -1;
                                    if (revVersionFieldIndex != -1)
                                    {
                                        osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                                    }

                                    // into multiple sets
                                    if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                                    {
                                        // add any outstanding creations to the changeset items
                                        if (listOfDeletes != null && uploadDelete != null)
                                        {
                                            uploadDelete.Items = listOfDeletes.ToArray();
                                            // in case there are any creates let's add them to the changeset document
                                            changeSetItems.Add(uploadDelete);
                                        }

                                        // add all the changeset items to the changeset document
                                        osmChangeDocument.Items = changeSetItems.ToArray();

                                        // submit changeset
                                        try
                                        {
                                            httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/" + changeSetID + "/upload") as HttpWebRequest;
                                            httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                                            httpClient.Method = "POST";
                                            SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                                            httpClient.Timeout = secondsToTimeout * 1000;

                                            string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(osmChangeDocument, null, Encoding.UTF8, "text/xml");
                                            OsmRest.HttpUtils.Post(httpClient, sContent);

                                            httpResponse = httpClient.GetResponse() as HttpWebResponse;
                                            diffResult diffResultResonse = OsmRest.HttpUtils.GetResponse(httpResponse);

                                            // parse changes locally and update local data sources
                                            if (diffResultResonse != null)
                                            {
                                                ParseResultDiff(diffResultResonse, revisionTable, pointFeatureClass, lineFeatureClass, polygonFeatureClass, relationTable, user_displayname, userID, changeSetID, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                            }

                                        }
                                        catch (Exception ex)
                                        {
                                            closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                            message.AddError(120009, ex.Message);

                                            if (ex is WebException)
                                            {
                                                WebException webException = ex as WebException;
                                                string serverErrorMessage = webException.Response.Headers["Error"];
                                                if (!String.IsNullOrEmpty(serverErrorMessage))
                                                {
                                                    message.AddError(120009, serverErrorMessage);
                                                }
                                            }

                                            if (httpResponse != null)
                                            {
                                                httpResponse.Close();
                                            }

                                            return;
                                        }
                                        finally
                                        {
                                            // reset the list and containers of modifications for the next batch
                                            listOfDeletes.Clear();
                                            changeSetItems.Clear();

                                            if (httpResponse != null)
                                            {
                                                httpResponse.Close();
                                            }
                                        }

                                        if (TrackCancel.Continue() == false)
                                        {
                                            closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                            return;
                                        }

                                        CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                    }

                                    switch (elementType)
                                    {
                                        case "node":
                                            IPoint deletePoint = null;
                                            if (revLongitudeFieldIndex != -1 && revLatitudeFieldIndex != -1)
                                            {
                                                try
                                                {
                                                    // let's reconstruct the delete point
                                                    deletePoint = new PointClass();
                                                    deletePoint.X = Convert.ToDouble(searchRowToUpdate.get_Value(revLongitudeFieldIndex));
                                                    deletePoint.Y = Convert.ToDouble(searchRowToUpdate.get_Value(revLatitudeFieldIndex));
                                                    deletePoint.SpatialReference = m_wgs84;
                                                }
                                                catch (Exception ex)
                                                {
                                                    message.AddWarning(ex.Message);
                                                }

                                                if (deletePoint == null)
                                                {
                                                    // inform the about the issue - no successful creation of point and continue on to the next delete instruction
                                                    // in the revision table
                                                    message.AddWarning(resourceManager.GetString("GPTools_OSMGPUpload_invalidPoint"));
                                                    searchRowToUpdate = searchCursor.NextRow();
                                                    continue;
                                                }
                                            }

                                            node deleteNode = CreateNodeRepresentation(pointFeatureClass, action, osmOldID, changeSetID, osmVersion, deletePoint, internalExtensionVersion);
                                            listOfDeletes.Add(deleteNode);
                                            break;
                                        case "way":
                                            way deleteWay = null;
                                            if (sourceFCName.Contains("_osm_ln"))
                                            {
                                                deleteWay = CreateWayRepresentation(lineFeatureClass, action, osmOldID, changeSetID, osmVersion, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                            }
                                            else if (sourceFCName.Contains("_osm_ply"))
                                            {
                                                deleteWay = CreateWayRepresentation(polygonFeatureClass, action, osmOldID, changeSetID, osmVersion, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                            }
                                            listOfDeletes.Add(deleteWay);
                                            break;
                                        case "relation":
                                            relation deleteRelation = null;
                                            if (sourceFCName.Contains("_osm_ln"))
                                            {
                                                deleteRelation = CreateRelationRepresentation((ITable)lineFeatureClass, action, osmOldID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                            }
                                            else if (sourceFCName.Contains("_osm_ply"))
                                            {
                                                deleteRelation = CreateRelationRepresentation((ITable)polygonFeatureClass, action, osmOldID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                            }
                                            else if (sourceFCName.Contains("_osm_relation"))
                                            {
                                                deleteRelation = CreateRelationRepresentation(relationTable, action, osmOldID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                            }
                                            listOfDeletes.Add(deleteRelation);
                                            break;
                                        default:
                                            break;
                                    }

                                    // track the update/sync requests against the server
                                    featureUpdateCounter = featureUpdateCounter + 1;
                                }
                                catch
                                {
                                }

                                searchRowToUpdate = searchCursor.NextRow();
                            }

                        }
                    }

                    if (listOfDeletes != null && uploadDelete != null)
                    {
                        uploadDelete.Items = listOfDeletes.ToArray();
                        // in case there are any creates let's add them to the changeset document
                        changeSetItems.Add(uploadDelete);
                    }
                    #endregion

                    if (TrackCancel.Continue() == false)
                    {
                        closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                        return;
                    }

                    // add all the changeset items to the changeset document
                    osmChangeDocument.Items = changeSetItems.ToArray();

                    // submit changeset
                    try
                    {
                        httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/changeset/" + changeSetID + "/upload") as HttpWebRequest;
                        httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                        httpClient.Method = "POST";
                        SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                        httpClient.Timeout = secondsToTimeout * 1000;

                        message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPUpload_featureSubmit"), featureUpdateCounter));

                        string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(osmChangeDocument, null, Encoding.UTF8, "text/xml");
                        OsmRest.HttpUtils.Post(httpClient, sContent);

                        httpResponse = httpClient.GetResponse() as HttpWebResponse;

                        //Exception with an error HTTP 400
                        diffResult diffResultResonse = OsmRest.HttpUtils.GetResponse(httpResponse);

                        message.AddMessage(resourceManager.GetString("GPTools_OSMGPUpload_updatelocalData"));

                        // parse changes locally and update local data sources
                        if (diffResultResonse != null)
                        {
                            ParseResultDiff(diffResultResonse, revisionTable, pointFeatureClass, lineFeatureClass, polygonFeatureClass, relationTable, user_displayname, userID, changeSetID, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                        }
                    }
                    catch (Exception ex)
                    {
                        message.AddError(120009, ex.Message);

                        try
                        {
                            if (ex is WebException)
                            {
                                WebException webException = ex as WebException;
                                string serverErrorMessage = webException.Response.Headers["Error"];
                                if (!String.IsNullOrEmpty(serverErrorMessage))
                                {
                                    message.AddError(120009, serverErrorMessage);
                                }
                            }
                        }
                        catch (Exception innerexception)
                        {
                            message.AddError(120009, innerexception.Message);
                        }
                    }
                    finally
                    {
                        if (httpResponse != null)
                        {
                            httpResponse.Close();
                        }
                    }
                    #endregion
                }
                else
                {
                    #region single upload format
                    #region submit the create nodes first
                    queryFilter.WhereClause = "(" +
                        sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'create'  AND "
                        + sqlFormatter.SqlIdentifier("osmelementtype") + " = 'node'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {
                            try
                            {
                                string action = String.Empty;
                                if (revActionFieldIndex != -1)
                                {
                                    action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                }
                                string elementType = String.Empty;
                                if (revElementTypeFieldIndex != -1)
                                {
                                    elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                }

                                string sourceFCName = String.Empty;
                                if (revFCNameFieldIndex != -1)
                                {
                                    sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                }

                                long osmOldID = -1;
                                if (revOldIDFieldIndex != -1)
                                {
                                    osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                }

                                // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                                // into multiple sets
                                if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                                {
                                    CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                }

                                osm createNode = CreateOSMNodeRepresentation(pointFeatureClass, action, osmOldID, changeSetID, -1, null, internalExtensionVersion);

                                httpClient = null;
                                httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/" + elementType + "/create") as HttpWebRequest;
                                httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                                httpClient.Method = "PUT";
                                SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                                httpClient.Timeout = secondsToTimeout * 1000;

                                httpResponse = null;

                                string nodeContent = OsmRest.SerializeUtils.CreateXmlSerializable(createNode, serializer, Encoding.UTF8, "text/xml");

                                if (String.IsNullOrEmpty(nodeContent))
                                {
                                    continue;
                                }

                                OsmRest.HttpUtils.Put(httpClient, nodeContent);

                                httpResponse = httpClient.GetResponse() as HttpWebResponse;

                                createNode = null;

                                // track the update/sync requests against the server
                                featureUpdateCounter = featureUpdateCounter + 1;

                                if (httpResponse != null)
                                {
                                    string newIDString = OsmRest.HttpUtils.GetResponseContent(httpResponse);

                                    nodeosmIDLookup.Add(osmOldID, Convert.ToInt64(newIDString));

                                    // update the revision table
                                    if (revNewIDFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revNewIDFieldIndex, Convert.ToString(newIDString));
                                    }
                                    if (revVersionFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revVersionFieldIndex, 1);
                                    }
                                    if (revStatusFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revStatusFieldIndex, httpResponse.StatusCode.ToString());
                                    }
                                    if (revStatusCodeFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                    }
                                    if (revChangeSetIDFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revChangeSetIDFieldIndex, Convert.ToString(changeSetID));
                                    }

                                    // update the source point feature class as well
                                    updateSource((ITable)pointFeatureClass, action, osmOldID, Convert.ToInt64(newIDString), user_displayname, userID, 1, Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup,internalExtensionVersion);
                                }
                            }
                            catch (Exception ex)
                            {

                                message.AddError(120009, ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }
                            }
                            finally
                            {
                                try
                                {
                                    searchRowToUpdate.Store();
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.Message);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                return;
                            }
                        }
                    }
                    #endregion

                    #region next the create ways
                    queryFilter.WhereClause = "(" +
                        sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'create'  AND "
                        + sqlFormatter.SqlIdentifier("osmelementtype") + " = 'way'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {
                            try
                            {
                                string action = String.Empty;
                                if (revActionFieldIndex != -1)
                                {
                                    action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                }
                                string elementType = String.Empty;
                                if (revElementTypeFieldIndex != -1)
                                {
                                    elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                }

                                string sourceFCName = String.Empty;
                                if (revFCNameFieldIndex != -1)
                                {
                                    sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                }

                                long osmOldID = -1;
                                if (revOldIDFieldIndex != -1)
                                {
                                    osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                }

                                bool isPolygon = false;
                                if (sourceFCName.IndexOf("_osm_ply") > -1)
                                {
                                    isPolygon = true;
                                }

                                // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                                // into multiple sets
                                if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                                {
                                    CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                }

                                osm createWay = new osm();
                                if (isPolygon == false)
                                {
                                    createWay = CreateOSMWayRepresentation(lineFeatureClass, action, osmOldID, changeSetID, -1, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                }
                                else
                                {
                                    createWay = CreateOSMWayRepresentation(polygonFeatureClass, action, osmOldID, changeSetID, -1, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                }

                                try
                                {
                                    HttpWebRequest httpClient3 = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/" + elementType + "/create") as HttpWebRequest;
                                    httpClient3 = OSMGPDownload.AssignProxyandCredentials(httpClient3);
                                    httpClient3.Method = "PUT";
                                    SetBasicAuthHeader(httpClient3, userCredentialGPValue.EncodedUserNamePassWord);
                                    httpClient.Timeout = secondsToTimeout * 1000;

                                    string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(createWay, serializer, Encoding.UTF8, "text/xml");
                                    OsmRest.HttpUtils.Put(httpClient3, sContent);
                                    createWay = null;

                                    httpResponse = null;
                                    httpResponse = httpClient3.GetResponse() as HttpWebResponse;

                                    // track the update/sync requests against the server
                                    featureUpdateCounter = featureUpdateCounter + 1;
                                }
                                catch (Exception ex)
                                {
                                    message.AddError(120009, ex.Message);

                                    if (ex is WebException)
                                    {
                                        updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                    }
                                }

                                if (httpResponse != null)
                                {
                                    string newIDString = OsmRest.HttpUtils.GetResponseContent(httpResponse);

                                    wayosmIDLookup.Add(osmOldID, Convert.ToInt64(newIDString));

                                    // update the revision table
                                    if (revNewIDFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revNewIDFieldIndex, Convert.ToString(newIDString));
                                    }
                                    if (revVersionFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revVersionFieldIndex, 1);
                                    }
                                    if (revStatusFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revStatusFieldIndex, httpResponse.StatusCode.ToString());
                                    }
                                    if (revStatusCodeFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                    }
                                    if (revChangeSetIDFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revChangeSetIDFieldIndex, Convert.ToString(changeSetID));
                                    }

                                    // update the source line/polygon feature class as well
                                    if (isPolygon == false)
                                    {
                                        updateSource((ITable)lineFeatureClass, action, osmOldID, Convert.ToInt64(newIDString), user_displayname, userID, 1, Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                    }
                                    else
                                    {
                                        updateSource((ITable)polygonFeatureClass, action, osmOldID, Convert.ToInt64(newIDString), user_displayname, userID, 1, Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                    }
                                }
                            }

                            catch (Exception ex)
                            {
                                message.AddError(120009, ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }
                            }
                            finally
                            {
                                try
                                {
                                    searchRowToUpdate.Store();
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.Message);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);

                                return;
                            }
                        }
                    }
                    #endregion

                    #region and then create relations
                    queryFilter.WhereClause = "(" +
                        sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'create'  AND "
                        + sqlFormatter.SqlIdentifier("osmelementtype") + " = 'relation'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {
                            try
                            {

                                string action = String.Empty;
                                if (revActionFieldIndex != -1)
                                {
                                    action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                }
                                string elementType = String.Empty;
                                if (revElementTypeFieldIndex != -1)
                                {
                                    elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                }

                                string sourceFCName = String.Empty;
                                if (revFCNameFieldIndex != -1)
                                {
                                    sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                }

                                long osmOldID = -1;
                                if (revOldIDFieldIndex != -1)
                                {
                                    osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                }

                                bool isPolygon = false;
                                if (sourceFCName.IndexOf("_osm_ply") > -1)
                                {
                                    isPolygon = true;
                                }

                                // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                                // into multiple sets
                                if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                                {
                                    CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                }

                                osm createRelation = null;
                                // the relation is acutally multi-part line
                                if (sourceFCName.Contains("_osm_ln"))
                                {
                                    createRelation = CreateOSMRelationRepresentation((ITable)lineFeatureClass, action, osmOldID, changeSetID, -1, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                }
                                else if (sourceFCName.Contains("_osm_ply"))
                                {
                                    createRelation = CreateOSMRelationRepresentation((ITable)polygonFeatureClass, action, osmOldID, changeSetID, -1, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                }
                                else
                                {
                                    createRelation = CreateOSMRelationRepresentation(relationTable, action, osmOldID, changeSetID, -1, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                }
                                try
                                {
                                    HttpWebRequest httpClient4 = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/" + elementType + "/create") as HttpWebRequest;
                                    httpClient4 = OSMGPDownload.AssignProxyandCredentials(httpClient4);
                                    SetBasicAuthHeader(httpClient4, userCredentialGPValue.EncodedUserNamePassWord);
                                    httpClient4.Timeout = secondsToTimeout * 1000;
                                    string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(createRelation, serializer, Encoding.UTF8, "text/xml");

                                    OsmRest.HttpUtils.Put(httpClient4, sContent);

                                    httpResponse = null;
                                    httpResponse = httpClient4.GetResponse() as HttpWebResponse;
                                    // track the update/sync requests against the server
                                    featureUpdateCounter = featureUpdateCounter + 1;
                                }
                                catch (Exception ex)
                                {
                                    message.AddError(120009, ex.Message);

                                    if (ex is WebException)
                                    {
                                        updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                    }
                                }

                                if (httpResponse != null)
                                {
                                    string newIDString = OsmRest.HttpUtils.GetResponseContent(httpResponse);

                                    relationosmIDLookup.Add(osmOldID, Convert.ToInt64(newIDString));

                                    // update the revision table
                                    if (revNewIDFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revNewIDFieldIndex, Convert.ToString(newIDString));
                                    }
                                    if (revVersionFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revVersionFieldIndex, 1);
                                    }
                                    if (revStatusFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revStatusFieldIndex, httpResponse.StatusCode.ToString());
                                    }
                                    if (revStatusCodeFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                    }
                                    if (revChangeSetIDFieldIndex != -1)
                                    {
                                        searchRowToUpdate.set_Value(revChangeSetIDFieldIndex, Convert.ToInt32(changeSetID));
                                    }

                                    if (sourceFCName.Contains("_osm_ln"))
                                    {
                                        updateSource((ITable)lineFeatureClass, action, osmOldID, Convert.ToInt64(newIDString), user_displayname, userID, 1, Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                    }
                                    else if (sourceFCName.Contains("_osm_ply"))
                                    {
                                        updateSource((ITable)polygonFeatureClass, action, osmOldID, Convert.ToInt64(newIDString), user_displayname, userID, 1, Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                    }
                                    else
                                    {
                                        // update the source table holding the relation information class as well
                                        updateSource(relationTable, action, osmOldID, Convert.ToInt64(newIDString), user_displayname, userID, 1, Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                    }
                                }
                            }

                            catch (Exception ex)
                            {
                                message.AddError(120009, ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }
                            }
                            finally
                            {
                                try
                                {
                                    searchRowToUpdate.Store();
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.Message);
                                }
                            }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                return;
                            }
                        }
                    }
                    #endregion

                    #region after that submit the modify node, way, relation
                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        queryFilter.WhereClause = "(" +
                            sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                            + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                            + sqlFormatter.SqlIdentifier("osmaction") + " = 'modify'";

                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {
                            try
                            {

                                string action = String.Empty;
                                if (revActionFieldIndex != -1)
                                {
                                    action = searchRowToUpdate.get_Value(revActionFieldIndex) as string;
                                }
                                string elementType = String.Empty;
                                if (revElementTypeFieldIndex != -1)
                                {
                                    elementType = searchRowToUpdate.get_Value(revElementTypeFieldIndex) as string;
                                }

                                string sourceFCName = String.Empty;
                                if (revFCNameFieldIndex != -1)
                                {
                                    sourceFCName = searchRowToUpdate.get_Value(revFCNameFieldIndex) as string;
                                }

                                long osmOldID = -1;
                                if (revOldIDFieldIndex != -1)
                                {
                                    osmOldID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                }

                                // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                                // into multiple sets
                                if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                                {
                                    CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                                }

                                switch (elementType)
                                {
                                    case "node":
                                        #region submit nodes to OSM server
                                        switch (action)
                                        {
                                            case "modify":
                                                long modifyID = -1;

                                                if (revNewIDFieldIndex != -1)
                                                {
                                                    object osmIDValue = searchRowToUpdate.get_Value(revNewIDFieldIndex);

                                                    if (osmIDValue == DBNull.Value)
                                                    {
                                                        osmIDValue = osmOldID;
                                                    }

                                                    try
                                                    {
                                                        modifyID = Convert.ToInt64(osmIDValue);
                                                    }
                                                    catch { }

                                                    // modifies should only happen to osm IDs > 0
                                                    // if that condition is not met let's skip this feature as something is not right
                                                    if (modifyID < 0)
                                                    {
                                                        continue;
                                                    }
                                                }

                                                int osmVersion = -1;
                                                if (revVersionFieldIndex != -1)
                                                {
                                                    osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                                                }

                                                try
                                                {
                                                    HttpWebRequest httpClient5 = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/" + elementType + "/" + modifyID.ToString()) as HttpWebRequest;
                                                    httpClient5 = OSMGPDownload.AssignProxyandCredentials(httpClient5);
                                                    SetBasicAuthHeader(httpClient5, userCredentialGPValue.EncodedUserNamePassWord);

                                                    osm updateNode = CreateOSMNodeRepresentation(pointFeatureClass, action, modifyID, changeSetID, osmVersion, null, internalExtensionVersion);

                                                    string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(updateNode, serializer, Encoding.UTF8, "text/xml");

                                                    // if the serialized node at this time is a null or an empty string let's continue to the next point
                                                    if (String.IsNullOrEmpty(sContent))
                                                    {
                                                        continue;
                                                    }

                                                    OsmRest.HttpUtils.Put(httpClient5, sContent);

                                                    httpResponse = httpClient5.GetResponse() as HttpWebResponse;

                                                    // track the update/sync requests against the server
                                                    featureUpdateCounter = featureUpdateCounter + 1;

                                                    if (httpResponse != null)
                                                    {
                                                        string newVersionString = OsmRest.HttpUtils.GetResponseContent(httpResponse);

                                                        // update the revision table
                                                        if (revVersionFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revVersionFieldIndex, Convert.ToString(newVersionString));
                                                        }
                                                        if (revStatusFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revStatusFieldIndex, httpResponse.StatusCode.ToString());
                                                        }
                                                        if (revStatusCodeFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                                        }
                                                        if (revChangeSetIDFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revChangeSetIDFieldIndex, Convert.ToInt32(changeSetID));
                                                        }

                                                        // for a modify the old id is still the new id
                                                        if (revNewIDFieldIndex != -1 && revOldIDFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revNewIDFieldIndex, searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                                        }

                                                        // update the source point feature class as well
                                                        updateSource((ITable)pointFeatureClass, action, modifyID, modifyID, user_displayname, userID, Convert.ToInt32(newVersionString), Convert.ToInt32(changeSetID), null, null, null, internalExtensionVersion);

                                                        httpResponse.Close();
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    message.AddError(120009, ex.Message);

                                                    if (ex is WebException)
                                                    {
                                                        updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                                    }
                                                }

                                                break;
                                            case "delete":
                                                // the delete operations are handled separately
                                                break;
                                            default:
                                                break;
                                        }
                                        break;
                                        #endregion
                                    case "way":
                                        #region submit ways to the OSM server
                                        // determine if we have a polygon or a polyline feature class
                                        bool isPolygon = false;
                                        if (sourceFCName.IndexOf("_osm_ply") > -1)
                                        {
                                            isPolygon = true;
                                        }

                                        switch (action)
                                        {
                                            case "modify":

                                                long modifyID = -1;

                                                if (revNewIDFieldIndex != -1)
                                                {
                                                    object osmIDValue = searchRowToUpdate.get_Value(revNewIDFieldIndex);

                                                    if (osmIDValue == DBNull.Value)
                                                    {
                                                        osmIDValue = osmOldID;
                                                    }

                                                    try
                                                    {
                                                        modifyID = Convert.ToInt64(osmIDValue);
                                                    }
                                                    catch { }

                                                    // modifies should only happen to osm IDs > 0
                                                    // if that condition is not met let's skip this feature as something is not right
                                                    if (modifyID < 0)
                                                    {
                                                        continue;
                                                    }
                                                }

                                                int osmVersion = -1;
                                                if (revVersionFieldIndex != -1)
                                                {
                                                    osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                                                }

                                                osm updateWay = new osm();
                                                if (isPolygon == false)
                                                {
                                                    updateWay = CreateOSMWayRepresentation(lineFeatureClass, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                                }
                                                else
                                                {
                                                    updateWay = CreateOSMWayRepresentation(polygonFeatureClass, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);
                                                }
                                                try
                                                {
                                                    string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(updateWay, serializer, Encoding.UTF8, "text/xml");
                                                    httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/" + elementType + "/" + modifyID.ToString()) as HttpWebRequest;
                                                    httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                                                    SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                                                    OsmRest.HttpUtils.Put(httpClient, sContent);

                                                    httpResponse = httpClient.GetResponse() as HttpWebResponse;

                                                    // track the update/sync requests against the server
                                                    featureUpdateCounter = featureUpdateCounter + 1;

                                                    if (httpResponse != null)
                                                    {
                                                        string newVersionString = OsmRest.HttpUtils.GetResponseContent(httpResponse);

                                                        // update the revision table
                                                        if (revVersionFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revVersionFieldIndex, Convert.ToString(newVersionString));
                                                        }
                                                        if (revStatusFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revStatusFieldIndex, httpResponse.StatusCode.ToString());
                                                        }
                                                        if (revStatusCodeFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                                        }
                                                        if (revChangeSetIDFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revChangeSetIDFieldIndex, Convert.ToInt32(changeSetID));
                                                        }

                                                        // for a modify the old id is still the new id
                                                        if (revNewIDFieldIndex != -1 && revOldIDFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revNewIDFieldIndex, searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                                        }

                                                        // update the source line/polygon feature class as well
                                                        if (isPolygon == false)
                                                        {
                                                            updateSource((ITable)lineFeatureClass, action, modifyID, modifyID, user_displayname, userID, Convert.ToInt32(newVersionString), Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                                        }
                                                        else
                                                        {
                                                            updateSource((ITable)polygonFeatureClass, action, modifyID, modifyID, user_displayname, userID, Convert.ToInt32(newVersionString), Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);
                                                        }

                                                        httpResponse.Close();
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    message.AddError(120009, ex.Message);

                                                    if (ex is WebException)
                                                    {
                                                        updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                                    }
                                                }
                                                break;
                                            case "delete":
                                                // the delete operations are handled separately
                                                break;
                                            default:
                                                break;
                                        }
                                        break;
                                        #endregion
                                    case "relation":
                                        #region submit relations to the OSM server
                                        switch (action)
                                        {
                                            case "create":
                                                break;
                                            case "modify":

                                                long modifyID = -1;

                                                if (revNewIDFieldIndex != -1)
                                                {
                                                    object osmIDValue = searchRowToUpdate.get_Value(revNewIDFieldIndex);

                                                    if (osmIDValue == DBNull.Value)
                                                    {
                                                        osmIDValue = osmOldID;
                                                    }

                                                    try
                                                    {
                                                        modifyID = Convert.ToInt64(osmIDValue);
                                                    }
                                                    catch { }

                                                    // modifies should only happen to osm IDs > 0
                                                    // if that condition is not met let's skip this feature as something is not right
                                                    if (modifyID < 0)
                                                    {
                                                        continue;
                                                    }
                                                }

                                                int osmVersion = -1;
                                                if (revVersionFieldIndex != -1)
                                                {
                                                    osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                                                }

                                                osm updateRelation = CreateOSMRelationRepresentation(relationTable, action, modifyID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);

                                                try
                                                {
                                                    string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(updateRelation, serializer, Encoding.UTF8, "text/xml");
                                                    httpClient = HttpWebRequest.Create(baseURLGPString.Value + "/api/0.6/" + elementType + "/" + modifyID.ToString()) as HttpWebRequest;
                                                    httpClient = OSMGPDownload.AssignProxyandCredentials(httpClient);
                                                    SetBasicAuthHeader(httpClient, userCredentialGPValue.EncodedUserNamePassWord);
                                                    OsmRest.HttpUtils.Put(httpClient, sContent);

                                                    httpResponse = httpClient.GetResponse() as HttpWebResponse;

                                                    // track the update/sync requests against the server
                                                    featureUpdateCounter = featureUpdateCounter + 1;

                                                    if (httpResponse != null)
                                                    {
                                                        string newVersionString = OsmRest.HttpUtils.GetResponseContent(httpResponse);

                                                        // update the revision table
                                                        if (revVersionFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revVersionFieldIndex, Convert.ToInt32(newVersionString));
                                                        }
                                                        if (revStatusFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revStatusFieldIndex, httpResponse.StatusCode.ToString());
                                                        }
                                                        if (revStatusCodeFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                                        }
                                                        if (revChangeSetIDFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revChangeSetIDFieldIndex, Convert.ToInt32(changeSetID));
                                                        }

                                                        // for a modify the old id is still the new id
                                                        if (revNewIDFieldIndex != -1 && revOldIDFieldIndex != -1)
                                                        {
                                                            searchRowToUpdate.set_Value(revNewIDFieldIndex, searchRowToUpdate.get_Value(revOldIDFieldIndex));
                                                        }

                                                        // update the source table holding the relation information class as well
                                                        updateSource(relationTable, action, modifyID, modifyID, user_displayname, userID, Convert.ToInt32(newVersionString), Convert.ToInt32(changeSetID), nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);

                                                        httpResponse.Close();
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    message.AddError(120009, ex.Message);

                                                    if (ex is WebException)
                                                    {
                                                        updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                                    }
                                                }
                                                break;
                                            case "delete":
                                                // the delete operations are handled separately

                                                break;
                                            default:
                                                break;
                                        }
                                        break;
                                        #endregion
                                    default:
                                        break;
                                }
                            }

                            catch (Exception ex)
                            {
                                message.AddAbort(ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }
                            }
                            finally
                            {
                                try
                                {
                                    searchRowToUpdate.Store();
                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Debug.WriteLine(ex.Message);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);

                                return;
                            }

                        }
                    }
                    #endregion

                    #region now let's handle the delete in the reverse order - relation first, then ways, and then nodes as the last entity
                    #region delete relations
                    // now let's handle the delete in the reverse order - relation first, then ways, and then nodes as the last entity
                    queryFilter.WhereClause = "("
                        + sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'delete' AND "
                        + sqlFormatter.SqlIdentifier("osmelementtype") + " = 'relation'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {

                            // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                            // into multiple sets
                            if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                            {
                                CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                            }

                            long osmID = -1;
                            if (revOldIDFieldIndex != -1)
                            {
                                osmID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                            }
                            int osmVersion = -1;
                            if (revVersionFieldIndex != -1)
                            {
                                osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                            }

                            osm deleteRelation = CreateOSMRelationRepresentation(relationTable, "delete", osmID, changeSetID, osmVersion, nodeosmIDLookup, wayosmIDLookup, relationosmIDLookup, internalExtensionVersion);

                            string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(deleteRelation, serializer, Encoding.UTF8, "text/xml");

                            string errorMessage = String.Empty;
                            try
                            {
                                httpResponse = OsmRest.HttpUtils.Delete(baseURLGPString.Value + "/api/0.6/relation/" + Convert.ToString(osmID), sContent, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout) as HttpWebResponse;

                                // track the update/sync requests against the server
                                featureUpdateCounter = featureUpdateCounter + 1;

                                if (revStatusFieldIndex != -1)
                                {
                                    searchRowToUpdate.set_Value(revStatusFieldIndex, (int)httpResponse.StatusCode);
                                }
                                if (revStatusCodeFieldIndex != -1)
                                {
                                    searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120009, ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }

                            try
                            {
                                searchRowToUpdate.Store();
                            }
                            catch { }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                return;
                            }

                        }
                    }

                    #endregion

                    #region handle delete ways
                    queryFilter.WhereClause = "("
                        + sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'delete' AND "
                        + sqlFormatter.SqlIdentifier("osmelementtype") + " = 'way'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {

                            // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                            // into multiple sets
                            if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                            {
                                CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                            }

                            long osmID = -1;
                            if (revOldIDFieldIndex != -1)
                            {
                                osmID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                            }
                            int osmVersion = -1;
                            if (revVersionFieldIndex != -1)
                            {
                                osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                            }

                            osm deleteWay = CreateOSMWayRepresentation(lineFeatureClass, "delete", osmID, changeSetID, osmVersion, wayosmIDLookup, pointFeatureClass, pointOSMIDFieldIndex, internalExtensionVersion);

                            string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(deleteWay, serializer, Encoding.UTF8, "text/xml");

                            try
                            {
                                httpResponse = null;
                                httpResponse = OsmRest.HttpUtils.Delete(baseURLGPString.Value + "/api/0.6/way/" + Convert.ToString(osmID), sContent, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout) as HttpWebResponse;

                                // track the update/sync requests against the server
                                featureUpdateCounter = featureUpdateCounter + 1;

                                string errorMessage = String.Empty;
                                // just grab the response and set it on the database
                                if (revStatusFieldIndex != -1)
                                {
                                    searchRowToUpdate.set_Value(revStatusFieldIndex, (int)httpResponse.StatusCode);
                                }
                                if (revStatusCodeFieldIndex != -1)
                                {
                                    searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }

                            catch (Exception ex)
                            {
                                message.AddError(1200009, ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }

                            try
                            {
                                searchRowToUpdate.Store();
                            }
                            catch { }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                return;
                            }
                        }
                    }

                    #endregion

                    #region handle delete points
                    queryFilter.WhereClause = "("
                        + sqlFormatter.SqlIdentifier("osmstatuscode") + " <> 200 OR "
                        + sqlFormatter.SqlIdentifier("osmstatus") + " IS NULL) AND "
                        + sqlFormatter.SqlIdentifier("osmaction") + " = 'delete' AND "
                        + sqlFormatter.SqlIdentifier("osmelementtype") + " = 'node'";

                    using (ComReleaser comReleaser = new ComReleaser())
                    {
                        searchCursor = revisionTable.Search(queryFilter, false);
                        comReleaser.ManageLifetime(searchCursor);

                        while ((searchRowToUpdate = searchCursor.NextRow()) != null)
                        {
                            // if the overall number of uploaded elements is too big for a single changeset we do need to split it up
                            // into multiple sets
                            if ((featureUpdateCounter % maxElementsinChangeSet) == 0)
                            {
                                CreateNextChangeSet(message, createChangeSetOSM, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, ref changeSetID, baseURLGPString, ref featureUpdateCounter);
                            }

                            long osmID = -1;
                            if (revOldIDFieldIndex != -1)
                            {
                                osmID = Convert.ToInt64(searchRowToUpdate.get_Value(revOldIDFieldIndex));
                            }
                            int osmVersion = -1;
                            if (revVersionFieldIndex != -1)
                            {
                                osmVersion = Convert.ToInt32(searchRowToUpdate.get_Value(revVersionFieldIndex));
                            }

                            IPoint deletePoint = null;
                            if (revLongitudeFieldIndex != -1 && revLatitudeFieldIndex != -1)
                            {
                                try
                                {
                                    // let's reconstruct the delete point
                                    deletePoint = new PointClass();
                                    deletePoint.X = Convert.ToDouble(searchRowToUpdate.get_Value(revLongitudeFieldIndex));
                                    deletePoint.Y = Convert.ToDouble(searchRowToUpdate.get_Value(revLatitudeFieldIndex));
                                    deletePoint.SpatialReference = m_wgs84;
                                }
                                catch (Exception ex)
                                {
                                    message.AddWarning(ex.Message);
                                }

                                if (deletePoint == null)
                                {
                                    // inform the about the issue - no successful creation of point and continue on to the next delete instruction
                                    // in the revision table
                                    message.AddWarning(resourceManager.GetString("GPTools_OSMGPUpload_invalidPoint"));
                                    continue;
                                }
                            }

                            osm deleteNode = CreateOSMNodeRepresentation(pointFeatureClass, "delete", osmID, changeSetID, osmVersion, deletePoint, internalExtensionVersion);

                            string sContent = OsmRest.SerializeUtils.CreateXmlSerializable(deleteNode, serializer, Encoding.UTF8, "text/xml");

                            if (String.IsNullOrEmpty(sContent))
                            {
                                continue;
                            }

                            string errorMessage = String.Empty;

                            try
                            {
                                httpResponse = OsmRest.HttpUtils.Delete(baseURLGPString.Value + "/api/0.6/node/" + Convert.ToString(osmID), sContent, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout) as HttpWebResponse;

                                if (revStatusFieldIndex != -1)
                                {
                                    searchRowToUpdate.set_Value(revStatusFieldIndex, (int)httpResponse.StatusCode);
                                }

                                if (revStatusCodeFieldIndex != -1)
                                {
                                    searchRowToUpdate.set_Value(revStatusCodeFieldIndex, (int)httpResponse.StatusCode);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120009, ex.Message);

                                if (ex is WebException)
                                {
                                    updateErrorStatus(message, revStatusFieldIndex, revStatusCodeFieldIndex, revErrorMessageFieldIndex, ref searchRowToUpdate, ex);
                                }

                                if (httpResponse != null)
                                {
                                    httpResponse.Close();
                                }

                            }

                            // track the update/sync requests against the server
                            featureUpdateCounter = featureUpdateCounter + 1;

                            try
                            {
                                searchRowToUpdate.Store();
                            }
                            catch { }

                            if (TrackCancel.Continue() == false)
                            {
                                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);
                                return;
                            }
                        }
                    }

                    #endregion
                    #endregion
                    #endregion
                }
            }
            catch (Exception ex)
            {
                message.AddError(120058, ex.Message);
                message.AddError(120058, ex.StackTrace);
            }
            finally
            {
                closeChangeSet(message, userCredentialGPValue.EncodedUserNamePassWord, secondsToTimeout, changeSetID, baseURLGPString);

                if (revisionTable != null)
                {
                    try
                    {
                        ISchemaLock tableSchemaLock = revisionTable as ISchemaLock;

                        if (tableSchemaLock != null)
                        {
                            tableSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
                        }
                    }
                    catch (Exception eLock)
                    {
                        message.AddError(120059, resourceManager.GetString("GPTools_OSMGPUpload_LockErrorTitle") + eLock.Message);
                    }
                }

                if (revisionTable != null)
                {
                    Marshal.FinalReleaseComObject(revisionTable);
                }

                // if the searchCursor still has a reference somewhere do release it now - and as a result release any remaining table locks
                if (searchCursor != null)
                {
                    Marshal.FinalReleaseComObject(searchCursor);
                }

                gpUtilities3.RemoveInternalData();
                gpUtilities3.ReleaseInternals();

                //Marshal.ReleaseComObject(gpUtilities3);
            }
        }
        private void WayToFeatureClass(osm osmDocument, IFeatureClass lineFeatureClass, IFeatureClass polygonFeatureClass)
        {
            // initial safety check -- consider throwing an exception instead
            if (osmDocument == null)
            {
                return;
            }

            if (osmDocument.Items == null)
            {
                return;
            }

            int osmTagsLineFieldIndex = -1;
            int osmUserLineFieldIndex = -1;
            int osmUIDLineFieldIndex = -1;
            int osmVisibleLineFieldIndex = -1;
            int osmVersionLineFieldIndex = -1;
            int osmChangesetLineFieldIndex = -1;
            int osmTimeStampLineFieldIndex = -1;
            int osmIDLineFieldIndex = -1;

            int osmTagsPolygonFieldIndex = -1;
            int osmUserPolygonFieldIndex = -1;
            int osmUIDPolygonFieldIndex = -1;
            int osmVisiblePolygonFieldIndex = -1;
            int osmVersionPolygonFieldIndex = -1;
            int osmChangesetPolygonFieldIndex = -1;
            int osmTimeStampPolygonFieldIndex = -1;
            int osmIDPolygonFieldIndex = -1;

            if (lineFeatureClass != null)
            {
                //
                osmTagsLineFieldIndex = lineFeatureClass.FindField("osmTags");
                osmUserLineFieldIndex = lineFeatureClass.FindField("osmuser");
                osmUIDLineFieldIndex = lineFeatureClass.FindField("osmuid");
                osmVisibleLineFieldIndex = lineFeatureClass.FindField("osmvisible");
                osmVersionLineFieldIndex = lineFeatureClass.FindField("osmversion");
                osmChangesetLineFieldIndex = lineFeatureClass.FindField("osmchangeset");
                osmTimeStampLineFieldIndex = lineFeatureClass.FindField("osmtimestamp");
                osmIDLineFieldIndex = lineFeatureClass.FindField("OSMID");
            }

            if (polygonFeatureClass != null)
            {
                osmTagsPolygonFieldIndex = polygonFeatureClass.FindField("osmTags");
                osmUserPolygonFieldIndex = polygonFeatureClass.FindField("osmuser");
                osmUIDPolygonFieldIndex = polygonFeatureClass.FindField("osmuid");
                osmVisiblePolygonFieldIndex = polygonFeatureClass.FindField("osmvisible");
                osmVersionPolygonFieldIndex = polygonFeatureClass.FindField("osmversion");
                osmChangesetPolygonFieldIndex = polygonFeatureClass.FindField("osmchangeset");
                osmTimeStampPolygonFieldIndex = polygonFeatureClass.FindField("osmtimestamp");
                osmIDPolygonFieldIndex = polygonFeatureClass.FindField("OSMID");
            }

            ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference wgs84 = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984) as ISpatialReference;

            Dictionary<string, IPoint> nodeCollection = new Dictionary<string, IPoint>();
            foreach (var documentItem in osmDocument.Items)
            {
                if (documentItem is ESRI.ArcGIS.OSM.OSMClassExtension.node)
                {
                    ESRI.ArcGIS.OSM.OSMClassExtension.node currentNode = documentItem as ESRI.ArcGIS.OSM.OSMClassExtension.node;

                    if (nodeCollection.ContainsKey(currentNode.id) == false)
                    {
                        IPoint newNodePoint = new PointClass();
                        newNodePoint.X = Convert.ToDouble(currentNode.lon, new CultureInfo("en-US"));
                        newNodePoint.Y = Convert.ToDouble(currentNode.lat, new CultureInfo("en-US"));
                        newNodePoint.SpatialReference = wgs84;

                        IPointIDAware idAware = newNodePoint as IPointIDAware;
                        idAware.PointIDAware = true;
                        newNodePoint.ID = Convert.ToInt32(currentNode.id);

                        nodeCollection.Add(currentNode.id, newNodePoint);
                    }
                }
            }

            int osmTagsFieldIndex = -1;
            int osmUserFieldIndex = -1;
            int osmUIDFieldIndex = -1;
            int osmVisibleFieldIndex = -1;
            int osmVersionFieldIndex = -1;
            int osmChangesetFieldIndex = -1;
            int osmTimeStampFieldIndex = -1;
            int osmIDFieldIndex = -1;

            foreach (var documentItem in osmDocument.Items)
            {
                if (documentItem is ESRI.ArcGIS.OSM.OSMClassExtension.way)
                {
                    ESRI.ArcGIS.OSM.OSMClassExtension.way currentWay = documentItem as ESRI.ArcGIS.OSM.OSMClassExtension.way;
                    IFeatureClass targetFeatureClass = null;

                    if (OSMToolHelper.IsThisWayALine(currentWay))
                    {
                        targetFeatureClass = lineFeatureClass;
                        osmTagsFieldIndex = osmTagsLineFieldIndex;
                        osmUserFieldIndex = osmUserLineFieldIndex;
                        osmUIDFieldIndex = osmUIDLineFieldIndex;
                        osmVisibleFieldIndex = osmVisibleLineFieldIndex;
                        osmVersionFieldIndex = osmVersionLineFieldIndex;
                        osmChangesetFieldIndex = osmChangesetLineFieldIndex;
                        osmTimeStampFieldIndex = osmTimeStampLineFieldIndex;
                        osmIDFieldIndex = osmIDLineFieldIndex;
                    }
                    else
                    {
                        targetFeatureClass = polygonFeatureClass;
                        osmTagsFieldIndex = osmTagsPolygonFieldIndex;
                        osmUserFieldIndex = osmUserPolygonFieldIndex;
                        osmUIDFieldIndex = osmUIDPolygonFieldIndex;
                        osmVisibleFieldIndex = osmVisiblePolygonFieldIndex;
                        osmVersionFieldIndex = osmVersionPolygonFieldIndex;
                        osmChangesetFieldIndex = osmChangesetPolygonFieldIndex;
                        osmTimeStampFieldIndex = osmChangesetPolygonFieldIndex;
                        osmIDFieldIndex = osmIDPolygonFieldIndex;
                    }

                    if (targetFeatureClass != null)
                    {
                        IFeature newWayFeature = targetFeatureClass.CreateFeature();

                        IPointCollection pointCollection = null;
                        if (targetFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                        {
                            pointCollection = new PolylineClass() as IPointCollection;
                        }
                        else
                        {
                            pointCollection = new PolygonClass() as IPointCollection;
                        }

                        IPointIDAware topGeometryIDAware = pointCollection as IPointIDAware;
                        topGeometryIDAware.PointIDAware = true;

                        foreach (var wayNode in currentWay.nd)
                        {
                            if (nodeCollection.ContainsKey(wayNode.@ref))
                            {
                                pointCollection.AddPoint(nodeCollection[wayNode.@ref]);
                            }
                        }

                        newWayFeature.Shape = pointCollection as IGeometry;

                        if (osmUserFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmUserFieldIndex, currentWay.user);
                        }
                        if (osmUIDFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmUIDFieldIndex, currentWay.uid);
                        }
                        if (osmVisibleFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmVisibleFieldIndex, currentWay.visible.ToString());
                        }
                        if (osmVersionFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmVersionFieldIndex, Convert.ToInt32(currentWay.version));
                        }
                        if (osmChangesetFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmChangesetFieldIndex, Convert.ToInt32(currentWay.changeset));
                        }
                        if (osmTimeStampFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmTimeStampFieldIndex, Convert.ToDateTime(currentWay.timestamp));
                        }
                        if (osmIDFieldIndex != -1)
                        {
                            newWayFeature.set_Value(osmIDFieldIndex, Convert.ToInt32(currentWay.id));
                        }

                        _osmUtility.insertOSMTags(osmTagsFieldIndex, newWayFeature, currentWay.tag);

                        newWayFeature.Store();
                    }

                }
            }
        }
        private void NodeToFeatureClass(osm osmDocument, IFeatureClass pointFeatureClass)
        {
            // initial safety check -- consider throwing an exception instead
            if (osmDocument == null)
            {
                return;
            }

            if (osmDocument.Items == null)
            {
                return;
            }

            if (pointFeatureClass == null)
            {
                return;
            }

            //
            int tagCollectionPointFieldIndex = pointFeatureClass.FindField("osmTags");
            int osmUserPointFieldIndex = pointFeatureClass.FindField("osmuser");
            int osmUIDPointFieldIndex = pointFeatureClass.FindField("osmuid");
            int osmVisiblePointFieldIndex = pointFeatureClass.FindField("osmvisible");
            int osmVersionPointFieldIndex = pointFeatureClass.FindField("osmversion");
            int osmChangesetPointFieldIndex = pointFeatureClass.FindField("osmchangeset");
            int osmTimeStampPointFieldIndex = pointFeatureClass.FindField("osmtimestamp");
            int osmIDPointFieldIndex = pointFeatureClass.FindField("OSMID");

            ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass();
            ISpatialReference wgs84 = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984) as ISpatialReference;

            foreach (var documentItem in osmDocument.Items)
            {
                if (documentItem is node)
                {
                   // if (DoesHaveKeys(documentItem))
                   // {
                        node currentNode = documentItem as node;

                        IFeature newPointFeature = pointFeatureClass.CreateFeature();

                        IPoint pointGeometry = new PointClass();
                        pointGeometry.X = Convert.ToDouble(currentNode.lon, new CultureInfo("en-US"));
                        pointGeometry.Y = Convert.ToDouble(currentNode.lat, new CultureInfo("en-US"));
                        pointGeometry.SpatialReference = wgs84;

                        newPointFeature.Shape = pointGeometry;

                        if (osmUserPointFieldIndex != -1)
                        {
                        newPointFeature.set_Value(osmUserPointFieldIndex, currentNode.user);
                        }
                        if (osmUIDPointFieldIndex != -1)
                        {
                            newPointFeature.set_Value(osmUIDPointFieldIndex, currentNode.uid);
                        }
                        if (osmVisiblePointFieldIndex != -1)
                        {
                            newPointFeature.set_Value(osmVisiblePointFieldIndex, currentNode.visible);
                        }
                        if (osmVersionPointFieldIndex != -1)
                        {
                            newPointFeature.set_Value(osmVersionPointFieldIndex, Convert.ToInt32(currentNode.version));
                        }
                        if (osmChangesetPointFieldIndex != -1)
                        {
                            newPointFeature.set_Value(osmChangesetPointFieldIndex, Convert.ToInt32(currentNode.changeset));
                        }
                        if (osmTimeStampPointFieldIndex != -1)
                        {
                            newPointFeature.set_Value(osmTimeStampPointFieldIndex, Convert.ToDateTime(currentNode.timestamp));
                        }
                        if (osmIDPointFieldIndex != -1)
                        {
                            newPointFeature.set_Value(osmIDPointFieldIndex, Convert.ToInt32(currentNode.id));
                        }

                        _osmUtility.insertOSMTags(tagCollectionPointFieldIndex, newPointFeature, currentNode.tag);

                        newPointFeature.Store();
                 //   }
                }
            }
        }