public static PolygonBag2 <double> ToGeom(PostgisMultiPolygon geom) { if (geom != null) { List <List <List <Point2 <double> > > > pts = Points(geom); List <Polygon2 <double> > polys = new List <Polygon2 <double> >(); foreach (List <List <Point2 <double> > > cur in pts) { polys.Add(ToGeom(cur)); } return(factory.ConstructPolygonBag(polys)); } return(null); }
/// <summary> /// 1- Convert each Polygon from input PostGisMultiPolygon /// 2- Create a NetTopologyMultiPolygon with all polygons converted from previous step. /// Throw ArgumentNullException if any of input marameter is null. /// </summary> /// <param name="geometry">a PostgisMultiPolygon. No restriction.</param> /// <returns>a NetTopology MultiPolygon</returns> private static Geometry ProcessMultiPolygon(PostgisMultiPolygon geometry) { if (geometry != null) { //1- int polygonCount = geometry.PolygonCount; IPolygon[] netTopoPolygons = new Polygon[polygonCount]; for (int i = 0; i < polygonCount; i++) { netTopoPolygons[i] = (Polygon)ProcessPolygon(geometry[i]); } //2- return(new MultiPolygon(netTopoPolygons));; } else { throw new ArgumentNullException(); } }
public void MultiPolygonWithMultiplePolygons() { var geom2 = new PostgisMultiPolygon(new[] { new PostgisPolygon(new[] { new[] { new Coordinate2D(40, 40), new Coordinate2D(20, 45), new Coordinate2D(45, 30), new Coordinate2D(40, 40) } }), new PostgisPolygon(new[] { new[] { new Coordinate2D(20, 35), new Coordinate2D(10, 30), new Coordinate2D(10, 10), new Coordinate2D(30, 5), new Coordinate2D(45, 20), new Coordinate2D(20, 35) } }) }) { SRID = 4326 }; using (var conn = OpenConnection()) using (var command = conn.CreateCommand()) { command.Parameters.AddWithValue("p1", geom2); command.CommandText = "Select :p1"; command.ExecuteScalar(); } }
public Task Write(PostgisMultiPolygon value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async) => Write((PostgisGeometry)value, buf, lengthCache, parameter, async);
public int ValidateAndGetLength(PostgisMultiPolygon value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter) => value.GetLen(true);
public override bool Read([CanBeNull] out PostgisGeometry result) { Contract.Assert(_inByteaMode != true); if (!_inByteaMode.HasValue) { _inByteaMode = false; } result = default(PostgisGeometry); if (_id == 0) { if (_readBuf.ReadBytesLeft < 5) { return(false); } _bo = (ByteOrder)_readBuf.ReadByte(); _id = _readBuf.ReadUInt32(_bo); } if (!_srid.HasValue) { if ((_id & (uint)EwkbModifier.HasSRID) != 0) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _srid = _readBuf.ReadUInt32(_bo); } else { _srid = 0; } } switch ((WkbIdentifier)(_id & 7)) { case WkbIdentifier.Point: _lastId = _id; if (_readBuf.ReadBytesLeft < 16) { return(false); } result = new PostgisPoint(_readBuf.ReadDouble(_bo), _readBuf.ReadDouble(_bo)) { SRID = _srid.Value }; return(true); case WkbIdentifier.LineString: _lastId = _id; if (_ipts == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _points = new Coordinate2D[_readBuf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _points.Length; _ipts++) { if (_readBuf.ReadBytesLeft < 16) { return(false); } _points[_ipts] = new Coordinate2D(_readBuf.ReadDouble(_bo), _readBuf.ReadDouble(_bo)); } result = new PostgisLineString(_points) { SRID = _srid.Value }; return(true); case WkbIdentifier.Polygon: _lastId = _id; if (_irng == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _rings = new Coordinate2D[_readBuf.ReadInt32(_bo)][]; _irng = 0; } for (; _irng < _rings.Length; _irng++) { if (_ipts == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _rings[_irng] = new Coordinate2D[_readBuf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _rings[_irng].Length; _ipts++) { if (_readBuf.ReadBytesLeft < 16) { return(false); } _rings[_irng][_ipts] = new Coordinate2D(_readBuf.ReadDouble(_bo), _readBuf.ReadDouble(_bo)); } _ipts = -1; } result = new PostgisPolygon(_rings) { SRID = _srid.Value }; return(true); case WkbIdentifier.MultiPoint: _lastId = _id; if (_ipts == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _points = new Coordinate2D[_readBuf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _points.Length; _ipts++) { if (_readBuf.ReadBytesLeft < 21) { return(false); } _readBuf.Skip(5); _points[_ipts] = new Coordinate2D(_readBuf.ReadDouble(_bo), _readBuf.ReadDouble(_bo)); } result = new PostgisMultiPoint(_points) { SRID = _srid.Value }; return(true); case WkbIdentifier.MultiLineString: _lastId = _id; if (_irng == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _rings = new Coordinate2D[_readBuf.ReadInt32(_bo)][]; _irng = 0; } for (; _irng < _rings.Length; _irng++) { if (_ipts == -1) { if (_readBuf.ReadBytesLeft < 9) { return(false); } _readBuf.Skip(5); _rings[_irng] = new Coordinate2D[_readBuf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _rings[_irng].Length; _ipts++) { if (_readBuf.ReadBytesLeft < 16) { return(false); } _rings[_irng][_ipts] = new Coordinate2D(_readBuf.ReadDouble(_bo), _readBuf.ReadDouble(_bo)); } _ipts = -1; } result = new PostgisMultiLineString(_rings) { SRID = _srid.Value }; return(true); case WkbIdentifier.MultiPolygon: _lastId = _id; if (_ipol == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _pols = new Coordinate2D[_readBuf.ReadInt32(_bo)][][]; _ipol = 0; } for (; _ipol < _pols.Length; _ipol++) { if (_irng == -1) { if (_readBuf.ReadBytesLeft < 9) { return(false); } _readBuf.Skip(5); _pols[_ipol] = new Coordinate2D[_readBuf.ReadInt32(_bo)][]; _irng = 0; } for (; _irng < _pols[_ipol].Length; _irng++) { if (_ipts == -1) { if (_readBuf.ReadBytesLeft < 4) { return(false); } _pols[_ipol][_irng] = new Coordinate2D[_readBuf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _pols[_ipol][_irng].Length; _ipts++) { if (_readBuf.ReadBytesLeft < 16) { return(false); } _pols[_ipol][_irng][_ipts] = new Coordinate2D(_readBuf.ReadDouble(_bo), _readBuf.ReadDouble(_bo)); } _ipts = -1; } _irng = -1; } result = new PostgisMultiPolygon(_pols) { SRID = _srid.Value }; return(true); case WkbIdentifier.GeometryCollection: PostgisGeometry[] g; int i; if (_icol.Count == 0) { if (_readBuf.ReadBytesLeft < 4) { _lastId = _id; return(false); } g = new PostgisGeometry[_readBuf.ReadInt32(_bo)]; i = 0; if (_newGeom) // We need to know whether we're in a nested geocoll or not. { _id = 0; _newGeom = false; } else { _id = _lastId; _lastId = 0; } } else { g = _geoms.Pop(); i = _icol.Pop(); if (_icol.Count == 0) { _id = _lastId; _lastId = 0; } } for (; i < g.Length; i++) { PostgisGeometry geom; if (!Read(out geom)) { _icol.Push(i); _geoms.Push(g); _id = (uint)WkbIdentifier.GeometryCollection; return(false); } g[i] = geom; Reset(); } result = new PostgisGeometryCollection(g) { SRID = _srid.Value }; return(true); default: throw new InvalidOperationException("Unknown Postgis identifier."); } }
public bool Read(out PostgisMultiPolygon result) => ReadConcrete(out result);
public Task Write(PostgisMultiPolygon value, NpgsqlWriteBuffer buf, NpgsqlLengthCache?lengthCache, NpgsqlParameter?parameter, bool async, CancellationToken cancellationToken = default) => Write((PostgisGeometry)value, buf, lengthCache, parameter, async, cancellationToken);
public bool Read(out PostgisGeometry result) { result = default(PostgisGeometry); if (_id == 0) { if (_buf.ReadBytesLeft < 5) { return(false); } _bo = (ByteOrder)_buf.ReadByte(); _id = _buf.ReadUInt32(_bo); } if (!_srid.HasValue) { if ((_id & (uint)EwkbModifier.HasSRID) != 0) { if (_buf.ReadBytesLeft < 4) { return(false); } _srid = _buf.ReadUInt32(_bo); } else { _srid = 0; } } switch ((WkbIdentifier)(_id & (uint)7)) { case WkbIdentifier.Point: if (_buf.ReadBytesLeft < 16) { return(false); } result = new PostgisPoint(_buf.ReadDouble(_bo), _buf.ReadDouble(_bo)) { SRID = _srid.Value }; return(true); case WkbIdentifier.LineString: if (_ipts == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _points = new Coordinate2D[_buf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _points.Length; _ipts++) { if (_buf.ReadBytesLeft < 16) { return(false); } _points[_ipts] = new Coordinate2D(_buf.ReadDouble(_bo), _buf.ReadDouble(_bo)); } result = new PostgisLineString(_points) { SRID = _srid.Value }; return(true); case WkbIdentifier.Polygon: if (_irng == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _rings = new Coordinate2D[_buf.ReadInt32(_bo)][]; _irng = 0; } for (; _irng < _rings.Length; _irng++) { if (_ipts == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _rings[_irng] = new Coordinate2D[_buf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _rings[_irng].Length; _ipts++) { if (_buf.ReadBytesLeft < 16) { return(false); } _rings[_irng][_ipts] = new Coordinate2D(_buf.ReadDouble(_bo), _buf.ReadDouble(_bo)); } _ipts = -1; } result = new PostgisPolygon(_rings) { SRID = _srid.Value }; return(true); case WkbIdentifier.MultiPoint: if (_ipts == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _points = new Coordinate2D[_buf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _points.Length; _ipts++) { if (_buf.ReadBytesLeft < 21) { return(false); } _buf.Skip(5); _points[_ipts] = new Coordinate2D(_buf.ReadDouble(_bo), _buf.ReadDouble(_bo)); } result = new PostgisMultiPoint(_points) { SRID = _srid.Value }; return(true); case WkbIdentifier.MultiLineString: if (_irng == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _rings = new Coordinate2D[_buf.ReadInt32(_bo)][]; _irng = 0; } for (; _irng < _rings.Length; _irng++) { if (_ipts == -1) { if (_buf.ReadBytesLeft < 9) { return(false); } _buf.Skip(5); _rings[_irng] = new Coordinate2D[_buf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _rings[_irng].Length; _ipts++) { if (_buf.ReadBytesLeft < 16) { return(false); } _rings[_irng][_ipts] = new Coordinate2D(_buf.ReadDouble(_bo), _buf.ReadDouble(_bo)); } _ipts = -1; } result = new PostgisMultiLineString(_rings) { SRID = _srid.Value }; return(true); case WkbIdentifier.MultiPolygon: if (_ipol == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _pols = new Coordinate2D[_buf.ReadInt32(_bo)][][]; _ipol = 0; } for (; _ipol < _pols.Length; _ipol++) { if (_irng == -1) { if (_buf.ReadBytesLeft < 9) { return(false); } _buf.Skip(5); _pols[_ipol] = new Coordinate2D[_buf.ReadInt32(_bo)][]; _irng = 0; } for (; _irng < _pols[_ipol].Length; _irng++) { if (_ipts == -1) { if (_buf.ReadBytesLeft < 4) { return(false); } _pols[_ipol][_irng] = new Coordinate2D[_buf.ReadInt32(_bo)]; _ipts = 0; } for (; _ipts < _pols[_ipol][_irng].Length; _ipts++) { if (_buf.ReadBytesLeft < 16) { return(false); } _pols[_ipol][_irng][_ipts] = new Coordinate2D(_buf.ReadDouble(_bo), _buf.ReadDouble(_bo)); } _ipts = -1; } _irng = -1; } result = new PostgisMultiPolygon(_pols) { SRID = _srid.Value }; return(true); case WkbIdentifier.GeometryCollection: if (_newGeom) { if (_buf.ReadBytesLeft < 4) { return(false); } _geoms.Push(new PostgisGeometry[_buf.ReadInt32(_bo)]); _icol.Push(new Counter()); } _id = 0; var g = _geoms.Peek(); var i = _icol.Peek(); for (; i < g.Length; i.Increment()) { PostgisGeometry geom; if (!Read(out geom)) { _newGeom = false; return(false); } g[i] = geom; Reset(); } result = new PostgisGeometryCollection(g) { SRID = _srid.Value }; _geoms.Pop(); _icol.Pop(); return(true); default: throw new InvalidOperationException("Unknown Postgis identifier."); } }