Example #1
0
        protected override void OpenCore()
        {
            if (!IsOpenCore)
            {
                isOpen = true;

                if (!File.Exists(idxPathFileName))
                {
                    RtreeSpatialIndex.CreateRectangleSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);

                    rTreeIndex = new RtreeSpatialIndex(idxPathFileName, RtreeSpatialIndexReadWriteMode.ReadWrite);
                    rTreeIndex.Open();

                    foreach (var rasterLayer in rasterLayers)
                    {
                        rasterLayer.Open();
                        RectangleShape boudingBox = rasterLayer.GetBoundingBox();

                        Feature feature = new Feature(boudingBox);
                        feature.Id = rasterLayer.PathFilename;
                        rTreeIndex.Add(feature);
                    }
                }
                else
                {
                    rTreeIndex = new RtreeSpatialIndex(idxPathFileName, RtreeSpatialIndexReadWriteMode.ReadOnly);
                    rTreeIndex.Open();
                }
            }
        }
        protected override void CloseCore()
        {
            if (rTreeIndex != null)
            {
                rTreeIndex.Close();
                rTreeIndex = null;
            }

            configurationInformation = null;
        }
        private void OpenRtree(string idxPathFileName, GeoFileReadWriteMode rTreeFileAccess)
        {
            if (RequireIndex)
            {
                if (rTreeIndex == null)
                {
                    rTreeIndex = new RtreeSpatialIndex(idxPathFileName, rTreeFileAccess);
                }

                rTreeIndex.Open();
            }
        }
Example #4
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);
     }
 }
        private static void BuildIndex(BasFeatureEntity featureEntity, RtreeSpatialIndex openedRtree, Projection openedProjection)
        {
            if (featureEntity != null)
            {
                BaseShape newShape = featureEntity.Shape;
                if (openedProjection != null)
                {
                    newShape    = openedProjection.ConvertToExternalProjection(featureEntity.Shape);
                    newShape.Id = featureEntity.Id;
                }

                openedRtree.Add(newShape);
            }
        }
        private void OpenRtree(GeoFileReadWriteMode rTreeFileAccess)
        {
            if (rTreeIndex == null && requireIndex)
            {
                rTreeIndex = new RtreeSpatialIndex(indexPathFileName, rTreeFileAccess);
            }

            //rTreeIndex.StreamLoading += new EventHandler<StreamLoadingEventArgs>(ShapeFileFeatureSource_StreamLoading);
            //rTreeIndex.IdsEngine.StreamLoading += new EventHandler<StreamLoadingEventArgs>(ShapeFileFeatureSource_StreamLoading);

            if (requireIndex)
            {
                rTreeIndex.Open();
            }

            //if (!rTreeIndex.HasIdx && requireIndex)
            //{
            //    throw new InvalidOperationException(ExceptionDescription.IndexFileNotExisted);
            //}
        }
        private static void CreateIndexFile(string idxPathFileName, string sourceShapeFilePath)
        {
            ShapeFileFeatureLayer sourceLayer = new ShapeFileFeatureLayer(sourceShapeFilePath);

            sourceLayer.RequireIndex = false;
            ShapeFileType shapeFileType = ShapeFileType.Null;

            sourceLayer.SafeProcess(() =>
            {
                shapeFileType = sourceLayer.GetShapeFileType();
            });
            //sourceLayer.Open();
            //ShapeFileType shapeFileType = sourceLayer.GetShapeFileType();
            //sourceLayer.Close();

            if (shapeFileType == ShapeFileType.Point || shapeFileType == ShapeFileType.PointZ || shapeFileType == ShapeFileType.PointM)
            {
                RtreeSpatialIndex.CreatePointSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
            }
            else
            {
                RtreeSpatialIndex.CreateRectangleSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
            }
        }
        protected virtual void BuildIndexFileCore(bool rebuild)
        {
            string idxPathFileName = Path.ChangeExtension(CsvPathFileName, ".idx");
            string idsPathFileName = Path.ChangeExtension(CsvPathFileName, ".ids");

            if (!File.Exists(idxPathFileName) && !File.Exists(idsPathFileName) || rebuild)
            {
                if (!string.IsNullOrEmpty(WktColumnName))
                {
                    RtreeSpatialIndex.CreateRectangleSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
                }
                else
                {
                    RtreeSpatialIndex.CreatePointSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
                }

                using (RtreeSpatialIndex tempRTree = new RtreeSpatialIndex(idxPathFileName, GeoFileReadWriteMode.ReadWrite))
                {
                    tempRTree.Open();
                    bool     isCanceled = false;
                    DateTime startDate  = DateTime.Now;
                    using (var csvReader = CreateCsvReader())
                    {
                        var allRecords = csvReader.DataRecords.ToArray();
                        int index      = 0;
                        foreach (var currentDataRecord in allRecords)
                        {
                            index++;
                            var feature = GetFeature(index.ToString(), currentDataRecord);
                            if (feature != null)
                            {
                                tempRTree.Add(feature);
                                BuildingIndexCsvFeatureSourceEventArgs e = new BuildingIndexCsvFeatureSourceEventArgs(allRecords.Length, index, feature, startDate, false);
                                OnBuildingIndex(e);
                                if (e.Cancel)
                                {
                                    isCanceled = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!isCanceled)
                    {
                        tempRTree.Flush();
                    }
                    else
                    {
                        if (File.Exists(idxPathFileName))
                        {
                            File.Delete(idxPathFileName);
                        }
                        if (File.Exists(idsPathFileName))
                        {
                            File.Delete(idsPathFileName);
                        }
                    }
                }
            }
        }
        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"));
                }
            }
        }