Ejemplo n.º 1
0
        async ValueTask <PostgisGeometry> DoRead(NpgsqlReadBuffer buf, WkbIdentifier id, bool le, bool async, CancellationToken cancellationToken = default)
        {
            switch (id)
            {
            case WkbIdentifier.Point:
                await buf.Ensure(16, async, cancellationToken);

                return(new PostgisPoint(buf.ReadDouble(le), buf.ReadDouble(le)));

            case WkbIdentifier.LineString:
            {
                await buf.Ensure(4, async, cancellationToken);

                var points = new Coordinate2D[buf.ReadInt32(le)];
                for (var ipts = 0; ipts < points.Length; ipts++)
                {
                    await buf.Ensure(16, async, cancellationToken);

                    points[ipts] = new Coordinate2D(buf.ReadDouble(le), buf.ReadDouble(le));
                }
                return(new PostgisLineString(points));
            }

            case WkbIdentifier.Polygon:
            {
                await buf.Ensure(4, async, cancellationToken);

                var rings = new Coordinate2D[buf.ReadInt32(le)][];

                for (var irng = 0; irng < rings.Length; irng++)
                {
                    await buf.Ensure(4, async, cancellationToken);

                    rings[irng] = new Coordinate2D[buf.ReadInt32(le)];
                    for (var ipts = 0; ipts < rings[irng].Length; ipts++)
                    {
                        await buf.Ensure(16, async, cancellationToken);

                        rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(le), buf.ReadDouble(le));
                    }
                }
                return(new PostgisPolygon(rings));
            }

            case WkbIdentifier.MultiPoint:
            {
                await buf.Ensure(4, async, cancellationToken);

                var points = new Coordinate2D[buf.ReadInt32(le)];
                for (var ipts = 0; ipts < points.Length; ipts++)
                {
                    await buf.Ensure(21, async, cancellationToken);

                    await buf.Skip(5, async, cancellationToken);

                    points[ipts] = new Coordinate2D(buf.ReadDouble(le), buf.ReadDouble(le));
                }
                return(new PostgisMultiPoint(points));
            }

            case WkbIdentifier.MultiLineString:
            {
                await buf.Ensure(4, async, cancellationToken);

                var rings = new Coordinate2D[buf.ReadInt32(le)][];

                for (var irng = 0; irng < rings.Length; irng++)
                {
                    await buf.Ensure(9, async, cancellationToken);

                    await buf.Skip(5, async, cancellationToken);

                    rings[irng] = new Coordinate2D[buf.ReadInt32(le)];
                    for (var ipts = 0; ipts < rings[irng].Length; ipts++)
                    {
                        await buf.Ensure(16, async, cancellationToken);

                        rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(le), buf.ReadDouble(le));
                    }
                }
                return(new PostgisMultiLineString(rings));
            }

            case WkbIdentifier.MultiPolygon:
            {
                await buf.Ensure(4, async, cancellationToken);

                var pols = new Coordinate2D[buf.ReadInt32(le)][][];

                for (var ipol = 0; ipol < pols.Length; ipol++)
                {
                    await buf.Ensure(9, async, cancellationToken);

                    await buf.Skip(5, async, cancellationToken);

                    pols[ipol] = new Coordinate2D[buf.ReadInt32(le)][];
                    for (var irng = 0; irng < pols[ipol].Length; irng++)
                    {
                        await buf.Ensure(4, async, cancellationToken);

                        pols[ipol][irng] = new Coordinate2D[buf.ReadInt32(le)];
                        for (var ipts = 0; ipts < pols[ipol][irng].Length; ipts++)
                        {
                            await buf.Ensure(16, async, cancellationToken);

                            pols[ipol][irng][ipts] = new Coordinate2D(buf.ReadDouble(le), buf.ReadDouble(le));
                        }
                    }
                }
                return(new PostgisMultiPolygon(pols));
            }

            case WkbIdentifier.GeometryCollection:
            {
                await buf.Ensure(4, async, cancellationToken);

                var g = new PostgisGeometry[buf.ReadInt32(le)];

                for (var i = 0; i < g.Length; i++)
                {
                    await buf.Ensure(5, async, cancellationToken);

                    var elemLe = buf.ReadByte() != 0;
                    var elemId = (WkbIdentifier)(buf.ReadUInt32(le) & 7);

                    g[i] = await DoRead(buf, elemId, elemLe, async, cancellationToken);
                }
                return(new PostgisGeometryCollection(g));
            }

            default:
                throw new InvalidOperationException("Unknown Postgis identifier.");
            }
        }
Ejemplo n.º 2
0
 public PostgisPoint(double x, double y)
 {
     _coord = new Coordinate2D(x, y);
 }