Exemplo n.º 1
0
        /// <summary>
        /// Create a feature class
        /// </summary>
        /// <param name="dataset">Name of the feature class to be created.</param>
        /// <param name="featureclassType">Type of feature class to be created. Options are:
        /// <list type="bullet">
        /// <item>POINT</item>
        /// <item>MULTIPOINT</item>
        /// <item>POLYLINE</item>
        /// <item>POLYGON</item></list></param>
        /// <param name="connection">connection path</param>
        /// <param name="spatialRef">SpatialReference</param>
        /// <param name="graphicsList">List of graphics</param>
        /// <param name="mapview">MapView object</param>
        /// <param name="isKML">Is this a kml output</param>
        /// <returns></returns>
        private static async Task CreateFeatureClass(string dataset, GeomType geomType, string connection, SpatialReference spatialRef, List <Graphic> graphicsList, MapView mapview, bool isKML = false)
        {
            try
            {
                string strGeomType = geomType == GeomType.PolyLine ? "POLYLINE" : "POLYGON";

                List <object> arguments = new List <object>();
                // store the results in the geodatabase
                arguments.Add(connection);
                // name of the feature class
                arguments.Add(dataset);
                // type of geometry
                arguments.Add(strGeomType);
                // no template
                arguments.Add("");
                // no z values
                arguments.Add("DISABLED");
                // no m values
                arguments.Add("DISABLED");
                arguments.Add(spatialRef);

                //IReadOnlyList<string> valueArray = null;
                //await QueuedTask.Run(async () =>
                //{
                //    valueArray = Geoprocessing.MakeValueArray(arguments.ToArray());
                //});

                //block the CIM for a second
                //Task.Delay(5000).Wait();
                var       valueArray = Geoprocessing.MakeValueArray(arguments.ToArray());
                IGPResult result     = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management", valueArray);

                await CreateFeatures(graphicsList);

                if (isKML)
                {
                    await KMLUtils.ConvertLayerToKML(connection, dataset, MapView.Active);

                    // Delete temporary Shapefile
                    string[] extensionNames     = { ".cpg", ".dbf", ".prj", ".shx", ".shp" };
                    string   datasetNoExtension = Path.GetFileNameWithoutExtension(dataset);
                    foreach (string extension in extensionNames)
                    {
                        string shapeFile = Path.Combine(connection, datasetNoExtension + extension);
                        File.Delete(shapeFile);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// Create a feature class
        /// </summary>
        /// <param name="dataset">Name of the feature class to be created.</param>
        /// <param name="featureclassType">Type of feature class to be created. Options are:
        /// <list type="bullet">
        /// <item>POINT</item>
        /// <item>MULTIPOINT</item>
        /// <item>POLYLINE</item>
        /// <item>POLYGON</item></list></param>
        /// <param name="connection">connection path</param>
        /// <param name="spatialRef">SpatialReference</param>
        /// <param name="graphicsList">List of graphics</param>
        /// <param name="mapview">MapView object</param>
        /// <param name="isKML">Is this a kml output</param>
        /// <returns></returns>
        private static async Task CreateFeatureClass(string dataset, GeomType geomType, string connection, SpatialReference spatialRef, List <Graphic> graphicsList, MapView mapview, bool isKML = false)
        {
            try
            {
                List <Graphic> list = ClearTempGraphics(graphicsList);

                if ((list == null) || (list.Count == 0))
                {
                    return;
                }

                string strGeomType = geomType == GeomType.PolyLine ? "POLYLINE" : "POLYGON";

                List <object> arguments = new List <object>();
                // store the results in the geodatabase
                arguments.Add(connection);
                // name of the feature class
                arguments.Add(dataset);
                // type of geometry
                arguments.Add(strGeomType);
                // no template
                arguments.Add(null);
                // no m values
                arguments.Add("DISABLED");
                // no z values
                arguments.Add("DISABLED");
                arguments.Add(spatialRef.Wkid.ToString());


                // store the results in the geodatabase
                object[] argArray = arguments.ToArray();

                var environments = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
                //var valueArray = Geoprocessing.MakeValueArray(argArray);

                IGPResult result = await Geoprocessing.ExecuteToolAsync("CreateFeatureclass_management",
                                                                        Geoprocessing.MakeValueArray(argArray),
                                                                        environments,
                                                                        null,
                                                                        null);


                // Add additional fields based on type of graphic
                string nameNoExtension = Path.GetFileNameWithoutExtension(dataset);
                string featureClass    = "";
                if (isKML)
                {
                    featureClass = connection + "/" + nameNoExtension + ".shp";
                }
                else
                {
                    featureClass = connection + "/" + dataset;
                }

                string graphicsType = list[0].p.GetType().ToString().Replace("ProAppDistanceAndDirectionModule.", "");
                switch (graphicsType)
                {
                case "LineAttributes":
                {
                    IGPResult result2 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Distance", "DOUBLE"));

                    IGPResult result3 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DistUnit", "TEXT"));

                    IGPResult result4 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "OriginX", "DOUBLE"));

                    IGPResult result5 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "OriginY", "DOUBLE"));

                    IGPResult result6 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DestX", "DOUBLE"));

                    IGPResult result7 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DestY", "DOUBLE"));

                    IGPResult result8 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Angle", "DOUBLE"));

                    IGPResult result9 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "AngleUnit", "TEXT"));

                    break;
                }

                case "CircleAttributes":
                {
                    IGPResult result2 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Distance", "DOUBLE"));

                    IGPResult result3 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DistUnit", "TEXT"));

                    IGPResult result4 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DistType", "TEXT"));

                    IGPResult result5 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "CenterX", "DOUBLE"));

                    IGPResult result6 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "CenterY", "DOUBLE"));

                    break;
                }

                case "EllipseAttributes":
                {
                    IGPResult result2 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Minor", "DOUBLE"));

                    IGPResult result3 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Major", "DOUBLE"));

                    IGPResult result4 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DistUnit", "TEXT"));

                    IGPResult result5 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "CenterX", "DOUBLE"));

                    IGPResult result6 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "CenterY", "DOUBLE"));

                    IGPResult result7 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Angle", "DOUBLE"));

                    IGPResult result8 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "AngleUnit", "TEXT"));

                    break;
                }

                case "RangeAttributes":
                {
                    IGPResult result2 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Rings", "LONG"));

                    IGPResult result3 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Distance", "DOUBLE"));

                    IGPResult result4 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "DistUnit", "TEXT"));

                    IGPResult result5 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "Radials", "LONG"));

                    IGPResult result6 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "CenterX", "DOUBLE"));

                    IGPResult result7 = await Geoprocessing.ExecuteToolAsync("AddField_management", makeValueArray(featureClass, "CenterY", "DOUBLE"));

                    break;
                }
                }


                await CreateFeatures(list, isKML);

                if (isKML)
                {
                    await KMLUtils.ConvertLayerToKML(connection, dataset, MapView.Active);

                    // Delete temporary Shapefile
                    string[] extensionNames     = { ".cpg", ".dbf", ".prj", ".shx", ".shp", ".sbn", ".sbx" };
                    string   datasetNoExtension = Path.GetFileNameWithoutExtension(dataset);
                    foreach (string extension in extensionNames)
                    {
                        string shapeFile     = Path.Combine(connection, datasetNoExtension + extension);
                        string shapefileproj = Path.Combine(connection, datasetNoExtension + "_proj" + extension);
                        if (File.Exists(shapeFile))
                        {
                            File.Delete(shapeFile);
                        }
                        if (File.Exists(shapefileproj))
                        {
                            File.Delete(shapefileproj);
                        }
                    }
                    DirectoryInfo  dir = new DirectoryInfo(connection);
                    FileSystemInfo fsi = dir;
                    fsi.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }