Exemplo n.º 1
0
        /// <summary>
        /// 导出图层中选定要素到单独的shp文件
        /// </summary>
        /// <param name="featureLayer">要素图层</param>
        /// <param name="selectionSet">要素选择集</param>
        /// <param name="outName">输出shp文件路径</param>
        public void exportSelectedFeatureToShp(IFeatureLayer featureLayer, ISelectionSet selectionSet, string outName)
        {
            if (featureLayer == null)
            {
                return;
            }
            if (!Directory.Exists(System.IO.Path.GetDirectoryName(outName)))
            {
                return;
            }

            // 裁剪要素
            IDataset          dataset         = featureLayer as IDataset;
            IFeatureClassName infeatClassName = dataset.FullName as IFeatureClassName;
            IDatasetName      datasetName     = infeatClassName as IDatasetName;

            // 输出要素类
            IFeatureClassName outFeatClassName = new FeatureClassName() as IFeatureClassName;

            outFeatClassName.FeatureType    = esriFeatureType.esriFTSimple;
            outFeatClassName.ShapeType      = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny;
            outFeatClassName.ShapeFieldName = "Shape";

            // 输出文件
            IDatasetName outDatasetName = outFeatClassName as IDatasetName;

            outDatasetName.Name = System.IO.Path.GetFileNameWithoutExtension(outName);
            IWorkspaceName workspaceName = new WorkspaceName() as IWorkspaceName;

            workspaceName.PathName = System.IO.Path.GetDirectoryName(outName);
            workspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.ShapefileWorkspaceFactory";
            outDatasetName.WorkspaceName         = workspaceName;

            // 导出
            IExportOperation exportOper = new ExportOperation();

            exportOper.ExportFeatureClass(datasetName, null, selectionSet, null, outFeatClassName, 0);
        }
        private void Export(string tempMdbPathName, string attributeDatasetName, bool selectedOnly)
        {
            IRelationshipClass relClass = null;
            IFeatureClass outFeatureClass = null;
            IFeatureClass joinFeatureClass = null;
            IFeatureLayer joinLayer = null;

            IDataset attributeDataset;
            ITable exportAttributes;
            IWorkspaceFactory joinWorkspaceFactory = new OLEDBWorkspaceFactoryClass();
            object outWS = null;

            try
            {
                SetCursor(true);

                OpenOleDbWorkspace(tempMdbPathName, attributeDatasetName, ref joinWorkspaceFactory,
                    out attributeDataset, out exportAttributes);

                IDisplayTable hluDisplayTable = (IDisplayTable)_hluLayer;
                IFeatureClass hluDisplayTableFeatureClass = (IFeatureClass)hluDisplayTable.DisplayTable;

                // Set the selected and total feature counts.
                int selectedFeatureCount = _hluFeatureSelection.SelectionSet.Count;
                int totalFeatureCount = _hluLayer.FeatureClass.FeatureCount(null);
                int exportFeatureCount = 0;

                // Prompt the user for where to save the export layer.
                IExportOperation exportOp = new ExportOperation();
                bool saveProjection;
                esriExportTableOptions exportOptions;
                IDatasetName exportDatasetName = exportOp.GetOptions(hluDisplayTableFeatureClass,
                    _hluLayer.Name, _hluFeatureSelection != null && _hluFeatureSelection.SelectionSet.Count > 0, 
                    true, _application.hWnd, out saveProjection, out exportOptions);

                // If no export dataset name was chosen by the user then cancel the export.
                if (exportDatasetName == null)
                {
                    _pipeData.Add("cancelled");
                    return;
                }

                // Open the export dataset workspace.
                outWS = ((IName)exportDatasetName.WorkspaceName).Open();

                // Determine if the export layer is a shapefile.
                bool isShp = IsShp(outWS as IWorkspace);

                //---------------------------------------------------------------------
                // FIX: 050 Warn ArcGIS users if field names may be truncated or
                // renamed exporting to shapefiles.
                //
                // If the export layer is a shapefile check if any of
                // the attribute field names will be truncated.
                if (isShp)
                {
                    bool fieldNamesTruncated = false;
                    for (int i = 0; i < exportAttributes.Fields.FieldCount; i++)
                    {
                        IField attributeField = exportAttributes.Fields.get_Field(i);
                        if (attributeField.Name.Length > 10)
                        {
                            fieldNamesTruncated = true;
                            break;
                        }
                    }

                    // Warn the user that some field names may get truncated.
                    if (fieldNamesTruncated)
                    {
                        MessageBoxResult userResponse = MessageBoxResult.No;
                        userResponse = MessageBox.Show("Some field names may get truncated or renamed exporting to a shapefile.\n\nDo you wish to proceed?", "HLU: Export",
                            MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (userResponse != MessageBoxResult.Yes)
                        {
                            _pipeData.Add("cancelled");
                            return;
                        }
                    }
                }
                //---------------------------------------------------------------------

                // Get the geometry definition for the feature layer.
                IGeometryDef geomDef = _hluFeatureClass.Fields.get_Field(_hluFeatureClass.FindField(
                    _hluFeatureClass.ShapeFieldName)).GeometryDef;

                ITable joinLayerTable;
                IDisplayTable joinDisplayTable;

                //---------------------------------------------------------------------
                // CHANGED: CR13 (Export features performance)
                //
                // If only a sub-set of features are being exported then
                // export the sub-set to a temporary feature class before
                // joining the temporary layer to the attribute dataset.
                if (selectedOnly)
                {
                    // Set the export options for which records to export.
                    exportOptions = esriExportTableOptions.esriExportSelectedRecords;

                    // Set the input DataSet name
                    IDataset inDataset;
                    inDataset = (IDataset)hluDisplayTable.DisplayTable;
                    IDatasetName inDatasetName;
                    inDatasetName = (IDatasetName)inDataset.FullName;

                    // set the output temporary DataSet name
                    IFeatureClassName outFCName = new FeatureClassNameClass();
                    IDatasetName outDatasetName = (IDatasetName)outFCName;
                    outDatasetName.Name = String.Format("{0}_temp", exportDatasetName.Name);
                    outDatasetName.WorkspaceName = exportDatasetName.WorkspaceName;

                    // Get the selected features for export
                    ISelectionSet selectionSet = _hluFeatureSelection.SelectionSet;

                    // If there is no selection cancel the export.
                    if (_hluFeatureSelection.SelectionSet.Count == 0)
                    {
                        _pipeData.Add("noselection");
                        return;
                    }

                    // Export the selected features to the temporary dataset.
                    exportOp.ExportFeatureClass(inDatasetName, null, selectionSet, geomDef, (IFeatureClassName)outDatasetName, _application.hWnd);

                    // Cast the workspace to IFeatureWorkspace and open the feature class.
                    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)outWS;
                    joinFeatureClass = featureWorkspace.OpenFeatureClass(outDatasetName.Name);

                    // Add an attribute index to the incid field.
                    AddFieldIndex(joinFeatureClass, String.Format("IX_{0}",
                        _hluLayerStructure.incidColumn.ColumnName),
                        _hluLayerStructure.incidColumn.ColumnName);

                    // Set the join layer to the temporary feature class.
                    joinLayer = new FeatureLayerClass();
                    joinLayer.FeatureClass = joinFeatureClass;
                    joinLayer.Name = joinFeatureClass.AliasName;

                    // Set the join layer table to the temporary feature class.
                    joinDisplayTable = (IDisplayTable)joinLayer;
                    //IFeatureClass joinDisplayTableFC = (IFeatureClass)joinDisplayTable.DisplayTable;
                    IFeatureClass joinDisplayTableFC = joinFeatureClass;
                    joinLayerTable = (ITable)joinDisplayTableFC;

                    // Set the count for the number of features to be exported.
                    exportFeatureCount = selectedFeatureCount;
                }
                // Otherwise, join the whole feature layer to the
                // attribute dataset.
                else
                {
                    // Clear any current selection.
                    _hluFeatureSelection.SelectionSet = null;

                    // Set the export options for which records to export.
                    exportOptions = esriExportTableOptions.esriExportAllRecords;

                    // Set the join feature class to the current HLU feature class.
                    joinFeatureClass = _hluFeatureClass;

                    // Set the join layer to the current HLU feature layer.
                    joinLayer = _hluLayer;
                    joinLayerTable = (ITable)hluDisplayTableFeatureClass;
                    joinDisplayTable = hluDisplayTable;

                    // Set the count for the number of features to be exported.
                    exportFeatureCount = totalFeatureCount;
                }
                //---------------------------------------------------------------------

                // Get the field names to be used when joining the attribute data and the feature layer
                string originPKJoinField = _hluLayerStructure.incidColumn.ColumnName;
                string originFKJoinField =
                    _hluFeatureClass.Fields.get_Field(_hluFieldMap[_hluLayerStructure.incidColumn.Ordinal]).Name;

                // Get a list of all the fields to be used in the export layer (plus separate lists of all
                // those fields that will come from the attribute table and those that will come from the
                // feature layer).
                List<IField> attributeFields;
                List<IField> featClassFields;
                List<IField> exportFields = ExportFieldLists(isShp, originPKJoinField, originFKJoinField, joinFeatureClass,
                    exportAttributes, out attributeFields, out featClassFields);

                // Add x/y, length, or area and length fields to the list of fields in the export layer
                // if the export layer is a shapefile.
                ExportAddGeometryPropertyFields(isShp, exportFields);

                // Create a virtual relationship between the feature class
                // and the attribute dataset.
                IMemoryRelationshipClassFactory memoryRelFactory = new MemoryRelationshipClassFactoryClass();
                relClass = memoryRelFactory.Open("ExportRelClass", (IObjectClass)exportAttributes,
                    originPKJoinField, (IObjectClass)joinLayerTable, originFKJoinField, "forward", "backward",
                    esriRelCardinality.esriRelCardinalityOneToMany);

                // Use the relationship to perform a join.
                IDisplayRelationshipClass displayRelClass = (IDisplayRelationshipClass)joinLayer;
                displayRelClass.DisplayRelationshipClass(relClass, esriJoinType.esriLeftInnerJoin);

                // Create query filter for the export cursor so that
                // only the required fields are retrieved.
                bool featClassFieldsQualified;
                bool attributeFieldsQualified;
                IQueryFilter exportQueryFilter = ExportQueryFilter(originPKJoinField, joinLayer, joinFeatureClass, joinDisplayTable,
                    attributeDataset, featClassFields, attributeFields, out featClassFieldsQualified,
                    out attributeFieldsQualified);

                // Create a collection of fields for the output feature class.
                // Adds OID and SHAPE at beginning.
                IFields outFields = CreateFieldsCollection(true, geomDef.HasZ, geomDef.HasM, outWS,
                    joinFeatureClass.ShapeType, exportFields.Select(f => f.Length).ToArray(),
                    exportFields.Select(f => f.Name).ToArray(), exportFields.Select(f => f.Name).ToArray(),
                    exportFields.Select(f => f.Type).ToArray(), exportFields.Select(f => f.Type != 
                        esriFieldType.esriFieldTypeOID).ToArray(), geomDef.SpatialReference);

                // Create the output feature class.
                outFeatureClass = CreateFeatureClass(exportDatasetName.Name, null, outWS,
                    outFields, esriFeatureType.esriFTSimple, joinFeatureClass.ShapeType, null, null);

                // Map the fields between the display and table and the output feature
                // class as the display table always includes all fields.
                // The first two fields are always OID and SHAPE.
                int[] exportFieldMap = new int[] { 0, 1 }.Concat(featClassFields
                    .Select(f => joinDisplayTable.DisplayTable.Fields.FindField(featClassFieldsQualified ?
                        joinLayer.Name + "." + f.Name : f.Name))).Concat(attributeFields
                    .Select(f => joinDisplayTable.DisplayTable.Fields.FindField(attributeFieldsQualified ?
                        attributeDataset.Name + "." + f.Name : f.Name))).ToArray();

                //---------------------------------------------------------------------
                // FIX: 038 Display the export progress bar correctly when exporting
                // from ArcGIS.
                // Pass the number of features to be exported, not the number of incids,
                // so that the export progress is displayed corectly
                //
                // Insert the features and attributes into the new feature class.
                ExportInsertFeatures(joinDisplayTable, exportQueryFilter, exportFeatureCount,
                    exportFieldMap, isShp, outWS, outFeatureClass);
                //---------------------------------------------------------------------

                //---------------------------------------------------------------------
                // CHANGED: CR16 (Adding exported features)
                // Ask the user if they want to add the new export layer
                // to the active map.
                MessageBoxResult addResponse = MessageBoxResult.No;
                addResponse = MessageBox.Show("The export operation succeeded.\n\nAdd the exported layer to the current map?", "HLU: Export",
                    MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (addResponse == MessageBoxResult.Yes)
                {
                    // Add the exported feature layer to the active map.
                    IFeatureLayer hluExportLayer;
                    hluExportLayer = new FeatureLayer();
                    hluExportLayer.FeatureClass = outFeatureClass;
                    hluExportLayer.Name = outFeatureClass.AliasName;
                    _focusMap.AddLayer(hluExportLayer);
                }
                //---------------------------------------------------------------------

            }
            catch (Exception ex) { _pipeData.Add(ex.Message); }
            finally
            {
                // Remove the virtual relationship.
                if (relClass != null)
                {
                    //IRelationshipClassCollectionEdit relClassEdit = (IRelationshipClassCollectionEdit)joinLayer;
                    //relClassEdit.RemoveAllRelationshipClasses();
                    ((IDisplayRelationshipClass)joinLayer).DisplayRelationshipClass(
                        null, esriJoinType.esriLeftInnerJoin);
                }

                // Destroy workspace factory so the attribute dataset can
                // be deleted later.
                attributeDataset = null;
                exportAttributes = null;
                joinWorkspaceFactory = null;

                // Delete the temporary feature class.
                try
                {
                    if (joinFeatureClass != _hluFeatureClass)
                    {
                        IDataset tempDataset = (IDataset)joinFeatureClass;
                        if (tempDataset != null) tempDataset.Delete();
                    }
                }
                catch { }

                SetCursor(false);
            }
        }
Exemplo n.º 3
0
        public static void Util_Extract(IFeatureLayer pInFLayer, string strValue, string strField)
        {
            //****** Author:  Jim Detwiler
            //******** Date:  11/17/2001
            //* Description:  Sub procedure that accepts a feature layer, attribute
            //*               value, and field name as parameters.  Using these
            //*               parameters, it will select out all features having
            //*               the attribute value in the specified field, the
            //*               write them to a new shapefile.
            //**** Calls to:
            //**** Calls by:  Rivers_Subset
            //***** Globals:
            //****** Locals:  pInFLayer, strValue, strField, pInFClass, pInDataset,
            //******          pFeatureWorkspace, pInFClassName, strShpName, pFields,
            //******          lngGeomIndex, pField, pGeomDef, pQFilter,
            //******          pWorkspaceFactory, pOutWorkspaceName, pOutFClassName,
            //******          pOutDatasetName, pSelSet, pExportOp
            //**** Location:
            //****** Source:
            //******* Notes:
            //****************************************
            //* Revision Author:  Andrew Murdoch
            //** Revision Date:  5/28/2011
            //** Revision Notes:  Updated for ArcGIS 10 and VB.NET
            //** Revision Date:  11/8/2014
            //** Revision Notes:  Updated for C#


            // Getting the input feature class info
            IFeatureClass pInFClass;

            pInFClass = pInFLayer.FeatureClass;

            IDataset pInDataset;

            pInDataset = (IDataset)pInFLayer;
            // QI to get workspace

            IFeatureWorkspace pFeatureWorkspace;

            pFeatureWorkspace = (IFeatureWorkspace)pInDataset.Workspace;

            IFeatureClassName pInFClassName;

            pInFClassName = (IFeatureClassName)pInDataset.FullName;

            //Get geometry definition from input featureclass.
            string strShpName;

            strShpName = pInFClass.ShapeFieldName;
            IFields pFields;

            pFields = pInFClass.Fields;
            int lngGeomIndex = 0;

            lngGeomIndex = pFields.FindField(strShpName);
            IField pField;

            pField = pFields.get_Field(lngGeomIndex);
            IGeometryDef pGeomDef;

            pGeomDef = pField.GeometryDef;

            // Setting up the query filter based on the supplied field and value
            IQueryFilter pQFilter;

            pQFilter             = new QueryFilter();
            pQFilter.WhereClause = strField + " = '" + strValue + "'";

            IWorkspaceFactory pWorkspaceFactory;

            pWorkspaceFactory = new ShapefileWorkspaceFactory();

            //Set up outworkspacename for new shape file.
            IWorkspaceName pOutWorkspaceName;

            pOutWorkspaceName = (IWorkspaceName) new WorkspaceName();
            pOutWorkspaceName.WorkspaceFactoryProgID = "esricore.shapefileworkspacefactory.1";
            pOutWorkspaceName.PathName = "c:\\temp";
            //path to where I want the shapefile.
            IFeatureClassName pOutFClassName;

            pOutFClassName = (IFeatureClassName) new FeatureClassName();
            IDatasetName pOutDatasetName;

            pOutDatasetName      = (IDatasetName)pOutFClassName;
            pOutDatasetName.Name = strValue;
            //Output shapefile name.
            pOutDatasetName.WorkspaceName = pOutWorkspaceName;

            // Performing the selection
            ISelectionSet pSelSet;

            pSelSet = pInFClass.Select(pQFilter, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, pInDataset.Workspace);

            // Exporting the selection
            IExportOperation pExportOp;

            pExportOp = new ExportOperation();
            pExportOp.ExportFeatureClass((IDatasetName)pInFClassName, pQFilter, pSelSet, pGeomDef, (IFeatureClassName)pOutDatasetName, 0);
        }