Ejemplo n.º 1
0
        private List <CPoint> LookingForNeighboursDT(IFeatureLayer pFeatureLayer, double dblThreshold, int intPtNumLast,
                                                     ref long lngTimeForSearching, ref long lngTimeForDT, ref long lngMemory, ref long lngMemoryDT,
                                                     ref long lngMemoryDTProcess, ref int intOutPut, string strTINPath)
        {
            lngMemory = GC.GetTotalMemory(true);
            //lngMemoryDT = GC.GetTotalMemory(true);
            //lngMemoryDTProcess = Process.GetProcessesByName("ContinuousGeneralizer.vshost")[0].WorkingSet64;
            long lngStartTime = System.Environment.TickCount; //record the start time

            //lngMemoryDT = Process.GetProcessesByName("ContinuousGeneralizer.vshost")[0].WorkingSet64;
            //Process .GetProcessesByName("ContinuousGeneralizer.vshost")[0].
            //create TIN
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IGeoDataset   pGDS          = (IGeoDataset)pFeatureClass;
            IEnvelope     pEnv          = (IEnvelope)pGDS.Extent;

            pEnv.SpatialReference = pGDS.SpatialReference;
            IFields pFields      = pFeatureClass.Fields;
            IField  pHeightFiled = new FieldClass();

            try
            {
                pHeightFiled = pFields.get_Field(pFields.FindField("Id"));
            }
            catch (Exception)
            {
                pHeightFiled = pFields.get_Field(pFields.FindField("ID"));
                throw;
            }

            //ITinWorkspace
            ITinEdit pTinEdit = new TinClass();

            pTinEdit.InitNew(pEnv);
            object Missing = Type.Missing;

            pTinEdit.AddFromFeatureClass(pFeatureClass, null, pHeightFiled, null, esriTinSurfaceType.esriTinHardLine, ref Missing);

            lngTimeForDT = System.Environment.TickCount - lngStartTime;   //Time for constructing DT
            //long lngMemoryafterTinEdit = GC.GetTotalMemory(false) - lngMemoryDT;
            //GC.Collect();
            //lngMemoryDT = GC.GetTotalMemory(true) - lngMemoryDT;
            //lngMemoryDTProcess = Process.GetProcessesByName("ContinuousGeneralizer.vshost")[0].WorkingSet64 - lngMemoryDTProcess;

            //C5.LinkedList<int> intLLt = new C5.LinkedList<int>();


            ITinNodeCollection pTinNodeCollection = (ITinNodeCollection)pTinEdit;
            List <CPoint>      CptLt = new List <CPoint>(pTinNodeCollection.NodeCount);

            for (int i = 1; i <= pTinNodeCollection.NodeCount; i++)   //i=1-4: super node
            {
                ITinNode pNode = pTinNodeCollection.GetNode(i);
                CPoint   cpt   = new CPoint(pNode);
                //cpt.intTS = new C5.TreeSet<int>();
                CptLt.Add(cpt);
            }
            //long lngMemoryafterfetching = GC.GetTotalMemory(true);
            //lngMemoryDT = GC.GetTotalMemory(true) - lngMemoryDT;

            //Looking for neighbours based on Breadth First Search

            if (intPtNumLast != -1)
            {
                dblThreshold = dblThreshold * Math.Pow(Convert.ToDouble(intPtNumLast) / Convert.ToDouble(CptLt.Count - 4), 0.5);  //---------------dblThreshold------------------------------------------------//

                //Math.Pow(1.8 / Convert.ToDouble(CptLt.Count - 4), 0.5);//---------------dblThreshold------------------------------------------------//
            }
            //double dblThresholdDT = (1 + Math.Sqrt(2)) / 2 * dblThreshold;

            lngTimeForSearching = System.Environment.TickCount;  //the start time
            SCG.LinkedList <int> intTargetLLt = new SCG.LinkedList <int>();
            SCG.LinkedList <int> intAllLLt    = new SCG.LinkedList <int>();
            for (int i = 0; i < CptLt.Count; i++)
            {
                CptLt[i].isTraversed = true;

                intTargetLLt = new SCG.LinkedList <int>();
                intTargetLLt.AddLast(CptLt[i].pTinNode.Index);
                intAllLLt = new SCG.LinkedList <int>();
                intAllLLt.AddLast(CptLt[i].pTinNode.Index);

                while (intTargetLLt.Count > 0)
                {
                    intTargetLLt = BSF(CptLt[i], ref CptLt, ref intTargetLLt, ref intAllLLt, dblThreshold, ref intOutPut);
                }
                intOutPut--;  //the point will take itself as a close point, so we have to minus 1
                //CptLt[i].intTS.Remove(CptLt[i].pTinNode.Index);
                RestoreIsTraversed(ref CptLt, ref intAllLLt);
                //intAllLLt.Dispose();
            }
            //long lngMemoryaftersearch = GC.GetTotalMemory(true);
            lngTimeForSearching = System.Environment.TickCount - lngTimeForSearching;  //the result time
            lngMemory           = GC.GetTotalMemory(true) - lngMemory;
            pTinEdit.SaveAs(strTINPath + "\\" + pFeatureLayer.Name, true);
            long lngFileSize = CHelpFunc.GetDirectoryLength(strTINPath + "\\" + pFeatureLayer.Name);

            lngMemory  += lngFileSize;
            lngMemoryDT = lngFileSize;

            return(CptLt);
        }
Ejemplo n.º 2
0
        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
        {
            // Get Parameters
            IGPParameter3 inputParameter   = (IGPParameter3)paramvalues.get_Element(0);
            IGPParameter3 polygonParameter = (IGPParameter3)paramvalues.get_Element(1);
            IGPParameter3 outputParameter  = (IGPParameter3)paramvalues.get_Element(2);
            IGPParameter3 fieldParameter   = (IGPParameter3)paramvalues.get_Element(3);

            // UnPackGPValue. This ensures you get the value from either the dataelement or GpVariable (ModelBuilder)
            IGPValue inputParameterValue   = m_GPUtilities.UnpackGPValue(inputParameter);
            IGPValue polygonParameterValue = m_GPUtilities.UnpackGPValue(polygonParameter);
            IGPValue outputParameterValue  = m_GPUtilities.UnpackGPValue(outputParameter);
            IGPValue fieldParameterValue   = m_GPUtilities.UnpackGPValue(fieldParameter);

            // Decode Input Feature Layers
            IFeatureClass inputFeatureClass;
            IFeatureClass polygonFeatureClass;

            IQueryFilter inputFeatureClassQF;
            IQueryFilter polygonFeatureClassQF;

            m_GPUtilities.DecodeFeatureLayer(inputParameterValue, out inputFeatureClass, out inputFeatureClassQF);
            m_GPUtilities.DecodeFeatureLayer(polygonParameterValue, out polygonFeatureClass, out polygonFeatureClassQF);

            if (inputFeatureClass == null)
            {
                message.AddError(2, "Could not open input dataset.");
                return;
            }

            if (polygonFeatureClass == null)
            {
                message.AddError(2, "Could not open clipping polygon dataset.");
                return;
            }

            if (polygonFeatureClass.FeatureCount(null) > 1)
            {
                message.AddWarning("Clipping polygon feature class contains more than one feature.");
            }

            // Create the Geoprocessor
            Geoprocessor gp = new Geoprocessor();

            // Create Output Polygon Feature Class
            CreateFeatureclass cfc      = new CreateFeatureclass();
            IName               name    = m_GPUtilities.CreateFeatureClassName(outputParameterValue.GetAsText());
            IDatasetName        dsName  = name as IDatasetName;
            IFeatureClassName   fcName  = dsName as IFeatureClassName;
            IFeatureDatasetName fdsName = fcName.FeatureDatasetName as IFeatureDatasetName;

            // Check if output is in a FeatureDataset or not. Set the output path parameter for CreateFeatureClass tool.
            if (fdsName != null)
            {
                cfc.out_path = fdsName;
            }
            else
            {
                cfc.out_path = dsName.WorkspaceName.PathName;
            }

            // Set the output Coordinate System for CreateFeatureClass tool.
            // ISpatialReference3 sr = null;
            IGPEnvironment env = envMgr.FindEnvironment("outputCoordinateSystem");

            // Same as Input
            if (env.Value.IsEmpty())
            {
                IGeoDataset ds = inputFeatureClass as IGeoDataset;
                cfc.spatial_reference = ds.SpatialReference as ISpatialReference3;
            }
            // Use the environment setting
            else
            {
                IGPCoordinateSystem cs = env.Value as IGPCoordinateSystem;
                cfc.spatial_reference = cs.SpatialReference as ISpatialReference3;
            }

            // Remaining properties for Create Feature Class Tool
            cfc.out_name      = dsName.Name;
            cfc.geometry_type = "POLYGON";

            // Execute Geoprocessor
            gp.Execute(cfc, null);

            // Get Unique Field
            int    iField      = inputFeatureClass.FindField(fieldParameterValue.GetAsText());
            IField uniqueField = inputFeatureClass.Fields.get_Field(iField);

            // Extract Clipping Polygon Geometry
            IFeature polygonFeature  = polygonFeatureClass.GetFeature(0);
            IPolygon clippingPolygon = (IPolygon)polygonFeature.Shape;

            // Spatial Filter
            ISpatialFilter spatialFilter = new SpatialFilterClass();

            spatialFilter.Geometry   = polygonFeature.ShapeCopy;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

            // Debug Message
            message.AddMessage("Generating TIN...");

            // Create TIN
            ITinEdit tinEdit = new TinClass();

            // Advanced TIN Functions
            ITinAdvanced2 tinAdv = (ITinAdvanced2)tinEdit;

            try
            {
                // Initialize New TIN
                IGeoDataset gds = inputFeatureClass as IGeoDataset;
                tinEdit.InitNew(gds.Extent);

                // Add Mass Points to TIN
                tinEdit.StartEditing();
                tinEdit.AddFromFeatureClass(inputFeatureClass, spatialFilter, uniqueField, uniqueField, esriTinSurfaceType.esriTinMassPoint);
                tinEdit.Refresh();

                // Get TIN Nodes
                ITinNodeCollection tinNodeCollection = (ITinNodeCollection)tinEdit;

                // Report Node Count
                message.AddMessage("Input Node Count: " + inputFeatureClass.FeatureCount(null).ToString());
                message.AddMessage("TIN Node Count: " + tinNodeCollection.NodeCount.ToString());

                // Open Output Feature Class
                IFeatureClass outputFeatureClass = m_GPUtilities.OpenFeatureClassFromString(outputParameterValue.GetAsText());

                // Debug Message
                message.AddMessage("Generating Polygons...");

                // Create Voronoi Polygons
                tinNodeCollection.ConvertToVoronoiRegions(outputFeatureClass, null, clippingPolygon, "", "");

                // Release COM Objects
                tinEdit.StopEditing(false);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tinNodeCollection);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tinEdit);
            }
            catch (Exception ex)
            {
                message.AddError(2, ex.Message);
            }
        }