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")); } } }