protected override void CloseCore()
        {
            if (rTreeIndex != null)
            {
                rTreeIndex.Close();
                rTreeIndex = null;
            }

            configurationInformation = null;
        }
예제 #2
0
 private static int GetRecordCountInternal(ShapeFileFeatureLayer featureLayer)
 {
     if (featureLayer.RequireIndex && File.Exists(featureLayer.IndexPathFilename))
     {
         using (RtreeSpatialIndex index = new RtreeSpatialIndex(featureLayer.IndexPathFilename))
         {
             index.Open();
             int recordCount = index.GetFeatureCount();
             index.Close();
             return(recordCount);
         }
     }
     else
     {
         int          recordCount   = featureLayer.QueryTools.GetCount();
         string       shxFilePath   = Path.ChangeExtension(featureLayer.ShapePathFilename, "shx");
         FileStream   shxFileStream = File.OpenRead(shxFilePath);
         BinaryReader shxReader     = new BinaryReader(shxFileStream);
         try
         {
             shxReader.BaseStream.Seek(shxHeaderLength, SeekOrigin.Begin);
             bool needBreak = false;
             while (shxReader.BaseStream.Position != shxReader.BaseStream.Length)
             {
                 shxReader.BaseStream.Seek(recordLength, SeekOrigin.Current);
                 int length = 0;
                 if (shxReader.BaseStream.Position != shxReader.BaseStream.Length)
                 {
                     length = shxReader.ReadInt32();
                 }
                 else
                 {
                     needBreak = true;
                 }
                 if (length == 0)
                 {
                     recordCount--;
                 }
                 if (needBreak)
                 {
                     break;
                 }
             }
         }
         finally
         {
             shxFileStream.Close();
             shxReader.Close();
         }
         return(recordCount);
     }
 }
        public static void BuildIndexFile(string basPathFilename, string indexPathFilename, Projection projection, string columnName, string regularExpression, BuildIndexMode buildIndexMode, Encoding encoding)
        {
            //Validators.CheckParameterIsNotNullOrEmpty(basPathFilename, "shapePathFileName");
            //Validators.CheckShapeFileNameIsValid(basPathFilename, "shapePathFileName");
            //Validators.CheckParameterIsNotNullOrEmpty(indexPathFilename, "indexPathFileName");
            //Validators.CheckParameterIsNotNull(columnName, "columnName");
            //Validators.CheckParameterIsNotNull(regularExpression, "regularExpression");
            //Validators.CheckParameterIsNotNull(encoding, "encoding");
            //Validators.CheckBuildIndexModeIsValid(buildIndexMode, "buildIndexMode");

            string tmpIndexPathFilename = Path.GetDirectoryName(indexPathFilename) + "\\TMP" + Path.GetFileName(indexPathFilename);

            if (!(File.Exists(indexPathFilename) && File.Exists(Path.ChangeExtension(indexPathFilename, ".ids"))) || buildIndexMode == BuildIndexMode.Rebuild)
            {
                RtreeSpatialIndex rTreeIndex = new RtreeSpatialIndex(tmpIndexPathFilename, GeoFileReadWriteMode.ReadWrite);

                TobinBasFeatureSource featureSource = new TobinBasFeatureSource(basPathFilename);
                featureSource.Encoding     = encoding;
                featureSource.RequireIndex = false;
                featureSource.Open();
                if (projection != null)
                {
                    if (!projection.IsOpen)
                    {
                        projection.Open();
                    }
                }
                try
                {
                    //// TODO Make sure the rtree will open only once.
                    //ShapeFileType shapeType = ShapeFileType.Null;

                    //ShapeFileType currentRecordType = featureSource.basReader.GetShapeFileType();
                    //shapeType = currentRecordType;
                    RtreeSpatialIndex.CreateRectangleSpatialIndex(tmpIndexPathFilename, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
                    rTreeIndex.Open();

                    Collection <BasFeatureEntity> allFeatureEntities = featureSource.basReader.GetAllFeatureEntities();

                    int recordCount = allFeatureEntities.Count;

                    DateTime startProcessTime = DateTime.Now;

                    for (int i = 0; i < recordCount; i++)
                    {
                        BasFeatureEntity currentShape = null;
                        currentShape = allFeatureEntities[i];

                        long   offset = currentShape.Offset;
                        string id     = offset.ToString(CultureInfo.InvariantCulture);;

                        if (currentShape != null)
                        {
                            bool isMatch = false;
                            if (string.IsNullOrEmpty(columnName) && string.IsNullOrEmpty(regularExpression))
                            {
                                isMatch = true;
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(columnName))
                                {
                                    string columnValue = featureSource.GetValueByColumnName(id.ToString(CultureInfo.InvariantCulture), columnName);
                                    isMatch = Regex.IsMatch(columnValue, regularExpression, RegexOptions.IgnoreCase);
                                }
                            }

                            if (isMatch)
                            {
                                currentShape.Id = id;

                                BuildingIndexBasFileFeatureSourceEventArgs buildingIndexBasFileFeatureSourceEventArgs = new BuildingIndexBasFileFeatureSourceEventArgs(recordCount, offset, new Feature(currentShape.Shape), startProcessTime, false, basPathFilename, i);
                                OnBuildingIndex(buildingIndexBasFileFeatureSourceEventArgs);
                                if (!buildingIndexBasFileFeatureSourceEventArgs.Cancel)
                                {
                                    BuildIndex(currentShape, rTreeIndex, projection);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    rTreeIndex.Flush();
                    rTreeIndex.Close();
                }
                finally
                {
                    if (rTreeIndex != null)
                    {
                        rTreeIndex.Close();
                    }
                    if (featureSource != null)
                    {
                        featureSource.Close();
                    }
                    if (projection != null)
                    {
                        projection.Close();
                    }

                    // Replace the old file.
                    MoveFile(tmpIndexPathFilename, indexPathFilename);
                    MoveFile(Path.ChangeExtension(tmpIndexPathFilename, ".ids"), Path.ChangeExtension(indexPathFilename, ".ids"));

                    DeleteFile(tmpIndexPathFilename);
                    DeleteFile(Path.ChangeExtension(tmpIndexPathFilename, ".ids"));
                }
            }
        }