private Task ChangeUSHighwaysLayerDataConnectionAsync(FeatureLayer featureLayer, string catalogPath)
        {
            return(QueuedTask.Run(() => {
                CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();

                string connection = System.IO.Path.GetDirectoryName(catalogPath);
                string suffix = System.IO.Path.GetExtension(connection).ToLower();

                var workspaceConnectionString = string.Empty;
                WorkspaceFactory wf = WorkspaceFactory.FileGDB;
                if (suffix == ".sde")
                {
                    wf = WorkspaceFactory.SDE;
                    var dbGdbConnection = new DatabaseConnectionFile(new Uri(connection, UriKind.Absolute));
                    workspaceConnectionString = new Geodatabase(dbGdbConnection).GetConnectionString();
                }
                else
                {
                    var dbGdbConnectionFile = new FileGeodatabaseConnectionPath(new Uri(connection, UriKind.Absolute));
                    workspaceConnectionString = new Geodatabase(dbGdbConnectionFile).GetConnectionString();
                }

                string dataset = System.IO.Path.GetFileName(catalogPath);
                // provide a replace data connection method
                CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection()
                {
                    WorkspaceConnectionString = workspaceConnectionString,
                    WorkspaceFactory = wf,
                    Dataset = dataset,
                    DatasetType = esriDatasetType.esriDTFeatureClass
                };

                featureLayer.SetDataConnection(updatedDataConnection);

                //For a RDBMS, it might look like this:
                //string connection = "C:\\Work\\temp.sde";
                //Geodatabase sde = new Geodatabase(connection);

                //// provide a replace data connection method
                //CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection();
                //updatedDataConnection.WorkspaceConnectionString = sde.GetConnectionString();
                //updatedDataConnection.WorkspaceFactory = WorkspaceFactory.SDE;
                //updatedDataConnection.Dataset = "vtest.usa.states";
                //updatedDataConnection.DatasetType = esriDatasetType.esriDTFeatureClass;



                //// Alternatively, use Layer.FindAndReplaceWorkspacePath()
                ////Note: this will not allow changing the dataset name or workspace type
                ////
                ////string connection = "C:\\Work\\temp.sde";
                ////Geodatabase sde = new Geodatabase(connection);
                ////featureLayer.FindAndReplaceWorkspacePath(((CIMStandardDataConnection)currentDataConnection).WorkspaceConnectionString,
                ////                        sde.GetConnectionString(), true);


                //////////////////////////////////////////////
                ////Please Read
                ////
                //ok, so at this point we have a couple of bugs at 1.1 AND 1.2.....
                //
                //#1: if you switched to a Datasource that invalidates the Renderer, the Renderer does
                //not get invalidated in the UI
                //(eg You had a UniqueValueRenderer on a Field called "CATEGORY", the new datasource
                //does NOT have that field and so the renderer is invalid).
                //
                //#2: By default, Layers are added with a permanent cache. The cache is NOT automatically
                //invalidated so data (eg in the Attribute table, on the screen for draws) does NOT get
                //Refreshed so you have to invalidate the cache manually...

                //So, Bug #1 - we arbitrarily switch the Renderer to a simple renderer as a work around for that...
                featureLayer.SetRenderer(featureLayer.CreateRenderer(new SimpleRendererDefinition()));

                //Bug #2, we manually invalidate the cache
                featureLayer.ClearDisplayCache();
            }));
        }
コード例 #2
0
        private Task ChangeUSHighwaysLayerDataConnectionAsync(FeatureLayer featureLayer, string catalogPath) {
            return QueuedTask.Run(() => {
                CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();

                string connection = System.IO.Path.GetDirectoryName(catalogPath);
                string suffix = System.IO.Path.GetExtension(connection).ToLower();

                WorkspaceFactory wf = WorkspaceFactory.FileGDB;
                if (suffix == ".sde") {
                    wf = WorkspaceFactory.SDE;
                }

                string dataset = System.IO.Path.GetFileName(catalogPath);

                // provide a replace data connection method
                CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection() {
                    WorkspaceConnectionString = new Geodatabase(connection).GetConnectionString(),
                    WorkspaceFactory = wf,
                    Dataset = dataset,
                    DatasetType = esriDatasetType.esriDTFeatureClass
                };

                featureLayer.SetDataConnection(updatedDataConnection);

                //For a RDBMS, it might look like this:
                //string connection = "C:\\Work\\temp.sde";
                //Geodatabase sde = new Geodatabase(connection);

                //// provide a replace data connection method
                //CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection();
                //updatedDataConnection.WorkspaceConnectionString = sde.GetConnectionString();
                //updatedDataConnection.WorkspaceFactory = WorkspaceFactory.SDE;
                //updatedDataConnection.Dataset = "vtest.usa.states";
                //updatedDataConnection.DatasetType = esriDatasetType.esriDTFeatureClass;



                //// Alternatively, use Layer.FindAndReplaceWorkspacePath()
                ////Note: this will not allow changing the dataset name or workspace type
                ////
                ////string connection = "C:\\Work\\temp.sde";
                ////Geodatabase sde = new Geodatabase(connection);
                ////featureLayer.FindAndReplaceWorkspacePath(((CIMStandardDataConnection)currentDataConnection).WorkspaceConnectionString, 
                ////                        sde.GetConnectionString(), true);


                //////////////////////////////////////////////
                ////Please Read
                ////
                //ok, so at this point we have a couple of bugs at 1.1 AND 1.2.....
                //
                //#1: if you switched to a Datasource that invalidates the Renderer, the Renderer does
                //not get invalidated in the UI
                //(eg You had a UniqueValueRenderer on a Field called "CATEGORY", the new datasource
                //does NOT have that field and so the renderer is invalid).
                //
                //#2: By default, Layers are added with a permanent cache. The cache is NOT automatically
                //invalidated so data (eg in the Attribute table, on the screen for draws) does NOT get
                //Refreshed so you have to invalidate the cache manually...

                //So, Bug #1 - we arbitrarily switch the Renderer to a simple renderer as a work around for that...
                featureLayer.SetRenderer(featureLayer.CreateRenderer(new SimpleRendererDefinition()));

                //Bug #2, we manually invalidate the cache
                featureLayer.ClearDisplayCache();
            });
        }