private static object FindInDataset(GxCatalog _catalog, string path, out int numberFound) { numberFound = 0; int lastBackslashIndex = path.LastIndexOf('\\'); int lastBackslashIndexPlusOne = lastBackslashIndex + 1; if (lastBackslashIndex == -1 || lastBackslashIndexPlusOne == path.Length) return null; string dbName = path.Substring(0,lastBackslashIndex); string fcName = path.Substring(lastBackslashIndexPlusOne, path.Length - lastBackslashIndexPlusOne); object obj = _catalog.GetObjectFromFullName(dbName, out numberFound); if (numberFound == 0) return null; IGxObject geoObject; if (numberFound == 1) geoObject = obj as IGxObject; else geoObject = ((IEnumGxObject)obj).Next(); //get the first and ignore the rest. numberFound = 0; if (geoObject == null) return null; IGxDatabase db = geoObject as IGxDatabase; if (db == null || db.Workspace == null) return null; foreach (string dsName in GetFeatureDataSetNames(db.Workspace)) { string newPath = dbName + "\\" + dsName + "\\" + fcName; obj = _catalog.GetObjectFromFullName(newPath, out numberFound); if (numberFound != 0) return obj; } return null; }
//may be called by a background thread with the "Loading" dialog internal static void LoadWithCatalog(string path) { Trace.TraceInformation("{0}: Begin get ESRI Metadata With Catalog for {1}", DateTime.Now, path); Stopwatch time = new Stopwatch(); time.Start(); if (_catalog == null) _catalog = new GxCatalogClass(); // Fairly quick to return. May throw an exception. if (_catalog == null) throw new Exception("Unable to communicate with ArcCatalog"); int numberFound; //This takes a long time on the first call (loads up the ESRI libraries) //FIXME - If this is the first time, and we are on the UI thread, then provide feedback Trace.TraceInformation("{0}: Begin _catalog.GetObjectFromFullName({1}), elapsed ms = {2}", DateTime.Now, path, time.Elapsed.TotalMilliseconds); time.Reset(); time.Start(); object obj = _catalog.GetObjectFromFullName(path, out numberFound); Trace.TraceInformation("{0}: End _catalog.GetObjectFromFullName({1}), elapsed ms = {2}", DateTime.Now, path, time.Elapsed.TotalMilliseconds); time.Reset(); time.Start(); if (numberFound == 0) { // If the user does not have a folder connection to some point above this dataset, // then ArcGIS will not find the dataset. Therefore: // try adding the root of path as a new folder connection, and trying again. _catalog.ConnectFolder(Path.GetPathRoot(path)); obj = _catalog.GetObjectFromFullName(path, out numberFound); if (numberFound == 0) // TM2.2 stored the data path without the dataset, so // database/dataset/featureclass was saved as just database/featureclass // Therefore if we didn't find database/featureclass, try looking for database/*/featureclass // FIXME - store the corrected path in the xml file, store uncorrected path in mdb obj = FindInDataset(_catalog, path, out numberFound); if (numberFound == 0) throw new ArgumentException("Path (" + path + ") not found"); } IGxObject geoObject; if (numberFound == 1) geoObject = obj as IGxObject; else geoObject = ((IEnumGxObject)obj).Next(); //get the first and ignore the rest. IPropertySet propertySet; if (geoObject is IMetadata) propertySet = ((IMetadata)geoObject).Metadata; else throw new ArgumentException("Path (" + path + ") has no metadata"); if (!(propertySet is IXmlPropertySet2)) throw new ArgumentException("Path (" + path + ") metadata not available as XML"); string text = ((IXmlPropertySet2)propertySet).GetXml(""); Trace.TraceInformation("{0}: End get ESRI Metadata With Catalog for {1}, elapsed ms = {2}", DateTime.Now, path, time.Elapsed.TotalMilliseconds); _cache[path] = text; return; }