private static IGeometry Read(PostGis2GeometryHeader header, BinaryReader reader)
        {
            var geometryType = (PostGis2GeometryType)reader.ReadUInt32();

            switch (geometryType)
            {
            case PostGis2GeometryType.Point:
                return(header.Factory.CreatePoint(ReadSequence(header, reader, -1)));

            case PostGis2GeometryType.LineString:
                return(ReadLineString(header, reader, -1, false));

            case PostGis2GeometryType.Polygon:
                return(ReadPolygon(header, reader));

            case PostGis2GeometryType.MultiPoint:
                return(ReadMultiPoint(header, reader));

            case PostGis2GeometryType.MultiLineString:
                return(ReadMultiLineString(header, reader));

            case PostGis2GeometryType.MultiPolygon:
                return(ReadMultiPolygon(header, reader));

            case PostGis2GeometryType.GeometryCollection:
                return(ReadGeometryCollection(header, reader));

            default:
                throw new NotSupportedException();
            }
        }
Example #2
0
        private static void Write(IGeometry geometry, BinaryWriter writer, Ordinates handleOrdinates, bool isGeodetic)
        {
            var pgh = new PostGis2GeometryHeader(geometry, handleOrdinates)
            {
                IsGeodetic = isGeodetic
            };

            pgh.Write(writer);

            switch (geometry.OgcGeometryType)
            {
            case OgcGeometryType.Point:
                WritePoint((IPoint)geometry, pgh, writer);
                break;

            case OgcGeometryType.LineString:
                WriteLineString((ILineString)geometry, pgh, writer);
                break;

            case OgcGeometryType.Polygon:
                WritePolygon((IPolygon)geometry, pgh, writer);
                break;

            case OgcGeometryType.MultiPoint:
            case OgcGeometryType.MultiLineString:
            case OgcGeometryType.MultiPolygon:
            case OgcGeometryType.GeometryCollection:
                WriteCollection((IGeometryCollection)geometry, pgh, writer);
                break;

            default:
                throw new NotSupportedException();
            }
        }
Example #3
0
        private static void WritePolygon(IPolygon geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
        {
            writer.Write((uint)PostGis2GeometryType.Polygon);
            if (geometry.IsEmpty)
            {
                writer.Write(0);
                return;
            }

            var numRings = 1 + geometry.NumInteriorRings;

            //shell
            writer.Write(numRings);
            writer.Write(geometry.Shell.NumPoints);

            //holes
            for (var i = 0; i < geometry.NumInteriorRings; i++)
            {
                writer.Write(geometry.GetInteriorRingN(i).NumPoints);
            }

            //pad
            if (numRings % 2 != 0)
            {
                writer.Write(0);
            }

            WriteSequence(geometry.Shell.CoordinateSequence, pgh, writer, geometry.Shell.NumPoints);
            for (var i = 0; i < geometry.NumInteriorRings; i++)
            {
                var sequence = geometry.GetInteriorRingN(i).CoordinateSequence;
                WriteSequence(sequence, pgh, writer, sequence.Count);
            }
        }
Example #4
0
        private static void WriteCollection(IGeometryCollection geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
        {
            writer.Write((uint)geometry.OgcGeometryType.ToPostGis2());
            if (geometry.IsEmpty)
            {
                writer.Write(0);
                return;
            }

            for (var i = 0; i < geometry.NumGeometries; i++)
            {
                Write(geometry.GetGeometryN(i), writer, pgh.Ordinates, pgh.IsGeodetic);
            }
        }
Example #5
0
        private static void WriteSequence(ICoordinateSequence sequence, PostGis2GeometryHeader pgh, BinaryWriter writer, int numPoints)
        {
            if (numPoints == -1)
            {
                numPoints = sequence.Count;
                writer.Write(numPoints);
            }

            var ordinateIndices = pgh.OrdinateIndices;

            for (var i = 0; i < numPoints; i++)
            {
                foreach (var ordinateIndex in ordinateIndices)
                {
                    writer.Write(sequence.GetOrdinate(i, ordinateIndex));
                }
            }
        }
        private static ICoordinateSequence ReadSequence(PostGis2GeometryHeader header, BinaryReader reader, int numPoints)
        {
            if (numPoints < 0)
            {
                numPoints = reader.ReadInt32();
            }

            var sequence        = header.Factory.CoordinateSequenceFactory.Create(numPoints, header.Ordinates);
            var ordinateIndices = header.OrdinateIndices;

            for (var i = 0; i < numPoints; i++)
            {
                foreach (var ord in ordinateIndices)
                {
                    sequence.SetOrdinate(i, ord, reader.ReadDouble());
                }
            }

            return(sequence);
        }
        private static IPolygon ReadPolygon(PostGis2GeometryHeader header, BinaryReader reader)
        {
            var numRings = reader.ReadInt32();

            if (numRings == 0)
            {
                return(header.Factory.CreatePolygon((ILinearRing)null));
            }

            var pointsPerRing = new int[numRings];
            int i;

            for (i = 0; i < numRings; i++)
            {
                pointsPerRing[i] = reader.ReadInt32();
            }

            //padding
            if (numRings % 2 != 0)
            {
                reader.ReadInt32();
            }

            //read shell
            var shell = (ILinearRing)ReadLineString(header, reader, pointsPerRing[0], true);

            //read holes
            var holes = new ILinearRing[numRings - 1];

            for (i = 1; i < numRings; i++)
            {
                holes[i - 1] = (ILinearRing)ReadLineString(header, reader, pointsPerRing[i], true);
            }

            //create polygon
            return(header.Factory.CreatePolygon(shell, holes));
        }
Example #8
0
 private static void WriteLineString(ILineString geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
 {
     writer.Write((uint)PostGis2GeometryType.LineString);
     WriteSequence(geometry.CoordinateSequence, pgh, writer, -1);
 }
Example #9
0
 private static void WritePoint(IPoint geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
 {
     writer.Write((uint)PostGis2GeometryType.Point);
     WriteSequence(geometry.CoordinateSequence, pgh, writer, -1);
 }
 private static void WriteLineString(ILineString geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
 {
     writer.Write((uint)PostGis2GeometryType.LineString);
     WriteSequence(geometry.CoordinateSequence, pgh, writer, -1);
 }
        private static void WritePolygon(IPolygon geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
        {
            writer.Write((uint)PostGis2GeometryType.Polygon);
            if (geometry.IsEmpty)
            {
                writer.Write(0);
                return;
            }

            var numRings = 1 + geometry.NumInteriorRings;
            
            //shell
            writer.Write(numRings);
            writer.Write(geometry.Shell.NumPoints);

            //holes
            for (var i = 0; i < geometry.NumInteriorRings; i++ )
                writer.Write(geometry.GetInteriorRingN(i).NumPoints);
            
            //pad
            if (numRings % 2 != 0)
                writer.Write(0);

            WriteSequence(geometry.Shell.CoordinateSequence, pgh, writer, geometry.Shell.NumPoints);
            for (var i = 0; i < geometry.NumInteriorRings; i++ )
            {
                var sequence = geometry.GetInteriorRingN(i).CoordinateSequence;
                WriteSequence(sequence, pgh, writer, sequence.Count);
            }
        }
        private static void WriteCollection(IGeometryCollection geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
        {
            writer.Write((uint)geometry.OgcGeometryType.ToPostGis2());
            if (geometry.IsEmpty)
            {
                writer.Write(0);
                return;
            }

            for (var i = 0; i < geometry.NumGeometries; i++)
                Write(geometry.GetGeometryN(i), writer, pgh.Ordinates, pgh.IsGeodetic);
        }
        private static IGeometry Read(BinaryReader reader)
        {
            var header = new PostGis2GeometryHeader(reader);

            return(Read(header, reader));
        }
 private static void WritePoint(IPoint geometry, PostGis2GeometryHeader pgh, BinaryWriter writer)
 {
     writer.Write((uint)PostGis2GeometryType.Point);
     WriteSequence(geometry.CoordinateSequence, pgh, writer, -1);
 }
        private static void Write(IGeometry geometry, BinaryWriter writer, Ordinates handleOrdinates, bool isGeodetic)
        {
            var pgh = new PostGis2GeometryHeader(geometry, handleOrdinates) {IsGeodetic = isGeodetic};
            pgh.Write(writer);

            switch (geometry.OgcGeometryType)
            {
                case OgcGeometryType.Point:
                    WritePoint((IPoint) geometry, pgh, writer);
                    break;
                case OgcGeometryType.LineString:
                    WriteLineString((ILineString)geometry, pgh, writer);
                    break;
                case OgcGeometryType.Polygon:
                    WritePolygon((IPolygon)geometry, pgh, writer);
                    break;
                case OgcGeometryType.MultiPoint:
                case OgcGeometryType.MultiLineString:
                case OgcGeometryType.MultiPolygon:
                case OgcGeometryType.GeometryCollection:
                    WriteCollection((IGeometryCollection) geometry, pgh, writer);
                    break;
                default:
                    throw new NotSupportedException();
            }
        }
        private static IGeometry ReadMultiPoint(PostGis2GeometryHeader header, BinaryReader reader)
        {
            var geoms = ReadGeometryArray <IPoint>(reader);

            return(header.Factory.CreateMultiPoint(geoms));
        }
        private static IGeometry ReadMultiLineString(PostGis2GeometryHeader header, BinaryReader reader)
        {
            var geoms = ReadGeometryArray <ILineString>(reader);

            return(header.Factory.CreateMultiLineString(geoms));
        }
        private static IGeometry ReadGeometryCollection(PostGis2GeometryHeader header, BinaryReader reader)
        {
            var geoms = ReadGeometryArray <IGeometry>(reader);

            return(header.Factory.CreateGeometryCollection(geoms));
        }
 private static ILineString ReadLineString(PostGis2GeometryHeader header, BinaryReader reader, int numPoints, bool ring)
 {
     return(ring
         ? header.Factory.CreateLinearRing(ReadSequence(header, reader, numPoints))
         : header.Factory.CreateLineString(ReadSequence(header, reader, numPoints)));
 }
 private static void WriteSequence(ICoordinateSequence sequence, PostGis2GeometryHeader pgh, BinaryWriter writer, int numPoints)
 {
     if (numPoints == -1)
     {
         numPoints = sequence.Count;
         writer.Write(numPoints);
     }
     
     var ordinateIndices = pgh.OrdinateIndices;
     for (var i = 0; i < numPoints; i++)
     {
         foreach (var ordinateIndex in ordinateIndices)
             writer.Write(sequence.GetOrdinate(i, ordinateIndex));
     }
 }