예제 #1
0
        internal void SetMap(string filePath)
        {
            // open the map document
            ESRI.ArcGIS.Carto.IMapDocument mapDocument = new ESRI.ArcGIS.Carto.MapDocumentClass();
            mapDocument.Open(filePath, null);
            // get first map (data frame)
            ESRI.ArcGIS.Carto.IMap map = mapDocument.get_Map(0);
            // track whether we need to save properties
            string[] tTmp = new string[m_ExtractionLayerProperties.Keys.Count];
            m_ExtractionLayerProperties.Keys.CopyTo(tTmp, 0);
            // we will remove items from this previously configured list and anything left we know is no longer in map, so remove
            // from saved properties
            List <string> tPreviouslyConfiguredLayers = tTmp.ToList <string>();

            // get raster layers
            ESRI.ArcGIS.esriSystem.UID rasterLayerId = new ESRI.ArcGIS.esriSystem.UIDClass();
            // GUID for a raster layer
            rasterLayerId.Value = "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}";
            ESRI.ArcGIS.Carto.IEnumLayer rasterEnumLayer = map.get_Layers(rasterLayerId, true);

            int dirSelectedIndex = 0;
            int accSelectedIndex = 0;

            // set a default value of none
            // if either box is left at none then the SOE will know not to
            // expose the watershed operation
            ComboFlowDir.Items.Add("NONE");
            ComboFlowAcc.Items.Add("NONE");
            ESRI.ArcGIS.Carto.IRasterLayer rasterLayer = null;
            while ((rasterLayer = rasterEnumLayer.Next() as ESRI.ArcGIS.Carto.IRasterLayer) !=
                   null)
            {
                ESRI.ArcGIS.DataSourcesRaster.IRasterProps tRasterProps =
                    rasterLayer.Raster as ESRI.ArcGIS.DataSourcesRaster.IRasterProps;
                ESRI.ArcGIS.Geodatabase.rstPixelType tPixelType = tRasterProps.PixelType;
                // flow dir can only be an integer raster of short or long types
                if (tPixelType == ESRI.ArcGIS.Geodatabase.rstPixelType.PT_UCHAR ||
                    tPixelType == ESRI.ArcGIS.Geodatabase.rstPixelType.PT_SHORT ||
                    tPixelType == ESRI.ArcGIS.Geodatabase.rstPixelType.PT_ULONG ||
                    tPixelType == ESRI.ArcGIS.Geodatabase.rstPixelType.PT_LONG)
                {
                    ComboFlowDir.Items.Add(rasterLayer.Name);
                    ComboFlowAcc.Items.Add(rasterLayer.Name);
                }
                // flow acc can theoretically be floating point as far as i can see
                // albeit it won't normally be
                else if (tPixelType == ESRI.ArcGIS.Geodatabase.rstPixelType.PT_FLOAT ||
                         tPixelType == ESRI.ArcGIS.Geodatabase.rstPixelType.PT_DOUBLE)
                {
                    ComboFlowAcc.Items.Add(rasterLayer.Name);
                }
                if (rasterLayer.Name == m_FlowAccLayerName)
                {
                    accSelectedIndex = ComboFlowAcc.Items.Count - 1;
                }
                else if (rasterLayer.Name == m_FlowDirLayerName)
                {
                    dirSelectedIndex = ComboFlowDir.Items.Count - 1;
                }
                string tRasterType = "ContinuousRaster";
                if (tRasterProps.IsInteger)
                {
                    tRasterType = "CategoricalRaster";
                }

                ExtractionLayerProperties tLayerInMap = new ExtractionLayerProperties(rasterLayer.Name, false, rasterLayer.Name.Substring(0, 6),
                                                                                      tRasterType, "NONE", "NONE", "NONE", null, null, null);
                ExtractionLayerProperties tCurrentConfigForLayer;
                bool alreadyconfigured = m_ExtractionLayerProperties.TryGetValue(rasterLayer.Name, out tCurrentConfigForLayer);
                if (alreadyconfigured)
                {
                    tLayerInMap.Enabled   = tCurrentConfigForLayer.Enabled;
                    tLayerInMap.ParamName = tCurrentConfigForLayer.ParamName;
                    // that's all that needs doing for a raster raster
                }
                m_ExtractionLayerProperties[rasterLayer.Name] = tLayerInMap;
                tPreviouslyConfiguredLayers.Remove(rasterLayer.Name);
            }
            int extSelectedIndex = 0;

            ESRI.ArcGIS.esriSystem.UID featlyrId = new ESRI.ArcGIS.esriSystem.UIDClass();
            featlyrId.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
            ESRI.ArcGIS.Carto.IEnumLayer    featureEnumLayer = map.get_Layers(featlyrId, true);
            ESRI.ArcGIS.Carto.IFeatureLayer featureLayer     = null;
            while ((featureLayer = featureEnumLayer.Next() as ESRI.ArcGIS.Carto.IFeatureLayer) !=
                   null)
            {
                if (featureLayer.FeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon &&
                    featureLayer.FeatureClass.FeatureType == ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple)
                {
                    ComboExtentFeatures.Items.Add(featureLayer.Name);
                }
                if (featureLayer.Name == m_ExtentFeatureLayerName)
                {
                    extSelectedIndex = ComboExtentFeatures.Items.Count - 1;
                }
                // now stuff for the extraction layer configuration
                List <string> catfields  = new List <string>();
                List <string> valfields  = new List <string>();
                List <string> measfields = new List <string>();
                catfields.Add("NONE");
                valfields.Add("NONE");
                measfields.Add("NONE");
                ESRI.ArcGIS.Geodatabase.IFields tFLFields = featureLayer.FeatureClass.Fields;

                for (int i = 0; i < tFLFields.FieldCount; i++)
                {
                    ESRI.ArcGIS.Geodatabase.IField        tField     = featureLayer.FeatureClass.Fields.get_Field(i);
                    ESRI.ArcGIS.Geodatabase.esriFieldType tFieldType = tField.Type;
                    switch (tFieldType)
                    {
                    case ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble:
                    case ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeSingle:
                        valfields.Add(tField.Name);
                        measfields.Add(tField.Name);
                        break;

                    case ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger:
                    case ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeSmallInteger:
                        valfields.Add(tField.Name);
                        catfields.Add(tField.Name);
                        measfields.Add(tField.Name);
                        break;

                    case ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString:
                    case ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDate:
                        catfields.Add(tField.Name);
                        break;
                    }
                }
                ExtractionLayerProperties tLayerInMap = new ExtractionLayerProperties(featureLayer.Name, false, featureLayer.Name.Substring(0, 6),
                                                                                      "FeatureLayer", "NONE", "NONE", "NONE", catfields, valfields, measfields);
                ExtractionLayerProperties tCurrentConfigForLayer;
                bool alreadyconfigured = m_ExtractionLayerProperties.TryGetValue(featureLayer.Name, out tCurrentConfigForLayer);
                if (alreadyconfigured)
                {
                    // it is already there - loaded from properties - we will make a new object anyway but will copy over the configured
                    // parameter name, enabled, and selected fields.
                    tLayerInMap.Enabled   = tCurrentConfigForLayer.Enabled;
                    tLayerInMap.ParamName = tCurrentConfigForLayer.ParamName;
                    if (tCurrentConfigForLayer.CategoryFieldName != "NONE" &&
                        featureLayer.FeatureClass.FindField(tCurrentConfigForLayer.CategoryFieldName) != -1)
                    {
                        tLayerInMap.CategoryFieldName = tCurrentConfigForLayer.CategoryFieldName;
                    }
                    if (tCurrentConfigForLayer.ValueFieldName != "NONE" &&
                        featureLayer.FeatureClass.FindField(tCurrentConfigForLayer.ValueFieldName) != -1)
                    {
                        tLayerInMap.ValueFieldName = tCurrentConfigForLayer.ValueFieldName;
                    }
                    if (tCurrentConfigForLayer.MeasureFieldName != "NONE" &&
                        featureLayer.FeatureClass.FindField(tCurrentConfigForLayer.ValueFieldName) != -1)
                    {
                        tLayerInMap.MeasureFieldName = tCurrentConfigForLayer.MeasureFieldName;
                    }
                }
                m_ExtractionLayerProperties[featureLayer.Name] = tLayerInMap;
                // now we need to delete from m_ExtractionLayerProperties any configs that haven't been found in the map
                tPreviouslyConfiguredLayers.Remove(featureLayer.Name);
            }
            foreach (string invalidLayerName in tPreviouslyConfiguredLayers)
            {
                m_ExtractionLayerProperties.Remove(invalidLayerName);
            }
            // now, m_extractionlayerproperties is a dictionary of extractionlayerproperties objects
            // where the objects contain the values from the previously saved lot if they were there
            foreach (ExtractionLayerProperties rowdetails in m_ExtractionLayerProperties.Values)
            {
                //  string[] rowParams = new string[]{
                //      rowdetails.LayerName,rowdetails.Enabled.ToString(),rowdetails.ParamName,rowdetails.CategoryFieldName,rowdetails.ValueFieldName
                //     };
                dataGridView1.Rows.Add();
                int             newRowIdx = dataGridView1.Rows.Count - 1;
                DataGridViewRow tRow      = dataGridView1.Rows[newRowIdx];
                tRow.Cells["LayerName"].Value  = rowdetails.LayerName;
                tRow.Cells["LayerAvail"].Value = rowdetails.Enabled.ToString();
                tRow.Cells["LayerParam"].Value = rowdetails.ParamName;
                if (rowdetails.ExtractionType == "FeatureLayer")
                {
                    DataGridViewComboBoxCell cfcell = (DataGridViewComboBoxCell)tRow.Cells["CatField"];

                    foreach (object itemToAdd in rowdetails.PossibleCategoryFields)
                    {
                        cfcell.Items.Add(itemToAdd);
                    }
                    cfcell.Value = rowdetails.CategoryFieldName;
                    DataGridViewComboBoxCell valcell = (DataGridViewComboBoxCell)tRow.Cells["ValField"];
                    foreach (object itemToAdd in rowdetails.PossibleValueFields)
                    {
                        valcell.Items.Add(itemToAdd);
                    }
                    valcell.Value = rowdetails.ValueFieldName;
                    DataGridViewComboBoxCell meascell = (DataGridViewComboBoxCell)tRow.Cells["MeasField"];
                    foreach (object itemToAdd in rowdetails.PossibleMeasureFields)
                    {
                        meascell.Items.Add(itemToAdd);
                    }
                    meascell.Value = rowdetails.MeasureFieldName;
                    tRow.Cells["MeasField"].Value = rowdetails.MeasureFieldName;
                }
                else
                {
                    tRow.Cells["CatField"].Value     = rowdetails.CategoryFieldName;
                    tRow.Cells["ValField"].Value     = rowdetails.ValueFieldName;
                    tRow.Cells["MeasField"].Value    = rowdetails.MeasureFieldName;
                    tRow.Cells["CatField"].ReadOnly  = true;
                    tRow.Cells["ValField"].ReadOnly  = true;
                    tRow.Cells["MeasField"].ReadOnly = true;
                }
                int rowIdx = dataGridView1.Rows.Add(tRow);
            }
            mapDocument.Close();
            mapDocument = null;
            map         = null;
            ComboFlowAcc.SelectedIndex        = accSelectedIndex;
            ComboFlowDir.SelectedIndex        = dirSelectedIndex;
            ComboExtentFeatures.SelectedIndex = extSelectedIndex;
            m_initAcc = true;
            m_initDir = true;
            m_initExt = true;
            m_GridIsInSyncWithProperties = true;
            radioReadMap.Checked         = m_GetLayersFromMap;
            dataGridView1.Enabled        = radioReadMap.Checked;
        }
예제 #2
0
        public static void map_fields(OSGeo.OGR.Layer ogr_layer,
                                      out System.Collections.Hashtable outFieldMap,
                                      out ESRI.ArcGIS.Geodatabase.IFields outFields,
                                      out ESRI.ArcGIS.Geodatabase.esriDatasetType outDatasetType,
                                      out ESRI.ArcGIS.Geometry.esriGeometryType outGeometryType,
                                      out int outShapeIndex,
                                      out int outOIDFieldIndex,
                                      out ISpatialReference outSpatialReference)
        {
            outSpatialReference = null;
            outFields           = null;
            outDatasetType      = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable; // start assuming it is a table
            outGeometryType     = esriGeometryType.esriGeometryNull;                   //don't know what it is
            outOIDFieldIndex    = -1;
            outShapeIndex       = -1;
            outFieldMap         = new System.Collections.Hashtable();

            System.Collections.ArrayList fieldArray = new System.Collections.ArrayList();

            OSGeo.OGR.FeatureDefn featDef = ogr_layer.GetLayerDefn();

            int fieldsInserted = 0;

            // OIDs and Geometries can be pseudo fields in GDAL and are thus *may* not included in the OGR FieldDef
            // To account for that add those first (if they exist) and keep a mapping of fields using
            // fieldsInserted


            //////////////////////////////
            //
            // handle oid field pseudo column
            //
            ESRI.ArcGIS.Geodatabase.IFieldEdit2 oidFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass();

            if (ogr_layer.GetFIDColumn().Length > 0)
            {
                oidFieldEdit.Name_2      = ogr_layer.GetFIDColumn();
                oidFieldEdit.AliasName_2 = ogr_layer.GetFIDColumn();
            }
            else
            {
                oidFieldEdit.Name_2      = "FID";
                oidFieldEdit.AliasName_2 = "FID";
            }

            oidFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID;
            fieldArray.Add(oidFieldEdit);
            outOIDFieldIndex = fieldsInserted;
            fieldsInserted++;

            //////////////////////////////////////
            //
            // handle (optional) geometry field pseudo column
            //

            if (!(ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbNone ||
                  ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbUnknown))
            {
                ESRI.ArcGIS.Geodatabase.IFieldEdit2 geomFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass();


                if (ogr_layer.GetGeometryColumn().Length > 0)
                {
                    geomFieldEdit.Name_2      = ogr_layer.GetGeometryColumn();
                    geomFieldEdit.AliasName_2 = ogr_layer.GetGeometryColumn();
                }
                else
                {
                    geomFieldEdit.Name_2      = "Shape";
                    geomFieldEdit.AliasName_2 = "Shape";
                }

                geomFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry;

                // add geometry def

                ESRI.ArcGIS.Geometry.esriGeometryType gdbType;
                bool hasZ;
                ogr_geo_type_to_esri_geo_type(ogr_layer.GetGeomType(), out gdbType, out hasZ);

                ESRI.ArcGIS.Geodatabase.IGeometryDefEdit geomDef = new ESRI.ArcGIS.Geodatabase.GeometryDefClass();
                geomDef.GeometryType_2     = gdbType;
                geomDef.HasM_2             = false; //no M support on OGR
                geomDef.HasZ_2             = hasZ;
                geomDef.SpatialReference_2 = outSpatialReference = ogr_utils.get_spatialReference(ogr_layer.GetSpatialRef());

                geomFieldEdit.GeometryDef_2 = geomDef;

                fieldArray.Add(geomFieldEdit);

                outDatasetType  = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass; // upgrade to featureclass
                outGeometryType = gdbType;
                outShapeIndex   = fieldsInserted;

                fieldsInserted++;
            }

            int fieldCount = featDef.GetFieldCount();

            for (int i = 0; i < fieldCount; i++)
            {
                // map OGR field to ArcObjects
                OSGeo.OGR.FieldDefn fieldDef = featDef.GetFieldDefn(i);

                ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass();
                fieldEdit.Name_2      = fieldDef.GetName();
                fieldEdit.AliasName_2 = fieldDef.GetName();

                // map type
                OSGeo.OGR.FieldType ogrFieldType = fieldDef.GetFieldType();
                ESRI.ArcGIS.Geodatabase.esriFieldType mappedType;
                switch (ogrFieldType)
                {
                case OSGeo.OGR.FieldType.OFTInteger:
                    mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
                    break;

                case OSGeo.OGR.FieldType.OFTReal:
                    mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble;
                    break;

                case OSGeo.OGR.FieldType.OFTString:
                    mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
                    break;

                case OSGeo.OGR.FieldType.OFTBinary:
                    mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob;
                    break;

                case OSGeo.OGR.FieldType.OFTDateTime:
                    mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDate;
                    break;

                default:
                    mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
                    break;
                }


                fieldEdit.Type_2 = mappedType;

                outFieldMap.Add(fieldsInserted, i);

                fieldArray.Add(fieldEdit);

                fieldsInserted++;
            }

            // Add all the fields from the array to an ESRI fields class object. The reason that we do that
            // here is that we need to know the count in advance

            ESRI.ArcGIS.Geodatabase.IFieldsEdit fields = new ESRI.ArcGIS.Geodatabase.FieldsClass();
            fields.FieldCount_2 = fieldArray.Count;

            for (int i = 0; i < fieldArray.Count; i++)
            {
                fields.set_Field(i, fieldArray[i] as ESRI.ArcGIS.Geodatabase.IField);
            }

            outFields = fields;
        }
        /// <summary>
        /// Sets the buffer tube and strand counts based on the given configuration. If IPID and/or CABLEID are null, it also
        /// takes care of them
        /// </summary>
        /// <param name="feature">The FiberCable feature to configure</param>
        /// <param name="configuration">The tube/strand counts</param>
        /// <param name="isExistingOperation">Flag to control whether this method is being called from within an existing
        /// edit operation</param>
        /// <returns>Success</returns>
        protected bool ConfigureCable(ESRI.ArcGIS.Geodatabase.IFeature feature, FiberCableConfiguration configuration, bool isExistingOperation)
        {
            bool isComplete         = false;
            bool isOurOperationOpen = false;

            // The following assignments are defaults for the case where they are not already populated on the feature
            string fiberCableIpid = Guid.NewGuid().ToString("B").ToUpper();

            // The following will be set during Validation
            ESRI.ArcGIS.Geodatabase.IObjectClass ftClass = null;
            ESRI.ArcGIS.Geodatabase.IFields      fields  = null;
            int ipidIdx        = -1;
            int bufferCountIdx = -1;
            int strandCountIdx = -1;

            #region Validation

            if (null == feature)
            {
                throw new ArgumentNullException("feature");
            }

            if (null == configuration)
            {
                throw new ArgumentNullException("configuration");
            }

            if (_editor.EditState == ESRI.ArcGIS.Editor.esriEditState.esriStateNotEditing)
            {
                throw new InvalidOperationException("You must be editing the workspace to perform this operation.");
            }

            ftClass = feature.Class;
            fields  = ftClass.Fields;

            string missingFieldFormat = "Field {0} is missing.";

            ipidIdx = fields.FindField(ConfigUtil.IpidFieldName);
            if (-1 == ipidIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.IpidFieldName));
            }

            bufferCountIdx = fields.FindField(ConfigUtil.NumberOfBuffersFieldName);
            if (-1 == bufferCountIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.NumberOfBuffersFieldName));
            }

            strandCountIdx = fields.FindField(ConfigUtil.NumberOfFibersFieldName);
            if (-1 == strandCountIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.NumberOfFibersFieldName));
            }

            #endregion


            ESRI.ArcGIS.esriSystem.ITrackCancel    trackCancel    = new ESRI.ArcGIS.Display.CancelTrackerClass();
            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = _hookHelper.CreateProgressDialog(trackCancel, "Preparing to configure cable...", 1, configuration.TotalFiberCount, 1, "Starting edit operation...", "Fiber Configuration");
            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;

            progressDialog.ShowDialog();
            stepProgressor.Step();

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOurOperationOpen = true;
            }

            try
            {
                if (DBNull.Value == feature.get_Value(ipidIdx))
                {
                    feature.set_Value(ipidIdx, fiberCableIpid);
                }
                else
                {
                    fiberCableIpid = feature.get_Value(ipidIdx).ToString();
                }

                feature.set_Value(bufferCountIdx, configuration.BufferCount);
                feature.set_Value(strandCountIdx, configuration.FibersPerTube);

                isComplete = GenerateUnits(feature, configuration, progressDialog, trackCancel);

                progressDialog.Description = "Completing configuration...";
                stepProgressor.Step();

                if (isOurOperationOpen)
                {
                    if (isComplete)
                    {
                        feature.Store();
                        _editor.StopOperation("Configure Fiber");
                    }
                    else
                    {
                        _editor.AbortOperation();
                    }
                }
            }
            catch (Exception e)
            {
                if (isOurOperationOpen)
                {
                    _editor.AbortOperation();
                }
            }

            progressDialog.HideDialog();
            return(isComplete);
        }