Esempio n. 1
0
        public bool ConvertLayerToKML(string kmzOutputPath, string tmpShapefilePath,
                                      ESRI.ArcGIS.Carto.IMap map, GraphicTypes graphicType)
        {
            try
            {
                string kmzName = System.IO.Path.GetFileName(kmzOutputPath);

                IGeoProcessor2 gp = new GeoProcessorClass();
                gp.OverwriteOutput = true;
                IGeoFeatureLayer geoLayer = null;

                IVariantArray parameters = new VarArrayClass();
                parameters.Add(tmpShapefilePath);
                parameters.Add(kmzName);
                gp.Execute("MakeFeatureLayer_management", parameters, null);

                string layerFileName = getLayerFileFromGraphicType(graphicType);
                if (!string.IsNullOrEmpty(layerFileName))
                {
                    IVariantArray parametersASM = new VarArrayClass();
                    parametersASM.Add(kmzName);
                    parametersASM.Add(layerFileName);
                    gp.Execute("ApplySymbologyFromLayer_management", parametersASM, null);
                }

                IVariantArray parameters1 = new VarArrayClass();
                // assign  parameters
                parameters1.Add(kmzName);
                parameters1.Add(kmzOutputPath);

                gp.Execute("LayerToKML_conversion", parameters1, null);

                // Remove the temporary layer from the TOC
                for (int i = 0; i < map.LayerCount; i++)
                {
                    ILayer layer = map.get_Layer(i);
                    if ((layer.Name == "featureLayer") || (layer.Name == kmzName))
                    {
                        map.DeleteLayer(layer);
                        break;
                    }
                }
                if (geoLayer != null)
                {
                    map.DeleteLayer(geoLayer);
                }

                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(false);
            }
        }
        public bool ConvertLayerToKML(IFeatureClass fc, string kmzOutputPath, string tmpShapefilePath, ESRI.ArcGIS.Carto.IMap map)
        {
            try
            {
                string kmzName    = System.IO.Path.GetFileName(kmzOutputPath);
                string folderName = System.IO.Path.GetDirectoryName(kmzOutputPath);
                var    fcName     = System.IO.Path.GetFileNameWithoutExtension(kmzName);

                IFeatureLayer fLayer = new FeatureLayer();
                fLayer.FeatureClass = fc;
                var geoLayer = (fLayer as IGeoFeatureLayer);
                if (geoLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
                    pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
                    pSimpleMarkerSymbol.Size  = CoordinateConversionLibrary.Constants.SymbolSize;
                    pSimpleMarkerSymbol.Color = new RgbColorClass()
                    {
                        Red = 255
                    };
                    ISimpleRenderer pSimpleRenderer;
                    pSimpleRenderer        = new SimpleRenderer();
                    pSimpleRenderer.Symbol = (ISymbol)pSimpleMarkerSymbol;
                    geoLayer.Name          = fcName;
                    geoLayer.Renderer      = (IFeatureRenderer)pSimpleRenderer;
                }
                var featureLayer = geoLayer as FeatureLayer;

                map.AddLayer(geoLayer);

                // Initialize the geoprocessor.
                IGeoProcessor2 gp         = new GeoProcessorClass();
                IVariantArray  parameters = new VarArrayClass();
                parameters.Add(featureLayer.Name);
                parameters.Add(folderName + "\\" + kmzName);
                var result = gp.Execute(CoordinateConversionLibrary.Constants.LayerToKMLGPTool, parameters, null);

                // Remove the temporary layer from the TOC
                for (int i = 0; i < map.LayerCount; i++)
                {
                    ILayer layer = map.get_Layer(i);
                    if (layer.Name == fcName)
                    {
                        map.DeleteLayer(layer);
                        break;
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return(false);
        }
        public bool ConvertLayerToKML(string kmzOutputPath, string tmpShapefilePath, ESRI.ArcGIS.Carto.IMap map)
        {
            try
            {
                string kmzName    = System.IO.Path.GetFileName(kmzOutputPath);
                string folderName = System.IO.Path.GetDirectoryName(kmzOutputPath);

                IGeoProcessor2 gp         = new GeoProcessorClass();
                IVariantArray  parameters = new VarArrayClass();
                parameters.Add(tmpShapefilePath);
                parameters.Add(kmzName);
                gp.Execute("MakeFeatureLayer_management", parameters, null);

                IVariantArray parameters1 = new VarArrayClass();
                // assign  parameters
                parameters1.Add(kmzName);
                parameters1.Add(kmzOutputPath);

                gp.Execute("LayerToKML_conversion", parameters1, null);

                // Remove the temporary layer from the TOC
                for (int i = 0; i < map.LayerCount; i++)
                {
                    ILayer layer = map.get_Layer(i);
                    if (layer.Name == "featureLayer")
                    {
                        map.DeleteLayer(layer);
                        break;
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Esempio n. 4
0
        public bool CreateMxd(string sMxdTemplate,
                              string sPathToMXD,
                              string ArcGISServer,
                              string sMxdFile,
                              string sDBConn,
                              string sDataSet,
                              bool bSde)
        {
            if (sMxdTemplate.Length > 0)
            {
                _sMxdTemplate = sMxdTemplate;
            }

            _sPathToMXD   = sPathToMXD;
            _ArcGISServer = ArcGISServer;
            ESRI.ArcGIS.Carto.IMap pMap   = null;
            IFeatureClass          pOldFC = null;
            string fcName  = String.Empty;
            string sSuffix = String.Empty;

            IWorkspaceFactory2 wsf = null;
            IWorkspace2        ws2 = null;
            IFeatureWorkspace  fws = null;
            IWorkspace         ws  = null;

            try
            {
                if (bSde)
                {
                    // Get WS for SDE
                    ws = ArcSdeWorkspaceFromFile(sDBConn);
                }
                else
                {
                    // Get WS from file GDB.
                    wsf = new FileGDBWorkspaceFactoryClass() as IWorkspaceFactory2;
                    //if locks on gdb only path is passed in
                    string fileGdb = sDBConn.Contains(".gdb") ? sDBConn : sDBConn;

                    if (wsf.IsWorkspace(fileGdb))
                    {
                        ws = wsf.OpenFromFile(fileGdb, 0);
                    }
                }

                if (ws == null)
                {
                    return(false);
                }

                // Check if Mxd already exists
                if (File.Exists(sMxdFile))
                {
                    return(false);
                }

                // Create a Mxd from Overlays Template
                pMap = PrivateCreateMxd(sMxdFile);

                ws2 = (IWorkspace2)ws;
                fws = (IFeatureWorkspace)ws;

                // Loop through all layers in MXD and repoint data source to OverlayGDB Features
                IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
                pEnumLayer.Reset();
                ILayer pLayer = pEnumLayer.Next();
                while (pLayer != null)
                {
                    if (!(pLayer is IFeatureLayer))
                    {
                        pLayer = pEnumLayer.Next();
                        continue;
                    }

                    // Cast pLayer to featurelayer
                    IFeatureLayer pMapFeatureLayer = (IFeatureLayer)pLayer;
                    pOldFC = pMapFeatureLayer.FeatureClass;

                    if (pOldFC == null)
                    {
                        pLayer = pEnumLayer.Next();
                        continue;
                    }

                    // Get FC name
                    IDataset pDS = (IDataset)pOldFC;
                    fcName = pDS.Name;

                    // Feature Class: <Dataset>_osm_pt, <Dataset>_osm_ln, <Dataset>_osm_ply
                    sSuffix = fcName.Substring(fcName.IndexOf("_osm_"));

                    if (String.IsNullOrEmpty(sSuffix))
                    {
                        continue;
                    }

                    // Check if feature class exists in GDB
                    if (ws2.get_NameExists(esriDatasetType.esriDTFeatureClass, sDataSet + sSuffix))
                    {
                        // Get feature class
                        IFeatureClass ipFC = fws.OpenFeatureClass(sDataSet + sSuffix);
                        IFeatureLayer ipFL = (IFeatureLayer)pLayer;

                        // Create IMapAdmin2 from pMap
                        IMapAdmin2 pMapAdmin2 = (IMapAdmin2)pMap;

                        // Change FeatureClass of layer to FC in FGDB
                        ipFL.FeatureClass = ipFC;
                        pMapAdmin2.FireChangeFeatureClass(pOldFC, ipFC);

                        COMUtil.ReleaseObject(ipFC);
                        ipFC = null;

                        COMUtil.ReleaseObject(ipFL);
                        ipFL = null;
                    }
                    else
                    {
                        // Remove layer from map
                        pMap.DeleteLayer(pLayer);
                    }

                    pLayer = pEnumLayer.Next();
                }

                SaveMXD(sMxdFile, pMap);

                return(true);
            }
            catch (System.Runtime.InteropServices.COMException cx)
            {
                throw cx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                COMUtil.ReleaseObject(pOldFC);
                COMUtil.ReleaseObject(fws);
                COMUtil.ReleaseObject(ws2);
                COMUtil.ReleaseObject(ws);
                COMUtil.ReleaseObject(pMap);
                COMUtil.ReleaseObject(wsf);
                pOldFC        = null;
                fws           = null;
                ws2           = null;
                ws            = null;
                wsf           = null;
                pMap          = null;
                _pMapDocument = null;

                //Do not make any call to ArcObjects after ShutDownApplication()
                if (m_AOLicenseInitializer != null)
                {
                    m_AOLicenseInitializer.ShutdownApplication();
                }
                m_AOLicenseInitializer = null;
            }
        }