Example #1
0
 private static void ReadColumns(BasFeatureEntity featureEntity, RecordHeader header)
 {
     foreach (KeyValuePair <string, string> item in header.HeaderColumns)
     {
         featureEntity.Columns.Add(item.Key, item.Value);
     }
 }
Example #2
0
        public static BasFeatureEntity Convert(BasEntity entity)
        {
            BasFeatureEntity basFeature = new BasFeatureEntity();

            basFeature.Offset = entity.Offset;
            ReadColumns(basFeature, entity.HeaderResult);
            ReadGeometry(basFeature, entity.CoordinatesResult);
            ReadAnnocation(basFeature, entity.AnnotationsResult);
            return(basFeature);
        }
        private void PushToCache(BasFeatureEntity entity)
        {
            if (!cachedBasFeatureEntities.ContainsKey(entity.Id))
            {
                if (cachedBasFeatureEntities.Count > maxCacheCount)
                {
                    cachedBasFeatureEntities.RemoveAt(0);
                }

                cachedBasFeatureEntities.Add(entity.Id, entity);
            }
        }
        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);
            }
        }
Example #5
0
        public BasFeatureEntity GetFeatureEntityByOffset(long offset)
        {
            BasFeatureEntity feature = null;

            basReader.BaseStream.Seek(offset, SeekOrigin.Begin);

            BasEntity entity = BasEntityParser.Parse(basReader);

            if (entity != null)
            {
                feature = BasEntityToFeatureEntityConverter.Convert(entity);
            }

            return(feature);
        }
Example #6
0
        private static void ReadAnnocation(BasFeatureEntity featureEntity, Collection <RecordAnnotation> annotations)
        {
            foreach (var item in annotations)
            {
                BasAnnotation basAnnotition = new BasAnnotation();
                basAnnotition.TextString = item.Text;
                basAnnotition.Position   = new PointShape(ConvertHelper.LatLonToVertex(item.TextLocationLonlat));

                AnnotationFontStyle fontStyle = new AnnotationFontStyle();
                fontStyle.TextAngle     = ConvertHelper.TextAngleToAngle(item.TextAngle);
                fontStyle.TextSize      = float.Parse(item.TextSize);
                fontStyle.TextFont      = int.Parse(item.TextFont);
                basAnnotition.FontStyle = fontStyle;

                featureEntity.Annotations.Add(basAnnotition);
            }
        }
Example #7
0
        private static void ReadGeometry(BasFeatureEntity featureEntiye, Collection <RecordCoordinate> coordinates)
        {
            if (coordinates.Count > 0)
            {
                //MultilineShape multilineShape = new MultilineShape();
                //foreach (var coord in coordinates)
                //{
                //    Vertex vertex1 = ConvertHelper.LatLonToVertex(coord.StartPointLonLat);
                //    Vertex vertex2 = ConvertHelper.LatLonToVertex(coord.EndPointLonLat);
                //    multilineShape.Lines.Add(new LineShape(new Collection<Vertex>() { vertex1, vertex2 }));
                //}
                //ShapeValidationResult validateResult = multilineShape.Validate(ShapeValidationMode.Simple);
                //if (!validateResult.IsValid)
                //{
                //    throw new ArgumentException();
                //}
                MultipolygonShape multiPolygonShape = new MultipolygonShape();

                PolygonShape tempPolygon = new PolygonShape();
                foreach (var coord in coordinates)
                {
                    Vertex vertex1 = ConvertHelper.LatLonToVertex(coord.StartPointLonLat);
                    Vertex vertex2 = ConvertHelper.LatLonToVertex(coord.EndPointLonLat);
                    tempPolygon.OuterRing.Vertices.Add(vertex1);
                    tempPolygon.OuterRing.Vertices.Add(vertex2);

                    if (coord.EndOfPolygonFlag)
                    {
                        multiPolygonShape.Polygons.Add(tempPolygon);
                        tempPolygon = new PolygonShape();
                    }
                }
                ShapeValidationResult validateResult = multiPolygonShape.Validate(ShapeValidationMode.Simple);
                if (!validateResult.IsValid)
                {
                    throw new ArgumentException();
                }
                featureEntiye.Shape = multiPolygonShape;
            }
        }
        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"));
                }
            }
        }