/// <summary>Removes the OSM Class Extension from the given table</summary> /// <remarks>Obtains an exclusive schema lock if possible otherwise throws an exception</remarks> public static void RemoveOSMClassExtension(this ITable table) { if ((table == null) || !(table.Extension is IOSMClassExtension)) { return; } IClassSchemaEdit3 schemaEdit = table as IClassSchemaEdit3; if (schemaEdit == null) { return; } int osmIDIndex = table.Fields.FindField("OSMID"); using (SchemaLockManager lockMgr = new SchemaLockManager(table)) { IPropertySet extensionPropertSet = null; if (osmIDIndex > -1) { // at release 2.1 we changed the OSMID field type to string, hence only when we find the string we are assuming version 2 if (table.Fields.get_Field(osmIDIndex).Type == esriFieldType.esriFieldTypeString) { extensionPropertSet = new PropertySetClass(); extensionPropertSet.SetProperty("VERSION", _OSM_EXTENSION_VERSION); } } schemaEdit.AlterClassExtensionCLSID(null, extensionPropertSet); schemaEdit.AlterClassExtensionProperties(extensionPropertSet); } schemaEdit = null; }
/// <summary>Apply the OSM Class Extension to the given table</summary> /// <remarks>Obtains an exclusive schema lock if possible otherwise throws an exception</remarks> public static void ApplyOSMClassExtension(this ITable table) { if ((table == null) || (table.Extension is IOSMClassExtension)) return; IClassSchemaEdit3 schemaEdit = table as IClassSchemaEdit3; if (schemaEdit == null) return; int osmIDIndex = table.Fields.FindField("OSMID"); using (SchemaLockManager lockMgr = new SchemaLockManager(table)) { UID osmClassExtensionUID = new UIDClass() { Value = _OSM_CLASS_EXT_GUID }; IPropertySet extensionPropertSet = null; if (osmIDIndex > -1) { // at release 2.1 we changed the OSMID field type to string, hence only when we find the string we are assuming version 2 if (table.Fields.get_Field(osmIDIndex).Type == esriFieldType.esriFieldTypeString) { extensionPropertSet = new PropertySetClass(); extensionPropertSet.SetProperty("VERSION", _OSM_EXTENSION_VERSION); } } schemaEdit.AlterClassExtensionCLSID(osmClassExtensionUID, extensionPropertSet); schemaEdit.AlterClassExtensionProperties(extensionPropertSet); } }
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_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 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); } }
internal List<string> loadOSMWays(string osmFileLocation, ref ITrackCancel TrackCancel, ref IGPMessages message, IGPValue targetGPValue, IFeatureClass osmPointFeatureClass, IFeatureClass osmLineFeatureClass, IFeatureClass osmPolygonFeatureClass, bool conserveMemory, bool fastLoad, int wayCapacity, ref Dictionary<string, simplePointRef> osmNodeDictionary, IFeatureWorkspace featureWorkspace, ISpatialReference downloadSpatialReference, OSMDomains availableDomains, bool checkForExisting) { if (osmLineFeatureClass == null) { throw new ArgumentNullException("osmLineFeatureClass"); } if (osmPolygonFeatureClass == null) { throw new ArgumentNullException("osmPolygonFeatureClass"); } XmlReader osmFileXmlReader = null; XmlSerializer waySerializer = null; List<string> missingWays = null; try { missingWays = new List<string>(); int osmPointIDFieldIndex = osmPointFeatureClass.FindField("OSMID"); int osmWayRefCountFieldIndex = osmPointFeatureClass.FindField("wayRefCount"); int osmLineIDFieldIndex = osmLineFeatureClass.FindField("OSMID"); Dictionary<string, int> osmLineDomainAttributeFieldIndices = new Dictionary<string, int>(); Dictionary<string, int> osmLineDomainAttributeFieldLength = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmLineFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmLineDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); osmLineDomainAttributeFieldLength.Add(domains.name, osmLineFeatureClass.Fields.get_Field(currentFieldIndex).Length); } } 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"); int osmPolygonIDFieldIndex = osmPolygonFeatureClass.FindField("OSMID"); Dictionary<string, int> osmPolygonDomainAttributeFieldIndices = new Dictionary<string, int>(); Dictionary<string, int> osmPolygonDomainAttributeFieldLength = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmPolygonFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmPolygonDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); osmPolygonDomainAttributeFieldLength.Add(domains.name, osmPolygonFeatureClass.Fields.get_Field(currentFieldIndex).Length); } } 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"); ISpatialReferenceFactory spatialRef = new SpatialReferenceEnvironmentClass(); ISpatialReference wgs84 = spatialRef.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); bool shouldProject = !((IClone)wgs84).IsEqual((IClone)downloadSpatialReference); // set up the progress indicator IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = wayCapacity; stepProgressor.Position = 0; stepProgressor.Message = _resourceManager.GetString("GPTools_OSMGPFileReader_loadingWays"); stepProgressor.StepValue = 1; stepProgressor.Show(); } bool lineIndexRebuildRequired = false; bool polygonIndexRebuildRequired = false; int wayCount = 0; object missingValue = System.Reflection.Missing.Value; // enterprise GDB indicator -- supporting load only mode IFeatureClassLoad lineFeatureLoad = null; IFeatureClassLoad polygonFeatureLoad = null; using (SchemaLockManager lineLock = new SchemaLockManager(osmLineFeatureClass as ITable), polygonLock = new SchemaLockManager(osmPolygonFeatureClass as ITable)) { using (ComReleaser comReleaser = new ComReleaser()) { IFeatureCursor insertLineCursor = osmLineFeatureClass.Insert(true); comReleaser.ManageLifetime(insertLineCursor); IFeatureBuffer featureLineBuffer = osmLineFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(featureLineBuffer); IFeatureCursor insertPolygonCursor = osmPolygonFeatureClass.Insert(true); comReleaser.ManageLifetime(insertPolygonCursor); IFeatureBuffer featurePolygonBuffer = osmPolygonFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(featurePolygonBuffer); if (((IWorkspace)featureWorkspace).WorkspaceFactory.WorkspaceType == esriWorkspaceType.esriRemoteDatabaseWorkspace) { lineFeatureLoad = osmLineFeatureClass as IFeatureClassLoad; polygonFeatureLoad = osmPolygonFeatureClass as IFeatureClassLoad; } if (lineFeatureLoad != null) { lineFeatureLoad.LoadOnlyMode = true; } if (polygonFeatureLoad != null) { polygonFeatureLoad.LoadOnlyMode = true; } ISpatialReference nativeLineSpatialReference = ((IGeoDataset)osmLineFeatureClass).SpatialReference; ISpatialReference nativePolygonSpatialReference = ((IGeoDataset)osmPolygonFeatureClass).SpatialReference; IQueryFilter osmIDQueryFilter = new QueryFilterClass(); string sqlPointOSMID = osmPointFeatureClass.SqlIdentifier("OSMID"); IFeatureCursor updatePointCursor = null; osmFileXmlReader = System.Xml.XmlReader.Create(osmFileLocation); waySerializer = new XmlSerializer(typeof(way)); // the point query filter for updates will not changes, so let's do that ahead of time try { osmIDQueryFilter.SubFields = osmPointFeatureClass.ShapeFieldName + "," + osmPointFeatureClass.Fields.get_Field(osmPointIDFieldIndex).Name + "," + osmPointFeatureClass.Fields.get_Field(osmWayRefCountFieldIndex).Name; } catch { } osmFileXmlReader.MoveToContent(); while (osmFileXmlReader.Read()) { if (osmFileXmlReader.IsStartElement()) { if (osmFileXmlReader.Name == "way") { string currentwayString = osmFileXmlReader.ReadOuterXml(); // assuming the way to be a polyline is sort of a safe assumption // and won't cause any topology problem due to orientation and closeness bool wayIsLine = true; bool wayIsComplete = true; way currentWay = null; try { using (StringReader wayReader = new System.IO.StringReader(currentwayString)) { currentWay = waySerializer.Deserialize(wayReader) as way; } // if the deserialization fails then go ahead and read the next xml element if (currentWay == null) { continue; } // and we are expecting at least some nodes on the way itself if (currentWay.nd == null) { continue; } featureLineBuffer = osmLineFeatureClass.CreateFeatureBuffer(); featurePolygonBuffer = osmPolygonFeatureClass.CreateFeatureBuffer(); IPointCollection wayPointCollection = null; wayIsLine = IsThisWayALine(currentWay); if (wayIsLine) { // check if a feature with the same OSMID already exists, because the can only be one if (checkForExisting == true) { if (CheckIfExists(osmLineFeatureClass as ITable, currentWay.id)) { continue; } } IPolyline wayPolyline = new PolylineClass(); comReleaser.ManageLifetime(wayPointCollection); wayPolyline.SpatialReference = downloadSpatialReference; IPointIDAware polylineIDAware = wayPolyline as IPointIDAware; polylineIDAware.PointIDAware = true; wayPointCollection = wayPolyline as IPointCollection; # region generate line geometry if (conserveMemory == false) { for (int ndIndex = 0; ndIndex < currentWay.nd.Length; ndIndex++) { string ndID = currentWay.nd[ndIndex].@ref; if (osmNodeDictionary.ContainsKey(ndID)) { IPoint newPoint = new PointClass(); newPoint.X = osmNodeDictionary[ndID].Longitude; newPoint.Y = osmNodeDictionary[ndID].Latitude; newPoint.SpatialReference = wgs84; if (shouldProject) { newPoint.Project(((IGeoDataset)osmLineFeatureClass).SpatialReference); } IPointIDAware idAware = newPoint as IPointIDAware; idAware.PointIDAware = true; newPoint.ID = osmNodeDictionary[ndID].pointObjectID; wayPointCollection.AddPoint(newPoint, ref missingValue, ref missingValue); osmNodeDictionary[ndID].RefCounter = osmNodeDictionary[ndID].RefCounter + 1; } else { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_undeterminedline_node"), currentWay.id, ndID)); // set the flag that the way is complete due to a missing node wayIsComplete = false; break; } } } else { for (int pointIndex = 0; pointIndex < currentWay.nd.Length; pointIndex++) { wayPointCollection.AddPoint(new PointClass()); } List<string> idRequests = SplitOSMIDRequests(currentWay, 2); // build a list of node ids we can use to determine the point index in the line geometry // as well as a dictionary to determine the position in the list in case of duplicates nodes Dictionary<string, int> nodePositionDictionary = new Dictionary<string, int>(currentWay.nd.Length); List<string> nodeIDs = new List<string>(currentWay.nd.Length); foreach (nd wayNode in currentWay.nd) { nodeIDs.Add(wayNode.@ref); if (nodePositionDictionary.ContainsKey(wayNode.@ref) == false) { nodePositionDictionary.Add(wayNode.@ref, 0); } } try { osmIDQueryFilter.SubFields = osmPointFeatureClass.ShapeFieldName + "," + osmPointFeatureClass.Fields.get_Field(osmPointIDFieldIndex).Name + "," + osmPointFeatureClass.Fields.get_Field(osmWayRefCountFieldIndex).Name; } catch { } foreach (string request in idRequests) { string idCompareString = request; osmIDQueryFilter.WhereClause = sqlPointOSMID + " IN " + request; using (ComReleaser innerComReleaser = new ComReleaser()) { updatePointCursor = osmPointFeatureClass.Update(osmIDQueryFilter, true); innerComReleaser.ManageLifetime(updatePointCursor); IFeature nodeFeature = updatePointCursor.NextFeature(); while (nodeFeature != null) { // determine the index of the point in with respect to the node position string nodeOSMIDString = Convert.ToString(nodeFeature.get_Value(osmPointIDFieldIndex)); // remove the ID from the request string idCompareString = idCompareString.Replace(nodeOSMIDString, String.Empty); int nodePositionIndex = -1; while ((nodePositionIndex = nodeIDs.IndexOf(nodeOSMIDString, nodePositionDictionary[nodeOSMIDString])) != -1) { //// update the new position start search index nodePositionDictionary[nodeOSMIDString] = nodePositionIndex + 1; wayPointCollection.UpdatePoint(nodePositionIndex, (IPoint)nodeFeature.Shape); // increase the reference counter if (osmWayRefCountFieldIndex != -1) { nodeFeature.set_Value(osmWayRefCountFieldIndex, ((int)nodeFeature.get_Value(osmWayRefCountFieldIndex)) + 1); updatePointCursor.UpdateFeature(nodeFeature); } } if (nodeFeature != null) Marshal.ReleaseComObject(nodeFeature); nodeFeature = updatePointCursor.NextFeature(); } idCompareString = CleanReportedNodes(idCompareString); // after removing the commas we should be left with only paranthesis left, meaning a string of length 2 // if we have more then we have found a missing node, resulting in an incomplete way geometry if (idCompareString.Length > 2) { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_undeterminedline_node"), currentWay.id, idCompareString)); wayIsComplete = false; } } } } #endregion if (wayIsComplete == false) { // if the way geometry is incomplete due to a missing node let's continue to the next way element missingWays.Add(currentWay.id); continue; } featureLineBuffer.Shape = wayPolyline; featureLineBuffer.set_Value(osmLineIDFieldIndex, currentWay.id); } else { // check if a feature with the same OSMID already exists, because the can only be one if (checkForExisting == true) { if (CheckIfExists(osmPolygonFeatureClass as ITable, currentWay.id)) { continue; } } IPolygon wayPolygon = new PolygonClass(); comReleaser.ManageLifetime(wayPointCollection); wayPolygon.SpatialReference = downloadSpatialReference; IPointIDAware polygonIDAware = wayPolygon as IPointIDAware; polygonIDAware.PointIDAware = true; wayPointCollection = wayPolygon as IPointCollection; #region generate polygon geometry if (conserveMemory == false) { for (int ndIndex = 0; ndIndex < currentWay.nd.Length; ndIndex++) { string ndID = currentWay.nd[ndIndex].@ref; if (osmNodeDictionary.ContainsKey(ndID)) { IPoint newPoint = new PointClass(); newPoint.X = osmNodeDictionary[ndID].Longitude; newPoint.Y = osmNodeDictionary[ndID].Latitude; newPoint.SpatialReference = wgs84; if (shouldProject) { newPoint.Project(nativePolygonSpatialReference); } IPointIDAware idAware = newPoint as IPointIDAware; idAware.PointIDAware = true; newPoint.ID = osmNodeDictionary[ndID].pointObjectID; wayPointCollection.AddPoint(newPoint, ref missingValue, ref missingValue); } else { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_undeterminedpolygon_node"), currentWay.id, ndID)); wayIsComplete = false; break; } } } else { for (int pointIndex = 0; pointIndex < currentWay.nd.Length; pointIndex++) { wayPointCollection.AddPoint(new PointClass()); } List<string> idRequests = SplitOSMIDRequests(currentWay, 2); // build a list of node ids we can use to determine the point index in the line geometry // as well as a dictionary to determine the position in the list in case of duplicates nodes Dictionary<string, int> nodePositionDictionary = new Dictionary<string, int>(currentWay.nd.Length); List<string> nodeIDs = new List<string>(currentWay.nd.Length); foreach (nd wayNode in currentWay.nd) { nodeIDs.Add(wayNode.@ref); if (nodePositionDictionary.ContainsKey(wayNode.@ref) == false) { nodePositionDictionary.Add(wayNode.@ref, 0); } } try { osmIDQueryFilter.SubFields = osmPointFeatureClass.ShapeFieldName + "," + osmPointFeatureClass.Fields.get_Field(osmPointIDFieldIndex).Name + "," + osmPointFeatureClass.Fields.get_Field(osmWayRefCountFieldIndex).Name; } catch { } foreach (string osmIDRequest in idRequests) { string idCompareString = osmIDRequest; using (ComReleaser innercomReleaser = new ComReleaser()) { osmIDQueryFilter.WhereClause = sqlPointOSMID + " IN " + osmIDRequest; updatePointCursor = osmPointFeatureClass.Update(osmIDQueryFilter, false); innercomReleaser.ManageLifetime(updatePointCursor); IFeature nodeFeature = updatePointCursor.NextFeature(); while (nodeFeature != null) { // determine the index of the point in with respect to the node position string nodeOSMIDString = Convert.ToString(nodeFeature.get_Value(osmPointIDFieldIndex)); idCompareString = idCompareString.Replace(nodeOSMIDString, String.Empty); int nodePositionIndex = nodeIDs.IndexOf(nodeOSMIDString, nodePositionDictionary[nodeOSMIDString]); // update the new position start search index nodePositionDictionary[nodeOSMIDString] = nodePositionIndex + 1; wayPointCollection.UpdatePoint(nodePositionIndex, (IPoint)nodeFeature.Shape); // increase the reference counter if (osmWayRefCountFieldIndex != -1) { nodeFeature.set_Value(osmWayRefCountFieldIndex, ((int)nodeFeature.get_Value(osmWayRefCountFieldIndex)) + 1); updatePointCursor.UpdateFeature(nodeFeature); } if (nodeFeature != null) Marshal.ReleaseComObject(nodeFeature); nodeFeature = updatePointCursor.NextFeature(); } idCompareString = CleanReportedNodes(idCompareString); if (idCompareString.Length > 2) { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_undeterminedpolygon_node"), currentWay.id, idCompareString)); wayIsComplete = false; } } } } #endregion if (wayIsComplete == false) { continue; } // remove the last point as OSM considers them to be coincident wayPointCollection.RemovePoints(wayPointCollection.PointCount - 1, 1); ((IPolygon)wayPointCollection).Close(); featurePolygonBuffer.Shape = (IPolygon)wayPointCollection; featurePolygonBuffer.set_Value(osmPolygonIDFieldIndex, currentWay.id); } if (wayIsLine) { insertTags(osmLineDomainAttributeFieldIndices, osmLineDomainAttributeFieldLength, tagCollectionPolylineFieldIndex, featureLineBuffer, currentWay.tag); } else { insertTags(osmPolygonDomainAttributeFieldIndices, osmPolygonDomainAttributeFieldLength, tagCollectionPolygonFieldIndex, featurePolygonBuffer, currentWay.tag); } // store the administrative attributes // user, uid, version, changeset, timestamp, visible if (fastLoad == false) { if (!String.IsNullOrEmpty(currentWay.user)) { if (wayIsLine) { if (osmUserPolylineFieldIndex != -1) { featureLineBuffer.set_Value(osmUserPolylineFieldIndex, currentWay.user); } } else { if (osmUserPolygonFieldIndex != -1) { featurePolygonBuffer.set_Value(osmUserPolygonFieldIndex, currentWay.user); } } } if (!String.IsNullOrEmpty(currentWay.uid)) { if (wayIsLine) { if (osmUIDPolylineFieldIndex != -1) { featureLineBuffer.set_Value(osmUIDPolylineFieldIndex, Convert.ToInt32(currentWay.uid)); } } else { if (osmUIDPolygonFieldIndex != -1) { featurePolygonBuffer.set_Value(osmUIDPolygonFieldIndex, Convert.ToInt32(currentWay.uid)); } } } if (wayIsLine) { if (osmVisiblePolylineFieldIndex != -1) { featureLineBuffer.set_Value(osmVisiblePolylineFieldIndex, currentWay.visible.ToString()); } } else { if (osmVisiblePolygonFieldIndex != -1) { featurePolygonBuffer.set_Value(osmVisiblePolygonFieldIndex, currentWay.visible.ToString()); } } if (!String.IsNullOrEmpty(currentWay.version)) { if (wayIsLine) { if (osmVersionPolylineFieldIndex != -1) { featureLineBuffer.set_Value(osmVersionPolylineFieldIndex, Convert.ToInt32(currentWay.version)); } } else { if (osmVersionPolygonFieldIndex != -1) { featurePolygonBuffer.set_Value(osmVersionPolygonFieldIndex, Convert.ToInt32(currentWay.version)); } } } if (!String.IsNullOrEmpty(currentWay.changeset)) { if (wayIsLine) { if (osmChangesetPolylineFieldIndex != -1) { featureLineBuffer.set_Value(osmChangesetPolylineFieldIndex, Convert.ToInt32(currentWay.changeset)); } } else { if (osmChangesetPolygonFieldIndex != -1) { featurePolygonBuffer.set_Value(osmChangesetPolygonFieldIndex, Convert.ToInt32(currentWay.changeset)); } } } if (!String.IsNullOrEmpty(currentWay.timestamp)) { try { if (wayIsLine) { if (osmTimeStampPolylineFieldIndex != -1) { featureLineBuffer.set_Value(osmTimeStampPolylineFieldIndex, Convert.ToDateTime(currentWay.timestamp)); } } else { if (osmTimeStampPolygonFieldIndex != -1) { featurePolygonBuffer.set_Value(osmTimeStampPolygonFieldIndex, Convert.ToDateTime(currentWay.timestamp)); } } } catch (Exception ex) { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_invalidTimeFormat"), ex.Message)); } } if (wayIsLine) { if (osmSupportingElementPolylineFieldIndex > -1) { if (currentWay.tag == null) { featureLineBuffer.set_Value(osmSupportingElementPolylineFieldIndex, "yes"); } else { featureLineBuffer.set_Value(osmSupportingElementPolylineFieldIndex, "no"); } } } else { if (osmSupportingElementPolygonFieldIndex > -1) { if (currentWay.tag == null) { featurePolygonBuffer.set_Value(osmSupportingElementPolygonFieldIndex, "yes"); } else { featurePolygonBuffer.set_Value(osmSupportingElementPolygonFieldIndex, "no"); } } } } // fast load try { if (wayIsLine) { insertLineCursor.InsertFeature(featureLineBuffer); lineIndexRebuildRequired = true; } else { insertPolygonCursor.InsertFeature(featurePolygonBuffer); polygonIndexRebuildRequired = true; } wayCount = wayCount + 1; if (stepProgressor != null) { stepProgressor.Position = wayCount; } if ((wayCount % 50000) == 0) { message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_waysloaded"), wayCount)); } } catch (Exception ex) { message.AddWarning(ex.Message); message.AddWarning(currentwayString); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(ex.StackTrace); } finally { if (featureLineBuffer != null) { Marshal.ReleaseComObject(featureLineBuffer); if (featureLineBuffer != null) featureLineBuffer = null; } if (featurePolygonBuffer != null) { Marshal.ReleaseComObject(featurePolygonBuffer); if (featurePolygonBuffer != null) featurePolygonBuffer = null; } currentWay = null; } if (TrackCancel.Continue() == false) { insertPolygonCursor.Flush(); if (polygonFeatureLoad != null) { polygonFeatureLoad.LoadOnlyMode = false; } insertLineCursor.Flush(); if (lineFeatureLoad != null) { lineFeatureLoad.LoadOnlyMode = false; } return missingWays; } } } } osmFileXmlReader.Close(); if (stepProgressor != null) { stepProgressor.Hide(); } message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_waysloaded"), wayCount)); insertPolygonCursor.Flush(); if (polygonFeatureLoad != null) { polygonFeatureLoad.LoadOnlyMode = false; } insertLineCursor.Flush(); if (lineFeatureLoad != null) { lineFeatureLoad.LoadOnlyMode = false; } } } IGeoProcessor2 geoProcessor = new GeoProcessorClass(); IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); bool storedOriginal = geoProcessor.AddOutputsToMap; IVariantArray parameterArrary = null; IGeoProcessorResult2 gpResults2 = null; try { geoProcessor.AddOutputsToMap = false; if (lineIndexRebuildRequired) { IIndexes featureClassIndexes = osmLineFeatureClass.Indexes; int indexPosition = -1; featureClassIndexes.FindIndex("osmID_IDX", out indexPosition); string fcLocation = GetLocationString(targetGPValue, osmLineFeatureClass); if (indexPosition == -1) { message.AddMessage(_resourceManager.GetString("GPTools_buildinglineidx")); // Addd index for osmid column parameterArrary = CreateAddIndexParameterArray(fcLocation, "OSMID", "osmID_IDX", "UNIQUE", ""); gpResults2 = geoProcessor.Execute("AddIndex_management", parameterArrary, TrackCancel) as IGeoProcessorResult2; } if (wayCount > 100) { // in this case we are dealing with a file geodatabase if (lineFeatureLoad == null) { UpdateSpatialGridIndex(TrackCancel, message, geoProcessor, fcLocation); } } } if (polygonIndexRebuildRequired) { IIndexes featureClassIndexes = osmPolygonFeatureClass.Indexes; int indexPosition = -1; featureClassIndexes.FindIndex("osmID_IDX", out indexPosition); string fcLocation = GetLocationString(targetGPValue, osmPolygonFeatureClass); if (indexPosition == -1) { message.AddMessage(_resourceManager.GetString("GPTools_buildingpolygonidx")); IGPValue polygonFeatureClassGPValue = gpUtilities3.MakeGPValueFromObject(osmPolygonFeatureClass); if (polygonFeatureClassGPValue != null) { // Addd index for osmid column parameterArrary = CreateAddIndexParameterArray(fcLocation, "OSMID", "osmID_IDX", "UNIQUE", ""); gpResults2 = geoProcessor.Execute("AddIndex_management", parameterArrary, TrackCancel) as IGeoProcessorResult2; } } if (wayCount > 100) { if (polygonFeatureLoad == null) { UpdateSpatialGridIndex(TrackCancel, message, geoProcessor, fcLocation); } } } } catch (Exception ex) { message.AddWarning(ex.Message); } finally { geoProcessor.AddOutputsToMap = storedOriginal; Marshal.FinalReleaseComObject(gpUtilities3); Marshal.FinalReleaseComObject(geoProcessor); } } catch (Exception ex) { message.AddWarning(ex.Message); } finally { if (waySerializer != null) waySerializer = null; if (osmFileXmlReader != null) osmFileXmlReader = null; System.GC.Collect(); System.GC.WaitForPendingFinalizers(); } return missingWays; }
internal List<string> loadOSMRelations(string osmFileLocation, ref ITrackCancel TrackCancel, ref IGPMessages message, IGPValue targetGPValue, IFeatureClass osmPointFeatureClass, IFeatureClass osmLineFeatureClass, IFeatureClass osmPolygonFeatureClass, int relationCapacity, ITable relationTable, OSMDomains availableDomains, bool fastLoad, bool checkForExisting) { List<string> missingRelations = null; XmlReader osmFileXmlReader = null; XmlSerializer relationSerializer = null; try { missingRelations = new List<string>(); if (osmLineFeatureClass == null) { throw new ArgumentNullException("osmLineFeatureClass"); } if (osmPolygonFeatureClass == null) { throw new ArgumentNullException("osmPolygonFeatureClass"); } if (relationTable == null) { throw new ArgumentNullException("relationTable"); } 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"); int osmLineIDFieldIndex = osmLineFeatureClass.FindField("OSMID"); Dictionary<string, int> osmLineDomainAttributeFieldIndices = new Dictionary<string, int>(); Dictionary<string, int> osmLineDomainAttributeFieldLength = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmLineFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmLineDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); osmLineDomainAttributeFieldLength.Add(domains.name, osmLineFeatureClass.Fields.get_Field(currentFieldIndex).Length); } } 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"); int osmPolygonIDFieldIndex = osmPolygonFeatureClass.FindField("OSMID"); Dictionary<string, int> osmPolygonDomainAttributeFieldIndices = new Dictionary<string, int>(); Dictionary<string, int> osmPolygonDomainAttributeFieldLength = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmPolygonFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmPolygonDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); osmPolygonDomainAttributeFieldLength.Add(domains.name, osmPolygonFeatureClass.Fields.get_Field(currentFieldIndex).Length); } } 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"); 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"); // list for reference count and relation list for lines/polygons/relations // set up the progress indicator IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = relationCapacity; stepProgressor.Position = 0; stepProgressor.Message = _resourceManager.GetString("GPTools_OSMGPFileReader_loadingRelations"); stepProgressor.StepValue = 1; stepProgressor.Show(); } bool relationIndexRebuildRequired = false; if (relationTable != null) { osmFileXmlReader = System.Xml.XmlReader.Create(osmFileLocation); relationSerializer = new XmlSerializer(typeof(relation)); using (ComReleaser comReleaser = new ComReleaser()) { using (SchemaLockManager linelock = new SchemaLockManager(osmLineFeatureClass as ITable), polygonLock = new SchemaLockManager(osmPolygonFeatureClass as ITable), relationLock = new SchemaLockManager(relationTable)) { ICursor rowCursor = relationTable.Insert(true); comReleaser.ManageLifetime(rowCursor); IRowBuffer rowBuffer = relationTable.CreateRowBuffer(); comReleaser.ManageLifetime(rowBuffer); IFeatureCursor lineFeatureInsertCursor = osmLineFeatureClass.Insert(true); comReleaser.ManageLifetime(lineFeatureInsertCursor); IFeatureBuffer lineFeatureBuffer = osmLineFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(lineFeatureBuffer); IFeatureCursor polygonFeatureInsertCursor = osmPolygonFeatureClass.Insert(true); comReleaser.ManageLifetime(polygonFeatureInsertCursor); IFeatureBuffer polygonFeatureBuffer = osmPolygonFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(polygonFeatureBuffer); int relationCount = 1; int relationDebugCount = 1; string lineSQLIdentifier = osmLineFeatureClass.SqlIdentifier("OSMID"); string polygonSQLIdentifier = osmPolygonFeatureClass.SqlIdentifier("OSMID"); message.AddMessage(_resourceManager.GetString("GPTools_OSMGPFileReader_resolvegeometries")); osmFileXmlReader.MoveToContent(); while (osmFileXmlReader.Read()) { if (osmFileXmlReader.IsStartElement()) { if (osmFileXmlReader.Name == "relation") { relation currentRelation = null; try { // read the full relation node string currentrelationString = osmFileXmlReader.ReadOuterXml(); using (StringReader relationReader = new System.IO.StringReader(currentrelationString)) { // de-serialize the xml into to the class instance currentRelation = relationSerializer.Deserialize(relationReader) as relation; } if (currentRelation == null) continue; relationDebugCount = relationDebugCount + 1; esriGeometryType detectedGeometryType = determineRelationGeometryType(osmLineFeatureClass, osmPolygonFeatureClass, relationTable, currentRelation); if (checkForExisting) { switch (detectedGeometryType) { case esriGeometryType.esriGeometryAny: if (CheckIfExists(relationTable, currentRelation.id)) continue; break; case esriGeometryType.esriGeometryBag: if (CheckIfExists(relationTable, currentRelation.id)) continue; break; case esriGeometryType.esriGeometryBezier3Curve: break; case esriGeometryType.esriGeometryCircularArc: break; case esriGeometryType.esriGeometryEllipticArc: break; case esriGeometryType.esriGeometryEnvelope: break; case esriGeometryType.esriGeometryLine: if (CheckIfExists(osmLineFeatureClass as ITable, currentRelation.id)) continue; break; case esriGeometryType.esriGeometryMultiPatch: break; case esriGeometryType.esriGeometryMultipoint: break; case esriGeometryType.esriGeometryNull: if (CheckIfExists(relationTable, currentRelation.id)) continue; break; case esriGeometryType.esriGeometryPath: break; case esriGeometryType.esriGeometryPoint: break; case esriGeometryType.esriGeometryPolygon: if (CheckIfExists(osmPolygonFeatureClass as ITable, currentRelation.id)) continue; break; case esriGeometryType.esriGeometryPolyline: if (CheckIfExists(osmLineFeatureClass as ITable, currentRelation.id)) continue; break; case esriGeometryType.esriGeometryRay: break; case esriGeometryType.esriGeometryRing: break; case esriGeometryType.esriGeometrySphere: break; case esriGeometryType.esriGeometryTriangleFan: break; case esriGeometryType.esriGeometryTriangleStrip: break; case esriGeometryType.esriGeometryTriangles: break; default: break; } } List<tag> relationTagList = new List<tag>(); List<member> relationMemberList = new List<member>(); Dictionary<string, string> wayList = new Dictionary<string, string>(); // sanity check that the overall relation notation contains at least something if (currentRelation.Items == null) continue; List<OSMNodeFeature> osmPointList = null; List<OSMLineFeature> osmLineList = null; List<OSMPolygonFeature> osmPolygonList = null; List<OSMRelation> osmRelationList = null; rowBuffer = relationTable.CreateRowBuffer(); comReleaser.ManageLifetime(rowBuffer); lineFeatureBuffer = osmLineFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(lineFeatureBuffer); polygonFeatureBuffer = osmPolygonFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(polygonFeatureBuffer); foreach (var item in currentRelation.Items) { if (item is member) { member memberItem = item as member; relationMemberList.Add(memberItem); switch (memberItem.type) { case memberType.way: if (!wayList.ContainsKey(memberItem.@ref)) wayList.Add(memberItem.@ref, memberItem.role); if (IsThisWayALine(memberItem.@ref, osmLineFeatureClass, lineSQLIdentifier, osmPolygonFeatureClass, polygonSQLIdentifier)) { if (osmLineList == null) { osmLineList = new List<OSMLineFeature>(); } OSMLineFeature lineFeature = new OSMLineFeature(); lineFeature.relationList = new List<string>(); lineFeature.lineID = memberItem.@ref; if (detectedGeometryType == esriGeometryType.esriGeometryPolygon) { lineFeature.relationList.Add(currentRelation.id + "_ply"); } else if (detectedGeometryType == esriGeometryType.esriGeometryPolyline) { lineFeature.relationList.Add(currentRelation.id + "_ln"); } else { lineFeature.relationList.Add(currentRelation.id + "_rel"); } osmLineList.Add(lineFeature); } else { if (osmPolygonList == null) { osmPolygonList = new List<OSMPolygonFeature>(); } OSMPolygonFeature polygonFeature = new OSMPolygonFeature(); polygonFeature.relationList = new List<string>(); polygonFeature.polygonID = memberItem.@ref; if (detectedGeometryType == esriGeometryType.esriGeometryPolygon) { polygonFeature.relationList.Add(currentRelation.id + "_ply"); } else if (detectedGeometryType == esriGeometryType.esriGeometryPolyline) { polygonFeature.relationList.Add(currentRelation.id + "_ln"); } else { polygonFeature.relationList.Add(currentRelation.id + "_rel"); } osmPolygonList.Add(polygonFeature); } break; case memberType.node: if (osmPointList == null) { osmPointList = new List<OSMNodeFeature>(); } OSMNodeFeature nodeFeature = new OSMNodeFeature(); nodeFeature.relationList = new List<string>(); nodeFeature.nodeID = memberItem.@ref; nodeFeature.relationList.Add(currentRelation.id + "_rel"); osmPointList.Add(nodeFeature); break; case memberType.relation: if (osmRelationList == null) { osmRelationList = new List<OSMRelation>(); } OSMRelation relation = new OSMRelation(); relation.relationList = new List<string>(); relation.relationID = memberItem.@ref; relation.relationList.Add(currentRelation.id + "_rel"); break; default: break; } } else if (item is tag) { relationTagList.Add((tag)item); } } // if there is a defined geometry type use it to generate a multipart geometry if (detectedGeometryType == esriGeometryType.esriGeometryPolygon) { #region create multipart polygon geometry //IFeature mpFeature = osmPolygonFeatureClass.CreateFeature(); IPolygon relationMPPolygon = new PolygonClass(); relationMPPolygon.SpatialReference = ((IGeoDataset)osmPolygonFeatureClass).SpatialReference; ISegmentCollection relationPolygonGeometryCollection = relationMPPolygon as ISegmentCollection; IQueryFilter osmIDQueryFilter = new QueryFilterClass(); string sqlPolyOSMID = osmPolygonFeatureClass.SqlIdentifier("OSMID"); object missing = Type.Missing; bool relationComplete = true; // loop through the list of referenced ways that are listed in a relation // for each of the items we need to make a decision if they have merit to qualify as stand-alone features // due to the presence of meaningful attributes (tags) foreach (KeyValuePair<string, string> wayKey in wayList) { if (relationComplete == false) break; if (TrackCancel.Continue() == false) { return missingRelations; } osmIDQueryFilter.WhereClause = sqlPolyOSMID + " = '" + wayKey.Key + "'"; System.Diagnostics.Debug.WriteLine("Relation (Polygon) #: " + relationDebugCount + " :___: " + currentRelation.id + " :___: " + wayKey.Key); using (ComReleaser relationComReleaser = new ComReleaser()) { IFeatureCursor featureCursor = osmPolygonFeatureClass.Search(osmIDQueryFilter, false); relationComReleaser.ManageLifetime(featureCursor); IFeature partFeature = featureCursor.NextFeature(); // set the appropriate field attribute to become invisible as a standalone features if (partFeature != null) { IGeometryCollection ringCollection = partFeature.Shape as IGeometryCollection; // test for available content in the geometry collection if (ringCollection.GeometryCount > 0) { // test if we dealing with a valid geometry if (ringCollection.get_Geometry(0).IsEmpty == false) { // add it to the new geometry and mark the added geometry as a supporting element relationPolygonGeometryCollection.AddSegmentCollection((ISegmentCollection)ringCollection.get_Geometry(0)); if (osmSupportingElementPolygonFieldIndex > -1) { // if the member of a relation has the role of "inner" and it has tags, then let's keep it // as a standalone feature as well // the geometry is then a hole in the relation but due to the tags it also has merits to be // considered a stand-alone feature if (wayKey.Value.ToLower().Equals("inner")) { if (!_osmUtility.DoesHaveKeys(partFeature, tagCollectionPolygonFieldIndex, null)) { partFeature.set_Value(osmSupportingElementPolygonFieldIndex, "yes"); } } else { // relation member without an explicit role or the role of "outer" are turned into // supporting features if they don't have relevant attribute if (!_osmUtility.DoesHaveKeys(partFeature, tagCollectionPolylineFieldIndex, null)) { partFeature.set_Value(osmSupportingElementPolygonFieldIndex, "yes"); } } } partFeature.Store(); } } } else { // it still can be a line geometry that will be pieced together into a polygon IFeatureCursor lineFeatureCursor = osmLineFeatureClass.Search(osmIDQueryFilter, false); relationComReleaser.ManageLifetime(lineFeatureCursor); partFeature = lineFeatureCursor.NextFeature(); if (partFeature != null) { IGeometryCollection ringCollection = partFeature.Shape as IGeometryCollection; // test for available content in the geometry collection if (ringCollection.GeometryCount > 0) { // test if we dealing with a valid geometry if (ringCollection.get_Geometry(0).IsEmpty == false) { // add it to the new geometry and mark the added geometry as a supporting element relationPolygonGeometryCollection.AddSegmentCollection((ISegmentCollection)ringCollection.get_Geometry(0)); if (osmSupportingElementPolylineFieldIndex > -1) { // if the member of a relation has the role of "inner" and it has tags, then let's keep it // as a standalone feature as well // the geometry is then a hole in the relation but due to the tags it also has merits to be // considered a stand-alone feature if (wayKey.Value.ToLower().Equals("inner")) { if (!_osmUtility.DoesHaveKeys(partFeature, tagCollectionPolylineFieldIndex, null)) { partFeature.set_Value(osmSupportingElementPolylineFieldIndex, "yes"); } } else { // relation member without an explicit role or the role of "outer" are turned into // supporting features if they don't have relevant attribute if (!_osmUtility.DoesHaveKeys(partFeature, tagCollectionPolylineFieldIndex, null)) { partFeature.set_Value(osmSupportingElementPolylineFieldIndex, "yes"); } } } partFeature.Store(); } } } else { relationComplete = false; continue; } } } } // mark the relation as incomplete if (relationComplete == false) { missingRelations.Add(currentRelation.id); continue; } // transform the added collections for geometries into a topological correct geometry representation ((IPolygon4)relationMPPolygon).SimplifyEx(true, false, true); polygonFeatureBuffer.Shape = relationMPPolygon; if (_osmUtility.DoesHaveKeys(currentRelation)) { } else { relationTagList = MergeTagsFromOuterPolygonToRelation(currentRelation, osmPolygonFeatureClass); } insertTags(osmPolygonDomainAttributeFieldIndices, osmPolygonDomainAttributeFieldLength, tagCollectionPolygonFieldIndex, polygonFeatureBuffer, relationTagList.ToArray()); if (fastLoad == false) { if (osmMembersPolygonFieldIndex > -1) { _osmUtility.insertMembers(osmMembersPolygonFieldIndex, (IFeature)polygonFeatureBuffer, relationMemberList.ToArray()); } // store the administrative attributes // user, uid, version, changeset, timestamp, visible if (osmUserPolygonFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.user)) { polygonFeatureBuffer.set_Value(osmUserPolygonFieldIndex, currentRelation.user); } } if (osmUIDPolygonFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.uid)) { polygonFeatureBuffer.set_Value(osmUIDPolygonFieldIndex, Convert.ToInt32(currentRelation.uid)); } } if (osmVisiblePolygonFieldIndex != -1) { if (String.IsNullOrEmpty(currentRelation.visible) == false) { polygonFeatureBuffer.set_Value(osmVisiblePolygonFieldIndex, currentRelation.visible.ToString()); } else { polygonFeatureBuffer.set_Value(osmVisiblePolygonFieldIndex, "unknown"); } } if (osmVersionPolygonFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.version)) { polygonFeatureBuffer.set_Value(osmVersionPolygonFieldIndex, Convert.ToInt32(currentRelation.version)); } } if (osmChangesetPolygonFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.changeset)) { polygonFeatureBuffer.set_Value(osmChangesetPolygonFieldIndex, Convert.ToInt32(currentRelation.changeset)); } } if (osmTimeStampPolygonFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.timestamp)) { polygonFeatureBuffer.set_Value(osmTimeStampPolygonFieldIndex, Convert.ToDateTime(currentRelation.timestamp)); } } if (osmPolygonIDFieldIndex != -1) { polygonFeatureBuffer.set_Value(osmPolygonIDFieldIndex, currentRelation.id); } if (osmSupportingElementPolygonFieldIndex > -1) { polygonFeatureBuffer.set_Value(osmSupportingElementPolygonFieldIndex, "no"); } } try { //mpFeature.Store(); polygonFeatureInsertCursor.InsertFeature(polygonFeatureBuffer); } catch (Exception ex) { message.AddWarning(ex.Message); } #endregion } else if (detectedGeometryType == esriGeometryType.esriGeometryPolyline) { #region create multipart polyline geometry //IFeature mpFeature = osmLineFeatureClass.CreateFeature(); IPolyline relationMPPolyline = new PolylineClass(); relationMPPolyline.SpatialReference = ((IGeoDataset)osmLineFeatureClass).SpatialReference; IGeometryCollection relationPolylineGeometryCollection = relationMPPolyline as IGeometryCollection; IQueryFilter osmIDQueryFilter = new QueryFilterClass(); object missing = Type.Missing; // loop through the foreach (KeyValuePair<string, string> wayKey in wayList) { if (TrackCancel.Continue() == false) { return missingRelations; } osmIDQueryFilter.WhereClause = osmLineFeatureClass.WhereClauseByExtensionVersion(wayKey.Key, "OSMID", 2); System.Diagnostics.Debug.WriteLine("Relation (Polyline) #: " + relationDebugCount + " :___: " + currentRelation.id + " :___: " + wayKey); using (ComReleaser relationComReleaser = new ComReleaser()) { IFeatureCursor featureCursor = osmLineFeatureClass.Search(osmIDQueryFilter, false); relationComReleaser.ManageLifetime(featureCursor); IFeature partFeature = featureCursor.NextFeature(); // set the appropriate field attribute to become invisible as a standalone features if (partFeature != null) { if (partFeature.Shape.IsEmpty == false) { IGeometryCollection pathCollection = partFeature.Shape as IGeometryCollection; relationPolylineGeometryCollection.AddGeometry(pathCollection.get_Geometry(0), ref missing, ref missing); if (osmSupportingElementPolylineFieldIndex > -1) { if (!_osmUtility.DoesHaveKeys(partFeature, tagCollectionPolylineFieldIndex, null)) { partFeature.set_Value(osmSupportingElementPolylineFieldIndex, "yes"); } } partFeature.Store(); } } } } lineFeatureBuffer.Shape = relationMPPolyline; insertTags(osmLineDomainAttributeFieldIndices, osmLineDomainAttributeFieldLength, tagCollectionPolylineFieldIndex, lineFeatureBuffer, relationTagList.ToArray()); if (fastLoad == false) { if (osmMembersPolylineFieldIndex > -1) { _osmUtility.insertMembers(osmMembersPolylineFieldIndex, (IFeature)lineFeatureBuffer, relationMemberList.ToArray()); } // store the administrative attributes // user, uid, version, changeset, timestamp, visible if (osmUserPolylineFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.user)) { lineFeatureBuffer.set_Value(osmUserPolylineFieldIndex, currentRelation.user); } } if (osmUIDPolylineFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.uid)) { lineFeatureBuffer.set_Value(osmUIDPolylineFieldIndex, Convert.ToInt32(currentRelation.uid)); } } if (osmVisiblePolylineFieldIndex != -1) { if (String.IsNullOrEmpty(currentRelation.visible) == false) { lineFeatureBuffer.set_Value(osmVisiblePolylineFieldIndex, currentRelation.visible.ToString()); } else { lineFeatureBuffer.set_Value(osmVisiblePolylineFieldIndex, "unknown"); } } if (osmVersionPolylineFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.version)) { lineFeatureBuffer.set_Value(osmVersionPolylineFieldIndex, Convert.ToInt32(currentRelation.version)); } } if (osmChangesetPolylineFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.changeset)) { lineFeatureBuffer.set_Value(osmChangesetPolylineFieldIndex, Convert.ToInt32(currentRelation.changeset)); } } if (osmTimeStampPolylineFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.timestamp)) { lineFeatureBuffer.set_Value(osmTimeStampPolylineFieldIndex, Convert.ToDateTime(currentRelation.timestamp)); } } if (osmLineIDFieldIndex != -1) { lineFeatureBuffer.set_Value(osmLineIDFieldIndex, currentRelation.id); } if (osmSupportingElementPolylineFieldIndex > -1) { lineFeatureBuffer.set_Value(osmSupportingElementPolylineFieldIndex, "no"); } } try { lineFeatureInsertCursor.InsertFeature(lineFeatureBuffer); } catch (Exception ex) { message.AddWarning(ex.Message); } #endregion } else if (detectedGeometryType == esriGeometryType.esriGeometryPoint) { System.Diagnostics.Debug.WriteLine("Relation #: " + relationDebugCount + " :____: POINT!!!"); if (TrackCancel.Continue() == false) { return missingRelations; } } else // otherwise it is relation that needs to be dealt with separately { if (TrackCancel.Continue() == false) { return missingRelations; } System.Diagnostics.Debug.WriteLine("Relation #: " + relationDebugCount + " :____: Kept as relation"); if (tagCollectionRelationFieldIndex != -1) { _osmUtility.insertOSMTags(tagCollectionRelationFieldIndex, rowBuffer, relationTagList.ToArray()); } if (fastLoad == false) { if (osmMembersRelationFieldIndex != -1) { _osmUtility.insertMembers(osmMembersRelationFieldIndex, rowBuffer, relationMemberList.ToArray()); } // store the administrative attributes // user, uid, version, changeset, timestamp, visible if (osmUserRelationFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.user)) { rowBuffer.set_Value(osmUserRelationFieldIndex, currentRelation.user); } } if (osmUIDRelationFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.uid)) { rowBuffer.set_Value(osmUIDRelationFieldIndex, Convert.ToInt64(currentRelation.uid)); } } if (osmVisibleRelationFieldIndex != -1) { if (currentRelation.visible != null) { rowBuffer.set_Value(osmVisibleRelationFieldIndex, currentRelation.visible.ToString()); } } if (osmVersionRelationFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.version)) { rowBuffer.set_Value(osmVersionRelationFieldIndex, Convert.ToInt32(currentRelation.version)); } } if (osmChangesetRelationFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.changeset)) { rowBuffer.set_Value(osmChangesetRelationFieldIndex, Convert.ToInt32(currentRelation.changeset)); } } if (osmTimeStampRelationFieldIndex != -1) { if (!String.IsNullOrEmpty(currentRelation.timestamp)) { try { rowBuffer.set_Value(osmTimeStampRelationFieldIndex, Convert.ToDateTime(currentRelation.timestamp)); } catch (Exception ex) { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_invalidTimeFormat"), ex.Message)); } } } if (osmRelationIDFieldIndex != -1) { rowBuffer.set_Value(osmRelationIDFieldIndex, currentRelation.id); } } try { rowCursor.InsertRow(rowBuffer); relationCount = relationCount + 1; relationIndexRebuildRequired = true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } // check for user interruption if (TrackCancel.Continue() == false) { return missingRelations; } } // update the isMemberOf fields of the attached features if (osmPointList != null) { foreach (OSMNodeFeature nodeFeature in osmPointList) { updateIsMemberOf(osmLineFeatureClass, osmMemberOfPolylineFieldIndex, nodeFeature.nodeID, nodeFeature.relationList); } } if (osmLineList != null) { foreach (OSMLineFeature lineFeature in osmLineList) { updateIsMemberOf(osmLineFeatureClass, osmMemberOfPolylineFieldIndex, lineFeature.lineID, lineFeature.relationList); } } if (osmPolygonList != null) { foreach (OSMPolygonFeature polygonFeature in osmPolygonList) { updateIsMemberOf(osmLineFeatureClass, osmMemberOfPolylineFieldIndex, polygonFeature.polygonID, polygonFeature.relationList); } } if (stepProgressor != null) { stepProgressor.Position = relationCount; } if ((relationCount % 50000) == 0) { message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_relationsloaded"), relationCount)); } } catch (Exception ex) { message.AddWarning(ex.Message); } finally { if (rowBuffer != null) { Marshal.ReleaseComObject(rowBuffer); if (rowBuffer != null) rowBuffer = null; } if (lineFeatureBuffer != null) { Marshal.ReleaseComObject(lineFeatureBuffer); if (lineFeatureBuffer != null) lineFeatureBuffer = null; } if (polygonFeatureBuffer != null) { Marshal.ReleaseComObject(polygonFeatureBuffer); if (polygonFeatureBuffer != null) polygonFeatureBuffer = null; } currentRelation = null; } } } } // close the OSM file osmFileXmlReader.Close(); // flush any remaining entities from the cursor rowCursor.Flush(); polygonFeatureInsertCursor.Flush(); lineFeatureInsertCursor.Flush(); // force a garbage collection System.GC.Collect(); // let the user know that we are done dealing with the relations message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_relationsloaded"), relationCount)); } } if (stepProgressor != null) { stepProgressor.Hide(); } // Addd index for osmid column as well IGeoProcessor2 geoProcessor = new GeoProcessorClass(); bool storedOriginalLocal = geoProcessor.AddOutputsToMap; IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); try { geoProcessor.AddOutputsToMap = false; if (relationIndexRebuildRequired) { IIndexes tableIndexes = relationTable.Indexes; int indexPosition = -1; tableIndexes.FindIndex("osmID_IDX", out indexPosition); if (indexPosition == -1) { IGPValue relationTableGPValue = gpUtilities3.MakeGPValueFromObject(relationTable); string sddd = targetGPValue.GetAsText(); string tableLocation = GetLocationString(targetGPValue, relationTable); IVariantArray parameterArrary = CreateAddIndexParameterArray(tableLocation, "OSMID", "osmID_IDX", "UNIQUE", ""); IGeoProcessorResult2 gpResults2 = geoProcessor.Execute("AddIndex_management", parameterArrary, TrackCancel) as IGeoProcessorResult2; } } } catch (Exception ex) { message.AddWarning(ex.Message); } finally { geoProcessor.AddOutputsToMap = storedOriginalLocal; Marshal.FinalReleaseComObject(gpUtilities3); Marshal.FinalReleaseComObject(geoProcessor); } } } catch (Exception ex) { } finally { if (relationSerializer != null) relationSerializer = null; if (osmFileXmlReader != null) osmFileXmlReader = null; System.GC.Collect(); System.GC.WaitForPendingFinalizers(); } return missingRelations; }
internal void loadOSMNodes(string osmFileLocation, ref ITrackCancel TrackCancel, ref IGPMessages message,IGPValue targetGPValue, IFeatureClass osmPointFeatureClass, bool conserveMemory, bool fastLoad, int nodeCapacity, ref Dictionary<string, simplePointRef> osmNodeDictionary, IFeatureWorkspace featureWorkspace, ISpatialReference downloadSpatialReference, OSMDomains availableDomains, bool checkForExisting) { XmlReader osmFileXmlReader = null; XmlSerializer nodeSerializer = null; try { osmFileXmlReader = System.Xml.XmlReader.Create(osmFileLocation); nodeSerializer = new XmlSerializer(typeof(node)); ISpatialReferenceFactory spatialRef = new SpatialReferenceEnvironmentClass(); ISpatialReference wgs84 = spatialRef.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); bool shouldProject = !((IClone)wgs84).IsEqual((IClone)downloadSpatialReference); int osmPointIDFieldIndex = osmPointFeatureClass.FindField("OSMID"); Dictionary<string, int> osmPointDomainAttributeFieldIndices = new Dictionary<string, int>(); Dictionary<string, int> osmPointDomainAttributeFieldLength = new Dictionary<string, int>(); foreach (var domains in availableDomains.domain) { int currentFieldIndex = osmPointFeatureClass.FindField(domains.name); if (currentFieldIndex != -1) { osmPointDomainAttributeFieldIndices.Add(domains.name, currentFieldIndex); osmPointDomainAttributeFieldLength.Add(domains.name, osmPointFeatureClass.Fields.get_Field(currentFieldIndex).Length); } } 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"); // set up the progress indicator IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = nodeCapacity; stepProgressor.StepValue = (1); stepProgressor.Message = _resourceManager.GetString("GPTools_OSMGPFileReader_loadingNodes"); stepProgressor.Position = 0; stepProgressor.Show(); } // flag to determine if a computation of indices is required bool indexBuildRequired = false; if (nodeCapacity > 0) indexBuildRequired = true; int pointCount = 0; // let's insert all the points first if (osmPointFeatureClass != null) { IFeatureBuffer pointFeature = null; IFeatureClassLoad pointFeatureLoad = null; using (ComReleaser comReleaser = new ComReleaser()) { using (SchemaLockManager schemaLockManager = new SchemaLockManager(osmPointFeatureClass as ITable)) { if (((IWorkspace)featureWorkspace).WorkspaceFactory.WorkspaceType == esriWorkspaceType.esriRemoteDatabaseWorkspace) { pointFeatureLoad = osmPointFeatureClass as IFeatureClassLoad; } IFeatureCursor pointInsertCursor = osmPointFeatureClass.Insert(true); comReleaser.ManageLifetime(pointInsertCursor); pointFeature = osmPointFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(pointFeature); if (pointFeatureLoad != null) { pointFeatureLoad.LoadOnlyMode = true; } osmFileXmlReader.MoveToContent(); while (osmFileXmlReader.Read()) { if (osmFileXmlReader.IsStartElement()) { if (osmFileXmlReader.Name == "node") { string currentNodeString = osmFileXmlReader.ReadOuterXml(); // turn the xml node representation into a node class representation ESRI.ArcGIS.OSM.OSMClassExtension.node currentNode = null; using (StringReader nodeReader = new System.IO.StringReader(currentNodeString)) { currentNode = nodeSerializer.Deserialize(nodeReader) as ESRI.ArcGIS.OSM.OSMClassExtension.node; } // check if a feature with the same OSMID already exists, because the can only be one if (checkForExisting == true) { if (CheckIfExists(osmPointFeatureClass as ITable, currentNode.id)) { continue; } } try { //if (pointFeature == null) //{ pointFeature = osmPointFeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(pointFeature); //} IPoint pointGeometry = new PointClass(); comReleaser.ManageLifetime(pointGeometry); pointGeometry.X = Convert.ToDouble(currentNode.lon, new CultureInfo("en-US")); pointGeometry.Y = Convert.ToDouble(currentNode.lat, new CultureInfo("en-US")); pointGeometry.SpatialReference = wgs84; if (shouldProject) { pointGeometry.Project(downloadSpatialReference); } pointFeature.Shape = pointGeometry; pointFeature.set_Value(osmPointIDFieldIndex, currentNode.id); string isSupportingNode = ""; if (_osmUtility.DoesHaveKeys(currentNode)) { // if case it has tags I assume that the node presents an entity of it own, // hence it is not a supporting node in the context of supporting a way or relation isSupportingNode = "no"; if (conserveMemory == false) { osmNodeDictionary[currentNode.id] = new simplePointRef(Convert.ToSingle(currentNode.lon, new CultureInfo("en-US")), Convert.ToSingle(currentNode.lat, new CultureInfo("en-US")), 0, 0); } } else { // node has no tags -- at this point I assume that the absence of tags indicates that it is a supporting node // for a way or a relation isSupportingNode = "yes"; if (conserveMemory == false) { osmNodeDictionary[currentNode.id] = new simplePointRef(Convert.ToSingle(currentNode.lon, new CultureInfo("en-US")), Convert.ToSingle(currentNode.lat, new CultureInfo("en-US")), 0, 0); } } insertTags(osmPointDomainAttributeFieldIndices, osmPointDomainAttributeFieldLength, tagCollectionPointFieldIndex, pointFeature, currentNode.tag); if (fastLoad == false) { if (osmSupportingElementPointFieldIndex > -1) { pointFeature.set_Value(osmSupportingElementPointFieldIndex, isSupportingNode); } if (osmWayRefCountFieldIndex > -1) { pointFeature.set_Value(osmWayRefCountFieldIndex, 0); } // store the administrative attributes // user, uid, version, changeset, timestamp, visible if (osmUserPointFieldIndex > -1) { if (!String.IsNullOrEmpty(currentNode.user)) { pointFeature.set_Value(osmUserPointFieldIndex, currentNode.user); } } if (osmUIDPointFieldIndex > -1) { if (!String.IsNullOrEmpty(currentNode.uid)) { pointFeature.set_Value(osmUIDPointFieldIndex, Convert.ToInt32(currentNode.uid)); } } if (osmVisiblePointFieldIndex > -1) { pointFeature.set_Value(osmVisiblePointFieldIndex, currentNode.visible.ToString()); } if (osmVersionPointFieldIndex > -1) { if (!String.IsNullOrEmpty(currentNode.version)) { pointFeature.set_Value(osmVersionPointFieldIndex, Convert.ToInt32(currentNode.version)); } } if (osmChangesetPointFieldIndex > -1) { if (!String.IsNullOrEmpty(currentNode.changeset)) { pointFeature.set_Value(osmChangesetPointFieldIndex, Convert.ToInt32(currentNode.changeset)); } } if (osmTimeStampPointFieldIndex > -1) { if (!String.IsNullOrEmpty(currentNode.timestamp)) { try { pointFeature.set_Value(osmTimeStampPointFieldIndex, Convert.ToDateTime(currentNode.timestamp)); } catch (Exception ex) { message.AddWarning(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_invalidTimeFormat"), ex.Message)); } } } } try { pointInsertCursor.InsertFeature(pointFeature); pointCount = pointCount + 1; if (stepProgressor != null) { stepProgressor.Position = pointCount; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); message.AddWarning(ex.Message); } if (TrackCancel.Continue() == false) { return; } if ((pointCount % 50000) == 0) { message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_pointsloaded"), pointCount)); pointInsertCursor.Flush(); System.GC.Collect(); } if (pointGeometry != null) Marshal.ReleaseComObject(pointGeometry); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); message.AddWarning(ex.Message); } finally { if (pointFeature != null) { Marshal.ReleaseComObject(pointFeature); if (pointFeature != null) pointFeature = null; } } currentNode = null; } } } if (stepProgressor != null) { stepProgressor.Hide(); } pointInsertCursor.Flush(); osmFileXmlReader.Close(); message.AddMessage(String.Format(_resourceManager.GetString("GPTools_OSMGPFileReader_pointsloaded"), pointCount)); message.AddMessage(_resourceManager.GetString("GPTools_buildingpointidx")); if (pointFeatureLoad != null) { pointFeatureLoad.LoadOnlyMode = false; } } } if (TrackCancel.Continue() == false) { return; } using (ComReleaser comReleaser = new ComReleaser()) { IFeatureCursor updatePoints = osmPointFeatureClass.Update(null, false); comReleaser.ManageLifetime(updatePoints); IFeature feature2Update = updatePoints.NextFeature(); while (feature2Update != null) { IPoint pointGeometry = feature2Update.Shape as IPoint; pointGeometry.ID = feature2Update.OID; feature2Update.Shape = pointGeometry; if (conserveMemory == false) { string osmid = Convert.ToString(feature2Update.get_Value(osmPointIDFieldIndex)); if (osmNodeDictionary.ContainsKey(osmid)) osmNodeDictionary[osmid].pointObjectID = feature2Update.OID; } updatePoints.UpdateFeature(feature2Update); if (TrackCancel.Continue() == false) { return; } if (feature2Update != null) Marshal.ReleaseComObject(feature2Update); if (pointGeometry != null) Marshal.ReleaseComObject(pointGeometry); feature2Update = updatePoints.NextFeature(); } } if (indexBuildRequired) { IGeoProcessor2 geoProcessor = new GeoProcessorClass(); bool storedOriginal = geoProcessor.AddOutputsToMap; try { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); IGPValue pointFeatureClass = gpUtilities3.MakeGPValueFromObject(osmPointFeatureClass); string fcLocation = GetLocationString(targetGPValue, osmPointFeatureClass); IIndexes featureClassIndexes = osmPointFeatureClass.Indexes; int indexPosition = -1; featureClassIndexes.FindIndex("osmID_IDX", out indexPosition); if (indexPosition == -1) { { geoProcessor.AddOutputsToMap = false; IVariantArray parameterArrary = CreateAddIndexParameterArray(fcLocation, "OSMID", "osmID_IDX", "UNIQUE", ""); IGeoProcessorResult2 gpResults2 = geoProcessor.Execute("AddIndex_management", parameterArrary, TrackCancel) as IGeoProcessorResult2; } } if (pointCount > 500) { if (pointFeatureLoad == null) { UpdateSpatialGridIndex(TrackCancel, message, geoProcessor, fcLocation); } } } catch (COMException comEx) { message.AddWarning(comEx.Message); } catch (Exception ex) { message.AddWarning(ex.Message); } finally { geoProcessor.AddOutputsToMap = storedOriginal; } } } } catch (Exception ex) { message.AddError(120100, String.Format(_resourceManager.GetString("GPTools_Utility_NodeLoadError"), ex.Message)); } finally { if (osmFileXmlReader != null) osmFileXmlReader = null; if (nodeSerializer != null) nodeSerializer = null; } }