예제 #1
0
        public List <RecordData> Join()
        {
            List <RecordData> listJoinInfos = new List <RecordData>();

            RecordData joinInfo;

            IFeature        ft;
            IFeature        ftJoined;
            List <IFeature> listSpatialJoinResults;
            List <IFeature> listSpatialJoinResultsFiltered;

            int count             = 0;
            int total             = InputLayer.FeatureCount(null);
            int percent           = 0;
            int updateInterval    = 10;
            int nextUpdatePercent = updateInterval;

            using (ComReleaser comReleaser = new ComReleaser())
            {
                IFeatureCursor cursor = InputLayer.Search(null, true);
                comReleaser.ManageLifetime(cursor);
                bool shouldExecute = true;

                while ((ft = cursor.NextFeature()) != null)
                {
                    if (OnFilterInputFeature != null)
                    {
                        shouldExecute = OnFilterInputFeature(ft);
                    }

                    if (shouldExecute)
                    {
                        listSpatialJoinResults = SpatialJoinMethod.GetJoinFeatures(ft, JoinLayer);

                        // Filter SpatialJoinResult out
                        if (OnFilterSpatialJoinResult != null)
                        {
                            listSpatialJoinResultsFiltered = new List <IFeature>();
                            for (int i = 0; i < listSpatialJoinResults.Count; i++)
                            {
                                ftJoined = listSpatialJoinResults[i];
                                if (OnFilterSpatialJoinResult(ft, ftJoined) == true)
                                {
                                    listSpatialJoinResultsFiltered.Add(ftJoined);
                                }
                            }
                            listSpatialJoinResults = listSpatialJoinResultsFiltered;
                        }

                        // Call back custom function after get join result.
                        OnResultSpatialJoinPerRecord?.Invoke(ft, listSpatialJoinResults);

                        if (listSpatialJoinResults.Count > 0)
                        {
                            // Compose additionFields
                            List <RecordData> additionFieldsRecords = OnComposeSpatialJoinRelationResultPerRecord?.Invoke(ft, listSpatialJoinResults);
                            if (additionFieldsRecords != null &&
                                additionFieldsRecords.Count > 0 &&
                                additionFieldsRecords.Count != listSpatialJoinResults.Count)
                            {
                                throw new Exception("Result of OnComposeSpatialJoinRelationResultPerRecord must return List<RecordData> count = param List<IFeature> count.");
                            }

                            RecordData additionFields;

                            // Compose spatial join result infos
                            for (int i = 0; i < listSpatialJoinResults.Count; i++)
                            {
                                ftJoined = listSpatialJoinResults[i];

                                joinInfo = NewSpatialJoinInfo();
                                joinInfo[FieldInput_ObjectID] = ft.OID;
                                joinInfo[FieldRef_DsPath]     = LayerUtil.GetDataSourcePath(JoinLayer);
                                joinInfo[FieldRef_ObjectID]   = ftJoined.OID;

                                #region consider use later
                                //// Compose additionFields
                                //List<RecordData> additionFieldsRecords = OnComposeSpatialJoinRelationResultPerRecord?.Invoke(ft, listSpatialJoinResults);
                                //if (additionFieldsRecords != null)
                                //{
                                //    foreach (var additionFields in additionFieldsRecords)
                                //    {
                                //        string[] additionalFieldNames = additionFields.GetFieldNames();
                                //        string additionalFieldName;

                                //        for (int j = 0; j < additionalFieldNames.Length; j++)
                                //        {
                                //            additionalFieldName = additionalFieldNames[j];
                                //            joinInfo[additionalFieldName] = additionFields[additionalFieldName];
                                //        }
                                //    }
                                //}
                                #endregion

                                // Add additionFields to joinInfo
                                if (additionFieldsRecords != null && additionFieldsRecords.Count > 0)
                                {
                                    additionFields = additionFieldsRecords[i];
                                    string[] additionalFieldNames = additionFields.GetFieldNames();
                                    string   additionalFieldName;

                                    for (int j = 0; j < additionalFieldNames.Length; j++)
                                    {
                                        additionalFieldName           = additionalFieldNames[j];
                                        joinInfo[additionalFieldName] = additionFields[additionalFieldName];
                                    }
                                }

                                listJoinInfos.Add(joinInfo);
                            }
                        }
                    }

                    // Progress
                    count++;
                    percent = CalPercent(count, total);

                    if (percent >= nextUpdatePercent)
                    {
                        OnProgressCallback?.Invoke(percent);
                        nextUpdatePercent += updateInterval;
                    }
                }
            }

            return(listJoinInfos);
        }