Exemplo n.º 1
0
        public ShapeFileRW(string filename)
        {
            shapefilename = filename;

            FileStream   fsr = new FileStream(shapefilename, FileMode.Open);
            BinaryReader br  = new BinaryReader(fsr);

            byte[]   buff   = br.ReadBytes(Marshal.SizeOf(typeof(ShapeFileHeader)));
            GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);

            shapefileheader = (ShapeFileHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ShapeFileHeader));
            handle.Free();//Give control of the buffer back to the GC



            ShapeType = br.ReadInt32();

            buff            = br.ReadBytes(Marshal.SizeOf(typeof(ShapeFileExtent)));
            handle          = GCHandle.Alloc(buff, GCHandleType.Pinned);
            shapefileextent = (ShapeFileExtent)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ShapeFileExtent));
            handle.Free();//Give control of the buffer back to the GC

            LayerExtent = new MapExtent(shapefileextent.Xmin, shapefileextent.Xmax,
                                        shapefileextent.Ymin, shapefileextent.Ymax);

            ReadSpatialObject(br);

            br.Close();
            fsr.Close();
        }
Exemplo n.º 2
0
        public ZipArchiveProblems Validate(ZipArchiveEntry entry)
        {
            if (entry == null) throw new ArgumentNullException(nameof(entry));

            var problems = ZipArchiveProblems.None;

            using (var stream = entry.Open())
            using (var reader = new BinaryReader(stream, _encoding))
            {
                ShapeFileHeader header = null;
                try
                {
                    header = ShapeFileHeader.Read(reader);
                }
                catch (Exception exception)
                {
                    problems += entry.HasShapeHeaderFormatError(exception);
                }

                if (header != null)
                {
                    using (var records = header.CreateShapeRecordEnumerator(reader))
                    {
                        problems += _recordValidator.Validate(entry, records);
                    }
                }
            }

            return problems;
        }
Exemplo n.º 3
0
        static ShapeFileHeader ReadFileHeader(BinaryReader br)
        {
            byte[]          buff   = br.ReadBytes(Marshal.SizeOf(typeof(ShapeFileHeader)));
            GCHandle        handle = GCHandle.Alloc(buff, GCHandleType.Pinned);
            ShapeFileHeader header = (ShapeFileHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ShapeFileHeader));

            handle.Free();
            return(header);
        }
        public TranslatedChanges Translate(ZipArchiveEntry entry, TranslatedChanges changes)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            if (changes == null)
            {
                throw new ArgumentNullException(nameof(changes));
            }

            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream, _encoding))
                {
                    var header     = ShapeFileHeader.Read(reader);
                    var enumerator = header.CreateShapeRecordEnumerator(reader);
                    return(_recordTranslator.Translate(entry, enumerator, changes));
                }
        }
        public void ValidatePassesExpectedShapeRecordsToShapeRecordValidator()
        {
            var validator = new CollectShapeRecordValidator();
            var sut       = new ZipArchiveShapeEntryValidator(Encoding.UTF8, validator);
            var records   = _fixture.CreateMany <ShapeRecord>(2).ToArray();
            var fileSize  = records.Aggregate(ShapeFileHeader.Length, (length, record) => length.Plus(record.Length));
            var header    = new ShapeFileHeader(
                fileSize,
                ShapeType.Point,
                BoundingBox3D.Empty);

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                        using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
                        {
                            header.Write(writer);
                            foreach (var record in records)
                            {
                                record.Write(writer);
                            }

                            entryStream.Flush();
                        }
                }

                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(ZipArchiveProblems.None, result);
                    Assert.Equal(records, validator.Collected, new ShapeRecordEqualityComparer());
                }
            }
        }
        public void ValidateReturnsExpectedResultWhenShapeRecordValidatorReturnsErrors()
        {
            var problems = new FileProblem[]
            {
                new FileError("file1", "error1", new ProblemParameter("parameter1", "value1")),
                new FileWarning("file2", "error2", new ProblemParameter("parameter2", "value2"))
            };
            var sut = new ZipArchiveShapeEntryValidator(
                Encoding.UTF8,
                new FakeShapeRecordValidator(problems));
            var header = new ShapeFileHeader(
                ShapeFileHeader.Length,
                ShapeType.Point,
                BoundingBox3D.Empty);

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                        using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
                        {
                            header.Write(writer);
                            entryStream.Flush();
                        }
                }

                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(
                        ZipArchiveProblems.None.AddRange(problems),
                        result);
                }
            }
        }
        public void TranslateReturnsExpectedResultWhenShapeRecordTranslatorReturnsChanges()
        {
            var changes = TranslatedChanges.Empty
                          .Append(new Uploads.AddRoadNode(new RecordNumber(1), new RoadNodeId(1), RoadNodeType.RealNode))
                          .Append(new Uploads.AddRoadNode(new RecordNumber(1), new RoadNodeId(1), RoadNodeType.RealNode));
            var sut = new ZipArchiveShapeEntryTranslator(
                Encoding.UTF8,
                new FakeShapeRecordTranslator(ignored => changes));
            var header = new ShapeFileHeader(
                ShapeFileHeader.Length,
                ShapeType.Point,
                BoundingBox3D.Empty);

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                        using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
                        {
                            header.Write(writer);
                            entryStream.Flush();
                        }
                }

                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Translate(entry, TranslatedChanges.Empty);

                    Assert.Equal(
                        changes,
                        result,
                        new TranslatedChangeEqualityComparer());
                }
            }
        }
Exemplo n.º 8
0
        public async Task WriteAsync(ZipArchive archive, BackOfficeContext context, CancellationToken cancellationToken)
        {
            if (archive == null)
            {
                throw new ArgumentNullException(nameof(archive));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var count = await context.RoadNodes.CountAsync(cancellationToken);

            var dbfEntry  = archive.CreateEntry("Wegknoop.dbf");
            var dbfHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(count),
                RoadNodeDbaseRecord.Schema
                );

            using (var dbfEntryStream = dbfEntry.Open())
                using (var dbfWriter =
                           new DbaseBinaryWriter(
                               dbfHeader,
                               new BinaryWriter(dbfEntryStream, _encoding, true)))
                {
                    var dbfRecord = new RoadNodeDbaseRecord();
                    foreach (var data in context.RoadNodes.OrderBy(_ => _.Id).Select(_ => _.DbaseRecord))
                    {
                        dbfRecord.FromBytes(data, _manager, _encoding);
                        dbfWriter.Write(dbfRecord);
                    }
                    dbfWriter.Writer.Flush();
                    await dbfEntryStream.FlushAsync(cancellationToken);
                }

            var shpBoundingBox =
                (await context.RoadNodeBoundingBox.SingleOrDefaultAsync(cancellationToken))?.ToBoundingBox3D()
                ?? new BoundingBox3D(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
            var info = await context.RoadNetworkInfo.SingleAsync(cancellationToken);

            var shpEntry  = archive.CreateEntry("Wegknoop.shp");
            var shpHeader = new ShapeFileHeader(
                new WordLength(info.TotalRoadNodeShapeLength),
                ShapeType.Point,
                shpBoundingBox);

            using (var shpEntryStream = shpEntry.Open())
                using (var shpWriter =
                           new ShapeBinaryWriter(
                               shpHeader,
                               new BinaryWriter(shpEntryStream, _encoding, true)))
                {
                    var number = RecordNumber.Initial;
                    foreach (var data in context.RoadNodes.OrderBy(_ => _.Id).Select(_ => _.ShapeRecordContent))
                    {
                        shpWriter.Write(
                            ShapeContentFactory
                            .FromBytes(data, _manager, _encoding)
                            .RecordAs(number)
                            );
                        number = number.Next();
                    }
                    shpWriter.Writer.Flush();
                    await shpEntryStream.FlushAsync(cancellationToken);
                }

            var shxEntry  = archive.CreateEntry("Wegknoop.shx");
            var shxHeader = shpHeader.ForIndex(new ShapeRecordCount(count));

            using (var shxEntryStream = shxEntry.Open())
                using (var shxWriter =
                           new ShapeIndexBinaryWriter(
                               shxHeader,
                               new BinaryWriter(shxEntryStream, _encoding, true)))
                {
                    var offset = ShapeIndexRecord.InitialOffset;
                    var number = RecordNumber.Initial;
                    foreach (var data in context.RoadNodes.OrderBy(_ => _.Id).Select(_ => _.ShapeRecordContent))
                    {
                        var shpRecord = ShapeContentFactory
                                        .FromBytes(data, _manager, _encoding)
                                        .RecordAs(number);
                        shxWriter.Write(shpRecord.IndexAt(offset));
                        number = number.Next();
                        offset = offset.Plus(shpRecord.Length);
                    }
                    shxWriter.Writer.Flush();
                    await shxEntryStream.FlushAsync(cancellationToken);
                }
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            Directory.CreateDirectory("output");

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            var node1Record = new RoadNodeChangeDbaseRecord();

            node1Record.TYPE.Value       = (short)RoadNodeType.EndNode.Translation.Identifier;
            node1Record.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;
            node1Record.WEGKNOOPID.Value = 1;
            var node1Shape = new PointShapeContent(new Point(0.0, 0.0));

            var node2Record = new RoadNodeChangeDbaseRecord();

            node2Record.TYPE.Value       = (short)RoadNodeType.EndNode.Translation.Identifier;
            node2Record.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;
            node2Record.WEGKNOOPID.Value = 2;
            var node2Shape = new PointShapeContent(new Point(0.0, 1.0));

            var segment1Record = new RoadSegmentChangeDbaseRecord();

            segment1Record.WS_OIDN.Value    = 1;
            segment1Record.B_WK_OIDN.Value  = 1;
            segment1Record.E_WK_OIDN.Value  = 2;
            segment1Record.TGBEP.Value      = (short)RoadSegmentAccessRestriction.PublicRoad.Translation.Identifier;
            segment1Record.STATUS.Value     = RoadSegmentStatus.InUse.Translation.Identifier;
            segment1Record.WEGCAT.Value     = RoadSegmentCategory.SecondaryRoad.Translation.Identifier;
            segment1Record.METHODE.Value    = (short)RoadSegmentGeometryDrawMethod.Measured.Translation.Identifier;
            segment1Record.MORFOLOGIE.Value = (short)RoadSegmentMorphology.Motorway.Translation.Identifier;
            segment1Record.LSTRNMID.Value   = 123;
            segment1Record.RSTRNMID.Value   = 456;
            segment1Record.BEHEERDER.Value  = "-8";
            segment1Record.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;
            var segmentShape = new PolyLineMShapeContent(GeometryTranslator.FromGeometryMultiLineString(new NetTopologySuite.Geometries.MultiLineString(new NetTopologySuite.Geometries.LineString[]
            {
                new NetTopologySuite.Geometries.LineString(new NetTopologySuite.Geometries.Implementation.CoordinateArraySequence(new[]
                {
                    new NetTopologySuite.Geometries.CoordinateM(0.0, 0.0, 0.0),
                    new NetTopologySuite.Geometries.CoordinateM(0.0, 1.0, 1.0)
                }), GeometryConfiguration.GeometryFactory)
            })));

            var laneRecord = new RoadSegmentLaneChangeDbaseRecord();

            laneRecord.RS_OIDN.Value    = 1;
            laneRecord.WS_OIDN.Value    = 1;
            laneRecord.RICHTING.Value   = (short)RoadSegmentLaneDirection.Independent.Translation.Identifier;
            laneRecord.AANTAL.Value     = 2;
            laneRecord.VANPOSITIE.Value = 0.0;
            laneRecord.TOTPOSITIE.Value = 1.0;
            laneRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var surfaceRecord = new RoadSegmentSurfaceChangeDbaseRecord();

            surfaceRecord.WV_OIDN.Value    = 1;
            surfaceRecord.WS_OIDN.Value    = 1;
            surfaceRecord.TYPE.Value       = (short)RoadSegmentSurfaceType.SolidSurface.Translation.Identifier;
            surfaceRecord.VANPOSITIE.Value = 0.0;
            surfaceRecord.TOTPOSITIE.Value = 1.0;
            surfaceRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var widthRecord = new RoadSegmentWidthChangeDbaseRecord();

            widthRecord.WB_OIDN.Value    = 1;
            widthRecord.WS_OIDN.Value    = 1;
            widthRecord.BREEDTE.Value    = 1;
            widthRecord.VANPOSITIE.Value = 0.0;
            widthRecord.TOTPOSITIE.Value = 1.0;
            widthRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var nodeDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(2),
                new RoadNodeChangeDbaseSchema());
            var nodeShapeHeader = new ShapeFileHeader(
                ShapeFileHeader.Length.Plus(node1Shape.ContentLength).Plus(node2Shape.ContentLength),
                ShapeType.Point,
                BoundingBox3D
                .FromGeometry(node1Shape.Shape)
                .ExpandWith(
                    BoundingBox3D.FromGeometry(node2Shape.Shape)
                    )
                );

            // node

            using (var nodeDbaseFile = new BinaryWriter(File.OpenWrite("output/WEGKNOOP_ALL.dbf")))
                using (var nodeDbaseWriter = new DbaseBinaryWriter(nodeDbaseHeader, nodeDbaseFile))
                {
                    nodeDbaseWriter.Write(node1Record);
                    nodeDbaseWriter.Write(node2Record);
                }
            using (var nodeShapeIndexFile = new BinaryWriter(File.OpenWrite("output/WEGKNOOP_ALL.shx")))
                using (var nodeShapeFile = new BinaryWriter(File.OpenWrite("output/WEGKNOOP_ALL.shp")))
                    using (var nodeShapeWriter = new ShapeBinaryWriter(nodeShapeHeader, nodeShapeFile))
                        using (var nodeShapeIndexWriter =
                                   new ShapeIndexBinaryWriter(nodeShapeHeader.ForIndex(new ShapeRecordCount(2)), nodeShapeIndexFile))
                        {
                            var node1ShapeRecord = node1Shape.RecordAs(RecordNumber.Initial);
                            nodeShapeWriter.Write(node1ShapeRecord);
                            var node2ShapeRecord = node2Shape.RecordAs(RecordNumber.Initial.Next());
                            nodeShapeWriter.Write(node2ShapeRecord);

                            var offset = ShapeIndexRecord.InitialOffset;
                            var node1ShapeIndexRecord = node1ShapeRecord.IndexAt(offset);
                            var node2ShapeIndexRecord = node2ShapeRecord.IndexAt(offset.Plus(node1ShapeRecord.Length));
                            nodeShapeIndexWriter.Write(node1ShapeIndexRecord);
                            nodeShapeIndexWriter.Write(node2ShapeIndexRecord);
                        }

            // segment

            var segmentDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new RoadSegmentChangeDbaseSchema());
            var segmentShapeHeader = new ShapeFileHeader(
                ShapeFileHeader.Length.Plus(segmentShape.ContentLength),
                ShapeType.PolyLineM,
                BoundingBox3D
                .FromGeometry(segmentShape.Shape)
                );

            using (var segmentDbaseFile = new BinaryWriter(File.OpenWrite("output/WEGSEGMENT_ALL.dbf")))
                using (var segmentDbaseWriter = new DbaseBinaryWriter(segmentDbaseHeader, segmentDbaseFile))
                {
                    segmentDbaseWriter.Write(segment1Record);
                }
            using (var segmentShapeIndexFile = new BinaryWriter(File.OpenWrite("output/WEGSEGMENT_ALL.shx")))
                using (var segmentShapeFile = new BinaryWriter(File.OpenWrite("output/WEGSEGMENT_ALL.shp")))
                    using (var segmentShapeWriter = new ShapeBinaryWriter(segmentShapeHeader, segmentShapeFile))
                        using (var segmentShapeIndexWriter =
                                   new ShapeIndexBinaryWriter(segmentShapeHeader.ForIndex(new ShapeRecordCount(2)), segmentShapeIndexFile))
                        {
                            var segment1ShapeRecord = segmentShape.RecordAs(RecordNumber.Initial);
                            segmentShapeWriter.Write(segment1ShapeRecord);

                            var offset = ShapeIndexRecord.InitialOffset;
                            var segment1ShapeIndexRecord = segment1ShapeRecord.IndexAt(offset);
                            segmentShapeIndexWriter.Write(segment1ShapeIndexRecord);
                        }

            // lane
            var laneDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new RoadSegmentLaneChangeDbaseSchema());

            using (var laneDbaseFile = new BinaryWriter(File.OpenWrite("output/ATTRIJSTROKEN_ALL.DBF")))
                using (var laneDbaseWriter = new DbaseBinaryWriter(laneDbaseHeader, laneDbaseFile))
                {
                    laneDbaseWriter.Write(laneRecord);
                }

            // width
            var widthDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new RoadSegmentWidthChangeDbaseSchema());

            using (var widthDbaseFile = new BinaryWriter(File.OpenWrite("output/ATTWEGBREEDTE_ALL.DBF")))
                using (var widthDbaseWriter = new DbaseBinaryWriter(widthDbaseHeader, widthDbaseFile))
                {
                    widthDbaseWriter.Write(widthRecord);
                }

            // surface
            var surfaceDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new RoadSegmentSurfaceChangeDbaseSchema());

            using (var surfaceDbaseFile = new BinaryWriter(File.OpenWrite("output/ATTWEGVERHARDING_ALL.DBF")))
                using (var surfaceDbaseWriter = new DbaseBinaryWriter(surfaceDbaseHeader, surfaceDbaseFile))
                {
                    surfaceDbaseWriter.Write(surfaceRecord);
                }

            // european road
            var europeanRoadRecord = new EuropeanRoadChangeDbaseRecord();

            europeanRoadRecord.EU_OIDN.Value    = 1;
            europeanRoadRecord.WS_OIDN.Value    = 1;
            europeanRoadRecord.EUNUMMER.Value   = EuropeanRoadNumber.E40.ToString();
            europeanRoadRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var europeanDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new EuropeanRoadChangeDbaseSchema());

            using (var europeanDbaseFile = new BinaryWriter(File.OpenWrite("output/ATTEUROPWEG_ALL.DBF")))
                using (var europeanDbaseWriter = new DbaseBinaryWriter(europeanDbaseHeader, europeanDbaseFile))
                {
                    europeanDbaseWriter.Write(europeanRoadRecord);
                }

            // national road
            var nationalRoadRecord = new NationalRoadChangeDbaseRecord();

            nationalRoadRecord.NW_OIDN.Value    = 1;
            nationalRoadRecord.WS_OIDN.Value    = 1;
            nationalRoadRecord.IDENT2.Value     = NationalRoadNumber.All[5].ToString();
            nationalRoadRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var nationalDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new NationalRoadChangeDbaseSchema());

            using (var nationalDbaseFile = new BinaryWriter(File.OpenWrite("output/ATTNATIONWEG_ALL.DBF")))
                using (var nationalDbaseWriter = new DbaseBinaryWriter(nationalDbaseHeader, nationalDbaseFile))
                {
                    nationalDbaseWriter.Write(nationalRoadRecord);
                }

            // numbered road
            var numberedRoadRecord = new NumberedRoadChangeDbaseRecord();

            numberedRoadRecord.GW_OIDN.Value    = 1;
            numberedRoadRecord.WS_OIDN.Value    = 1;
            numberedRoadRecord.IDENT8.Value     = NumberedRoadNumber.Parse("A0001231").ToString();
            numberedRoadRecord.RICHTING.Value   = (short)RoadSegmentNumberedRoadDirection.Backward.Translation.Identifier;
            numberedRoadRecord.VOLGNUMMER.Value = 1;
            numberedRoadRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var numberedDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new NumberedRoadChangeDbaseSchema());

            using (var numberedDbaseFile = new BinaryWriter(File.OpenWrite("output/ATTGENUMWEG_ALL.DBF")))
                using (var numberedDbaseWriter = new DbaseBinaryWriter(numberedDbaseHeader, numberedDbaseFile))
                {
                    numberedDbaseWriter.Write(numberedRoadRecord);
                }

            // grade separated junction
            var gradeSeparatedJunctionRecord = new GradeSeparatedJunctionChangeDbaseRecord();

            gradeSeparatedJunctionRecord.OK_OIDN.Value    = 1;
            gradeSeparatedJunctionRecord.BO_WS_OIDN.Value = 1;
            gradeSeparatedJunctionRecord.ON_WS_OIDN.Value = 1;
            gradeSeparatedJunctionRecord.TYPE.Value       = (short)GradeSeparatedJunctionType.Unknown.Translation.Identifier;
            gradeSeparatedJunctionRecord.RECORDTYPE.Value = (short)RecordType.Added.Translation.Identifier;

            var gradeSeparatedJunctionDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                new GradeSeparatedJunctionChangeDbaseSchema());

            using (var gradeSeparatedJunctionDbaseFile = new BinaryWriter(File.OpenWrite("output/RLTOGKRUISING_ALL.DBF")))
                using (var gradeSeparatedJunctionDbaseWriter = new DbaseBinaryWriter(gradeSeparatedJunctionDbaseHeader, gradeSeparatedJunctionDbaseFile))
                {
                    gradeSeparatedJunctionDbaseWriter.Write(gradeSeparatedJunctionRecord);
                }


            // transaction zone
            var transactionZoneRecord = new TransactionZoneDbaseRecord();

            transactionZoneRecord.ORG.Value        = "11053";
            transactionZoneRecord.OPERATOR.Value   = "Yves Reynhout";
            transactionZoneRecord.TYPE.Value       = 1;
            transactionZoneRecord.SOURCE_ID.Value  = 1;
            transactionZoneRecord.BESCHRIJV.Value  = "Nieuwe wijk";
            transactionZoneRecord.APPLICATIE.Value = "Wegenregister BLL";

            var transactionZoneDbaseHeader = new DbaseFileHeader(
                DateTime.Now,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(1),
                TransactionZoneDbaseRecord.Schema);

            using (var transactionZoneDbaseFile = new BinaryWriter(File.OpenWrite("output/TRANSACTIEZONE.DBF")))
                using (var transactionZoneDbaseWriter = new DbaseBinaryWriter(transactionZoneDbaseHeader, transactionZoneDbaseFile))
                {
                    transactionZoneDbaseWriter.Write(transactionZoneRecord);
                }

            var suffix =
                DateTimeOffset.UtcNow.ToString("yyyyMMdd") + "-" +
                Convert.ToInt32(DateTimeOffset.UtcNow.TimeOfDay.TotalSeconds);

            using (var archiveStream = File.OpenWrite($"output/oplading-{suffix}.zip"))
                using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
                {
                    foreach (var file in Directory.EnumerateFiles("output"))
                    {
                        if (file.ToLowerInvariant().EndsWith(".dbf") ||
                            file.ToLowerInvariant().EndsWith(".shp") ||
                            file.ToLowerInvariant().EndsWith(".shx"))
                        {
                            var entry = archive.CreateEntry(Path.GetFileName(file));
                            using (var fileStream = File.OpenRead(file))
                                using (var entryStream = entry.Open())
                                {
                                    fileStream.CopyTo(entryStream);
                                }
                        }
                    }
                }
        }
Exemplo n.º 10
0
        public static GISLayer ReadShapeFile(string shpfilename)
        {
            FileStream      fsr       = new FileStream(shpfilename, FileMode.Open);
            BinaryReader    br        = new BinaryReader(fsr);
            ShapeFileHeader sfh       = ReadFileHeader(br);
            SHAPETYPE       ShapeType = SHAPETYPE.POINT;

            if (sfh.ShapeType == 1)
            {
                ShapeType = SHAPETYPE.POINT;
            }
            else if (sfh.ShapeType == 3)
            {
                ShapeType = SHAPETYPE.LINE;
            }
            else if (sfh.ShapeType == 5)
            {
                ShapeType = SHAPETYPE.POLYGON;
            }
            else
            {
                return(null);
            }
            GISExtent extent = new GISExtent(new GISVertex(sfh.Xmin, sfh.Ymin), new GISVertex(sfh.Xmax, sfh.Ymax));

            string    dbffilename = shpfilename.Replace(".shp", ".dbf");
            DataTable table       = ReadDBF(dbffilename);
            GISLayer  layer       = new GISLayer(shpfilename, ShapeType, extent, ReadFields(table));
            int       rowindex    = 0;

            while (br.PeekChar() != -1)
            {
                RecordHeader rh            = ReadRecordHeader(br);
                int          RecordLength  = FromBigToLittle(rh.RecordLength);
                byte[]       RecordContent = br.ReadBytes(RecordLength * 2 - 4);
                if (ShapeType == SHAPETYPE.POINT)
                {
                    GISPoint   onepoint   = ReadPoint(RecordContent);
                    GISFeature onefeature = new GISFeature(onepoint, ReadAttribute(table, rowindex));
                    layer.Features.Add(onefeature);
                }
                if (ShapeType == SHAPETYPE.LINE)
                {
                    List <GISLine> lines = ReadLines(RecordContent);
                    for (int i = 0; i < lines.Count; i++)
                    {
                        GISFeature onefeature = new GISFeature(lines[i], ReadAttribute(table, rowindex));
                        layer.Features.Add(onefeature);
                    }
                }
                if (ShapeType == SHAPETYPE.POLYGON)
                {
                    List <GISPolygon> polygons = ReadPolygons(RecordContent);
                    for (int i = 0; i < polygons.Count; i++)
                    {
                        GISFeature onefeature = new GISFeature(polygons[i], ReadAttribute(table, rowindex));
                        layer.Features.Add(onefeature);
                    }
                }
                rowindex++;

                //其它代码,用于处理RecordContent
            }
            br.Close();
            fsr.Close();
            return(layer);
        }