Ejemplo n.º 1
0
 //保存图层
 private void ctMenuSaveAsLyr_Click(object sender, EventArgs e)
 {
     this.saveFileDialog.Title  = "保存为Lyr文件";
     this.saveFileDialog.Filter = "Lyr文件|*.lyr";
     if (this.saveFileDialog.ShowDialog() == DialogResult.OK)
     {
         string     direction  = System.IO.Path.GetDirectoryName(this.saveFileDialog.FileName);
         string     file       = System.IO.Path.GetFileName(this.saveFileDialog.FileName);
         ILayerFile pLayerFile = new LayerFileClass();
         pLayerFile.New(this.saveFileDialog.FileName);
         pLayerFile.ReplaceContents(this.TOCRightLayer);
         pLayerFile.Save();
     }
 }
Ejemplo n.º 2
0
        ///<summary>Write a Layer to a file on disk.</summary>
        ///
        ///<param name="layerFilePath">A System.String that is the path and filename for the layer file to be created. Example: "C:\temp\cities.lyr"</param>
        ///<param name="layer">An ILayer interface.</param>
        ///
        ///<remarks></remarks>
        public static void SaveToLayerFile(string layerFilePath, ILayer layer)
        {
            if (layer == null)
            {
                Log.Warning("No valid layer file was found. Save not completed.");
                return;
            }
            //create a new LayerFile instance
            ESRI.ArcGIS.Carto.ILayerFile layerFile = new LayerFileClass();

            //make sure that the layer file name is valid
            if (Path.GetExtension(layerFilePath) != ".lyr")
            {
                return;
            }
            if (layerFile.get_IsPresent(layerFilePath))
            {
                File.Delete(layerFilePath);
            }

            var fileInfo = new FileInfo(layerFilePath);

            if (!fileInfo.Directory.Exists)
            {
                Log.Information("Creating output directory {OutputDirectory}.", fileInfo.Directory.FullName);
                fileInfo.Directory.Create();
            }

            //create a new layer file
            layerFile.New(layerFilePath);

            //attach the layer file with the actual layer
            layerFile.ReplaceContents(layer);

            //save the layer file
            layerFile.Save();

            layerFile.Close();

            Log.Information("Layer saved.");

            // save output path so that the calling process can get it - useful when unique file name for output
            string tempFile = Path.Combine(Path.GetTempPath(), "Chameleon.txt");

            using (StreamWriter sw = new StreamWriter(tempFile))
            {
                sw.WriteLine(layerFilePath);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Write the NALayer out to disk as a layer file.
 /// </summary>
 private void SaveLayerToDisk(ILayer layer, String path)
 {
     try
     {
         Console.WriteLine("Writing layer file containing analysis to " + path);
         ILayerFile layerFile = new LayerFileClass();
         layerFile.New(path);
         layerFile.ReplaceContents(layer as ILayer);
         layerFile.Save();
         Console.WriteLine("Writing layer file successfully saved");
     }
     catch (Exception err)
     {
         // Write out errors
         Console.WriteLine(err.Message);
     }
 }
        private IFeatureLayer CreateFeatureLayer(esriGeometryType pesriGeometryType, string pstrLayerName,
                                                 List <string> pstrFieldNameLt = null, List <esriFieldType> pesriFieldTypeLt = null,
                                                 int intRed        = _intColor, int intGreen = _intColor, int intBlue = _intColor, double dblWidth = 1,
                                                 int intOutlineRed = _intColor, int intOutlineGreen = _intColor, int intOutlineBlue = _intColor,
                                                 esriSimpleLineStyle pesriSimpleLineStyle = esriSimpleLineStyle.esriSLSSolid,
                                                 esriSimpleFillStyle pesriSimpleFillStyle = esriSimpleFillStyle.esriSFSSolid,
                                                 string strSymbolLayerPath = null, bool blnVisible = true)
        {
            var pWorkspace    = CConstants.ParameterInitialize.pWorkspace;
            var pm_mapControl = CConstants.ParameterInitialize.m_mapControl;

            pstrLayerName += CHelpFunc.GetTimeStampWithPrefix();
            IFeatureClass pFeatureClass = CreateFeatureClass(pesriGeometryType, pstrLayerName, pWorkspace, pm_mapControl,
                                                             pstrFieldNameLt, pesriFieldTypeLt);
            IFeatureLayer pFLayer = new FeatureLayerClass();

            pFLayer.FeatureClass     = pFeatureClass;
            pFLayer.Name             = pFeatureClass.AliasName;
            pFLayer.SpatialReference = pm_mapControl.SpatialReference;

            RenderLayer(ref pFLayer, pesriGeometryType, intRed, intGreen, intBlue, dblWidth,
                        intOutlineRed, intOutlineGreen, intOutlineBlue, pesriSimpleLineStyle, pesriSimpleFillStyle, strSymbolLayerPath);

            //save Layer as layer file ".lyr"
            //create a new LayerFile instance
            ILayerFile layerFile = new LayerFileClass();

            //create a new layer file
            layerFile.New(pWorkspace.PathName + "\\" + pstrLayerName + ".lyr");
            //attach the layer file with the actual layer
            layerFile.ReplaceContents((ILayer)pFLayer);
            //save the layer file
            layerFile.Save();


            //***********************************************是否添加到当前文档中来***********************************************//
            //m_mapControl.AddLayer(pFLayer,m_mapControl .LayerCount);
            pm_mapControl.AddLayer(pFLayer);
            pFLayer.Visible = blnVisible;
            pm_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            return(pFLayer);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add SaveLayerFile.OnClick implementation
            ILayer layer = (ILayer)m_mapControl.CustomProperty;


            //ask the user to set a name for the new layer file
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter           = "Layer File|*.lyr|All Files|*.*";
            saveFileDialog.Title            = "Create Layer File";
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.FileName         = System.IO.Path.Combine(saveFileDialog.InitialDirectory, layer.Name + ".lyr");

            //get the layer name from the user
            DialogResult dr = saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName != "" && dr == DialogResult.OK)
            {
                if (System.IO.File.Exists(saveFileDialog.FileName))
                {
                    //try to delete the existing file
                    System.IO.File.Delete(saveFileDialog.FileName);
                }

                //create a new LayerFile instance
                ILayerFile layerFile = new LayerFileClass();
                //create a new layer file
                layerFile.New(saveFileDialog.FileName);
                //attach the layer file with the actual layer
                layerFile.ReplaceContents(layer);
                //save the layer file
                layerFile.Save();

                //ask the user whether he'd like to add the layer to the map
                if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    m_mapControl.AddLayerFromFile(saveFileDialog.FileName, 0);
                }
            }
        }
Ejemplo n.º 6
0
        private static void SaveLayerFile(string layerFilePath, ILayer layer)
        {
            ILayerFile layerFile = new LayerFileClass();

            if (layerFile.IsPresent[layerFilePath])
            {
                //The user gave permission in the file save dialog to replace the existing file.
                try
                {
                    System.IO.File.Delete(layerFilePath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to overwrite " + layerFilePath + Environment.NewLine + ex.Message);
                    Environment.Exit(4);
                }
            }
            layerFile.New(layerFilePath);
            layerFile.ReplaceContents(layer);
            layerFile.Save();
        }
Ejemplo n.º 7
0
        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 gpUtilities3 = new GPUtilitiesClass();

                if (TrackCancel == null)
                {
                    TrackCancel = new CancelTrackerClass();
                }

                // find feature class inside the given feature dataset
                IGPParameter      osmFeatureDatasetParameter = paramvalues.get_Element(in_osmFeatureDatasetNumber) as IGPParameter;
                IDEFeatureDataset osmFeatureDataset          = gpUtilities3.UnpackGPValue(osmFeatureDatasetParameter) as IDEFeatureDataset;

                string osmPointFeatureClassString   = ((IDataElement)osmFeatureDataset).Name + "_osm_pt";
                string osmLineFeatureClassString    = ((IDataElement)osmFeatureDataset).Name + "_osm_ln";
                string osmPolygonFeatureClassString = ((IDataElement)osmFeatureDataset).Name + "_osm_ply";

                IFeatureClass osmPointFeatureClass   = gpUtilities3.OpenFeatureClassFromString(((IDataElement)osmFeatureDataset).CatalogPath + "/" + osmPointFeatureClassString);
                IFeatureClass osmLineFeatureClass    = gpUtilities3.OpenFeatureClassFromString(((IDataElement)osmFeatureDataset).CatalogPath + "/" + osmLineFeatureClassString);
                IFeatureClass osmPoylgonFeatureClass = gpUtilities3.OpenFeatureClassFromString(((IDataElement)osmFeatureDataset).CatalogPath + "/" + osmPolygonFeatureClassString);

                // open the specified layers holding the symbology and editing templates
                IGPParameter osmPointSymbolTemplateParameter = paramvalues.get_Element(in_osmPointLayerNumber) as IGPParameter;
                IGPValue     osmGPPointLayerValue            = gpUtilities3.UnpackGPValue(osmPointSymbolTemplateParameter);

                IGPParameter outputPointGPParameter  = paramvalues.get_Element(out_osmPointLayerNumber) as IGPParameter;
                IGPValue     outputPointLayerGPValue = gpUtilities3.UnpackGPValue(outputPointGPParameter);

                bool isLayerOnDisk = false;

                // create a clone of the source layer
                // we will then go ahead and adjust the data source (dataset) of the cloned layer
                IObjectCopy     objectCopy = new ObjectCopyClass();
                ICompositeLayer adjustedPointTemplateLayer = objectCopy.Copy(osmGPPointLayerValue) as ICompositeLayer;

                IGPGroupLayer osmPointGroupTemplateLayer = adjustedPointTemplateLayer as IGPGroupLayer;

                ICompositeLayer compositeLayer = gpUtilities3.Open((IGPValue)osmPointGroupTemplateLayer) as ICompositeLayer;
                //ICompositeLayer adjustedPointTemplateLayer = osmGPPointLayerValue as ICompositeLayer;
                //IGPGroupLayer osmPointGroupTemplateLayer = osmGPPointLayerValue as IGPGroupLayer;
                //IClone cloneSource = osmPointGroupTemplateLayer as IClone;
                //ICompositeLayer compositeLayer = m_gpUtilities3.Open((IGPValue)cloneSource.Clone()) as ICompositeLayer;

                if (compositeLayer == null)
                {
                    ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
                    IFileName           layerFileName      = new FileNameClass();

                    layerFileName.Path = osmGPPointLayerValue.GetAsText();
                    IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName((IName)layerFileName);
                    enumLayer.Reset();

                    compositeLayer = enumLayer.Next() as ICompositeLayer;

                    isLayerOnDisk = true;
                }

                IFeatureLayerDefinition2 featureLayerDefinition2 = null;
                ISQLSyntax sqlSyntax = null;

                IGPLayer adjustedPointGPLayer = null;

                if (compositeLayer != null)
                {
                    for (int layerIndex = 0; layerIndex < compositeLayer.Count; layerIndex++)
                    {
                        IFeatureLayer2 geoFeatureLayer = compositeLayer.get_Layer(layerIndex) as IFeatureLayer2;

                        if (geoFeatureLayer != null)
                        {
                            if (geoFeatureLayer.ShapeType == osmPointFeatureClass.ShapeType)
                            {
                                try
                                {
                                    ((IDataLayer2)geoFeatureLayer).Disconnect();
                                }
                                catch { }

                                ((IDataLayer2)geoFeatureLayer).DataSourceName = ((IDataset)osmPointFeatureClass).FullName;

                                ((IDataLayer2)geoFeatureLayer).Connect(((IDataset)osmPointFeatureClass).FullName);

                                featureLayerDefinition2 = geoFeatureLayer as IFeatureLayerDefinition2;
                                if (featureLayerDefinition2 != null)
                                {
                                    string queryDefinition = featureLayerDefinition2.DefinitionExpression;

                                    sqlSyntax = ((IDataset)osmPointFeatureClass).Workspace as ISQLSyntax;
                                    string delimiterIdentifier = sqlSyntax.GetSpecialCharacter(esriSQLSpecialCharacters.esriSQL_DelimitedIdentifierPrefix);

                                    if (String.IsNullOrEmpty(queryDefinition) == false)
                                    {
                                        string stringToReplace = queryDefinition.Substring(0, 1);
                                        queryDefinition = queryDefinition.Replace(stringToReplace, delimiterIdentifier);
                                    }

                                    featureLayerDefinition2.DefinitionExpression = queryDefinition;
                                }
                            }
                        }
                    }

                    adjustedPointGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);

                    // save the newly adjusted layer information to disk
                    if (isLayerOnDisk == true)
                    {
                        ILayerFile pointLayerFile = new LayerFileClass();
                        if (pointLayerFile.get_IsPresent(outputPointLayerGPValue.GetAsText()))
                        {
                            try
                            {
                                File.Delete(outputPointLayerGPValue.GetAsText());
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120041, ex.Message);
                                return;
                            }
                        }

                        pointLayerFile.New(outputPointLayerGPValue.GetAsText());

                        pointLayerFile.ReplaceContents((ILayer)compositeLayer);

                        pointLayerFile.Save();

                        adjustedPointGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)pointLayerFile.Layer);
                    }

                    //   IGPLayer adjustedPointGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);
                    gpUtilities3.AddInternalLayer2((ILayer)compositeLayer, adjustedPointGPLayer);
                    gpUtilities3.PackGPValue((IGPValue)adjustedPointGPLayer, outputPointGPParameter);
                }


                isLayerOnDisk = false;

                IGPParameter osmLineSymbolTemplateParameter = paramvalues.get_Element(in_osmLineLayerNumber) as IGPParameter;
                IGPValue     osmGPLineLayerValue            = gpUtilities3.UnpackGPValue(osmLineSymbolTemplateParameter) as IGPValue;

                IGPParameter outputLineGPParameter  = paramvalues.get_Element(out_osmLineLayerNumber) as IGPParameter;
                IGPValue     outputLineLayerGPValue = gpUtilities3.UnpackGPValue(outputLineGPParameter);

                IGPValue adjustedLineTemplateLayer = objectCopy.Copy(osmGPLineLayerValue) as IGPValue;

                IGPGroupLayer osmLineGroupTemplateLayer = adjustedLineTemplateLayer as IGPGroupLayer;

                compositeLayer = gpUtilities3.Open((IGPValue)osmLineGroupTemplateLayer) as ICompositeLayer;

                if (compositeLayer == null)
                {
                    ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
                    IFileName           layerFileName      = new FileNameClass();

                    layerFileName.Path = osmGPLineLayerValue.GetAsText();
                    IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName((IName)layerFileName);
                    enumLayer.Reset();

                    compositeLayer = enumLayer.Next() as ICompositeLayer;

                    isLayerOnDisk = true;
                }


                if (compositeLayer != null)
                {
                    for (int layerIndex = 0; layerIndex < compositeLayer.Count; layerIndex++)
                    {
                        IFeatureLayer2 geoFeatureLayer = compositeLayer.get_Layer(layerIndex) as IFeatureLayer2;
                        if (geoFeatureLayer.ShapeType == osmLineFeatureClass.ShapeType)
                        {
                            try
                            {
                                ((IDataLayer2)geoFeatureLayer).Disconnect();
                            }
                            catch { }
                            ((IDataLayer2)geoFeatureLayer).DataSourceName = ((IDataset)osmLineFeatureClass).FullName;
                            ((IDataLayer2)geoFeatureLayer).Connect(((IDataset)osmLineFeatureClass).FullName);

                            featureLayerDefinition2 = geoFeatureLayer as IFeatureLayerDefinition2;
                            if (featureLayerDefinition2 != null)
                            {
                                string queryDefinition = featureLayerDefinition2.DefinitionExpression;

                                sqlSyntax = ((IDataset)osmLineFeatureClass).Workspace as ISQLSyntax;
                                string delimiterIdentifier = sqlSyntax.GetSpecialCharacter(esriSQLSpecialCharacters.esriSQL_DelimitedIdentifierPrefix);

                                if (string.IsNullOrEmpty(queryDefinition) == false)
                                {
                                    string stringToReplace = queryDefinition.Substring(0, 1);
                                    queryDefinition = queryDefinition.Replace(stringToReplace, delimiterIdentifier);
                                }

                                featureLayerDefinition2.DefinitionExpression = queryDefinition;
                            }
                        }
                    }

                    // save the newly adjusted layer information to disk
                    if (isLayerOnDisk == true)
                    {
                        ILayerFile lineLayerFile = new LayerFileClass();
                        if (lineLayerFile.get_IsPresent(outputLineLayerGPValue.GetAsText()))
                        {
                            try
                            {
                                File.Delete(outputLineLayerGPValue.GetAsText());
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120042, ex.Message);
                                return;
                            }
                        }

                        lineLayerFile.New(outputLineLayerGPValue.GetAsText());

                        lineLayerFile.ReplaceContents((ILayer)compositeLayer);

                        lineLayerFile.Save();
                    }

                    IGPLayer adjustLineGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);

                    gpUtilities3.AddInternalLayer2((ILayer)compositeLayer, adjustLineGPLayer);
                    gpUtilities3.PackGPValue((IGPValue)adjustLineGPLayer, outputLineGPParameter);
                }


                isLayerOnDisk = false;
                IGPParameter osmPolygonSymbolTemplateParameter = paramvalues.get_Element(in_osmPolygonLayerNumber) as IGPParameter;
                IGPValue     osmGPPolygonLayerValue            = gpUtilities3.UnpackGPValue(osmPolygonSymbolTemplateParameter);

                IGPParameter outputPolygonGPParameter  = paramvalues.get_Element(out_osmPolygonLayerNumber) as IGPParameter;
                IGPValue     outputPolygonLayerGPValue = gpUtilities3.UnpackGPValue(outputPolygonGPParameter);

                IGPValue adjustedPolygonTemplateLayer = objectCopy.Copy(osmGPPolygonLayerValue) as IGPValue;

                IGPGroupLayer osmPolygonGroupTemplateLayer = adjustedPolygonTemplateLayer as IGPGroupLayer;
                compositeLayer = gpUtilities3.Open((IGPValue)osmPolygonGroupTemplateLayer) as ICompositeLayer;

                if (compositeLayer == null)
                {
                    ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
                    IFileName           layerFileName      = new FileNameClass();

                    layerFileName.Path = osmGPPolygonLayerValue.GetAsText();
                    IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName((IName)layerFileName);
                    enumLayer.Reset();

                    compositeLayer = enumLayer.Next() as ICompositeLayer;

                    isLayerOnDisk = true;
                }

                if (compositeLayer != null)
                {
                    for (int layerIndex = 0; layerIndex < compositeLayer.Count; layerIndex++)
                    {
                        IFeatureLayer2 geoFeatureLayer = compositeLayer.get_Layer(layerIndex) as IFeatureLayer2;

                        if (geoFeatureLayer.ShapeType == osmPoylgonFeatureClass.ShapeType)
                        {
                            try
                            {
                                ((IDataLayer2)geoFeatureLayer).Disconnect();
                            }
                            catch { }
                            ((IDataLayer2)geoFeatureLayer).DataSourceName = ((IDataset)osmPoylgonFeatureClass).FullName;
                            ((IDataLayer2)geoFeatureLayer).Connect(((IDataset)osmPoylgonFeatureClass).FullName);

                            featureLayerDefinition2 = geoFeatureLayer as IFeatureLayerDefinition2;
                            if (featureLayerDefinition2 != null)
                            {
                                string queryDefinition = featureLayerDefinition2.DefinitionExpression;

                                sqlSyntax = ((IDataset)osmPoylgonFeatureClass).Workspace as ISQLSyntax;
                                string delimiterIdentifier = sqlSyntax.GetSpecialCharacter(esriSQLSpecialCharacters.esriSQL_DelimitedIdentifierPrefix);

                                if (String.IsNullOrEmpty(queryDefinition) == false)
                                {
                                    string stringToReplace = queryDefinition.Substring(0, 1);
                                    queryDefinition = queryDefinition.Replace(stringToReplace, delimiterIdentifier);
                                }

                                featureLayerDefinition2.DefinitionExpression = queryDefinition;
                            }
                        }
                    }

                    // save the newly adjusted layer information to disk
                    if (isLayerOnDisk == true)
                    {
                        ILayerFile polygonLayerFile = new LayerFileClass();
                        if (polygonLayerFile.get_IsPresent(outputPolygonLayerGPValue.GetAsText()))
                        {
                            try
                            {
                                File.Delete(outputPolygonLayerGPValue.GetAsText());
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120043, ex.Message);
                                return;
                            }
                        }

                        polygonLayerFile.New(outputPolygonLayerGPValue.GetAsText());

                        polygonLayerFile.ReplaceContents((ILayer)compositeLayer);

                        polygonLayerFile.Save();
                    }

                    IGPLayer adjustedPolygonGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);
                    gpUtilities3.AddInternalLayer2((ILayer)compositeLayer, adjustedPolygonGPLayer);

                    gpUtilities3.PackGPValue((IGPValue)adjustedPolygonGPLayer, outputPolygonGPParameter);
                }
            }
            catch (Exception ex)
            {
                message.AddError(-10, ex.Message);
            }
        }
Ejemplo n.º 8
0
 private static void SaveLayerFile(string layerFilePath, ILayer layer)
 {
     ILayerFile layerFile = new LayerFileClass();
     if (layerFile.IsPresent[layerFilePath])
     {
         //The user gave permission in the file save dialog to replace the existing file.
         try
         {
             System.IO.File.Delete(layerFilePath);
         }
         catch (Exception ex)
         {
             Console.WriteLine("Unable to overwrite " + layerFilePath + Environment.NewLine + ex.Message);
             Environment.Exit(4);
         }
     }
     layerFile.New(layerFilePath);
     layerFile.ReplaceContents(layer);
     layerFile.Save();
 }
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            //need to get the layer from the custom-property of the map
            if (null == m_hookHelper)
            {
                return;
            }

            //get the mapControl hook
            object hook = null;

            if (m_hookHelper.Hook is IToolbarControl2)
            {
                hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy;
            }
            else
            {
                hook = m_hookHelper.Hook;
            }

            //get the custom property from which is supposed to be the layer to be saved
            object       customProperty = null;
            IMapControl3 mapControl     = null;

            if (hook is IMapControl3)
            {
                mapControl     = (IMapControl3)hook;
                customProperty = mapControl.CustomProperty;
            }
            else
            {
                return;
            }

            if (null == customProperty || !(customProperty is ILayer))
            {
                return;
            }

            //ask the user to set a name for the new layer file
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter           = "Layer File|*.lyr|All Files|*.*";
            saveFileDialog.Title            = "Create Layer File";
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.FileName         = System.IO.Path.Combine(saveFileDialog.InitialDirectory, ((ILayer)customProperty).Name + ".lyr");

            //get the layer name from the user
            DialogResult dr = saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName != "" && dr == DialogResult.OK)
            {
                if (System.IO.File.Exists(saveFileDialog.FileName))
                {
                    //try to delete the existing file
                    System.IO.File.Delete(saveFileDialog.FileName);
                }

                //create a new LayerFile instance
                ILayerFile layerFile = new LayerFileClass();
                //create a new layer file
                layerFile.New(saveFileDialog.FileName);
                //attach the layer file with the actual layer
                layerFile.ReplaceContents((ILayer)customProperty);
                //save the layer file
                layerFile.Save();

                //ask the user whether he'd like to add the layer to the map
                if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    mapControl.AddLayerFromFile(saveFileDialog.FileName, 0);
                }
            }
        }
		/// <summary>
		/// Write the NALayer out to disk as a layer file.
		/// </summary>
		private void SaveLayerToDisk(ILayer layer, String path)
		{
			try
			{
				Console.WriteLine("Writing layer file containing analysis to " + path);
				ILayerFile layerFile = new LayerFileClass();
				layerFile.New(path);
				layerFile.ReplaceContents(layer as ILayer);
				layerFile.Save();
				Console.WriteLine("Writing layer file successfully saved");
			}
			catch (Exception err)
			{
				// Write out errors
				Console.WriteLine(err.Message);
			}
		}
Ejemplo n.º 11
0
        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 gpUtilities3 = new GPUtilitiesClass();

                if (TrackCancel == null)
                {
                    TrackCancel = new CancelTrackerClass();
                }

                // find feature class inside the given feature dataset
                IGPParameter osmFeatureDatasetParameter = paramvalues.get_Element(in_osmFeatureDatasetNumber) as IGPParameter;
                IDEFeatureDataset osmFeatureDataset = gpUtilities3.UnpackGPValue(osmFeatureDatasetParameter) as IDEFeatureDataset;

                string osmPointFeatureClassString = ((IDataElement)osmFeatureDataset).Name + "_osm_pt";
                string osmLineFeatureClassString = ((IDataElement)osmFeatureDataset).Name + "_osm_ln";
                string osmPolygonFeatureClassString = ((IDataElement)osmFeatureDataset).Name + "_osm_ply";

                IFeatureClass osmPointFeatureClass = gpUtilities3.OpenFeatureClassFromString(((IDataElement)osmFeatureDataset).CatalogPath + "/" + osmPointFeatureClassString);
                IFeatureClass osmLineFeatureClass = gpUtilities3.OpenFeatureClassFromString(((IDataElement)osmFeatureDataset).CatalogPath + "/" + osmLineFeatureClassString);
                IFeatureClass osmPoylgonFeatureClass = gpUtilities3.OpenFeatureClassFromString(((IDataElement)osmFeatureDataset).CatalogPath + "/" + osmPolygonFeatureClassString);

                // open the specified layers holding the symbology and editing templates
                IGPParameter osmPointSymbolTemplateParameter = paramvalues.get_Element(in_osmPointLayerNumber) as IGPParameter;
                IGPValue osmGPPointLayerValue = gpUtilities3.UnpackGPValue(osmPointSymbolTemplateParameter);

                IGPParameter outputPointGPParameter = paramvalues.get_Element(out_osmPointLayerNumber) as IGPParameter;
                IGPValue outputPointLayerGPValue = gpUtilities3.UnpackGPValue(outputPointGPParameter);

                bool isLayerOnDisk = false;

                // create a clone of the source layer
                // we will then go ahead and adjust the data source (dataset) of the cloned layer
                IObjectCopy objectCopy = new ObjectCopyClass();
                ICompositeLayer adjustedPointTemplateLayer = objectCopy.Copy(osmGPPointLayerValue) as ICompositeLayer;

                IGPGroupLayer osmPointGroupTemplateLayer = adjustedPointTemplateLayer as IGPGroupLayer;

                ICompositeLayer compositeLayer = gpUtilities3.Open((IGPValue)osmPointGroupTemplateLayer) as ICompositeLayer;
                //ICompositeLayer adjustedPointTemplateLayer = osmGPPointLayerValue as ICompositeLayer;
                //IGPGroupLayer osmPointGroupTemplateLayer = osmGPPointLayerValue as IGPGroupLayer;
                //IClone cloneSource = osmPointGroupTemplateLayer as IClone;
                //ICompositeLayer compositeLayer = m_gpUtilities3.Open((IGPValue)cloneSource.Clone()) as ICompositeLayer;

                if (compositeLayer == null)
                {
                    ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
                    IFileName layerFileName = new FileNameClass();

                    layerFileName.Path = osmGPPointLayerValue.GetAsText();
                    IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName((IName)layerFileName);
                    enumLayer.Reset();

                    compositeLayer = enumLayer.Next() as ICompositeLayer;

                    isLayerOnDisk = true;
                }

                IFeatureLayerDefinition2 featureLayerDefinition2 = null;
                ISQLSyntax sqlSyntax = null;

                IGPLayer adjustedPointGPLayer = null;

                if (compositeLayer != null)
                {
                    for (int layerIndex = 0; layerIndex < compositeLayer.Count; layerIndex++)
                    {

                        IFeatureLayer2 geoFeatureLayer = compositeLayer.get_Layer(layerIndex) as IFeatureLayer2;

                        if (geoFeatureLayer != null)
                        {
                            if (geoFeatureLayer.ShapeType == osmPointFeatureClass.ShapeType)
                            {
                                try
                                {
                                    ((IDataLayer2)geoFeatureLayer).Disconnect();
                                }
                                catch { }

                                ((IDataLayer2)geoFeatureLayer).DataSourceName = ((IDataset)osmPointFeatureClass).FullName;

                                ((IDataLayer2)geoFeatureLayer).Connect(((IDataset)osmPointFeatureClass).FullName);

                                featureLayerDefinition2 = geoFeatureLayer as IFeatureLayerDefinition2;
                                if (featureLayerDefinition2 != null)
                                {
                                    string queryDefinition = featureLayerDefinition2.DefinitionExpression;

                                    sqlSyntax = ((IDataset)osmPointFeatureClass).Workspace as ISQLSyntax;
                                    string delimiterIdentifier = sqlSyntax.GetSpecialCharacter(esriSQLSpecialCharacters.esriSQL_DelimitedIdentifierPrefix);

                                    if (String.IsNullOrEmpty(queryDefinition) == false)
                                    {
                                        string stringToReplace = queryDefinition.Substring(0, 1);
                                        queryDefinition = queryDefinition.Replace(stringToReplace, delimiterIdentifier);
                                    }

                                    featureLayerDefinition2.DefinitionExpression = queryDefinition;
                                }
                            }
                        }
                    }

                    adjustedPointGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);

                    // save the newly adjusted layer information to disk
                    if (isLayerOnDisk == true)
                    {
                        ILayerFile pointLayerFile = new LayerFileClass();
                        if (pointLayerFile.get_IsPresent(outputPointLayerGPValue.GetAsText()))
                        {
                            try
                            {
                                File.Delete(outputPointLayerGPValue.GetAsText());
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120041,ex.Message);
                                return;
                            }
                        }

                        pointLayerFile.New(outputPointLayerGPValue.GetAsText());

                        pointLayerFile.ReplaceContents((ILayer)compositeLayer);

                        pointLayerFile.Save();

                        adjustedPointGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)pointLayerFile.Layer);
                    }

                 //   IGPLayer adjustedPointGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);
                    gpUtilities3.AddInternalLayer2((ILayer)compositeLayer, adjustedPointGPLayer);
                    gpUtilities3.PackGPValue((IGPValue)adjustedPointGPLayer, outputPointGPParameter);

                }

                isLayerOnDisk = false;

                IGPParameter osmLineSymbolTemplateParameter = paramvalues.get_Element(in_osmLineLayerNumber) as IGPParameter;
                IGPValue osmGPLineLayerValue = gpUtilities3.UnpackGPValue(osmLineSymbolTemplateParameter) as IGPValue;

                IGPParameter outputLineGPParameter = paramvalues.get_Element(out_osmLineLayerNumber) as IGPParameter;
                IGPValue outputLineLayerGPValue = gpUtilities3.UnpackGPValue(outputLineGPParameter);

                IGPValue adjustedLineTemplateLayer = objectCopy.Copy(osmGPLineLayerValue) as IGPValue;

                IGPGroupLayer osmLineGroupTemplateLayer = adjustedLineTemplateLayer as IGPGroupLayer;

                compositeLayer = gpUtilities3.Open((IGPValue)osmLineGroupTemplateLayer) as ICompositeLayer;

                if (compositeLayer == null)
                {
                    ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
                    IFileName layerFileName = new FileNameClass();

                    layerFileName.Path = osmGPLineLayerValue.GetAsText();
                    IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName((IName)layerFileName);
                    enumLayer.Reset();

                    compositeLayer = enumLayer.Next() as ICompositeLayer;

                    isLayerOnDisk = true;
                }

                if (compositeLayer != null)
                {
                    for (int layerIndex = 0; layerIndex < compositeLayer.Count; layerIndex++)
                    {
                        IFeatureLayer2 geoFeatureLayer = compositeLayer.get_Layer(layerIndex) as IFeatureLayer2;
                        if (geoFeatureLayer.ShapeType == osmLineFeatureClass.ShapeType)
                        {
                            try
                            {
                                ((IDataLayer2)geoFeatureLayer).Disconnect();
                            }
                            catch { }
                            ((IDataLayer2)geoFeatureLayer).DataSourceName = ((IDataset)osmLineFeatureClass).FullName;
                            ((IDataLayer2)geoFeatureLayer).Connect(((IDataset)osmLineFeatureClass).FullName);

                            featureLayerDefinition2 = geoFeatureLayer as IFeatureLayerDefinition2;
                            if (featureLayerDefinition2 != null)
                            {
                                string queryDefinition = featureLayerDefinition2.DefinitionExpression;

                                sqlSyntax = ((IDataset)osmLineFeatureClass).Workspace as ISQLSyntax;
                                string delimiterIdentifier = sqlSyntax.GetSpecialCharacter(esriSQLSpecialCharacters.esriSQL_DelimitedIdentifierPrefix);

                                if (string.IsNullOrEmpty(queryDefinition) == false)
                                {
                                    string stringToReplace = queryDefinition.Substring(0, 1);
                                    queryDefinition = queryDefinition.Replace(stringToReplace, delimiterIdentifier);
                                }

                                featureLayerDefinition2.DefinitionExpression = queryDefinition;
                            }
                        }
                    }

                    // save the newly adjusted layer information to disk
                    if (isLayerOnDisk == true)
                    {
                        ILayerFile lineLayerFile = new LayerFileClass();
                        if (lineLayerFile.get_IsPresent(outputLineLayerGPValue.GetAsText()))
                        {
                            try
                            {
                                File.Delete(outputLineLayerGPValue.GetAsText());
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120042, ex.Message);
                                return;
                            }
                        }

                        lineLayerFile.New(outputLineLayerGPValue.GetAsText());

                        lineLayerFile.ReplaceContents((ILayer)compositeLayer);

                        lineLayerFile.Save();
                    }

                    IGPLayer adjustLineGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);

                    gpUtilities3.AddInternalLayer2((ILayer)compositeLayer, adjustLineGPLayer);
                    gpUtilities3.PackGPValue((IGPValue)adjustLineGPLayer, outputLineGPParameter);
                }

                isLayerOnDisk = false;
                IGPParameter osmPolygonSymbolTemplateParameter = paramvalues.get_Element(in_osmPolygonLayerNumber) as IGPParameter;
                IGPValue osmGPPolygonLayerValue = gpUtilities3.UnpackGPValue(osmPolygonSymbolTemplateParameter);

                IGPParameter outputPolygonGPParameter = paramvalues.get_Element(out_osmPolygonLayerNumber) as IGPParameter;
                IGPValue outputPolygonLayerGPValue = gpUtilities3.UnpackGPValue(outputPolygonGPParameter);

                IGPValue adjustedPolygonTemplateLayer = objectCopy.Copy(osmGPPolygonLayerValue) as IGPValue;

                IGPGroupLayer osmPolygonGroupTemplateLayer = adjustedPolygonTemplateLayer as IGPGroupLayer;
                compositeLayer = gpUtilities3.Open((IGPValue)osmPolygonGroupTemplateLayer) as ICompositeLayer;

                if (compositeLayer == null)
                {
                    ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
                    IFileName layerFileName = new FileNameClass();

                    layerFileName.Path = osmGPPolygonLayerValue.GetAsText();
                    IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName((IName)layerFileName);
                    enumLayer.Reset();

                    compositeLayer = enumLayer.Next() as ICompositeLayer;

                    isLayerOnDisk = true;
                }

                if (compositeLayer != null)
                {
                    for (int layerIndex = 0; layerIndex < compositeLayer.Count; layerIndex++)
                    {
                        IFeatureLayer2 geoFeatureLayer = compositeLayer.get_Layer(layerIndex) as IFeatureLayer2;

                        if (geoFeatureLayer.ShapeType == osmPoylgonFeatureClass.ShapeType)
                        {
                            try
                            {
                                ((IDataLayer2)geoFeatureLayer).Disconnect();
                            }
                            catch { }
                            ((IDataLayer2)geoFeatureLayer).DataSourceName = ((IDataset)osmPoylgonFeatureClass).FullName;
                            ((IDataLayer2)geoFeatureLayer).Connect(((IDataset)osmPoylgonFeatureClass).FullName);

                            featureLayerDefinition2 = geoFeatureLayer as IFeatureLayerDefinition2;
                            if (featureLayerDefinition2 != null)
                            {
                                string queryDefinition = featureLayerDefinition2.DefinitionExpression;

                                sqlSyntax = ((IDataset)osmPoylgonFeatureClass).Workspace as ISQLSyntax;
                                string delimiterIdentifier = sqlSyntax.GetSpecialCharacter(esriSQLSpecialCharacters.esriSQL_DelimitedIdentifierPrefix);

                                if (String.IsNullOrEmpty(queryDefinition) == false)
                                {
                                    string stringToReplace = queryDefinition.Substring(0, 1);
                                    queryDefinition = queryDefinition.Replace(stringToReplace, delimiterIdentifier);
                                }

                                featureLayerDefinition2.DefinitionExpression = queryDefinition;
                            }
                        }
                    }

                    // save the newly adjusted layer information to disk
                    if (isLayerOnDisk == true)
                    {
                        ILayerFile polygonLayerFile = new LayerFileClass();
                        if (polygonLayerFile.get_IsPresent(outputPolygonLayerGPValue.GetAsText()))
                        {
                            try
                            {
                                File.Delete(outputPolygonLayerGPValue.GetAsText());
                            }
                            catch (Exception ex)
                            {
                                message.AddError(120043, ex.Message);
                                return;
                            }
                        }

                        polygonLayerFile.New(outputPolygonLayerGPValue.GetAsText());

                        polygonLayerFile.ReplaceContents((ILayer)compositeLayer);

                        polygonLayerFile.Save();
                    }

                    IGPLayer adjustedPolygonGPLayer = gpUtilities3.MakeGPLayerFromLayer((ILayer)compositeLayer);
                    gpUtilities3.AddInternalLayer2((ILayer)compositeLayer, adjustedPolygonGPLayer);

                    gpUtilities3.PackGPValue((IGPValue)adjustedPolygonGPLayer, outputPolygonGPParameter);
                }
            }
            catch (Exception ex)
            {
                message.AddError(-10,ex.Message);
            }
        }
Ejemplo n.º 12
0
        void Button_Click(object sender, EventArgs e)
        {
            #region 识别
            if ((Button)sender == btn_Identify)
            {
                m_isIdentify = !m_isIdentify;
                if (m_isIdentify)
                {
                    m_isDraw              = false;
                    m_isAddpoint          = false;
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerIdentify;
                }
                else
                {
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerArrow;
                }
                return;
            }
            #endregion
            #region  择
            if ((Button)sender == btn_select)
            {
                m_isDraw = !m_isDraw;
                if (m_isDraw)
                {
                    m_isIdentify          = false;
                    m_isAddpoint          = false;
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
                }
                else
                {
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerArrow;
                }
                return;
            }
            #endregion
            #region 链接
            if ((Button)sender == btn_link)
            {
                ;

                Geoprocessor gp = new Geoprocessor()
                {
                    OverwriteOutput = true
                };
                string table = debugDir + "\\AirQuality\\kqzlzk.dbf";
                ESRI.ArcGIS.ConversionTools.ExcelToTable pExcelToTavle = new ESRI.ArcGIS.ConversionTools.ExcelToTable()
                {
                    Input_Excel_File = debugDir + "\\AirQuality\\空气质量状况.xls",
                    Output_Table     = table,
                    Sheet            = "Sheet1"
                };
                gp.Execute(pExcelToTavle, null);

                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "监测站");

                ESRI.ArcGIS.DataManagementTools.JoinField pJoinField = new ESRI.ArcGIS.DataManagementTools.JoinField()
                {
                    in_data    = pFeatureLayer,
                    in_field   = "Name",
                    join_table = table,
                    join_field = "NAME",
                    fields     = "StationID;PM2_5;SO2;NO2"
                };
                gp.Execute(pJoinField, null);
            }
            #endregion
            #region 注记
            if ((Button)sender == btn_anno)
            {
                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界");
                AeUtils.TextElementLabel(m_pMapC2, pFeatureLayer, "NAME");
            }
            #endregion
            #region 符号化
            if ((Button)sender == btn_symbol)
            {
                IFeatureLayer pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界");
                AeUtils.UniqueValueRenderer(pFeatureLayer, "NAME", m_pMapC2, axTOCControl_main);
            }
            #endregion
            #region 保存图层
            if ((Button)sender == btn_outlyr)
            {
                IFeatureLayer  pFeatureLayer = AeUtils.GetFeatureLayerByName(m_pMapC2.Map, "北京区县界");
                SaveFileDialog sfg           = new SaveFileDialog()
                {
                    Title  = "保存图层",
                    Filter = "图层文件 (*.lyr)|*.lyr"
                };
                if (sfg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ILayerFile layerfile = new LayerFileClass();
                    layerfile.New(sfg.FileName);
                    layerfile.ReplaceContents(pFeatureLayer);
                    layerfile.Save();
                }
            }
            #endregion
            if ((Button)sender == btn_addpt)
            {
                m_isAddpoint = !m_isAddpoint;
                if (m_isAddpoint)
                {
                    m_isDraw              = false;
                    m_isIdentify          = false;
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
                }
                else
                {
                    m_pMapC2.MousePointer = esriControlsMousePointer.esriPointerArrow;
                }
            }
        }
Ejemplo n.º 13
0
 //保存图层
 private void ctMenuSaveAsLyr_Click(object sender, EventArgs e)
 {
     this.saveFileDialog.Title = "保存为Lyr文件";
     this.saveFileDialog.Filter = "Lyr文件|*.lyr";
     if (this.saveFileDialog.ShowDialog() == DialogResult.OK)
     {
         string direction = System.IO.Path.GetDirectoryName(this.saveFileDialog.FileName);
         string file = System.IO.Path.GetFileName(this.saveFileDialog.FileName);
         ILayerFile pLayerFile = new LayerFileClass();
         pLayerFile.New(this.saveFileDialog.FileName);
         pLayerFile.ReplaceContents(this.TOCRightLayer);
         pLayerFile.Save();
     }
 }
Ejemplo n.º 14
0
        private void ClipArea(ILayer pLayer, int index)
        {
            ISpatialFilter pFilter = new SpatialFilterClass();

            pFilter.Geometry      = m_Geometry;
            pFilter.GeometryField = "SHAPE";
            pFilter.SpatialRel    = esriSpatialRelEnum.esriSpatialRelIntersects;

            IFeatureClass pFlnClass;
            //  IFields plnFCFields;
            IFeatureCursor plnCursor;
            IFeature       pFeature;

            IFeatureClass  pFOutClass;
            IFeatureCursor pOutCur;
            //IFeatureCursor pOutFeatureCur;
            IFeatureBuffer pOutBuff;
            IFeature       pOutFeature;

            //裁剪
            ITopologicalOperator pTopo;
            //int indexl;
            // int i;
            IGeometry pGeom;
            IFields   plnFields;

            if (pLayer is IFeatureLayer)
            {
                IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                pFlnClass = pFeatureLayer.FeatureClass;
                plnCursor = pFlnClass.Search(pFilter, false);
                plnFields = pFlnClass.Fields;
                IFeatureLayer pNewFeatrueLayer = null;
                try
                {
                    pNewFeatrueLayer = CreateFeatureLayerInmemeory(pLayer.Name, pLayer.Name + index.ToString(), m_hookHelper.FocusMap.SpatialReference, pFlnClass.ShapeType, plnFields);
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
                pFOutClass = pNewFeatrueLayer.FeatureClass;
                pFeature   = plnCursor.NextFeature();
                while (pFeature != null)
                {
                    pOutCur     = pFOutClass.Insert(true);
                    pOutBuff    = pFOutClass.CreateFeatureBuffer();
                    pOutFeature = pOutBuff as IFeature;

                    // indexl = pFeature.Fields.FindField("Shape");
                    pTopo = pFeature.Shape as ITopologicalOperator;//这里判断一下该地物几何类型
                    pGeom = pFeature.Shape;
                    if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
                    {
                        pGeom = pTopo.Intersect(m_Geometry, esriGeometryDimension.esriGeometry0Dimension); //这里的第二个参数就是区分点、线、面的
                    }
                    else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                    {
                        pGeom = pTopo.Intersect(m_Geometry, esriGeometryDimension.esriGeometry1Dimension);
                    }
                    else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        pGeom = pTopo.Intersect(m_Geometry, esriGeometryDimension.esriGeometry2Dimension);
                    }
                    pOutFeature.Shape = pGeom;

                    CopyAttributes(pFeature, pOutFeature);
                    pOutCur.InsertFeature(pOutBuff);
                    pNewFeatrueLayer.FeatureClass = pFOutClass;

                    ///////////////////////////////////
                    string     FilePath  = @"C:\任意打印\";
                    ILayerFile layerFile = new LayerFileClass();
                    layerFile.New(FilePath + pLayer.Name + ".lyr");
                    layerFile.ReplaceContents(pNewFeatrueLayer as ILayer);
                    layerFile.Save();

                    /////////////////////////////////


                    try
                    {
                        m_ClipMap.AddLayer(layerFile.Layer);
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.ToString());
                    }
                    pFeature = plnCursor.NextFeature();
                }
            }
            else if (pLayer is IRasterLayer)
            {
            }
        }
Ejemplo n.º 15
0
        private void ClipArea(ILayer pLayer,int index)
        {
            ISpatialFilter pFilter = new SpatialFilterClass();
            pFilter.Geometry = m_Geometry;
            pFilter.GeometryField = "SHAPE";
            pFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

            IFeatureClass pFlnClass;
              //  IFields plnFCFields;
            IFeatureCursor plnCursor;
            IFeature pFeature;

            IFeatureClass pFOutClass;
            IFeatureCursor pOutCur;
            //IFeatureCursor pOutFeatureCur;
            IFeatureBuffer pOutBuff;
            IFeature pOutFeature;

            //裁剪
            ITopologicalOperator pTopo;
            //int indexl;
            // int i;
            IGeometry pGeom;
            IFields plnFields;

            if (pLayer is IFeatureLayer)
            {
                IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                pFlnClass = pFeatureLayer.FeatureClass;
                plnCursor = pFlnClass.Search(pFilter, false);
                plnFields = pFlnClass.Fields;
                IFeatureLayer pNewFeatrueLayer = null;
                try
                {
                    pNewFeatrueLayer = CreateFeatureLayerInmemeory(pLayer.Name, pLayer .Name +index .ToString (),m_hookHelper .FocusMap .SpatialReference , pFlnClass.ShapeType, plnFields);
                }
                catch(Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
                pFOutClass = pNewFeatrueLayer.FeatureClass;
                pFeature = plnCursor.NextFeature();
                while (pFeature != null)
                {
                    pOutCur = pFOutClass.Insert(true);
                    pOutBuff = pFOutClass.CreateFeatureBuffer();
                    pOutFeature = pOutBuff as IFeature;

                   // indexl = pFeature.Fields.FindField("Shape");
                    pTopo = pFeature.Shape as ITopologicalOperator;//这里判断一下该地物几何类型
                    pGeom = pFeature.Shape;
                    if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
                    {
                        pGeom = pTopo.Intersect(m_Geometry, esriGeometryDimension.esriGeometry0Dimension); //这里的第二个参数就是区分点、线、面的

                    }
                    else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                    {
                        pGeom = pTopo.Intersect(m_Geometry, esriGeometryDimension.esriGeometry1Dimension);
                    }
                    else if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        pGeom = pTopo.Intersect(m_Geometry, esriGeometryDimension.esriGeometry2Dimension);

                    }
                    pOutFeature.Shape = pGeom;

                    CopyAttributes(pFeature, pOutFeature);
                    pOutCur.InsertFeature(pOutBuff);
                    pNewFeatrueLayer.FeatureClass = pFOutClass;

                    ///////////////////////////////////
                    string FilePath = @"C:\任意打印\";
                    ILayerFile layerFile = new LayerFileClass();
                    layerFile.New(FilePath + pLayer .Name+ ".lyr");
                    layerFile.ReplaceContents(pNewFeatrueLayer as ILayer);
                    layerFile.Save();

                    /////////////////////////////////

                    try
                    {
                        m_ClipMap.AddLayer(layerFile.Layer );
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.ToString());
                    }
                    pFeature = plnCursor.NextFeature();

                }

            }
            else if (pLayer is IRasterLayer)
            {

            }
        }
Ejemplo n.º 16
0
        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 gpUtilities3 = new GPUtilitiesClass();

                if (TrackCancel == null)
                {
                    TrackCancel = new CancelTrackerClass();
                }

                IGPValue inputLayersGPValue = gpUtilities3.UnpackGPValue(paramvalues.get_Element(in_LayersNumber));
                IGPMultiValue inputLayersMultiValue = gpUtilities3.UnpackGPValue(inputLayersGPValue) as IGPMultiValue;

                IGPParameter outputGroupLayerParameter = paramvalues.get_Element(out_groupLayerNumber) as IGPParameter;
                IGPValue outputGPGroupLayer = gpUtilities3.UnpackGPValue(outputGroupLayerParameter);
                IGPCompositeLayer outputCompositeLayer = outputGPGroupLayer as IGPCompositeLayer;

                if (outputCompositeLayer == null)
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), outputGroupLayerParameter.Name));
                    return;
                }

                IGroupLayer groupLayer = null;

                // find the last position of the "\" string
                // in case we find such a thing, i.e. position is >= -1 then let's assume that we are dealing with a layer file on disk
                // otherwise let's create a new group layer instance
                string outputGPLayerNameAsString = outputGPGroupLayer.GetAsText();
                int separatorPosition = outputGPLayerNameAsString.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
                string layerName = String.Empty;

                if (separatorPosition > -1)
                {
                    layerName = outputGPGroupLayer.GetAsText().Substring(separatorPosition + 1);
                }
                else
                {
                    layerName = outputGPGroupLayer.GetAsText();
                }

                ILayer foundLayer = null;
                IGPLayer existingGPLayer = gpUtilities3.FindMapLayer2(layerName, out foundLayer);

                if (foundLayer != null)
                {
                    gpUtilities3.RemoveFromMapEx((IGPValue)existingGPLayer);
                    gpUtilities3.RemoveInternalLayerEx(foundLayer);
                }

                groupLayer = new GroupLayer();
                ((ILayer)groupLayer).Name = layerName;

                for (int layerIndex = 0; layerIndex < inputLayersMultiValue.Count; layerIndex++)
                {
                    IGPValue gpLayerToAdd = inputLayersMultiValue.get_Value(layerIndex) as IGPValue;

                    ILayer sourceLayer = gpUtilities3.DecodeLayer(gpLayerToAdd);

                    groupLayer.Add(sourceLayer);
                }

                outputGPGroupLayer = gpUtilities3.MakeGPValueFromObject(groupLayer);

                if (separatorPosition > -1)
                {
                    try
                    {
                        // in the case that we are dealing with a layer file on disk
                        // let's persist the group layer information into the file
                        ILayerFile pointLayerFile = new LayerFileClass();

                        if (System.IO.Path.GetExtension(outputGPLayerNameAsString).ToUpper().Equals(".LYR"))
                        {
                            if (pointLayerFile.get_IsPresent(outputGPLayerNameAsString))
                            {
                                try
                                {
                                    gpUtilities3.Delete(outputGPGroupLayer);
                                }
                                catch (Exception ex)
                                {
                                    message.AddError(120001, ex.Message);
                                    return;
                                }
                            }

                            pointLayerFile.New(outputGPLayerNameAsString);

                            pointLayerFile.ReplaceContents(groupLayer);

                            pointLayerFile.Save();

                        }

                         outputGPGroupLayer = gpUtilities3.MakeGPValueFromObject(pointLayerFile.Layer);
                    }
                    catch (Exception ex)
                    {
                        message.AddError(120002, ex.Message);
                        return;
                    }
                }

                gpUtilities3.PackGPValue(outputGPGroupLayer, outputGroupLayerParameter);

            }
            catch (Exception ex)
            {
                message.AddError(120049, ex.Message);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            if (m_hookHelper != null)
            {
                //TODO: Add Map/PageLayout related logic
                object hook;
                if (m_hookHelper.Hook is IToolbarControl2)
                    hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy;
                else
                    hook = m_hookHelper.Hook;

                IMapControl3 mapControl = hook as IMapControl3;
                if (mapControl == null)
                    return;
                object customeProperty = mapControl.CustomProperty;

                if (customeProperty == null || !(customeProperty is ILayer))
                    return;

                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Layer File|*.lyr|All Files|*.*";
                saveFileDialog.Title = "Create Layer File";
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory, ((ILayer)customeProperty).Name + ".lyr");
                DialogResult dr = saveFileDialog.ShowDialog();
                if (saveFileDialog.FileName != "" && dr == DialogResult.OK)
                {
                    if (System.IO.File.Exists(saveFileDialog.FileName))
                    {
                        //try to delete the existing file
                        System.IO.File.Delete(saveFileDialog.FileName);
                    }

                    //create a new LayerFile instance
                    ILayerFile layerFile = new LayerFileClass();
                    //create a new layer file
                    layerFile.New(saveFileDialog.FileName);
                    //attach the layer file with the actual layer
                    layerFile.ReplaceContents((ILayer)customeProperty);
                    //save the layer file
                    layerFile.Save();

                    //ask the user whether he'd like to add the layer to the map
                    if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        mapControl.AddLayerFromFile(saveFileDialog.FileName, 0);
                    }
              }
            }
            else if (m_sceneHookHelper != null)
            {
                //TODO: Add Scene related logic
            }
            else if (m_globeHookHelper != null)
            {
                //TODO: Add Globe related logic
            }
        }
Ejemplo n.º 18
0
        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 gpUtilities3 = new GPUtilitiesClass();

                if (TrackCancel == null)
                {
                    TrackCancel = new CancelTrackerClass();
                }

                IGPValue      inputLayersGPValue    = gpUtilities3.UnpackGPValue(paramvalues.get_Element(in_LayersNumber));
                IGPMultiValue inputLayersMultiValue = gpUtilities3.UnpackGPValue(inputLayersGPValue) as IGPMultiValue;

                IGPParameter      outputGroupLayerParameter = paramvalues.get_Element(out_groupLayerNumber) as IGPParameter;
                IGPValue          outputGPGroupLayer        = gpUtilities3.UnpackGPValue(outputGroupLayerParameter);
                IGPCompositeLayer outputCompositeLayer      = outputGPGroupLayer as IGPCompositeLayer;

                if (outputCompositeLayer == null)
                {
                    message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), outputGroupLayerParameter.Name));
                    return;
                }


                IGroupLayer groupLayer = null;

                // find the last position of the "\" string
                // in case we find such a thing, i.e. position is >= -1 then let's assume that we are dealing with a layer file on disk
                // otherwise let's create a new group layer instance
                string outputGPLayerNameAsString = outputGPGroupLayer.GetAsText();
                int    separatorPosition         = outputGPLayerNameAsString.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
                string layerName = String.Empty;

                if (separatorPosition > -1)
                {
                    layerName = outputGPGroupLayer.GetAsText().Substring(separatorPosition + 1);
                }
                else
                {
                    layerName = outputGPGroupLayer.GetAsText();
                }

                ILayer   foundLayer      = null;
                IGPLayer existingGPLayer = gpUtilities3.FindMapLayer2(layerName, out foundLayer);

                if (foundLayer != null)
                {
                    gpUtilities3.RemoveFromMapEx((IGPValue)existingGPLayer);
                    gpUtilities3.RemoveInternalLayerEx(foundLayer);
                }

                groupLayer = new GroupLayer();
                ((ILayer)groupLayer).Name = layerName;

                for (int layerIndex = 0; layerIndex < inputLayersMultiValue.Count; layerIndex++)
                {
                    IGPValue gpLayerToAdd = inputLayersMultiValue.get_Value(layerIndex) as IGPValue;

                    ILayer sourceLayer = gpUtilities3.DecodeLayer(gpLayerToAdd);

                    groupLayer.Add(sourceLayer);
                }

                outputGPGroupLayer = gpUtilities3.MakeGPValueFromObject(groupLayer);

                if (separatorPosition > -1)
                {
                    try
                    {
                        // in the case that we are dealing with a layer file on disk
                        // let's persist the group layer information into the file
                        ILayerFile pointLayerFile = new LayerFileClass();

                        if (System.IO.Path.GetExtension(outputGPLayerNameAsString).ToUpper().Equals(".LYR"))
                        {
                            if (pointLayerFile.get_IsPresent(outputGPLayerNameAsString))
                            {
                                try
                                {
                                    gpUtilities3.Delete(outputGPGroupLayer);
                                }
                                catch (Exception ex)
                                {
                                    message.AddError(120001, ex.Message);
                                    return;
                                }
                            }

                            pointLayerFile.New(outputGPLayerNameAsString);

                            pointLayerFile.ReplaceContents(groupLayer);

                            pointLayerFile.Save();
                        }

                        outputGPGroupLayer = gpUtilities3.MakeGPValueFromObject(pointLayerFile.Layer);
                    }
                    catch (Exception ex)
                    {
                        message.AddError(120002, ex.Message);
                        return;
                    }
                }

                gpUtilities3.PackGPValue(outputGPGroupLayer, outputGroupLayerParameter);
            }
            catch (Exception ex)
            {
                message.AddError(120049, ex.Message);
            }
        }
    /// <summary>
    /// Occurs when this command is clicked
    /// </summary>
    public override void OnClick()
    {
      //need to get the layer from the custom-property of the map
      if (null == m_hookHelper)
        return;

      //get the mapControl hook
      object hook = null;
      if (m_hookHelper.Hook is IToolbarControl2)
      {
        hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy;
      }
      else
      {
        hook = m_hookHelper.Hook;
      }

      //get the custom property from which is supposed to be the layer to be saved
      object customProperty = null;
      IMapControl3 mapControl = null;
      if (hook is IMapControl3)
      {
        mapControl = (IMapControl3)hook;
        customProperty = mapControl.CustomProperty;
      }
      else
        return;

      if (null == customProperty || !(customProperty is ILayer))
        return;

      //ask the user to set a name for the new layer file
      SaveFileDialog saveFileDialog = new SaveFileDialog();
      saveFileDialog.Filter = "Layer File|*.lyr|All Files|*.*";
      saveFileDialog.Title = "Create Layer File";
      saveFileDialog.RestoreDirectory = true;
      saveFileDialog.FileName = System.IO.Path.Combine(saveFileDialog.InitialDirectory, ((ILayer)customProperty).Name + ".lyr");

      //get the layer name from the user
      DialogResult dr = saveFileDialog.ShowDialog();
      if (saveFileDialog.FileName != "" && dr == DialogResult.OK)
      {
        if (System.IO.File.Exists(saveFileDialog.FileName))
        {
          //try to delete the existing file
          System.IO.File.Delete(saveFileDialog.FileName);
        }

        //create a new LayerFile instance
        ILayerFile layerFile = new LayerFileClass();
        //create a new layer file
        layerFile.New(saveFileDialog.FileName);
        //attach the layer file with the actual layer
        layerFile.ReplaceContents((ILayer)customProperty);
        //save the layer file
        layerFile.Save();

        //ask the user whether he'd like to add the layer to the map
        if (DialogResult.Yes == MessageBox.Show("Would you like to add the layer to the map?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        {
          mapControl.AddLayerFromFile(saveFileDialog.FileName, 0);
        }
      }
    }
Ejemplo n.º 20
0
        private void ChangeBreaks()
        {
            IMxDocument pMxDoc = m_application.Document as IMxDocument;
            IMap pMap = pMxDoc.FocusMap;
            IGeoFeatureLayer pFeatureLayer = pMap.Layer[0] as IGeoFeatureLayer;

            IGxLayer pGXLayer = new GxLayer();
            IGxFile pGXFile = (IGxFile)pGXLayer;

            pGXFile.Path = @"S:\Projects\TARRANT-Witchita\GIS\Layers\infreq_layers\test_Layer_2.lyr";

            IGeoFeatureLayer pLayer = pGXLayer.Layer as IGeoFeatureLayer;
            IClassBreaksRenderer pClassBreaksRenderer = (IClassBreaksRenderer)pLayer.Renderer;
            int pNumberOfClassBreaks = pClassBreaksRenderer.BreakCount;

            for (int i = 0; i < pNumberOfClassBreaks; i++)
            {
                pClassBreaksRenderer.Break[i] = i + 1;
            }

            IClassBreaksRenderer qClassBreaksRenderer = (IClassBreaksRenderer)pFeatureLayer.Renderer;
            qClassBreaksRenderer = pClassBreaksRenderer;
            int qNumberOfClassBreaks = qClassBreaksRenderer.BreakCount;

            for (int i = 0; i < qNumberOfClassBreaks; i++)
            {
                qClassBreaksRenderer.Break[i] = i + 1;
            }

            //Create a new LayerFile instance.
            ILayerFile layerFile = new LayerFileClass();
            //Create a new layer file.
            layerFile.New(@"S:\Projects\TARRANT-Witchita\GIS\Layers\test_Layer_3.lyr");

            layerFile.ReplaceContents(pLayer);
            layerFile.Save();
        }