/// <summary>IDisposable::Dispose - clean up GP if possible</summary> public void Dispose() { if (_gp != null) { IGPUtilities3 gpUtil = new GPUtilitiesClass(); gpUtil.ClearInMemoryWorkspace(); gpUtil.ReleaseInternals(); gpUtil.RemoveInternalData(); gpUtil.RemoveInternalValues(); _gp.ResetEnvironments(); ComReleaser.ReleaseCOMObject(_gp); _gp = null; } }
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; IFeatureClass osmPointFeatureClass = null; IFeatureClass osmLineFeatureClass = null; IFeatureClass osmPolygonFeatureClass = null; try { DateTime syncTime = DateTime.Now; IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter baseURLParameter = paramvalues.get_Element(in_downloadURLNumber) as IGPParameter; IGPString baseURLString = gpUtilities3.UnpackGPValue(baseURLParameter) as IGPString; if (baseURLString == null) { message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), baseURLParameter.Name)); } IGPParameter downloadExtentParameter = paramvalues.get_Element(in_downloadExtentNumber) as IGPParameter; IGPValue downloadExtentGPValue = gpUtilities3.UnpackGPValue(downloadExtentParameter); esriGPExtentEnum gpExtent; IEnvelope downloadEnvelope = gpUtilities3.GetExtent(downloadExtentGPValue, out gpExtent); IGPParameter includeAllReferences = paramvalues.get_Element(in_includeReferencesNumber) as IGPParameter; IGPBoolean includeAllReferencesGPValue = gpUtilities3.UnpackGPValue(includeAllReferences) as IGPBoolean; if (includeAllReferencesGPValue == null) { message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), includeAllReferences.Name)); } IEnvelope newExtent = null; ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass() as ISpatialReferenceFactory; ISpatialReference wgs84 = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984) as ISpatialReference; // this determines the spatial reference as defined from the gp environment settings and the initial wgs84 SR ISpatialReference downloadSpatialReference = gpUtilities3.GetGPSpRefEnv(envMgr, wgs84, newExtent, 0, 0, 0, 0, null); downloadEnvelope.Project(wgs84); Marshal.ReleaseComObject(wgs84); Marshal.ReleaseComObject(spatialReferenceFactory); HttpWebRequest httpClient; System.Xml.Serialization.XmlSerializer serializer = null; serializer = new XmlSerializer(typeof(osm)); // get the capabilities from the server HttpWebResponse httpResponse = null; api apiCapabilities = null; CultureInfo enUSCultureInfo = new CultureInfo("en-US"); #if DEBUG Console.WriteLine("Debbuging"); message.AddMessage("Debugging..."); #endif message.AddMessage(resourceManager.GetString("GPTools_OSMGPDownload_startingDownloadRequest")); try { httpClient = HttpWebRequest.Create(baseURLString.Value + "/api/capabilities") as HttpWebRequest; httpClient = AssignProxyandCredentials(httpClient); 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; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); 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); } } } finally { if (httpResponse != null) { httpResponse.Close(); } httpClient = null; } if (apiCapabilities != null) { // check for the extent double roiArea = ((IArea)downloadEnvelope).Area; double capabilitiyArea = Convert.ToDouble(apiCapabilities.area.maximum, new CultureInfo("en-US")); if (roiArea > capabilitiyArea) { message.AddAbort(resourceManager.GetString("GPTools_OSMGPDownload_exceedDownloadROI")); return; } } // check for user interruption if (TrackCancel.Continue() == false) { return; } // list containing either only one document for a single bbox request or multiple if relation references need to be resolved List<string> downloadedOSMDocuments = new List<string>(); string requestURL = baseURLString.Value + "/api/0.6/map?bbox=" + downloadEnvelope.XMin.ToString("f5", enUSCultureInfo) + "," + downloadEnvelope.YMin.ToString("f5", enUSCultureInfo) + "," + downloadEnvelope.XMax.ToString("f5", enUSCultureInfo) + "," + downloadEnvelope.YMax.ToString("f5", enUSCultureInfo); string osmMasterDocument = downloadOSMDocument(ref message, requestURL, apiCapabilities); // check if the initial request was successfull // it might have failed at this point because too many nodes were requested or because of something else if (String.IsNullOrEmpty(osmMasterDocument)) { message.AddAbort(resourceManager.GetString("GPTools_OSMGPDownload_noValidOSMResponse")); return; } // add the "master document" ) original bbox request to the list downloadedOSMDocuments.Add(osmMasterDocument); if (includeAllReferencesGPValue.Value) { List<string> nodeList = new List<string>(); List<string> wayList = new List<string>(); List<string> relationList = new List<string>(); // check for user interruption if (TrackCancel.Continue() == false) { return; } parseOSMDocument(osmMasterDocument, ref message, ref nodeList, ref wayList, ref relationList, ref downloadedOSMDocuments, baseURLString.Value, apiCapabilities); } string metadataAbstract = resourceManager.GetString("GPTools_OSMGPDownload_metadata_abstract"); string metadataPurpose = resourceManager.GetString("GPTools_OSMGPDownload_metadata_purpose"); IGPParameter targetDatasetParameter = paramvalues.get_Element(out_targetDatasetNumber) as IGPParameter; IDEDataset2 targetDEDataset2 = gpUtilities3.UnpackGPValue(targetDatasetParameter) as IDEDataset2; IGPValue targetDatasetGPValue = gpUtilities3.UnpackGPValue(targetDatasetParameter); string targetDatasetName = ((IGPValue)targetDEDataset2).GetAsText(); IDataElement targetDataElement = targetDEDataset2 as IDataElement; IDataset targetDataset = gpUtilities3.OpenDatasetFromLocation(targetDataElement.CatalogPath); IName parentName = null; try { parentName = gpUtilities3.CreateParentFromCatalogPath(targetDataElement.CatalogPath); } catch { message.AddError(120033, resourceManager.GetString("GPTools_OSMGPFileReader_unable_to_create_fd")); return; } // test if the feature classes already exists, // if they do and the environments settings are such that an overwrite is not allowed we need to abort at this point IGeoProcessorSettings gpSettings = (IGeoProcessorSettings)envMgr; if (gpSettings.OverwriteOutput == true) { } else { if (gpUtilities3.Exists((IGPValue)targetDEDataset2) == true) { message.AddError(120010, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_basenamealreadyexists"), targetDataElement.Name)); return; } } string Container = ""; IDEUtilities deUtilities = new DEUtilitiesClass(); deUtilities.ParseContainer(targetDataElement.CatalogPath, ref Container); IFeatureWorkspace featureWorkspace = gpUtilities3.OpenFromString(Container) as IFeatureWorkspace; if (featureWorkspace == null) { message.AddError(120011, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nofeatureworkspace"), Container)); return; } // load the descriptions from which to derive the domain values OSMDomains availableDomains = null; System.Xml.XmlTextReader reader = null; try { if (File.Exists(m_editorConfigurationSettings["osmdomainsfilepath"])) { reader = new System.Xml.XmlTextReader(m_editorConfigurationSettings["osmdomainsfilepath"]); } } // If is in the server and hasn't been install all the configuration files catch { if (File.Exists(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(OSMGPDownload)).Location), "osm_domains.xml"))) { reader = new System.Xml.XmlTextReader(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(OSMGPDownload)).Location), "osm_domains.xml")); } } if (reader == null) { message.AddError(120012, resourceManager.GetString("GPTools_OSMGPDownload_NoDomainConfigFile")); return; } try { serializer = new XmlSerializer(typeof(OSMDomains)); availableDomains = serializer.Deserialize(reader) as OSMDomains; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(ex.StackTrace); message.AddError(120013, ex.Message); return; } #region define and add domains to the workspace // we are using domains to guide the edit templates in the editor for ArcGIS desktop Dictionary<string, IDomain> codedValueDomains = new Dictionary<string, IDomain>(); foreach (var domain in availableDomains.domain) { ICodedValueDomain pointCodedValueDomain = new CodedValueDomainClass(); ((IDomain)pointCodedValueDomain).Name = domain.name + "_pt"; ((IDomain)pointCodedValueDomain).FieldType = esriFieldType.esriFieldTypeString; ICodedValueDomain lineCodedValueDomain = new CodedValueDomainClass(); ((IDomain)lineCodedValueDomain).Name = domain.name + "_ln"; ((IDomain)lineCodedValueDomain).FieldType = esriFieldType.esriFieldTypeString; ICodedValueDomain polygonCodedValueDomain = new CodedValueDomainClass(); ((IDomain)polygonCodedValueDomain).Name = domain.name + "_ply"; ((IDomain)polygonCodedValueDomain).FieldType = esriFieldType.esriFieldTypeString; for (int i = 0; i < domain.domainvalue.Length; i++) { for (int domainGeometryIndex = 0; domainGeometryIndex < domain.domainvalue[i].geometrytype.Length; domainGeometryIndex++) { switch (domain.domainvalue[i].geometrytype[domainGeometryIndex]) { case geometrytype.point: pointCodedValueDomain.AddCode(domain.domainvalue[i].value, domain.domainvalue[i].value); break; case geometrytype.line: lineCodedValueDomain.AddCode(domain.domainvalue[i].value, domain.domainvalue[i].value); break; case geometrytype.polygon: polygonCodedValueDomain.AddCode(domain.domainvalue[i].value, domain.domainvalue[i].value); break; default: break; } } } // add the domain tables to the domains collection codedValueDomains.Add(((IDomain)pointCodedValueDomain).Name, (IDomain)pointCodedValueDomain); codedValueDomains.Add(((IDomain)lineCodedValueDomain).Name, (IDomain)lineCodedValueDomain); codedValueDomains.Add(((IDomain)polygonCodedValueDomain).Name, (IDomain)polygonCodedValueDomain); } IWorkspaceDomains workspaceDomain = featureWorkspace as IWorkspaceDomains; foreach (var domain in codedValueDomains.Values) { IDomain testDomain = null; try { testDomain = workspaceDomain.get_DomainByName(domain.Name); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(ex.StackTrace); } if (testDomain == null) { workspaceDomain.AddDomain(domain); } } #endregion IGPEnvironment configKeyword = getEnvironment(envMgr, "configKeyword"); IGPString gpString = null; if (configKeyword != null) gpString = configKeyword.Value as IGPString; string storageKeyword = String.Empty; if (gpString != null) { storageKeyword = gpString.Value; } IFeatureDataset targetFeatureDataset = null; if (gpUtilities3.Exists((IGPValue)targetDEDataset2)) { targetFeatureDataset = gpUtilities3.OpenDataset((IGPValue)targetDEDataset2) as IFeatureDataset; } else { targetFeatureDataset = featureWorkspace.CreateFeatureDataset(targetDataElement.Name, downloadSpatialReference); } ESRI.ArcGIS.esriSystem.UID osmClassExtensionUID = new ESRI.ArcGIS.esriSystem.UIDClass(); //GUID for the OSM feature class extension osmClassExtensionUID.Value = "{65CA4847-8661-45eb-8E1E-B2985CA17C78}"; downloadSpatialReference = ((IGeoDataset)targetFeatureDataset).SpatialReference; OSMToolHelper osmToolHelper = new OSMToolHelper(); #region create point/line/polygon feature classes and tables // points try { osmPointFeatureClass = osmToolHelper.CreatePointFeatureClass((IWorkspace2)featureWorkspace, targetFeatureDataset, targetDataElement.Name + "_osm_pt", null, null, osmClassExtensionUID, storageKeyword, availableDomains, metadataAbstract, metadataPurpose); } catch (Exception ex) { message.AddError(120014, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullpointfeatureclass"), ex.Message)); return; } if (osmPointFeatureClass == null) { return; } // change the property set of the osm class extension to skip any change detection during the initial data load osmPointFeatureClass.RemoveOSMClassExtension(); // lines try { osmLineFeatureClass = osmToolHelper.CreateLineFeatureClass((IWorkspace2)featureWorkspace, targetFeatureDataset, targetDataElement.Name + "_osm_ln", null, null, osmClassExtensionUID, storageKeyword, availableDomains, metadataAbstract, metadataPurpose); } catch (Exception ex) { message.AddError(120015, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nulllinefeatureclass"), ex.Message)); return; } if (osmLineFeatureClass == null) { return; } // change the property set of the osm class extension to skip any change detection during the initial data load osmLineFeatureClass.RemoveOSMClassExtension(); // polygons try { osmPolygonFeatureClass = osmToolHelper.CreatePolygonFeatureClass((IWorkspace2)featureWorkspace, targetFeatureDataset, targetDataElement.Name + "_osm_ply", null, null, osmClassExtensionUID, storageKeyword, availableDomains, metadataAbstract, metadataPurpose); } catch (Exception ex) { message.AddError(120016, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullpolygonfeatureclass"), ex.Message)); return; } if (osmPolygonFeatureClass == null) { return; } // change the property set of the osm class extension to skip any change detection during the initial data load osmPolygonFeatureClass.RemoveOSMClassExtension(); // relation table ITable relationTable = null; try { relationTable = osmToolHelper.CreateRelationTable((IWorkspace2)featureWorkspace, targetDataElement.Name + "_osm_relation", null, storageKeyword, metadataAbstract, metadataPurpose); } catch (Exception ex) { message.AddError(120017, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullrelationtable"), ex.Message)); return; } if (relationTable == null) { return; } // revision table ITable revisionTable = null; try { revisionTable = osmToolHelper.CreateRevisionTable((IWorkspace2)featureWorkspace, targetDataElement.Name + "_osm_revision", null, storageKeyword); } catch (Exception ex) { message.AddError(120018, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullrelationtable"), ex.Message)); return; } if (revisionTable == null) { return; } // check for user interruption if (TrackCancel.Continue() == false) { return; } #endregion #region clean any existing data from loading targets ESRI.ArcGIS.Geoprocessing.IGeoProcessor2 gp = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); IGeoProcessorResult gpResult = new GeoProcessorResultClass(); try { IVariantArray truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_pt"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_ln"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_ply"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "_osm_relation"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "_osm_revision"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); } catch (Exception ex) { message.AddWarning(ex.Message); } #endregion Dictionary<string, OSMToolHelper.simplePointRef> osmNodeDictionary = null; foreach (string osmDownloadDocument in downloadedOSMDocuments.Reverse<string>()) { long nodeCapacity = 0; long wayCapacity = 0; long relationCapacity = 0; message.AddMessage(resourceManager.GetString("GPTools_OSMGPFileReader_countingNodes")); osmToolHelper.countOSMStuff(osmDownloadDocument, ref nodeCapacity, ref wayCapacity, ref relationCapacity, ref TrackCancel); message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPFileReader_countedElements"), nodeCapacity, wayCapacity, relationCapacity)); if (osmNodeDictionary == null) osmNodeDictionary = new Dictionary<string, OSMToolHelper.simplePointRef>(Convert.ToInt32(nodeCapacity)); #region load points osmToolHelper.loadOSMNodes(osmDownloadDocument, ref TrackCancel, ref message, targetDatasetGPValue, osmPointFeatureClass, false, false, Convert.ToInt32(nodeCapacity), ref osmNodeDictionary, featureWorkspace, downloadSpatialReference, availableDomains, false); #endregion if (TrackCancel.Continue() == false) { return; } #region load ways if (wayCapacity > 0) { List<string> missingWays = null; missingWays = osmToolHelper.loadOSMWays(osmDownloadDocument, ref TrackCancel, ref message, targetDatasetGPValue, osmPointFeatureClass, osmLineFeatureClass, osmPolygonFeatureClass, false, false, Convert.ToInt32(wayCapacity), ref osmNodeDictionary, featureWorkspace, downloadSpatialReference, availableDomains, false); } #endregion if (TrackCancel.Continue() == false) { return; } # region for conserve memory condition, update refcount int refCounterFieldIndex = osmPointFeatureClass.Fields.FindField("wayRefCount"); if (refCounterFieldIndex > -1) { foreach (var refNode in osmNodeDictionary) { try { IFeature updateFeature = osmPointFeatureClass.GetFeature(refNode.Value.pointObjectID); int refCount = refNode.Value.RefCounter; if (refCount == 0) { refCount = 1; } updateFeature.set_Value(refCounterFieldIndex, refCount); updateFeature.Store(); } catch { } } } #endregion // check for user interruption if (TrackCancel.Continue() == false) { return; } ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); #region for local geodatabases enforce spatial integrity bool storedOriginal = geoProcessor.AddOutputsToMap; geoProcessor.AddOutputsToMap = false; try { if (osmLineFeatureClass != null) { if (((IDataset)osmLineFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { IVariantArray lineRepairParameters = new VarArrayClass(); lineRepairParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_ln"); lineRepairParameters.Add("DELETE_NULL"); IGeoProcessorResult2 gpResults = gp.Execute("RepairGeometry_management", lineRepairParameters, TrackCancel) as IGeoProcessorResult2; message.AddMessages(gpResults.GetResultMessages()); } } if (osmPolygonFeatureClass != null) { if (((IDataset)osmPolygonFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { IVariantArray polygonRepairParameters = new VarArrayClass(); polygonRepairParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_ply"); polygonRepairParameters.Add("DELETE_NULL"); IGeoProcessorResult2 gpResults = gp.Execute("RepairGeometry_management", polygonRepairParameters, TrackCancel) as IGeoProcessorResult2; message.AddMessages(gpResults.GetResultMessages()); } } } catch { message.AddWarning(resourceManager.GetString("GPTools_OSMGPDownload_repairgeometryfailure")); } geoProcessor.AddOutputsToMap = storedOriginal; #endregion #region load relations if (relationCapacity > 0) { List<string> missingRelations = null; missingRelations = osmToolHelper.loadOSMRelations(osmDownloadDocument, ref TrackCancel, ref message, targetDatasetGPValue, osmPointFeatureClass, osmLineFeatureClass, osmPolygonFeatureClass, Convert.ToInt32(relationCapacity), relationTable, availableDomains, false, false); } #endregion } #region update the references counts and member lists for nodes message.AddMessage(resourceManager.GetString("GPTools_OSMGPFileReader_updatereferences")); IFeatureCursor pointUpdateCursor = null; using (SchemaLockManager ptLockManager = new SchemaLockManager(osmPointFeatureClass as ITable)) { using (ComReleaser comReleaser = new ComReleaser()) { int updateCount = 0; pointUpdateCursor = osmPointFeatureClass.Update(null, false); updateCount = ((ITable)osmPointFeatureClass).RowCount(null); IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = updateCount; stepProgressor.Position = 0; stepProgressor.Message = resourceManager.GetString("GPTools_OSMGPFileReader_updatepointrefcount"); stepProgressor.StepValue = 1; stepProgressor.Show(); } comReleaser.ManageLifetime(pointUpdateCursor); IFeature pointFeature = pointUpdateCursor.NextFeature(); int osmPointIDFieldIndex = osmPointFeatureClass.FindField("OSMID"); int osmWayRefCountFieldIndex = osmPointFeatureClass.FindField("wayRefCount"); int positionCounter = 0; while (pointFeature != null) { positionCounter++; string nodeID = Convert.ToString(pointFeature.get_Value(osmPointIDFieldIndex)); // let get the reference counter from the internal node dictionary if (osmNodeDictionary[nodeID].RefCounter == 0) { pointFeature.set_Value(osmWayRefCountFieldIndex, 1); } else { pointFeature.set_Value(osmWayRefCountFieldIndex, osmNodeDictionary[nodeID].RefCounter); } pointUpdateCursor.UpdateFeature(pointFeature); if (pointFeature != null) Marshal.ReleaseComObject(pointFeature); pointFeature = pointUpdateCursor.NextFeature(); if (stepProgressor != null) { stepProgressor.Position = positionCounter; } } if (stepProgressor != null) { stepProgressor.Hide(); } } } #endregion // clean all the downloaded OSM files foreach (string osmFile in downloadedOSMDocuments) { if (File.Exists(osmFile)) { try { File.Delete(osmFile); } catch { } } } SyncState.StoreLastSyncTime(targetDatasetName, syncTime); gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; // repackage the feature class into their respective gp values IGPParameter pointFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter; IGPValue pointFeatureClassPackGPValue = gpUtilities3.UnpackGPValue(pointFeatureClassParameter); gpUtilities3.PackGPValue(pointFeatureClassPackGPValue, pointFeatureClassParameter); IGPParameter lineFeatureClassParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; IGPValue lineFeatureClassPackGPValue = gpUtilities3.UnpackGPValue(lineFeatureClassParameter); gpUtilities3.PackGPValue(lineFeatureClassPackGPValue, lineFeatureClassParameter); IGPParameter polygonFeatureClassParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; IGPValue polygon1FeatureClassPackGPValue = gpUtilities3.UnpackGPValue(polygonFeatureClassParameter); gpUtilities3.PackGPValue(polygon1FeatureClassPackGPValue, polygonFeatureClassParameter); gpUtilities3.ReleaseInternals(); Marshal.ReleaseComObject(gpUtilities3); Marshal.ReleaseComObject(baseURLString); Marshal.ReleaseComObject(downloadExtentGPValue); Marshal.ReleaseComObject(downloadEnvelope); Marshal.ReleaseComObject(includeAllReferences); Marshal.ReleaseComObject(downloadSpatialReference); if (osmToolHelper != null) osmToolHelper = null; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(ex.StackTrace); message.AddError(120019, ex.Message); } finally { try { if (osmPointFeatureClass != null) { osmPointFeatureClass.ApplyOSMClassExtension(); Marshal.ReleaseComObject(osmPointFeatureClass); } if (osmLineFeatureClass != null) { osmLineFeatureClass.ApplyOSMClassExtension(); Marshal.ReleaseComObject(osmLineFeatureClass); } if (osmPolygonFeatureClass != null) { osmPolygonFeatureClass.ApplyOSMClassExtension(); Marshal.ReleaseComObject(osmPolygonFeatureClass); } } catch (Exception ex) { message.AddError(120020, ex.ToString()); } } }
public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message) { IFeatureClass osmPointFeatureClass = null; IFeatureClass osmLineFeatureClass = null; IFeatureClass osmPolygonFeatureClass = null; OSMToolHelper osmToolHelper = null; try { DateTime syncTime = DateTime.Now; IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); osmToolHelper = new OSMToolHelper(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter osmFileParameter = paramvalues.get_Element(in_osmFileNumber) as IGPParameter; IGPValue osmFileLocationString = gpUtilities3.UnpackGPValue(osmFileParameter) as IGPValue; // ensure that the specified file does exist bool osmFileExists = false; try { osmFileExists = System.IO.File.Exists(osmFileLocationString.GetAsText()); } catch (Exception ex) { message.AddError(120029, String.Format(resourceManager.GetString("GPTools_OSMGPMultiLoader_problemaccessingfile"), ex.Message)); return; } if (osmFileExists == false) { message.AddError(120030, String.Format(resourceManager.GetString("GPTools_OSMGPFileReader_osmfiledoesnotexist"), osmFileLocationString.GetAsText())); return; } long nodeCapacity = 0; long wayCapacity = 0; long relationCapacity = 0; // this assume a clean, tidy XML file - if this is not the case, there will by sync issues later on osmToolHelper.countOSMStuffFast(osmFileLocationString.GetAsText(), ref nodeCapacity, ref wayCapacity, ref relationCapacity, ref TrackCancel); if (nodeCapacity == 0 && wayCapacity == 0 && relationCapacity == 0) { return; } message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPMultiLoader_countedElements"), nodeCapacity, wayCapacity, relationCapacity)); // determine the number of threads to be used IGPEnvironment parallelProcessingFactorEnvironment = OSMToolHelper.getEnvironment(envMgr, "parallelProcessingFactor"); IGPString parallelProcessingFactorString = parallelProcessingFactorEnvironment.Value as IGPString; // the default value is to use half the cores for additional threads - I am aware that we are comparing apples and oranges but I need a number int numberOfThreads = Convert.ToInt32(System.Environment.ProcessorCount / 2); if (!(parallelProcessingFactorEnvironment.Value.IsEmpty())) { if (!Int32.TryParse(parallelProcessingFactorString.Value, out numberOfThreads)) { // this case we have a percent string string resultString = Regex.Match(parallelProcessingFactorString.Value, @"\d+").Value; numberOfThreads = Convert.ToInt32(Int32.Parse(resultString) / 100 * System.Environment.ProcessorCount); } } // tread the special case of 0 if (numberOfThreads <= 0) numberOfThreads = 1; IGPEnvironment configKeyword = OSMToolHelper.getEnvironment(envMgr, "configKeyword"); IGPString gpString = configKeyword.Value as IGPString; string storageKeyword = String.Empty; if (gpString != null) { storageKeyword = gpString.Value; } // determine the temp folder to be use for the intermediate files IGPEnvironment scratchWorkspaceEnvironment = OSMToolHelper.getEnvironment(envMgr, "scratchWorkspace"); IDEWorkspace deWorkspace = scratchWorkspaceEnvironment.Value as IDEWorkspace; String scratchWorkspaceFolder = String.Empty; if (deWorkspace != null) { if (scratchWorkspaceEnvironment.Value.IsEmpty()) { scratchWorkspaceFolder = (new System.IO.FileInfo(osmFileLocationString.GetAsText())).DirectoryName; } else { if (deWorkspace.WorkspaceType == esriWorkspaceType.esriRemoteDatabaseWorkspace) { scratchWorkspaceFolder = System.IO.Path.GetTempPath(); } else if (deWorkspace.WorkspaceType == esriWorkspaceType.esriFileSystemWorkspace) { scratchWorkspaceFolder = ((IDataElement)deWorkspace).CatalogPath; } else { scratchWorkspaceFolder = (new System.IO.FileInfo(((IDataElement)deWorkspace).CatalogPath)).DirectoryName; } } } else { scratchWorkspaceFolder = (new System.IO.FileInfo(osmFileLocationString.GetAsText())).DirectoryName; } string metadataAbstract = resourceManager.GetString("GPTools_OSMGPFileReader_metadata_abstract"); string metadataPurpose = resourceManager.GetString("GPTools_OSMGPFileReader_metadata_purpose"); IGPParameter osmPointsFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter; IGPValue osmPointsFeatureClassGPValue = gpUtilities3.UnpackGPValue(osmPointsFeatureClassParameter) as IGPValue; IName workspaceName = gpUtilities3.CreateParentFromCatalogPath(osmPointsFeatureClassGPValue.GetAsText()); IWorkspace2 pointFeatureWorkspace = workspaceName.Open() as IWorkspace2; string[] pointFCNameElements = osmPointsFeatureClassGPValue.GetAsText().Split(System.IO.Path.DirectorySeparatorChar); IGPParameter tagPointCollectionParameter = paramvalues.get_Element(in_pointFieldNamesNumber) as IGPParameter; IGPMultiValue tagPointCollectionGPValue = gpUtilities3.UnpackGPValue(tagPointCollectionParameter) as IGPMultiValue; List<String> pointTagstoExtract = null; if (tagPointCollectionGPValue.Count > 0) { pointTagstoExtract = new List<string>(); for (int valueIndex = 0; valueIndex < tagPointCollectionGPValue.Count; valueIndex++) { string nameOfTag = tagPointCollectionGPValue.get_Value(valueIndex).GetAsText(); pointTagstoExtract.Add(nameOfTag); } } else { pointTagstoExtract = OSMToolHelper.OSMSmallFeatureClassFields(); } // points try { osmPointFeatureClass = osmToolHelper.CreateSmallPointFeatureClass(pointFeatureWorkspace, pointFCNameElements[pointFCNameElements.Length - 1], storageKeyword, metadataAbstract, metadataPurpose, pointTagstoExtract); } catch (Exception ex) { message.AddError(120035, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullpointfeatureclass"), ex.Message)); return; } if (osmPointFeatureClass == null) { return; } int osmPointIDFieldIndex = osmPointFeatureClass.FindField("OSMID"); Dictionary<string, int> mainPointAttributeFieldIndices = new Dictionary<string, int>(); foreach (string fieldName in OSMToolHelper.OSMSmallFeatureClassFields()) { int currentFieldIndex = osmPointFeatureClass.FindField(fieldName); if (currentFieldIndex != -1) { mainPointAttributeFieldIndices.Add(fieldName, currentFieldIndex); } } int tagCollectionPointFieldIndex = osmPointFeatureClass.FindField("osmTags"); int osmSupportingElementPointFieldIndex = osmPointFeatureClass.FindField("osmSupportingElement"); IGPParameter osmLineFeatureClassParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; IGPValue osmLineFeatureClassGPValue = gpUtilities3.UnpackGPValue(osmLineFeatureClassParameter) as IGPValue; IName lineWorkspaceName = gpUtilities3.CreateParentFromCatalogPath(osmLineFeatureClassGPValue.GetAsText()); IWorkspace2 lineFeatureWorkspace = lineWorkspaceName.Open() as IWorkspace2; string[] lineFCNameElements = osmLineFeatureClassGPValue.GetAsText().Split(System.IO.Path.DirectorySeparatorChar); IGPParameter tagLineCollectionParameter = paramvalues.get_Element(in_lineFieldNamesNumber) as IGPParameter; IGPMultiValue tagLineCollectionGPValue = gpUtilities3.UnpackGPValue(tagLineCollectionParameter) as IGPMultiValue; List<String> lineTagstoExtract = null; if (tagLineCollectionGPValue.Count > 0) { lineTagstoExtract = new List<string>(); for (int valueIndex = 0; valueIndex < tagLineCollectionGPValue.Count; valueIndex++) { string nameOfTag = tagLineCollectionGPValue.get_Value(valueIndex).GetAsText(); lineTagstoExtract.Add(nameOfTag); } } else { lineTagstoExtract = OSMToolHelper.OSMSmallFeatureClassFields(); } // lines try { osmLineFeatureClass = osmToolHelper.CreateSmallLineFeatureClass(lineFeatureWorkspace, lineFCNameElements[lineFCNameElements.Length -1], storageKeyword, metadataAbstract, metadataPurpose, lineTagstoExtract); } catch (Exception ex) { message.AddError(120036, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nulllinefeatureclass"), ex.Message)); return; } if (osmLineFeatureClass == null) { return; } int osmLineIDFieldIndex = osmLineFeatureClass.FindField("OSMID"); Dictionary<string, int> mainLineAttributeFieldIndices = new Dictionary<string, int>(); foreach (string fieldName in OSMToolHelper.OSMSmallFeatureClassFields()) { int currentFieldIndex = osmLineFeatureClass.FindField(fieldName); if (currentFieldIndex != -1) { mainLineAttributeFieldIndices.Add(fieldName, currentFieldIndex); } } int tagCollectionPolylineFieldIndex = osmLineFeatureClass.FindField("osmTags"); int osmSupportingElementPolylineFieldIndex = osmLineFeatureClass.FindField("osmSupportingElement"); IGPParameter osmPolygonFeatureClassParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; IGPValue osmPolygonFeatureClassGPValue = gpUtilities3.UnpackGPValue(osmPolygonFeatureClassParameter) as IGPValue; IName polygonWorkspaceName = gpUtilities3.CreateParentFromCatalogPath(osmPolygonFeatureClassGPValue.GetAsText()); IWorkspace2 polygonFeatureWorkspace = polygonWorkspaceName.Open() as IWorkspace2; string[] polygonFCNameElements = osmPolygonFeatureClassGPValue.GetAsText().Split(System.IO.Path.DirectorySeparatorChar); IGPParameter tagPolygonCollectionParameter = paramvalues.get_Element(in_polygonFieldNamesNumber) as IGPParameter; IGPMultiValue tagPolygonCollectionGPValue = gpUtilities3.UnpackGPValue(tagPolygonCollectionParameter) as IGPMultiValue; List<String> polygonTagstoExtract = null; if (tagPolygonCollectionGPValue.Count > 0) { polygonTagstoExtract = new List<string>(); for (int valueIndex = 0; valueIndex < tagPolygonCollectionGPValue.Count; valueIndex++) { string nameOfTag = tagPolygonCollectionGPValue.get_Value(valueIndex).GetAsText(); polygonTagstoExtract.Add(nameOfTag); } } else { polygonTagstoExtract = OSMToolHelper.OSMSmallFeatureClassFields(); } // polygons try { osmPolygonFeatureClass = osmToolHelper.CreateSmallPolygonFeatureClass(polygonFeatureWorkspace, polygonFCNameElements[polygonFCNameElements.Length -1], storageKeyword, metadataAbstract, metadataPurpose, polygonTagstoExtract); } catch (Exception ex) { message.AddError(120037, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullpolygonfeatureclass"), ex.Message)); return; } if (osmPolygonFeatureClass == null) { return; } int osmPolygonIDFieldIndex = osmPolygonFeatureClass.FindField("OSMID"); Dictionary<string, int> mainPolygonAttributeFieldIndices = new Dictionary<string, int>(); foreach (var fieldName in OSMToolHelper.OSMSmallFeatureClassFields()) { int currentFieldIndex = osmPolygonFeatureClass.FindField(fieldName); if (currentFieldIndex != -1) { mainPolygonAttributeFieldIndices.Add(fieldName, currentFieldIndex); } } int tagCollectionPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmTags"); int osmSupportingElementPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmSupportingElement"); ComReleaser.ReleaseCOMObject(osmPointFeatureClass); osmPointFeatureClass = null; ComReleaser.ReleaseCOMObject(osmLineFeatureClass); osmLineFeatureClass = null; ComReleaser.ReleaseCOMObject(osmPolygonFeatureClass); osmPolygonFeatureClass = null; List<string> nodeOSMFileNames = new List<string>(numberOfThreads); List<string> nodeGDBFileNames = new List<string>(numberOfThreads); List<string> wayOSMFileNames = new List<string>(numberOfThreads); List<string> wayGDBFileNames = new List<string>(numberOfThreads); List<string> relationOSMFileNames = new List<string>(numberOfThreads); List<string> relationGDBFileNames = new List<string>(numberOfThreads); if (TrackCancel.Continue() == false) { return; } // split the original OSM xml file into smaller pieces for the python processes osmToolHelper.splitOSMFile(osmFileLocationString.GetAsText(), scratchWorkspaceFolder, nodeCapacity, wayCapacity, relationCapacity, numberOfThreads, out nodeOSMFileNames, out nodeGDBFileNames, out wayOSMFileNames, out wayGDBFileNames, out relationOSMFileNames, out relationGDBFileNames); IGPParameter deleteSourceOSMFileParameter = paramvalues.get_Element(in_deleteOSMSourceFileNumber) as IGPParameter; IGPBoolean deleteSourceOSMFileGPValue = gpUtilities3.UnpackGPValue(deleteSourceOSMFileParameter) as IGPBoolean; if (deleteSourceOSMFileGPValue.Value) { try { System.IO.File.Delete(osmFileLocationString.GetAsText()); } catch (Exception ex) { message.AddWarning(ex.Message); } } if (TrackCancel.Continue() == false) { return; } if (nodeOSMFileNames.Count == 0) { nodeOSMFileNames.Add(osmFileLocationString.GetAsText()); nodeGDBFileNames.Add(osmPointsFeatureClassGPValue.GetAsText()); wayOSMFileNames.Add(osmFileLocationString.GetAsText()); wayGDBFileNames.Add(osmLineFeatureClassGPValue.GetAsText()); relationOSMFileNames.Add(osmFileLocationString.GetAsText()); relationGDBFileNames.Add(osmPolygonFeatureClassGPValue.GetAsText()); } else { // for the nodes let's load one of the parts directly into the target file geodatabase nodeGDBFileNames[0] = ((IWorkspace)pointFeatureWorkspace).PathName; } // define variables helping to invoke core tools for data management IGeoProcessorResult2 gpResults2 = null; IGeoProcessor2 geoProcessor = new GeoProcessorClass(); IGPParameter deleteSupportingNodesParameter = paramvalues.get_Element(in_deleteSupportNodesNumber) as IGPParameter; IGPBoolean deleteSupportingNodesGPValue = gpUtilities3.UnpackGPValue(deleteSupportingNodesParameter) as IGPBoolean; #region load points osmToolHelper.loadOSMNodes(nodeOSMFileNames, nodeGDBFileNames, pointFCNameElements[pointFCNameElements.Length - 1], osmPointsFeatureClassGPValue.GetAsText(), pointTagstoExtract, deleteSupportingNodesGPValue.Value, ref message, ref TrackCancel); #endregion if (TrackCancel.Continue() == false) { return; } #region load ways osmToolHelper.loadOSMWays(wayOSMFileNames, osmPointsFeatureClassGPValue.GetAsText(), wayGDBFileNames, lineFCNameElements[lineFCNameElements.Length - 1], polygonFCNameElements[polygonFCNameElements.Length - 1], lineTagstoExtract, polygonTagstoExtract, ref message, ref TrackCancel); #endregion #region for local geodatabases enforce spatial integrity bool storedOriginalLocal = geoProcessor.AddOutputsToMap; geoProcessor.AddOutputsToMap = false; try { osmLineFeatureClass = ((IFeatureWorkspace)lineFeatureWorkspace).OpenFeatureClass(lineFCNameElements[lineFCNameElements.Length - 1]); if (osmLineFeatureClass != null) { if (((IDataset)osmLineFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; IGPParameter outLinesParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; IGPValue lineFeatureClass = gpUtilities3.UnpackGPValue(outLinesParameter); DataManagementTools.RepairGeometry repairlineGeometry = new DataManagementTools.RepairGeometry(osmLineFeatureClass); IVariantArray repairGeometryParameterArray = new VarArrayClass(); repairGeometryParameterArray.Add(lineFeatureClass.GetAsText()); repairGeometryParameterArray.Add("DELETE_NULL"); gpResults2 = geoProcessor.Execute(repairlineGeometry.ToolName, repairGeometryParameterArray, TrackCancel) as IGeoProcessorResult2; message.AddMessages(gpResults2.GetResultMessages()); ComReleaser.ReleaseCOMObject(gpUtilities3); } } } catch { } finally { ComReleaser.ReleaseCOMObject(osmLineFeatureClass); } try { osmPolygonFeatureClass = ((IFeatureWorkspace)polygonFeatureWorkspace).OpenFeatureClass(polygonFCNameElements[polygonFCNameElements.Length - 1]); if (osmPolygonFeatureClass != null) { if (((IDataset)osmPolygonFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; IGPParameter outPolygonParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; IGPValue polygonFeatureClass = gpUtilities3.UnpackGPValue(outPolygonParameter); DataManagementTools.RepairGeometry repairpolygonGeometry = new DataManagementTools.RepairGeometry(osmPolygonFeatureClass); IVariantArray repairGeometryParameterArray = new VarArrayClass(); repairGeometryParameterArray.Add(polygonFeatureClass.GetAsText()); repairGeometryParameterArray.Add("DELETE_NULL"); gpResults2 = geoProcessor.Execute(repairpolygonGeometry.ToolName, repairGeometryParameterArray, TrackCancel) as IGeoProcessorResult2; message.AddMessages(gpResults2.GetResultMessages()); ComReleaser.ReleaseCOMObject(gpUtilities3); } } } catch { } finally { ComReleaser.ReleaseCOMObject(osmPolygonFeatureClass); } geoProcessor.AddOutputsToMap = storedOriginalLocal; #endregion if (TrackCancel.Continue() == false) { return; } #region load relations osmToolHelper.loadOSMRelations(relationOSMFileNames, osmLineFeatureClassGPValue.GetAsText(), osmPolygonFeatureClassGPValue.GetAsText(), relationGDBFileNames, lineTagstoExtract, polygonTagstoExtract, ref TrackCancel, ref message); #endregion // check for user interruption if (TrackCancel.Continue() == false) { return; } #region for local geodatabases enforce spatial integrity //storedOriginalLocal = geoProcessor.AddOutputsToMap; //geoProcessor.AddOutputsToMap = false; //try //{ // osmLineFeatureClass = ((IFeatureWorkspace)lineFeatureWorkspace).OpenFeatureClass(lineFCNameElements[lineFCNameElements.Length - 1]); // if (osmLineFeatureClass != null) // { // if (((IDataset)osmLineFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) // { // gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; // IGPParameter outLinesParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; // IGPValue lineFeatureClass = gpUtilities3.UnpackGPValue(outLinesParameter); // DataManagementTools.RepairGeometry repairlineGeometry = new DataManagementTools.RepairGeometry(osmLineFeatureClass); // IVariantArray repairGeometryParameterArray = new VarArrayClass(); // repairGeometryParameterArray.Add(lineFeatureClass.GetAsText()); // repairGeometryParameterArray.Add("DELETE_NULL"); // gpResults2 = geoProcessor.Execute(repairlineGeometry.ToolName, repairGeometryParameterArray, TrackCancel) as IGeoProcessorResult2; // message.AddMessages(gpResults2.GetResultMessages()); // ComReleaser.ReleaseCOMObject(gpUtilities3); // } // } //} //catch { } //finally //{ // ComReleaser.ReleaseCOMObject(osmLineFeatureClass); //} //try //{ // osmPolygonFeatureClass = ((IFeatureWorkspace)polygonFeatureWorkspace).OpenFeatureClass(polygonFCNameElements[polygonFCNameElements.Length - 1]); // if (osmPolygonFeatureClass != null) // { // if (((IDataset)osmPolygonFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) // { // gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; // IGPParameter outPolygonParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; // IGPValue polygonFeatureClass = gpUtilities3.UnpackGPValue(outPolygonParameter); // DataManagementTools.RepairGeometry repairpolygonGeometry = new DataManagementTools.RepairGeometry(osmPolygonFeatureClass); // IVariantArray repairGeometryParameterArray = new VarArrayClass(); // repairGeometryParameterArray.Add(polygonFeatureClass.GetAsText()); // repairGeometryParameterArray.Add("DELETE_NULL"); // gpResults2 = geoProcessor.Execute(repairpolygonGeometry.ToolName, repairGeometryParameterArray, TrackCancel) as IGeoProcessorResult2; // message.AddMessages(gpResults2.GetResultMessages()); // ComReleaser.ReleaseCOMObject(gpUtilities3); // } // } //} //catch { } //finally //{ // ComReleaser.ReleaseCOMObject(osmPolygonFeatureClass); //} //geoProcessor.AddOutputsToMap = storedOriginalLocal; #endregion if (TrackCancel.Continue() == false) { return; } if (deleteSupportingNodesGPValue.Value) { message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPMultiLoader_remove_supportNodes"))); storedOriginalLocal = geoProcessor.AddOutputsToMap; geoProcessor.AddOutputsToMap = false; // create a layer file to select the points that have attributes osmPointFeatureClass = ((IFeatureWorkspace)pointFeatureWorkspace).OpenFeatureClass(pointFCNameElements[pointFCNameElements.Length - 1]); IVariantArray makeFeatureLayerParameterArray = new VarArrayClass(); makeFeatureLayerParameterArray.Add(osmPointsFeatureClassGPValue.GetAsText()); string tempLayerFile = System.IO.Path.GetTempFileName(); makeFeatureLayerParameterArray.Add(tempLayerFile); makeFeatureLayerParameterArray.Add(String.Format("{0} = 'no'", osmPointFeatureClass.SqlIdentifier("osmSupportingElement"))); geoProcessor.Execute("MakeFeatureLayer_management", makeFeatureLayerParameterArray, TrackCancel); // copy the features into its own feature class IVariantArray copyFeatureParametersArray = new VarArrayClass(); copyFeatureParametersArray.Add(tempLayerFile); string tempFeatureClass = String.Join("\\", new string[] { ((IWorkspace)pointFeatureWorkspace).PathName, "t_" + pointFCNameElements[pointFCNameElements.Length - 1] }); copyFeatureParametersArray.Add(tempFeatureClass); geoProcessor.Execute("CopyFeatures_management", copyFeatureParametersArray, TrackCancel); // delete the temp file System.IO.File.Delete(tempLayerFile); // delete the original feature class IVariantArray deleteParameterArray = new VarArrayClass(); deleteParameterArray.Add(osmPointsFeatureClassGPValue.GetAsText()); geoProcessor.Execute("Delete_management", deleteParameterArray, TrackCancel); // rename the temp feature class back to the original IVariantArray renameParameterArray = new VarArrayClass(); renameParameterArray.Add(tempFeatureClass); renameParameterArray.Add(osmPointsFeatureClassGPValue.GetAsText()); geoProcessor.Execute("Rename_management", renameParameterArray, TrackCancel); geoProcessor.AddOutputsToMap = storedOriginalLocal; ComReleaser.ReleaseCOMObject(osmPointFeatureClass); ComReleaser.ReleaseCOMObject(geoProcessor); } gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; // repackage the feature class into their respective gp values IGPParameter pointFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter; IGPValue pointFeatureClassGPValue = gpUtilities3.UnpackGPValue(pointFeatureClassParameter); gpUtilities3.PackGPValue(pointFeatureClassGPValue, pointFeatureClassParameter); IGPParameter lineFeatureClassParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; IGPValue line1FeatureClassGPValue = gpUtilities3.UnpackGPValue(lineFeatureClassParameter); gpUtilities3.PackGPValue(line1FeatureClassGPValue, lineFeatureClassParameter); IGPParameter polygonFeatureClassParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; IGPValue polygon1FeatureClassGPValue = gpUtilities3.UnpackGPValue(polygonFeatureClassParameter); gpUtilities3.PackGPValue(polygon1FeatureClassGPValue, polygonFeatureClassParameter); ComReleaser.ReleaseCOMObject(osmFileLocationString); gpUtilities3.ReleaseInternals(); ComReleaser.ReleaseCOMObject(gpUtilities3); } catch (Exception ex) { message.AddError(120055, ex.Message); message.AddError(120055, ex.StackTrace); } finally { try { osmToolHelper = null; System.GC.Collect(); System.GC.WaitForPendingFinalizers(); } catch (Exception ex) { message.AddError(120056, ex.ToString()); } } }
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); } }
public void UpdateParameters(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager pEnvMgr) { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); IGPParameter revisionTableParameter = paramvalues.get_Element(in_changesTablesNumber) as IGPParameter; IGPValue revisionTableGPValue = gpUtilities3.UnpackGPValue(revisionTableParameter); if (revisionTableGPValue.IsEmpty() == false) { if (gpUtilities3.Exists(revisionTableGPValue) == false) { IGPEnvironment workspaceEnvironment = gpUtilities3.GetEnvironment(pEnvMgr.GetEnvironments(), "workspace"); IGPValue workspace = workspaceEnvironment.Value; if (workspace.IsEmpty() == false) { string old_locationValue = workspace.GetAsText() + System.IO.Path.DirectorySeparatorChar + revisionTableGPValue.GetAsText(); try { string location = gpUtilities3.QualifyOutputCatalogPath(old_locationValue); if (location.Length != old_locationValue.Length) { revisionTableGPValue.SetAsText(location); gpUtilities3.PackGPValue(revisionTableGPValue, revisionTableParameter); } } catch { } } } } gpUtilities3.ReleaseInternals(); }
public void UpdateMessages(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager pEnvMgr, ESRI.ArcGIS.Geodatabase.IGPMessages Messages) { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); // check for a valid download url IGPParameter uploadURLParameter = paramvalues.get_Element(in_uploadURLNumber) as IGPParameter; IGPString uploadURLGPString = uploadURLParameter.Value as IGPString; if (uploadURLGPString == null) { Messages.ReplaceError(in_uploadURLNumber, -198, String.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), uploadURLParameter.Value.GetAsText())); } else { try { if (uploadURLParameter.HasBeenValidated == false) { Uri downloadURI = new Uri(uploadURLGPString.Value); // check base url api osmAPICapabilities = OSMGPDownload.CheckValidServerURL(uploadURLGPString.Value); // if we can construct a valid URI class then we are accepting the value and store it in the user settings as well if (m_editorConfigurationSettings != null) { if (m_editorConfigurationSettings.ContainsKey("osmbaseurl")) { m_editorConfigurationSettings["osmbaseurl"] = uploadURLGPString.Value; } else { m_editorConfigurationSettings.Add("osmbaseurl", uploadURLGPString.Value); } OSMGPFactory.StoreOSMEditorSettings(m_editorConfigurationSettings); } } } catch (Exception ex) { StringBuilder errorMessage = new StringBuilder(); errorMessage.AppendLine(resourceManager.GetString("GPTools_OSMGPUpload_invaliduploadurl")); errorMessage.AppendLine(ex.Message); Messages.ReplaceError(in_uploadURLNumber, -3, errorMessage.ToString()); } } IGPParameter revisionTableParameter = paramvalues.get_Element(in_changesTablesNumber) as IGPParameter; IGPValue revisionTableGPValue = gpUtilities3.UnpackGPValue(revisionTableParameter); if (revisionTableGPValue.IsEmpty()) return; ITable revisionTable = null; IQueryFilter revisionTableQueryFilter = null; try { using (ComReleaser comReleaser = new ComReleaser()) { gpUtilities3.DecodeTableView(revisionTableGPValue, out revisionTable, out revisionTableQueryFilter); comReleaser.ManageLifetime(revisionTable); if (revisionTable is IFeatureClass) { Messages.ReplaceError(in_changesTablesNumber, -4, resourceManager.GetString("GPTools_OSMGPUpload_notarevisiontable")); return; } IDatasetEdit datasetEdit = revisionTable as IDatasetEdit; comReleaser.ManageLifetime(datasetEdit); if (datasetEdit == null) { return; } if (datasetEdit.IsBeingEdited()) { Messages.ReplaceError(in_changesTablesNumber, -4, resourceManager.GetString("GPTools_OSMGPUpload_inputnotvalidduringedit")); } } gpUtilities3.ReleaseInternals(); } catch { // check if we are dealing with a variable -- if we do then leave this string alone string tableName = revisionTableGPValue.GetAsText(); string tableNameModified = tableName.Replace("%", String.Empty); if (((tableName.Length - tableNameModified.Length) % 2) == 0) { Messages.Replace(in_changesTablesNumber, new GPMessageClass()); } } }
public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message) { try { IGPUtilities3 execute_Utilities = new GPUtilitiesClass(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter inputOSMParameter = paramvalues.get_Element(in_osmFeatureClass) as IGPParameter; IGPValue inputOSMGPValue = execute_Utilities.UnpackGPValue(inputOSMParameter); IGPParameter tagCollectionParameter = paramvalues.get_Element(in_attributeSelector) as IGPParameter; IGPMultiValue tagCollectionGPValue = execute_Utilities.UnpackGPValue(tagCollectionParameter) as IGPMultiValue; if (tagCollectionGPValue == null) { message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), tagCollectionParameter.Name)); return; } bool useUpdateCursor = false; IFeatureClass osmFeatureClass = null; ITable osmInputTable = null; IQueryFilter osmQueryFilter = null; try { execute_Utilities.DecodeFeatureLayer(inputOSMGPValue, out osmFeatureClass, out osmQueryFilter); if (osmFeatureClass != null) { if (osmFeatureClass.Extension is IOSMClassExtension) { useUpdateCursor = false; } else { useUpdateCursor = true; } } osmInputTable = osmFeatureClass as ITable; } catch { } try { if (osmInputTable == null) { execute_Utilities.DecodeTableView(inputOSMGPValue, out osmInputTable, out osmQueryFilter); } } catch { } if (osmInputTable == null) { string errorMessage = String.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelecto_unableopentable"), inputOSMGPValue.GetAsText()); message.AddError(120053, errorMessage); return; } // find the field that holds tag binary/xml field int osmTagCollectionFieldIndex = osmInputTable.FindField("osmTags"); // if the Field doesn't exist - wasn't found (index = -1) get out if (osmTagCollectionFieldIndex == -1) { message.AddError(120005, resourceManager.GetString("GPTools_OSMGPAttributeSelector_notagfieldfound")); return; } // check if the tag collection includes the keyword "ALL", if does then we'll need to extract all tags bool extractAll = false; for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { if (tagCollectionGPValue.get_Value(valueIndex).GetAsText().Equals("ALL")) { extractAll = true; break; } } //if (extractAll) //{ // if (osmTagKeyCodedValues == null) // extractAllTags(ref osmTagKeyCodedValues, osmInputTable, osmQueryFilter, osmTagCollectionFieldIndex, false); // if (osmTagKeyCodedValues == null) // { // message.AddAbort(resourceManager.GetString("GPTools_OSMGPAttributeSelector_Unable2RetrieveTags")); // return; // } // // empty the existing gp multivalue object // tagCollectionGPValue = new GPMultiValueClass(); // // fill the coded domain in gp multivalue object // for (int valueIndex = 0; valueIndex < osmTagKeyCodedValues.CodeCount; valueIndex++) // { // tagCollectionGPValue.AddValue(osmTagKeyCodedValues.get_Value(valueIndex)); // } //} // get an overall feature count as that determines the progress indicator int featureCount = osmInputTable.RowCount(osmQueryFilter); // set up the progress indicator IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = featureCount; stepProgressor.Position = 0; stepProgressor.Message = resourceManager.GetString("GPTools_OSMGPAttributeSelector_progressMessage"); stepProgressor.StepValue = 1; stepProgressor.Show(); } // let's get all the indices of the desired fields // if the field already exists get the index and if it doesn't exist create it Dictionary <string, int> tagsAttributesIndices = new Dictionary <string, int>(); Dictionary <int, int> attributeFieldLength = new Dictionary <int, int>(); IFeatureWorkspaceManage featureWorkspaceManage = ((IDataset)osmInputTable).Workspace as IFeatureWorkspaceManage; String illegalCharacters = String.Empty; ISQLSyntax sqlSyntax = ((IDataset)osmInputTable).Workspace as ISQLSyntax; if (sqlSyntax != null) { illegalCharacters = sqlSyntax.GetInvalidCharacters(); } IFieldsEdit fieldsEdit = osmInputTable.Fields as IFieldsEdit; using (SchemaLockManager lockMgr = new SchemaLockManager(osmInputTable)) { try { string tagKey = String.Empty; ESRI.ArcGIS.Geoprocessing.IGeoProcessor2 gp = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); // if we have explicitly defined tags to extract then go through the list of values now if (extractAll == false) { for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { if (TrackCancel.Continue() == false) { return; } try { // Check if the input field already exists. string nameofTag = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); tagKey = convert2AttributeFieldName(nameofTag, illegalCharacters); int fieldIndex = osmInputTable.FindField(tagKey); if (fieldIndex < 0) { // generate a new attribute field IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = tagKey; fieldEdit.AliasName_2 = nameofTag + resourceManager.GetString("GPTools_OSMGPAttributeSelector_aliasaddition"); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; osmInputTable.AddField(fieldEdit); message.AddMessage(string.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelector_addField"), tagKey, nameofTag)); // re-generate the attribute index fieldIndex = osmInputTable.FindField(tagKey); } if (fieldIndex > 0) { tagsAttributesIndices.Add(nameofTag, fieldIndex); attributeFieldLength.Add(fieldIndex, osmInputTable.Fields.get_Field(fieldIndex).Length); } } catch (Exception ex) { // the key is already there, this might result because from multiple upper and lower-case combinations of the same key message.AddWarning(ex.Message + " (" + convert2OSMKey(tagKey, illegalCharacters) + ")"); } } } else { List <string> listofAllTags = extractAllTags(osmInputTable, osmQueryFilter, osmTagCollectionFieldIndex); foreach (string nameOfTag in listofAllTags) { if (TrackCancel.Continue() == false) { return; } try { // Check if the input field already exists. tagKey = convert2AttributeFieldName(nameOfTag, illegalCharacters); int fieldIndex = osmInputTable.FindField(tagKey); if (fieldIndex < 0) { // generate a new attribute field IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = tagKey; fieldEdit.AliasName_2 = nameOfTag + resourceManager.GetString("GPTools_OSMGPAttributeSelector_aliasaddition"); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; osmInputTable.AddField(fieldEdit); message.AddMessage(string.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelector_addField"), tagKey, nameOfTag)); // re-generate the attribute index fieldIndex = osmInputTable.FindField(tagKey); } if (fieldIndex > 0) { tagsAttributesIndices.Add(nameOfTag, fieldIndex); attributeFieldLength.Add(fieldIndex, osmInputTable.Fields.get_Field(fieldIndex).Length); } } catch (Exception ex) { // the key is already there, this might result because from multiple upper and lower-case combinations of the same key message.AddWarning(ex.Message + " (" + convert2OSMKey(tagKey, illegalCharacters) + ")"); } } } } catch (Exception ex) { message.AddWarning(ex.Message); } } try { execute_Utilities.DecodeFeatureLayer(inputOSMGPValue, out osmFeatureClass, out osmQueryFilter); if (osmFeatureClass != null) { if (osmFeatureClass.Extension is IOSMClassExtension) { useUpdateCursor = false; } else { useUpdateCursor = true; } } osmInputTable = osmFeatureClass as ITable; } catch { } try { if (osmInputTable == null) { execute_Utilities.DecodeTableView(inputOSMGPValue, out osmInputTable, out osmQueryFilter); } } catch { } if (osmInputTable == null) { string errorMessage = String.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelecto_unableopentable"), inputOSMGPValue.GetAsText()); message.AddError(120053, errorMessage); return; } using (ComReleaser comReleaser = new ComReleaser()) { using (SchemaLockManager lockMgr = new SchemaLockManager(osmInputTable)) { // get an update cursor for all the features to process ICursor rowCursor = null; if (useUpdateCursor) { rowCursor = osmInputTable.Update(osmQueryFilter, false); } else { rowCursor = osmInputTable.Search(osmQueryFilter, false); } comReleaser.ManageLifetime(rowCursor); IRow osmRow = null; Dictionary <string, string> tagKeys = new Dictionary <string, string>(); int progessIndex = 0; #if DEBUG message.AddMessage("useUpdateCursor: " + useUpdateCursor.ToString()); #endif // as long as there are features.... while ((osmRow = rowCursor.NextRow()) != null) { // retrieve the tags of the current feature tag[] storedTags = _osmUtility.retrieveOSMTags(osmRow, osmTagCollectionFieldIndex, ((IDataset)osmInputTable).Workspace); bool rowChanged = false; if (storedTags != null) { foreach (tag tagItem in storedTags) { // Check for matching values so we only change a minimum number of rows if (tagsAttributesIndices.ContainsKey(tagItem.k)) { int fieldIndex = tagsAttributesIndices[tagItem.k]; //...then stored the value in the attribute field // ensure that the content of the tag actually does fit into the field length...otherwise do truncate it string tagValue = tagItem.v; int fieldLength = attributeFieldLength[fieldIndex]; if (tagValue.Length > fieldLength) { tagValue = tagValue.Substring(0, fieldLength); } osmRow.set_Value(fieldIndex, tagValue); rowChanged = true; } else { #if DEBUG //message.AddWarning(tagItem.k); #endif } } } storedTags = null; try { if (rowChanged) { if (useUpdateCursor) { rowCursor.UpdateRow(osmRow); } else { // update the feature through the cursor osmRow.Store(); } } progessIndex++; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); message.AddWarning(ex.Message); } if (osmRow != null) { Marshal.ReleaseComObject(osmRow); } if (stepProgressor != null) { // update the progress UI stepProgressor.Position = progessIndex; } // check for user cancellation (every 100 rows) if ((progessIndex % 100 == 0) && (TrackCancel.Continue() == false)) { return; } } if (stepProgressor != null) { stepProgressor.Hide(); } } } execute_Utilities.ReleaseInternals(); Marshal.ReleaseComObject(execute_Utilities); } catch (Exception ex) { message.AddError(120054, ex.Message); } }
public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message) { IFeatureClass osmPointFeatureClass = null; IFeatureClass osmLineFeatureClass = null; IFeatureClass osmPolygonFeatureClass = null; OSMToolHelper osmToolHelper = null; try { DateTime syncTime = DateTime.Now; IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); osmToolHelper = new OSMToolHelper(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter osmFileParameter = paramvalues.get_Element(in_osmFileNumber) as IGPParameter; IGPValue osmFileLocationString = gpUtilities3.UnpackGPValue(osmFileParameter) as IGPValue; // ensure that the specified file does exist bool osmFileExists = false; try { osmFileExists = System.IO.File.Exists(osmFileLocationString.GetAsText()); } catch (Exception ex) { message.AddError(120029, String.Format(resourceManager.GetString("GPTools_OSMGPFileReader_problemaccessingfile"), ex.Message)); return; } if (osmFileExists == false) { message.AddError(120030, String.Format(resourceManager.GetString("GPTools_OSMGPFileReader_osmfiledoesnotexist"), osmFileLocationString.GetAsText())); return; } IGPParameter conserveMemoryParameter = paramvalues.get_Element(in_conserveMemoryNumber) as IGPParameter; IGPBoolean conserveMemoryGPValue = gpUtilities3.UnpackGPValue(conserveMemoryParameter) as IGPBoolean; if (conserveMemoryGPValue == null) { message.AddError(120031, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), conserveMemoryParameter.Name)); return; } message.AddMessage(resourceManager.GetString("GPTools_OSMGPFileReader_countingNodes")); long nodeCapacity = 0; long wayCapacity = 0; long relationCapacity = 0; Dictionary<esriGeometryType, List<string>> attributeTags = new Dictionary<esriGeometryType, List<string>>(); IGPParameter tagCollectionParameter = paramvalues.get_Element(in_attributeSelector) as IGPParameter; IGPMultiValue tagCollectionGPValue = gpUtilities3.UnpackGPValue(tagCollectionParameter) as IGPMultiValue; List<String> tagstoExtract = null; if (tagCollectionGPValue.Count > 0) { tagstoExtract = new List<string>(); for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { string nameOfTag = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); if (nameOfTag.ToUpper().Equals("ALL")) { tagstoExtract = new List<string>(); break; } else { tagstoExtract.Add(nameOfTag); } } } // if there is an "ALL" keyword then we scan for all tags, otherwise we only add the desired tags to the feature classes and do a 'quick' // count scan if (tagstoExtract != null) { if (tagstoExtract.Count > 0) { // if the number of tags is > 0 then do a simple feature count and take name tags names from the gp value osmToolHelper.countOSMStuff(osmFileLocationString.GetAsText(), ref nodeCapacity, ref wayCapacity, ref relationCapacity, ref TrackCancel); attributeTags.Add(esriGeometryType.esriGeometryPoint, tagstoExtract); attributeTags.Add(esriGeometryType.esriGeometryPolyline, tagstoExtract); attributeTags.Add(esriGeometryType.esriGeometryPolygon, tagstoExtract); } else { // the count should be zero if we encountered the "ALL" keyword // in this case count the features and create a list of unique tags attributeTags = osmToolHelper.countOSMCapacityAndTags(osmFileLocationString.GetAsText(), ref nodeCapacity, ref wayCapacity, ref relationCapacity, ref TrackCancel); } } else { // no tags we defined, hence we do a simple count and create an empty list indicating that no additional fields // need to be created osmToolHelper.countOSMStuff(osmFileLocationString.GetAsText(), ref nodeCapacity, ref wayCapacity, ref relationCapacity, ref TrackCancel); attributeTags.Add(esriGeometryType.esriGeometryPoint, new List<string>()); attributeTags.Add(esriGeometryType.esriGeometryPolyline, new List<string>()); attributeTags.Add(esriGeometryType.esriGeometryPolygon, new List<string>()); } if (nodeCapacity == 0 && wayCapacity == 0 && relationCapacity == 0) { return; } if (conserveMemoryGPValue.Value == false) { osmNodeDictionary = new Dictionary<string, OSMToolHelper.simplePointRef>(Convert.ToInt32(nodeCapacity)); } message.AddMessage(String.Format(resourceManager.GetString("GPTools_OSMGPFileReader_countedElements"), nodeCapacity, wayCapacity, relationCapacity)); // prepare the feature dataset and classes IGPParameter targetDatasetParameter = paramvalues.get_Element(out_targetDatasetNumber) as IGPParameter; IGPValue targetDatasetGPValue = gpUtilities3.UnpackGPValue(targetDatasetParameter); IDEDataset2 targetDEDataset2 = targetDatasetGPValue as IDEDataset2; if (targetDEDataset2 == null) { message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), targetDatasetParameter.Name)); return; } string targetDatasetName = ((IGPValue)targetDEDataset2).GetAsText(); IDataElement targetDataElement = targetDEDataset2 as IDataElement; IDataset targetDataset = gpUtilities3.OpenDatasetFromLocation(targetDataElement.CatalogPath); IName parentName = null; try { parentName = gpUtilities3.CreateParentFromCatalogPath(targetDataElement.CatalogPath); } catch { message.AddError(120033, resourceManager.GetString("GPTools_OSMGPFileReader_unable_to_create_fd")); return; } // test if the feature classes already exists, // if they do and the environments settings are such that an overwrite is not allowed we need to abort at this point IGeoProcessorSettings gpSettings = (IGeoProcessorSettings)envMgr; if (gpSettings.OverwriteOutput == true) { } else { if (gpUtilities3.Exists((IGPValue)targetDEDataset2) == true) { message.AddError(120032, String.Format(resourceManager.GetString("GPTools_OSMGPFileReader_basenamealreadyexists"), targetDataElement.Name)); return; } } // load the descriptions from which to derive the domain values OSMDomains availableDomains = null; // Reading the XML document requires a FileStream. System.Xml.XmlTextReader reader = null; string xmlDomainFile = ""; m_editorConfigurationSettings.TryGetValue("osmdomainsfilepath", out xmlDomainFile); if (System.IO.File.Exists(xmlDomainFile)) { reader = new System.Xml.XmlTextReader(xmlDomainFile); } if (reader == null) { message.AddError(120033, resourceManager.GetString("GPTools_OSMGPDownload_NoDomainConfigFile")); return; } System.Xml.Serialization.XmlSerializer domainSerializer = null; try { domainSerializer = new XmlSerializer(typeof(OSMDomains)); availableDomains = domainSerializer.Deserialize(reader) as OSMDomains; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(ex.StackTrace); message.AddError(120034, ex.Message); return; } reader.Close(); message.AddMessage(resourceManager.GetString("GPTools_preparedb")); Dictionary<string, IDomain> codedValueDomains = new Dictionary<string, IDomain>(); foreach (var domain in availableDomains.domain) { ICodedValueDomain pointCodedValueDomain = new CodedValueDomainClass(); ((IDomain)pointCodedValueDomain).Name = domain.name + "_pt"; ((IDomain)pointCodedValueDomain).FieldType = esriFieldType.esriFieldTypeString; ICodedValueDomain lineCodedValueDomain = new CodedValueDomainClass(); ((IDomain)lineCodedValueDomain).Name = domain.name + "_ln"; ((IDomain)lineCodedValueDomain).FieldType = esriFieldType.esriFieldTypeString; ICodedValueDomain polygonCodedValueDomain = new CodedValueDomainClass(); ((IDomain)polygonCodedValueDomain).Name = domain.name + "_ply"; ((IDomain)polygonCodedValueDomain).FieldType = esriFieldType.esriFieldTypeString; for (int i = 0; i < domain.domainvalue.Length; i++) { for (int domainGeometryIndex = 0; domainGeometryIndex < domain.domainvalue[i].geometrytype.Length; domainGeometryIndex++) { switch (domain.domainvalue[i].geometrytype[domainGeometryIndex]) { case geometrytype.point: pointCodedValueDomain.AddCode(domain.domainvalue[i].value, domain.domainvalue[i].value); break; case geometrytype.line: lineCodedValueDomain.AddCode(domain.domainvalue[i].value, domain.domainvalue[i].value); break; case geometrytype.polygon: polygonCodedValueDomain.AddCode(domain.domainvalue[i].value, domain.domainvalue[i].value); break; default: break; } } } // add the domain tables to the domains collection codedValueDomains.Add(((IDomain)pointCodedValueDomain).Name, (IDomain)pointCodedValueDomain); codedValueDomains.Add(((IDomain)lineCodedValueDomain).Name, (IDomain)lineCodedValueDomain); codedValueDomains.Add(((IDomain)polygonCodedValueDomain).Name, (IDomain)polygonCodedValueDomain); } IWorkspaceDomains workspaceDomain = null; IFeatureWorkspace featureWorkspace = null; // if the target dataset already exists we can go ahead and QI to it directly if (targetDataset != null) { workspaceDomain = targetDataset.Workspace as IWorkspaceDomains; featureWorkspace = targetDataset.Workspace as IFeatureWorkspace; } else { // in case it doesn't exist yet we will open the parent (the workspace - geodatabase- itself) and // use it as a reference to create the feature dataset and the feature classes in it. IWorkspace newWorkspace = ((IName)parentName).Open() as IWorkspace; workspaceDomain = newWorkspace as IWorkspaceDomains; featureWorkspace = newWorkspace as IFeatureWorkspace; } foreach (var domain in codedValueDomains.Values) { IDomain testDomain = null; try { testDomain = workspaceDomain.get_DomainByName(domain.Name); } catch { } if (testDomain == null) { workspaceDomain.AddDomain(domain); } } // this determines the spatial reference as defined from the gp environment settings and the initial wgs84 SR ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass() as ISpatialReferenceFactory; ISpatialReference wgs84 = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984) as ISpatialReference; ISpatialReference downloadSpatialReference = gpUtilities3.GetGPSpRefEnv(envMgr, wgs84, null, 0, 0, 0, 0, null); Marshal.ReleaseComObject(wgs84); Marshal.ReleaseComObject(spatialReferenceFactory); IGPEnvironment configKeyword = OSMGPDownload.getEnvironment(envMgr, "configKeyword"); IGPString gpString = configKeyword.Value as IGPString; string storageKeyword = String.Empty; if (gpString != null) { storageKeyword = gpString.Value; } IFeatureDataset targetFeatureDataset = null; if (gpUtilities3.Exists((IGPValue)targetDEDataset2)) { targetFeatureDataset = gpUtilities3.OpenDataset((IGPValue)targetDEDataset2) as IFeatureDataset; } else { targetFeatureDataset = featureWorkspace.CreateFeatureDataset(targetDataElement.Name, downloadSpatialReference); } downloadSpatialReference = ((IGeoDataset)targetFeatureDataset).SpatialReference; string metadataAbstract = resourceManager.GetString("GPTools_OSMGPFileReader_metadata_abstract"); string metadataPurpose = resourceManager.GetString("GPTools_OSMGPFileReader_metadata_purpose"); // assign the custom class extension for use with the OSM feature inspector UID osmClassUID = new UIDClass(); osmClassUID.Value = "{65CA4847-8661-45eb-8E1E-B2985CA17C78}"; // points try { osmPointFeatureClass = osmToolHelper.CreatePointFeatureClass((IWorkspace2)featureWorkspace, targetFeatureDataset, targetDataElement.Name + "_osm_pt", null, null, null, storageKeyword, availableDomains, metadataAbstract, metadataPurpose, attributeTags[esriGeometryType.esriGeometryPoint]); } catch (Exception ex) { message.AddError(120035, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullpointfeatureclass"), ex.Message)); return; } if (osmPointFeatureClass == null) { return; } // change the property set of the osm class extension to skip any change detection during the initial data load osmPointFeatureClass.RemoveOSMClassExtension(); int osmPointIDFieldIndex = osmPointFeatureClass.FindField("OSMID"); Dictionary<string, int> osmPointDomainAttributeFieldIndices = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmPointFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmPointDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); } } int tagCollectionPointFieldIndex = osmPointFeatureClass.FindField("osmTags"); int osmUserPointFieldIndex = osmPointFeatureClass.FindField("osmuser"); int osmUIDPointFieldIndex = osmPointFeatureClass.FindField("osmuid"); int osmVisiblePointFieldIndex = osmPointFeatureClass.FindField("osmvisible"); int osmVersionPointFieldIndex = osmPointFeatureClass.FindField("osmversion"); int osmChangesetPointFieldIndex = osmPointFeatureClass.FindField("osmchangeset"); int osmTimeStampPointFieldIndex = osmPointFeatureClass.FindField("osmtimestamp"); int osmMemberOfPointFieldIndex = osmPointFeatureClass.FindField("osmMemberOf"); int osmSupportingElementPointFieldIndex = osmPointFeatureClass.FindField("osmSupportingElement"); int osmWayRefCountFieldIndex = osmPointFeatureClass.FindField("wayRefCount"); // lines try { osmLineFeatureClass = osmToolHelper.CreateLineFeatureClass((IWorkspace2)featureWorkspace, targetFeatureDataset, targetDataElement.Name + "_osm_ln", null, null, null, storageKeyword, availableDomains, metadataAbstract, metadataPurpose, attributeTags[esriGeometryType.esriGeometryPolyline]); } catch (Exception ex) { message.AddError(120036, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nulllinefeatureclass"), ex.Message)); return; } if (osmLineFeatureClass == null) { return; } // change the property set of the osm class extension to skip any change detection during the initial data load osmLineFeatureClass.RemoveOSMClassExtension(); int osmLineIDFieldIndex = osmLineFeatureClass.FindField("OSMID"); Dictionary<string, int> osmLineDomainAttributeFieldIndices = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmLineFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmLineDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); } } int tagCollectionPolylineFieldIndex = osmLineFeatureClass.FindField("osmTags"); int osmUserPolylineFieldIndex = osmLineFeatureClass.FindField("osmuser"); int osmUIDPolylineFieldIndex = osmLineFeatureClass.FindField("osmuid"); int osmVisiblePolylineFieldIndex = osmLineFeatureClass.FindField("osmvisible"); int osmVersionPolylineFieldIndex = osmLineFeatureClass.FindField("osmversion"); int osmChangesetPolylineFieldIndex = osmLineFeatureClass.FindField("osmchangeset"); int osmTimeStampPolylineFieldIndex = osmLineFeatureClass.FindField("osmtimestamp"); int osmMemberOfPolylineFieldIndex = osmLineFeatureClass.FindField("osmMemberOf"); int osmMembersPolylineFieldIndex = osmLineFeatureClass.FindField("osmMembers"); int osmSupportingElementPolylineFieldIndex = osmLineFeatureClass.FindField("osmSupportingElement"); // polygons try { osmPolygonFeatureClass = osmToolHelper.CreatePolygonFeatureClass((IWorkspace2)featureWorkspace, targetFeatureDataset, targetDataElement.Name + "_osm_ply", null, null, null, storageKeyword, availableDomains, metadataAbstract, metadataPurpose, attributeTags[esriGeometryType.esriGeometryPolygon]); } catch (Exception ex) { message.AddError(120037, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullpolygonfeatureclass"), ex.Message)); return; } if (osmPolygonFeatureClass == null) { return; } // change the property set of the osm class extension to skip any change detection during the initial data load osmPolygonFeatureClass.RemoveOSMClassExtension(); int osmPolygonIDFieldIndex = osmPolygonFeatureClass.FindField("OSMID"); Dictionary<string, int> osmPolygonDomainAttributeFieldIndices = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmPolygonFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmPolygonDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); } } int tagCollectionPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmTags"); int osmUserPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmuser"); int osmUIDPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmuid"); int osmVisiblePolygonFieldIndex = osmPolygonFeatureClass.FindField("osmvisible"); int osmVersionPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmversion"); int osmChangesetPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmchangeset"); int osmTimeStampPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmtimestamp"); int osmMemberOfPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmMemberOf"); int osmMembersPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmMembers"); int osmSupportingElementPolygonFieldIndex = osmPolygonFeatureClass.FindField("osmSupportingElement"); // relation table ITable relationTable = null; try { relationTable = osmToolHelper.CreateRelationTable((IWorkspace2)featureWorkspace, targetDataElement.Name + "_osm_relation", null, storageKeyword, metadataAbstract, metadataPurpose); } catch (Exception ex) { message.AddError(120038, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullrelationtable"), ex.Message)); return; } if (relationTable == null) { return; } int osmRelationIDFieldIndex = relationTable.FindField("OSMID"); int tagCollectionRelationFieldIndex = relationTable.FindField("osmTags"); int osmUserRelationFieldIndex = relationTable.FindField("osmuser"); int osmUIDRelationFieldIndex = relationTable.FindField("osmuid"); int osmVisibleRelationFieldIndex = relationTable.FindField("osmvisible"); int osmVersionRelationFieldIndex = relationTable.FindField("osmversion"); int osmChangesetRelationFieldIndex = relationTable.FindField("osmchangeset"); int osmTimeStampRelationFieldIndex = relationTable.FindField("osmtimestamp"); int osmMemberOfRelationFieldIndex = relationTable.FindField("osmMemberOf"); int osmMembersRelationFieldIndex = relationTable.FindField("osmMembers"); int osmSupportingElementRelationFieldIndex = relationTable.FindField("osmSupportingElement"); // revision table ITable revisionTable = null; try { revisionTable = osmToolHelper.CreateRevisionTable((IWorkspace2)featureWorkspace, targetDataElement.Name + "_osm_revision", null, storageKeyword); } catch (Exception ex) { message.AddError(120039, String.Format(resourceManager.GetString("GPTools_OSMGPDownload_nullrevisiontable"), ex.Message)); return; } if (revisionTable == null) { return; } #region clean any existing data from loading targets ESRI.ArcGIS.Geoprocessing.IGeoProcessor2 gp = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); IGeoProcessorResult gpResult = new GeoProcessorResultClass(); try { IVariantArray truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_pt"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_ln"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "\\" + targetDataElement.Name + "_osm_ply"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "_osm_relation"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); truncateParameters = new VarArrayClass(); truncateParameters.Add(((IWorkspace)featureWorkspace).PathName + "\\" + targetDataElement.Name + "_osm_revision"); gpResult = gp.Execute("TruncateTable_management", truncateParameters, TrackCancel); } catch (Exception ex) { message.AddWarning(ex.Message); } #endregion bool fastLoad = false; //// check for user interruption //if (TrackCancel.Continue() == false) //{ // message.AddAbort(resourceManager.GetString("GPTools_toolabort")); // return; //} //IFeatureCursor deleteCursor = null; //using (ComReleaser comReleaser = new ComReleaser()) //{ // // let's make sure that we clean out any old data that might have existed in the feature classes // deleteCursor = osmPointFeatureClass.Update(null, false); // comReleaser.ManageLifetime(deleteCursor); // for (IFeature feature = deleteCursor.NextFeature(); feature != null; feature = deleteCursor.NextFeature()) // { // feature.Delete(); // // check for user interruption // if (TrackCancel.Continue() == false) // { // message.AddAbort(resourceManager.GetString("GPTools_toolabort")); // return; // } // } //} //using (ComReleaser comReleaser = new ComReleaser()) //{ // deleteCursor = osmLineFeatureClass.Update(null, false); // comReleaser.ManageLifetime(deleteCursor); // for (IFeature feature = deleteCursor.NextFeature(); feature != null; feature = deleteCursor.NextFeature()) // { // feature.Delete(); // // check for user interruption // if (TrackCancel.Continue() == false) // { // message.AddAbort(resourceManager.GetString("GPTools_toolabort")); // return; // } // } //} //using (ComReleaser comReleaser = new ComReleaser()) //{ // deleteCursor = osmPolygonFeatureClass.Update(null, false); // comReleaser.ManageLifetime(deleteCursor); // for (IFeature feature = deleteCursor.NextFeature(); feature != null; feature = deleteCursor.NextFeature()) // { // feature.Delete(); // // check for user interruption // if (TrackCancel.Continue() == false) // { // message.AddAbort(resourceManager.GetString("GPTools_toolabort")); // return; // } // } //} //ICursor tableCursor = null; //using (ComReleaser comReleaser = new ComReleaser()) //{ // tableCursor = relationTable.Update(null, false); // comReleaser.ManageLifetime(tableCursor); // for (IRow row = tableCursor.NextRow(); row != null; row = tableCursor.NextRow()) // { // row.Delete(); // // check for user interruption // if (TrackCancel.Continue() == false) // { // message.AddAbort(resourceManager.GetString("GPTools_toolabort")); // return; // } // } //} //using (ComReleaser comReleaser = new ComReleaser()) //{ // tableCursor = revisionTable.Update(null, false); // comReleaser.ManageLifetime(tableCursor); // for (IRow row = tableCursor.NextRow(); row != null; row = tableCursor.NextRow()) // { // row.Delete(); // // check for user interruption // if (TrackCancel.Continue() == false) // { // message.AddAbort(resourceManager.GetString("GPTools_toolabort")); // return; // } // } //} // define variables helping to invoke core tools for data management IGeoProcessorResult2 gpResults2 = null; IGeoProcessor2 geoProcessor = new GeoProcessorClass(); #region load points osmToolHelper.loadOSMNodes(osmFileLocationString.GetAsText(), ref TrackCancel, ref message, targetDatasetGPValue, osmPointFeatureClass, conserveMemoryGPValue.Value, fastLoad, Convert.ToInt32(nodeCapacity), ref osmNodeDictionary, featureWorkspace, downloadSpatialReference, availableDomains, false); #endregion if (TrackCancel.Continue() == false) { return; } #region load ways List<string> missingWays = osmToolHelper.loadOSMWays(osmFileLocationString.GetAsText(), ref TrackCancel, ref message, targetDatasetGPValue, osmPointFeatureClass, osmLineFeatureClass, osmPolygonFeatureClass, conserveMemoryGPValue.Value, fastLoad, Convert.ToInt32(wayCapacity), ref osmNodeDictionary, featureWorkspace, downloadSpatialReference, availableDomains, false); #endregion if (downloadSpatialReference != null) Marshal.ReleaseComObject(downloadSpatialReference); #region for local geodatabases enforce spatial integrity bool storedOriginalLocal = geoProcessor.AddOutputsToMap; geoProcessor.AddOutputsToMap = false; if (osmLineFeatureClass != null) { if (((IDataset)osmLineFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; IGPParameter outLinesParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; IGPValue lineFeatureClass = gpUtilities3.UnpackGPValue(outLinesParameter); DataManagementTools.RepairGeometry repairlineGeometry = new DataManagementTools.RepairGeometry(osmLineFeatureClass); IVariantArray repairGeometryParameterArray = new VarArrayClass(); repairGeometryParameterArray.Add(lineFeatureClass.GetAsText()); repairGeometryParameterArray.Add("DELETE_NULL"); gpResults2 = geoProcessor.Execute(repairlineGeometry.ToolName, repairGeometryParameterArray, TrackCancel) as IGeoProcessorResult2; message.AddMessages(gpResults2.GetResultMessages()); ComReleaser.ReleaseCOMObject(gpUtilities3); } } if (osmPolygonFeatureClass != null) { if (((IDataset)osmPolygonFeatureClass).Workspace.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; IGPParameter outPolygonParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; IGPValue polygonFeatureClass = gpUtilities3.UnpackGPValue(outPolygonParameter); DataManagementTools.RepairGeometry repairpolygonGeometry = new DataManagementTools.RepairGeometry(osmPolygonFeatureClass); IVariantArray repairGeometryParameterArray = new VarArrayClass(); repairGeometryParameterArray.Add(polygonFeatureClass.GetAsText()); repairGeometryParameterArray.Add("DELETE_NULL"); gpResults2 = geoProcessor.Execute(repairpolygonGeometry.ToolName, repairGeometryParameterArray, TrackCancel) as IGeoProcessorResult2; message.AddMessages(gpResults2.GetResultMessages()); ComReleaser.ReleaseCOMObject(gpUtilities3); } } geoProcessor.AddOutputsToMap = storedOriginalLocal; #endregion if (TrackCancel.Continue() == false) { return; } #region load relations List<string> missingRelations = osmToolHelper.loadOSMRelations(osmFileLocationString.GetAsText(), ref TrackCancel, ref message, targetDatasetGPValue, osmPointFeatureClass, osmLineFeatureClass, osmPolygonFeatureClass, Convert.ToInt32(relationCapacity), relationTable, availableDomains, fastLoad, false); #endregion // check for user interruption if (TrackCancel.Continue() == false) { return; } //storedOriginalLocal = geoProcessor.AddOutputsToMap; //try //{ // geoProcessor.AddOutputsToMap = false; // // add indexes for revisions // //IGPValue revisionTableGPValue = gpUtilities3.MakeGPValueFromObject(revisionTable); // string revisionTableString = targetDatasetGPValue.GetAsText() + System.IO.Path.DirectorySeparatorChar + ((IDataset)revisionTable).BrowseName; // IVariantArray parameterArrary2 = osmToolHelper.CreateAddIndexParameterArray(revisionTableString, "osmoldid;osmnewid", "osmID_IDX", "", ""); // gpResults2 = geoProcessor.Execute("AddIndex_management", parameterArrary2, TrackCancel) as IGeoProcessorResult2; // message.AddMessages(gpResults2.GetResultMessages()); //} //catch (Exception ex) //{ // message.AddWarning(ex.Message); //} //finally //{ // geoProcessor.AddOutputsToMap = storedOriginalLocal; //} #region update the references counts and member lists for nodes message.AddMessage(resourceManager.GetString("GPTools_OSMGPFileReader_updatereferences")); IFeatureCursor pointUpdateCursor = null; IQueryFilter pointQueryFilter = new QueryFilterClass(); // adjust of number of all other reference counter from 0 to 1 if (conserveMemoryGPValue.Value == true) { pointQueryFilter.WhereClause = osmPointFeatureClass.SqlIdentifier("wayRefCount") + " = 0"; pointQueryFilter.SubFields = osmPointFeatureClass.OIDFieldName + ",wayRefCount"; } using (SchemaLockManager ptLockManager = new SchemaLockManager(osmPointFeatureClass as ITable)) { using (ComReleaser comReleaser = new ComReleaser()) { int updateCount = 0; if (conserveMemoryGPValue.Value == true) { pointUpdateCursor = osmPointFeatureClass.Update(pointQueryFilter, false); updateCount = ((ITable)osmPointFeatureClass).RowCount(pointQueryFilter); } else { pointUpdateCursor = osmPointFeatureClass.Update(null, false); updateCount = ((ITable)osmPointFeatureClass).RowCount(null); } IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = updateCount; stepProgressor.Position = 0; stepProgressor.Message = resourceManager.GetString("GPTools_OSMGPFileReader_updatepointrefcount"); stepProgressor.StepValue = 1; stepProgressor.Show(); } comReleaser.ManageLifetime(pointUpdateCursor); IFeature pointFeature = pointUpdateCursor.NextFeature(); int positionCounter = 0; while (pointFeature != null) { positionCounter++; string nodeID = Convert.ToString(pointFeature.get_Value(osmPointIDFieldIndex)); if (conserveMemoryGPValue.Value == false) { // let get the reference counter from the internal node dictionary if (osmNodeDictionary[nodeID].RefCounter == 0) { pointFeature.set_Value(osmWayRefCountFieldIndex, 1); } else { pointFeature.set_Value(osmWayRefCountFieldIndex, osmNodeDictionary[nodeID].RefCounter); } } else { // in the case of memory conservation let's go change the 0s to 1s pointFeature.set_Value(osmWayRefCountFieldIndex, 1); } pointUpdateCursor.UpdateFeature(pointFeature); if (pointFeature != null) Marshal.ReleaseComObject(pointFeature); pointFeature = pointUpdateCursor.NextFeature(); if (stepProgressor != null) { stepProgressor.Position = positionCounter; } } if (stepProgressor != null) { stepProgressor.Hide(); } Marshal.ReleaseComObject(pointQueryFilter); } } #endregion if (osmNodeDictionary != null) { // clear outstanding resources potentially holding points osmNodeDictionary = null; System.GC.Collect(2, GCCollectionMode.Forced); } if (missingRelations.Count > 0) { missingRelations.Clear(); missingRelations = null; } if (missingWays.Count > 0) { missingWays.Clear(); missingWays = null; } SyncState.StoreLastSyncTime(targetDatasetName, syncTime); gpUtilities3 = new GPUtilitiesClass() as IGPUtilities3; // repackage the feature class into their respective gp values IGPParameter pointFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter; IGPValue pointFeatureClassGPValue = gpUtilities3.UnpackGPValue(pointFeatureClassParameter); gpUtilities3.PackGPValue(pointFeatureClassGPValue, pointFeatureClassParameter); IGPParameter lineFeatureClassParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter; IGPValue line1FeatureClassGPValue = gpUtilities3.UnpackGPValue(lineFeatureClassParameter); gpUtilities3.PackGPValue(line1FeatureClassGPValue, lineFeatureClassParameter); IGPParameter polygonFeatureClassParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter; IGPValue polygon1FeatureClassGPValue = gpUtilities3.UnpackGPValue(polygonFeatureClassParameter); gpUtilities3.PackGPValue(polygon1FeatureClassGPValue, polygonFeatureClassParameter); ComReleaser.ReleaseCOMObject(relationTable); ComReleaser.ReleaseCOMObject(revisionTable); ComReleaser.ReleaseCOMObject(targetFeatureDataset); ComReleaser.ReleaseCOMObject(featureWorkspace); ComReleaser.ReleaseCOMObject(osmFileLocationString); ComReleaser.ReleaseCOMObject(conserveMemoryGPValue); ComReleaser.ReleaseCOMObject(targetDataset); gpUtilities3.ReleaseInternals(); ComReleaser.ReleaseCOMObject(gpUtilities3); } catch (Exception ex) { message.AddError(120055, ex.Message); message.AddError(120055, ex.StackTrace); } finally { try { // TE -- 1/7/2015 // this is a 'breaking' change as the default loader won't no longer enable the edit extension // the reasoning here is that most users would like the OSM in a 'read-only' fashion, and don't need to track // changes to be properly transmitted back to the OSM server //if (osmPointFeatureClass != null) //{ // osmPointFeatureClass.ApplyOSMClassExtension(); // ComReleaser.ReleaseCOMObject(osmPointFeatureClass); //} //if (osmLineFeatureClass != null) //{ // osmLineFeatureClass.ApplyOSMClassExtension(); // ComReleaser.ReleaseCOMObject(osmLineFeatureClass); //} //if (osmPolygonFeatureClass != null) //{ // osmPolygonFeatureClass.ApplyOSMClassExtension(); // ComReleaser.ReleaseCOMObject(osmPolygonFeatureClass); //} osmToolHelper = null; System.GC.Collect(); System.GC.WaitForPendingFinalizers(); } catch (Exception ex) { message.AddError(120056, ex.ToString()); } } }
public void UpdateParameters(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager pEnvMgr) { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); IGPParameter targetDatasetParameter = paramvalues.get_Element(out_targetDatasetNumber) as IGPParameter; IGPValue targetDatasetGPValue = gpUtilities3.UnpackGPValue(targetDatasetParameter); IDEFeatureDataset targetDEFeatureDataset = targetDatasetGPValue as IDEFeatureDataset; IGPParameter3 outPointsFeatureClassParameter = null; IGPValue outPointsFeatureClass = null; string outpointsPath = String.Empty; IGPParameter3 outLinesFeatureClassParameter = null; IGPValue outLinesFeatureClass = null; string outlinesPath = String.Empty; IGPParameter3 outPolygonFeatureClassParameter = null; IGPValue outPolygonFeatureClass = null; string outpolygonsPath = String.Empty; if (((IGPValue)targetDEFeatureDataset).GetAsText().Length != 0) { IDataElement dataElement = targetDEFeatureDataset as IDataElement; try { gpUtilities3.QualifyOutputDataElement(gpUtilities3.UnpackGPValue(targetDatasetParameter)); } catch { return; } string nameOfPointFeatureClass = dataElement.GetBaseName() + "_osm_pt"; string nameOfLineFeatureClass = dataElement.GetBaseName() + "_osm_ln"; string nameOfPolygonFeatureClass = dataElement.GetBaseName() + "_osm_ply"; try { outpointsPath = dataElement.CatalogPath + System.IO.Path.DirectorySeparatorChar + nameOfPointFeatureClass; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } outPointsFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter3; outPointsFeatureClass = gpUtilities3.UnpackGPValue(outPointsFeatureClassParameter); outlinesPath = dataElement.CatalogPath + System.IO.Path.DirectorySeparatorChar + nameOfLineFeatureClass; outLinesFeatureClassParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter3; outLinesFeatureClass = gpUtilities3.UnpackGPValue(outLinesFeatureClassParameter); outpolygonsPath = dataElement.CatalogPath + System.IO.Path.DirectorySeparatorChar + nameOfPolygonFeatureClass; outPolygonFeatureClassParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter3; outPolygonFeatureClass = gpUtilities3.UnpackGPValue(outPolygonFeatureClassParameter); } // TE - 10/20/2014 if (((IGPParameter)paramvalues.get_Element(in_attributeSelector)).Altered) { IGPParameter tagCollectionParameter = paramvalues.get_Element(in_attributeSelector) as IGPParameter; IGPMultiValue tagCollectionGPValue = gpUtilities3.UnpackGPValue(tagCollectionParameter) as IGPMultiValue; IGPCodedValueDomain codedTagDomain = tagCollectionParameter.Domain as IGPCodedValueDomain; for (int attributeValueIndex = 0; attributeValueIndex < tagCollectionGPValue.Count; attributeValueIndex++) { string valueString = tagCollectionGPValue.get_Value(attributeValueIndex).GetAsText(); IGPValue testFieldValue = codedTagDomain.FindValue(valueString); if (testFieldValue == null) { codedTagDomain.AddStringCode(valueString, valueString); } } string illegalCharacters = "`~@#$%^&()-+=,{}.![];"; IFieldsEdit fieldsEdit = new FieldsClass(); for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { string tagString = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); if (tagString != "ALL") { // Check if the input field already exists. string cleanedTagKey = OSMToolHelper.convert2AttributeFieldName(tagString, illegalCharacters); IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = cleanedTagKey; fieldEdit.AliasName_2 = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; fieldsEdit.AddField(fieldEdit); } } IFields fields = fieldsEdit as IFields; // Get the derived output feature class schema and empty the additional fields. This ensures // that you don't get dublicate entries. if (outPointsFeatureClassParameter != null) { IGPFeatureSchema polySchema = outPolygonFeatureClassParameter.Schema as IGPFeatureSchema; if (polySchema != null) { // Add the additional field to the polygon output. polySchema.AdditionalFields = fields; } else { polySchema = new GPFeatureSchemaClass(); polySchema.AdditionalFields = fields; polySchema.GeometryType = esriGeometryType.esriGeometryPolygon; polySchema.FeatureType = esriFeatureType.esriFTSimple; } } if (outLinesFeatureClassParameter != null) { IGPFeatureSchema lineSchema = outLinesFeatureClassParameter.Schema as IGPFeatureSchema; if (lineSchema != null) { // Add the additional field to the line output. lineSchema.AdditionalFields = fields; } else { lineSchema = new GPFeatureSchemaClass(); lineSchema.AdditionalFields = fields; lineSchema.GeometryType = esriGeometryType.esriGeometryPolyline; lineSchema.FeatureType = esriFeatureType.esriFTSimple; } } if (outPointsFeatureClassParameter != null) { IGPFeatureSchema pointSchema = outPointsFeatureClassParameter.Schema as IGPFeatureSchema; if (pointSchema != null) { // Add the additional field to the point output. pointSchema.AdditionalFields = fields; } else { pointSchema = new GPFeatureSchemaClass(); pointSchema.AdditionalFields = fields; pointSchema.GeometryType = esriGeometryType.esriGeometryPoint; pointSchema.FeatureType = esriFeatureType.esriFTSimple; } } } if (outPointsFeatureClassParameter != null) { outPointsFeatureClass.SetAsText(outpointsPath); gpUtilities3.PackGPValue(outPointsFeatureClass, outPointsFeatureClassParameter); } if (outLinesFeatureClassParameter != null) { outLinesFeatureClass.SetAsText(outlinesPath); gpUtilities3.PackGPValue(outLinesFeatureClass, outLinesFeatureClassParameter); } if (outPolygonFeatureClassParameter != null) { outPolygonFeatureClass.SetAsText(outpolygonsPath); gpUtilities3.PackGPValue(outPolygonFeatureClass, outPolygonFeatureClassParameter); } gpUtilities3.ReleaseInternals(); if (gpUtilities3 != null) ComReleaser.ReleaseCOMObject(gpUtilities3); }
public void UpdateMessages(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager pEnvMgr, ESRI.ArcGIS.Geodatabase.IGPMessages Messages) { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); IGPParameter targetDatasetParameter = paramvalues.get_Element(out_targetDatasetNumber) as IGPParameter; try { gpUtilities3.QualifyOutputDataElement(gpUtilities3.UnpackGPValue(targetDatasetParameter)); } catch { Messages.ReplaceError(out_targetDatasetNumber, -2, resourceManager.GetString("GPTools_OSMGPFileReader_targetDataset_notexist")); } // check for valid geodatabase path // if the user is pointing to a valid directory on disk, flag it as an error IGPValue targetDatasetGPValue = gpUtilities3.UnpackGPValue(targetDatasetParameter); if (targetDatasetGPValue.IsEmpty() == false) { if (System.IO.Directory.Exists(targetDatasetGPValue.GetAsText())) { Messages.ReplaceError(out_targetDatasetNumber, -4, resourceManager.GetString("GPTools_OSMGPDownload_directory_is_not_target_dataset")); } } // check one of the output feature classes for version compatibility IGPParameter pointFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter; IDEFeatureClass pointDEFeatureClass = gpUtilities3.UnpackGPValue(pointFeatureClassParameter) as IDEFeatureClass; if (pointDEFeatureClass != null) { if (((IGPValue)pointDEFeatureClass).IsEmpty() == false) { if (gpUtilities3.Exists((IGPValue)pointDEFeatureClass)) { IFeatureClass ptfc = gpUtilities3.Open(gpUtilities3.UnpackGPValue(pointFeatureClassParameter)) as IFeatureClass; IPropertySet osmExtensionPropertySet = ptfc.ExtensionProperties; if (osmExtensionPropertySet == null) { Messages.ReplaceError(out_targetDatasetNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmPointsNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmLinesNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmPolygonsNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); } else { try { int extensionVersion = Convert.ToInt32(osmExtensionPropertySet.GetProperty("VERSION")); if (extensionVersion != OSMClassExtensionManager.Version) { Messages.ReplaceError(out_targetDatasetNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), extensionVersion, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmPointsNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), extensionVersion, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmLinesNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), extensionVersion, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmPolygonsNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), extensionVersion, OSMClassExtensionManager.Version)); } } catch { Messages.ReplaceError(out_targetDatasetNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmPointsNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmLinesNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); Messages.ReplaceError(out_osmPolygonsNumber, -5, string.Format(resourceManager.GetString("GPTools_IncompatibleExtensionVersion"), 1, OSMClassExtensionManager.Version)); } } } } } gpUtilities3.ReleaseInternals(); if (gpUtilities3 != null) ComReleaser.ReleaseCOMObject(gpUtilities3); }
public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message) { try { IGPUtilities3 execute_Utilities = new GPUtilitiesClass(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter inputOSMParameter = paramvalues.get_Element(in_osmFeatureClass) as IGPParameter; IGPValue inputOSMGPValue = execute_Utilities.UnpackGPValue(inputOSMParameter); IGPParameter tagCollectionParameter = paramvalues.get_Element(in_attributeSelector) as IGPParameter; IGPMultiValue tagCollectionGPValue = execute_Utilities.UnpackGPValue(tagCollectionParameter) as IGPMultiValue; if (tagCollectionGPValue == null) { message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), tagCollectionParameter.Name)); return; } bool useUpdateCursor = false; IFeatureClass osmFeatureClass = null; ITable osmInputTable = null; IQueryFilter osmQueryFilter = null; try { execute_Utilities.DecodeFeatureLayer(inputOSMGPValue, out osmFeatureClass, out osmQueryFilter); if (osmFeatureClass != null) { if (osmFeatureClass.Extension is IOSMClassExtension) { useUpdateCursor = false; } else { useUpdateCursor = true; } } osmInputTable = osmFeatureClass as ITable; } catch { } try { if (osmInputTable == null) { execute_Utilities.DecodeTableView(inputOSMGPValue, out osmInputTable, out osmQueryFilter); } } catch { } if (osmInputTable == null) { string errorMessage = String.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelecto_unableopentable"), inputOSMGPValue.GetAsText()); message.AddError(120053, errorMessage); return; } // find the field that holds tag binary/xml field int osmTagCollectionFieldIndex = osmInputTable.FindField("osmTags"); // if the Field doesn't exist - wasn't found (index = -1) get out if (osmTagCollectionFieldIndex == -1) { message.AddError(120005, resourceManager.GetString("GPTools_OSMGPAttributeSelector_notagfieldfound")); return; } // check if the tag collection includes the keyword "ALL", if does then we'll need to extract all tags bool extractAll = false; for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { if (tagCollectionGPValue.get_Value(valueIndex).GetAsText().Equals("ALL")) { extractAll = true; break; } } //if (extractAll) //{ // if (osmTagKeyCodedValues == null) // extractAllTags(ref osmTagKeyCodedValues, osmInputTable, osmQueryFilter, osmTagCollectionFieldIndex, false); // if (osmTagKeyCodedValues == null) // { // message.AddAbort(resourceManager.GetString("GPTools_OSMGPAttributeSelector_Unable2RetrieveTags")); // return; // } // // empty the existing gp multivalue object // tagCollectionGPValue = new GPMultiValueClass(); // // fill the coded domain in gp multivalue object // for (int valueIndex = 0; valueIndex < osmTagKeyCodedValues.CodeCount; valueIndex++) // { // tagCollectionGPValue.AddValue(osmTagKeyCodedValues.get_Value(valueIndex)); // } //} // get an overall feature count as that determines the progress indicator int featureCount = osmInputTable.RowCount(osmQueryFilter); // set up the progress indicator IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = featureCount; stepProgressor.Position = 0; stepProgressor.Message = resourceManager.GetString("GPTools_OSMGPAttributeSelector_progressMessage"); stepProgressor.StepValue = 1; stepProgressor.Show(); } // let's get all the indices of the desired fields // if the field already exists get the index and if it doesn't exist create it Dictionary<string, int> tagsAttributesIndices = new Dictionary<string, int>(); Dictionary<int, int> attributeFieldLength = new Dictionary<int, int>(); IFeatureWorkspaceManage featureWorkspaceManage = ((IDataset)osmInputTable).Workspace as IFeatureWorkspaceManage; String illegalCharacters = String.Empty; ISQLSyntax sqlSyntax = ((IDataset)osmInputTable).Workspace as ISQLSyntax; if (sqlSyntax != null) { illegalCharacters = sqlSyntax.GetInvalidCharacters(); } IFieldsEdit fieldsEdit = osmInputTable.Fields as IFieldsEdit; using (SchemaLockManager lockMgr = new SchemaLockManager(osmInputTable)) { try { string tagKey = String.Empty; ESRI.ArcGIS.Geoprocessing.IGeoProcessor2 gp = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); // if we have explicitly defined tags to extract then go through the list of values now if (extractAll == false) { for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { if (TrackCancel.Continue() == false) return; try { // Check if the input field already exists. string nameofTag = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); tagKey = convert2AttributeFieldName(nameofTag, illegalCharacters); int fieldIndex = osmInputTable.FindField(tagKey); if (fieldIndex < 0) { // generate a new attribute field IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = tagKey; fieldEdit.AliasName_2 = nameofTag + resourceManager.GetString("GPTools_OSMGPAttributeSelector_aliasaddition"); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; osmInputTable.AddField(fieldEdit); message.AddMessage(string.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelector_addField"), tagKey, nameofTag)); // re-generate the attribute index fieldIndex = osmInputTable.FindField(tagKey); } if (fieldIndex > 0) { tagsAttributesIndices.Add(nameofTag, fieldIndex); attributeFieldLength.Add(fieldIndex, osmInputTable.Fields.get_Field(fieldIndex).Length); } } catch (Exception ex) { // the key is already there, this might result because from multiple upper and lower-case combinations of the same key message.AddWarning(ex.Message + " (" + convert2OSMKey(tagKey, illegalCharacters) + ")"); } } } else { List<string> listofAllTags = extractAllTags(osmInputTable, osmQueryFilter, osmTagCollectionFieldIndex); foreach (string nameOfTag in listofAllTags) { if (TrackCancel.Continue() == false) return; try { // Check if the input field already exists. tagKey = convert2AttributeFieldName(nameOfTag, illegalCharacters); int fieldIndex = osmInputTable.FindField(tagKey); if (fieldIndex < 0) { // generate a new attribute field IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = tagKey; fieldEdit.AliasName_2 = nameOfTag + resourceManager.GetString("GPTools_OSMGPAttributeSelector_aliasaddition"); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; osmInputTable.AddField(fieldEdit); message.AddMessage(string.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelector_addField"), tagKey, nameOfTag)); // re-generate the attribute index fieldIndex = osmInputTable.FindField(tagKey); } if (fieldIndex > 0) { tagsAttributesIndices.Add(nameOfTag, fieldIndex); attributeFieldLength.Add(fieldIndex, osmInputTable.Fields.get_Field(fieldIndex).Length); } } catch (Exception ex) { // the key is already there, this might result because from multiple upper and lower-case combinations of the same key message.AddWarning(ex.Message + " (" + convert2OSMKey(tagKey, illegalCharacters) + ")"); } } } } catch (Exception ex) { message.AddWarning(ex.Message); } } try { execute_Utilities.DecodeFeatureLayer(inputOSMGPValue, out osmFeatureClass, out osmQueryFilter); if (osmFeatureClass != null) { if (osmFeatureClass.Extension is IOSMClassExtension) { useUpdateCursor = false; } else { useUpdateCursor = true; } } osmInputTable = osmFeatureClass as ITable; } catch { } try { if (osmInputTable == null) { execute_Utilities.DecodeTableView(inputOSMGPValue, out osmInputTable, out osmQueryFilter); } } catch { } if (osmInputTable == null) { string errorMessage = String.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelecto_unableopentable"), inputOSMGPValue.GetAsText()); message.AddError(120053, errorMessage); return; } using (ComReleaser comReleaser = new ComReleaser()) { using (SchemaLockManager lockMgr = new SchemaLockManager(osmInputTable)) { // get an update cursor for all the features to process ICursor rowCursor = null; if (useUpdateCursor) { rowCursor = osmInputTable.Update(osmQueryFilter, false); } else { rowCursor = osmInputTable.Search(osmQueryFilter, false); } comReleaser.ManageLifetime(rowCursor); IRow osmRow = null; Dictionary<string, string> tagKeys = new Dictionary<string, string>(); int progessIndex = 0; #if DEBUG message.AddMessage("useUpdateCursor: " + useUpdateCursor.ToString()); #endif // as long as there are features.... while ((osmRow = rowCursor.NextRow()) != null) { // retrieve the tags of the current feature tag[] storedTags = _osmUtility.retrieveOSMTags(osmRow, osmTagCollectionFieldIndex, ((IDataset)osmInputTable).Workspace); bool rowChanged = false; if (storedTags != null) { foreach (tag tagItem in storedTags) { // Check for matching values so we only change a minimum number of rows if (tagsAttributesIndices.ContainsKey(tagItem.k)) { int fieldIndex = tagsAttributesIndices[tagItem.k]; //...then stored the value in the attribute field // ensure that the content of the tag actually does fit into the field length...otherwise do truncate it string tagValue = tagItem.v; int fieldLength = attributeFieldLength[fieldIndex]; if (tagValue.Length > fieldLength) tagValue = tagValue.Substring(0, fieldLength); osmRow.set_Value(fieldIndex, tagValue); rowChanged = true; } else { #if DEBUG //message.AddWarning(tagItem.k); #endif } } } storedTags = null; try { if (rowChanged) { if (useUpdateCursor) { rowCursor.UpdateRow(osmRow); } else { // update the feature through the cursor osmRow.Store(); } } progessIndex++; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); message.AddWarning(ex.Message); } if (osmRow != null) { Marshal.ReleaseComObject(osmRow); } if (stepProgressor != null) { // update the progress UI stepProgressor.Position = progessIndex; } // check for user cancellation (every 100 rows) if ((progessIndex % 100 == 0) && (TrackCancel.Continue() == false)) { return; } } if (stepProgressor != null) { stepProgressor.Hide(); } } } execute_Utilities.ReleaseInternals(); Marshal.ReleaseComObject(execute_Utilities); } catch (Exception ex) { message.AddError(120054, ex.Message); } }