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);
        }
        /// <summary>
        /// Add the feature layer to the map
        /// </summary>
        /// <param name="fc">IFeatureClass</param>
        private void AddFeatureLayerToMap(IFeatureClass fc)
        {
            IFeatureLayer outputFeatureLayer = new FeatureLayerClass();

            outputFeatureLayer.FeatureClass = fc;

            IGeoFeatureLayer geoLayer = outputFeatureLayer as IGeoFeatureLayer;

            if (geoLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
            {
                IFeatureRenderer pFeatureRender;
                pFeatureRender = (IFeatureRenderer) new SimpleRenderer();
                ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
                pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
                pSimpleMarkerSymbol.Size  = 3.0;

                ISimpleRenderer pSimpleRenderer;
                pSimpleRenderer        = new SimpleRenderer();
                pSimpleRenderer.Symbol = (ISymbol)pSimpleMarkerSymbol;

                geoLayer.Renderer = (IFeatureRenderer)pSimpleRenderer;
            }
            else if (geoLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
            {
                IFeatureRenderer pFeatureRender;
                pFeatureRender = (IFeatureRenderer) new SimpleRenderer();
                ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
                pSimpleFillSymbol.Style         = esriSimpleFillStyle.esriSFSHollow;
                pSimpleFillSymbol.Outline.Width = 0.4;

                ISimpleRenderer pSimpleRenderer;
                pSimpleRenderer        = new SimpleRenderer();
                pSimpleRenderer.Symbol = (ISymbol)pSimpleFillSymbol;

                geoLayer.Renderer = (IFeatureRenderer)pSimpleRenderer;
            }

            geoLayer.Name = fc.AliasName;

            ESRI.ArcGIS.Carto.IMap map = ArcMap.Document.FocusMap;
            map.AddLayer((ILayer)outputFeatureLayer);
        }
        /// <summary>
        /// Where all of the work is done.  Override from TabBaseViewModel
        /// </summary>
        internal override void CreateMapElement()
        {
            try
            {
                IsRunning = true;

                if (!CanCreateElement || ArcMap.Document == null || ArcMap.Document.FocusMap == null || string.IsNullOrWhiteSpace(SelectedSurfaceName))
                {
                    return;
                }

                //base.CreateMapElement();

                var surface = GetSurfaceFromMapByName(ArcMap.Document.FocusMap, SelectedSurfaceName);

                if (surface == null)
                {
                    return;
                }

                // Determine if selected surface is projected or geographic
                ILayer surfaceLayer = GetLayerFromMapByName(ArcMap.Document.FocusMap, SelectedSurfaceName);
                var    geoDataset   = surfaceLayer as IGeoDataset;
                SelectedSurfaceSpatialRef = geoDataset.SpatialReference;

                if (SelectedSurfaceSpatialRef is IGeographicCoordinateSystem)
                {
                    MessageBox.Show(VisibilityLibrary.Properties.Resources.RLOSUserPrompt, VisibilityLibrary.Properties.Resources.RLOSUserPromptCaption);
                    return;
                }

                if (geoDataset != null && ArcMap.Document.FocusMap.SpatialReference.FactoryCode != geoDataset.SpatialReference.FactoryCode)
                {
                    MessageBox.Show(VisibilityLibrary.Properties.Resources.LOSDataFrameMatch, VisibilityLibrary.Properties.Resources.LOSSpatialReferenceCaption);
                    return;
                }

                using (ComReleaser oComReleaser = new ComReleaser())
                {
                    // Create feature workspace
                    IFeatureWorkspace workspace = CreateFeatureWorkspace("tempWorkspace");

                    StartEditOperation((IWorkspace)workspace);

                    // Create feature class
                    IFeatureClass pointFc = CreateObserversFeatureClass(workspace, SelectedSurfaceSpatialRef, "Output" + RunCount.ToString());

                    double finalObserverOffset = GetOffsetInZUnits(ObserverOffset.Value, surface.ZFactor, OffsetUnitType);
                    double finalSurfaceOffset  = GetOffsetInZUnits(SurfaceOffset, surface.ZFactor, OffsetUnitType);

                    double conversionFactor     = GetConversionFactor(SelectedSurfaceSpatialRef);
                    double convertedMinDistance = MinDistance * conversionFactor;
                    double convertedMaxDistance = MaxDistance * conversionFactor;
                    double finalMinDistance     = GetLinearDistance(ArcMap.Document.FocusMap, convertedMinDistance, OffsetUnitType);
                    double finalMaxDistance     = GetLinearDistance(ArcMap.Document.FocusMap, convertedMaxDistance, OffsetUnitType);

                    double finalLeftHorizontalFOV  = GetAngularDistance(ArcMap.Document.FocusMap, LeftHorizontalFOV, AngularUnitType);
                    double finalRightHorizontalFOV = GetAngularDistance(ArcMap.Document.FocusMap, RightHorizontalFOV, AngularUnitType);
                    double finalBottomVerticalFOV  = GetAngularDistance(ArcMap.Document.FocusMap, BottomVerticalFOV, AngularUnitType);
                    double finalTopVerticalFOV     = GetAngularDistance(ArcMap.Document.FocusMap, TopVerticalFOV, AngularUnitType);

                    // Out radius geometries
                    List <IGeometry> radius2GeomList   = new List <IGeometry>();
                    List <IGeometry> radius1_2GeomList = new List <IGeometry>();
                    List <IGeometry> donutGeomList     = new List <IGeometry>();

                    foreach (var observerPoint in ObserverAddInPoints)
                    {
                        // Create buffer geometries for final Min/Max distance
                        ITopologicalOperator topologicalOperator = observerPoint.Point as ITopologicalOperator;
                        IGeometry            geom = topologicalOperator.Buffer(finalMaxDistance);
                        radius2GeomList.Add(geom);
                        radius1_2GeomList.Add(geom);
                        if (finalMinDistance > 0)
                        {
                            IGeometry geom2 = topologicalOperator.Buffer(finalMinDistance);

                            ITopologicalOperator eraseTopo  = geom as ITopologicalOperator;
                            IGeometry            erasedGeom = eraseTopo.Difference(geom2);
                            donutGeomList.Add(erasedGeom);
                        }
                        else
                        {
                            radius1_2GeomList.Add(geom);
                        }

                        double z1 = surface.GetElevation(observerPoint.Point) + finalObserverOffset;

                        //create a new point feature
                        IFeature ipFeature = pointFc.CreateFeature();

                        // Set the field values for the feature
                        SetFieldValues(finalObserverOffset, finalSurfaceOffset, finalMinDistance, finalMaxDistance, finalLeftHorizontalFOV,
                                       finalRightHorizontalFOV, finalBottomVerticalFOV, finalTopVerticalFOV, ipFeature);

                        if (double.IsNaN(z1))
                        {
                            System.Windows.MessageBox.Show(VisibilityLibrary.Properties.Resources.RLOSPointsOutsideOfSurfaceExtent, VisibilityLibrary.Properties.Resources.MsgCalcCancelled);
                            return;
                        }

                        //Create shape
                        IPoint point = new PointClass()
                        {
                            Z = z1, X = observerPoint.Point.X, Y = observerPoint.Point.Y, ZAware = true
                        };
                        ipFeature.Shape = point;
                        ipFeature.Store();
                    }

                    IFeatureClassDescriptor fd = new FeatureClassDescriptorClass();
                    fd.Create(pointFc, null, "OBJECTID");

                    StopEditOperation((IWorkspace)workspace);

                    try
                    {
                        ILayer layer     = GetLayerFromMapByName(ArcMap.Document.FocusMap, SelectedSurfaceName);
                        string layerPath = GetLayerPath(layer);

                        IFeatureLayer ipFeatureLayer = new FeatureLayerClass();
                        ipFeatureLayer.FeatureClass = pointFc;

                        IDataset ipDataset    = (IDataset)pointFc;
                        string   outputFcName = ipDataset.BrowseName + "_output";
                        string   strPath      = ipDataset.Workspace.PathName + "\\" + ipDataset.BrowseName;
                        string   outPath      = ipDataset.Workspace.PathName + "\\" + outputFcName;

                        IVariantArray parameters = new VarArrayClass();
                        parameters.Add(layerPath);
                        parameters.Add(strPath);
                        parameters.Add(outPath);

                        esriLicenseStatus status = GetSpatialAnalystLicense();
                        if (status == esriLicenseStatus.esriLicenseUnavailable || status == esriLicenseStatus.esriLicenseFailure ||
                            status == esriLicenseStatus.esriLicenseNotInitialized || status == esriLicenseStatus.esriLicenseNotLicensed ||
                            status == esriLicenseStatus.esriLicenseUnavailable)
                        {
                            System.Windows.MessageBox.Show(VisibilityLibrary.Properties.Resources.LOSSpatialAnalystLicenseInvalid, VisibilityLibrary.Properties.Resources.MsgCalcCancelled);
                            return;
                        }

                        IGeoProcessor2 gp = new GeoProcessorClass();

                        gp.AddOutputsToMap = false;

                        // Add a mask to buffer the output to selected distance
                        SetGPMask(workspace, radius2GeomList, gp, "radiusMask");

                        object oResult = gp.Execute("Visibility_sa", parameters, null);
                        IGeoProcessorResult ipResult = (IGeoProcessorResult)oResult;

                        ComReleaser.ReleaseCOMObject(gp);
                        gp = null;
                        GC.Collect();

                        // Add buffer geometries to the map
                        foreach (IGeometry geom in radius1_2GeomList)
                        {
                            var color = new RgbColorClass()
                            {
                                Blue = 255
                            } as IColor;
                            AddGraphicToMap(geom, color, true);
                        }

                        IRasterLayer outputRasterLayer = new RasterLayerClass();
                        outputRasterLayer.CreateFromFilePath(outPath);

                        string fcName = IntersectOutput(outputRasterLayer, ipDataset, workspace, donutGeomList);

                        IFeatureClass finalFc = workspace.OpenFeatureClass(fcName);

                        IFeatureLayer outputFeatureLayer = new FeatureLayerClass();
                        outputFeatureLayer.FeatureClass = finalFc;

                        //Add it to a map if the layer is valid.
                        if (outputFeatureLayer != null)
                        {
                            // set the renderer
                            IFeatureRenderer featRend = UniqueValueRenderer(workspace, finalFc);
                            IGeoFeatureLayer geoLayer = outputFeatureLayer as IGeoFeatureLayer;
                            geoLayer.Renderer = featRend;
                            geoLayer.Name     = "RLOS_Visibility_" + RunCount.ToString();

                            // Set the layer transparency
                            IDisplayFilterManager      filterManager = (IDisplayFilterManager)outputFeatureLayer;
                            ITransparencyDisplayFilter filter        = new TransparencyDisplayFilter();
                            filter.Transparency         = 80;
                            filterManager.DisplayFilter = filter;

                            ESRI.ArcGIS.Carto.IMap map = ArcMap.Document.FocusMap;
                            map.AddLayer((ILayer)outputFeatureLayer);

                            IEnvelope envelope = outputFeatureLayer.AreaOfInterest.Envelope;
                            ZoomToExtent(envelope);
                        }

                        RunCount += 1;
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show(VisibilityLibrary.Properties.Resources.MsgTryAgain, VisibilityLibrary.Properties.Resources.MsgCalcCancelled);
                    }

                    //Reset(true);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(VisibilityLibrary.Properties.Resources.MsgTryAgain, VisibilityLibrary.Properties.Resources.MsgCalcCancelled);
            }
            finally
            {
                IsRunning = false;
            }
        }
        /// <summary>
        /// Saves graphics to file gdb or shp file
        /// </summary>
        /// <param name="obj"></param>
        private void OnSaveAs(object obj)
        {
            var dlg = new GRSaveAsFormatView();

            dlg.DataContext = new SaveAsFormatViewModel();
            var vm = dlg.DataContext as SaveAsFormatViewModel;

            if (dlg.ShowDialog() == true)
            {
                IFeatureClass fc = null;

                // Get the graphics list for the selected tab
                List <Graphic> typeGraphicsList = new List <Graphic>();
                if (this is LinesViewModel)
                {
                    typeGraphicsList = GraphicsList.Where(g => g.GraphicType == GraphicTypes.Line).ToList();
                }
                else if (this is CircleViewModel)
                {
                    typeGraphicsList = GraphicsList.Where(g => g.GraphicType == GraphicTypes.Circle).ToList();
                }
                else if (this is EllipseViewModel)
                {
                    typeGraphicsList = GraphicsList.Where(g => g.GraphicType == GraphicTypes.Ellipse).ToList();
                }
                else if (this is RangeViewModel)
                {
                    typeGraphicsList = GraphicsList.Where(g => g.GraphicType == GraphicTypes.RangeRing).ToList();
                }

                string path = null;
                if (vm.FeatureShapeIsChecked)
                {
                    path = fcUtils.PromptUserWithGxDialog(ArcMap.Application.hWnd);
                    if (path != null)
                    {
                        if (System.IO.Path.GetExtension(path).Equals(".shp"))
                        {
                            fc = fcUtils.CreateFCOutput(path, SaveAsType.Shapefile, typeGraphicsList, ArcMap.Document.FocusMap.SpatialReference);
                        }
                        else
                        {
                            fc = fcUtils.CreateFCOutput(path, SaveAsType.FileGDB, typeGraphicsList, ArcMap.Document.FocusMap.SpatialReference);
                        }
                    }
                }
                else
                {
                    path = PromptSaveFileDialog();
                    if (path != null)
                    {
                        string        kmlName       = System.IO.Path.GetFileName(path);
                        string        folderName    = System.IO.Path.GetDirectoryName(path);
                        string        tempShapeFile = folderName + "\\tmpShapefile.shp";
                        IFeatureClass tempFc        = fcUtils.CreateFCOutput(tempShapeFile, SaveAsType.Shapefile, typeGraphicsList, ArcMap.Document.FocusMap.SpatialReference);

                        if (tempFc != null)
                        {
                            kmlUtils.ConvertLayerToKML(path, tempShapeFile, ArcMap.Document.FocusMap);

                            // delete the temporary shapefile
                            fcUtils.DeleteShapeFile(tempShapeFile);
                        }
                    }
                }

                if (fc != null)
                {
                    IFeatureLayer outputFeatureLayer = new FeatureLayerClass();
                    outputFeatureLayer.FeatureClass = fc;

                    IGeoFeatureLayer geoLayer = outputFeatureLayer as IGeoFeatureLayer;
                    geoLayer.Name = fc.AliasName;

                    ESRI.ArcGIS.Carto.IMap map = ArcMap.Document.FocusMap;
                    map.AddLayer((ILayer)outputFeatureLayer);
                }
            }
        }