private static bool TestWktWkb(int number, IGeometryFactory factory, string wkt, string wkb)
        {
            WKTReader r = new WKTReader(factory);
            IGeometry wktGeom = r.Read(wkt);
            WKBReader s = new WKBReader(factory);
            IGeometry wkbGeom = s.Read(WKBReader.HexToBytes(wkb));

            try
            {
                Assert.AreEqual(wkb, WKBWriter.ToHex(wktGeom.AsBinary()), "wkb's don't match");
                Assert.IsTrue(DiscreteHausdorffDistance.Distance(wktGeom, wkbGeom) < 1e-9, number + ": DiscreteHausdorffDistance.Distance(wktGeom, wkbGeom) < 1e-9");
                if (!wktGeom.EqualsExact(wkbGeom))
                {
                    Assert.AreEqual(wkt, wktGeom.AsText(), number + ": wkt.Equals(wktGeom.AsText())");
                    var wktGeom2 = s.Read(wktGeom.AsBinary());
                    Assert.AreEqual(wkt, wktGeom2.AsText(), number + ": wkt.Equals(wktGeom2.AsText())");
                    var diff = wkbGeom.Difference(wktGeom);
                    Assert.IsTrue(false, number + ": wktGeom.EqualsExact(wkbGeom)\n" + diff.AsText());
                }
                return false;
            }
            catch (AssertionException ex)
            {
                Console.WriteLine(ex.Message);
                return true;
            }
        }
        public void Setup()
        {
            wkbreader = new WKBReader(factory);

            geometry0 = wkbreader.Read(test00_Geom0_WkbByteArray);
            Debug.WriteLine(geometry0.ToString());
            geometry1 = wkbreader.Read(test00_Geom1_WkbByteArray);
            Debug.WriteLine(geometry1.ToString());
        }
        /// <inheritdoc cref="IGeometryReader{TSource}.Read(Stream)"/>>
        public IGeometry Read(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var reader = new BinaryReader(stream))
            {
                var header   = GeoPackageBinaryHeader.Read(reader);
                var services = new NtsGeometryServices(_coordinateSequenceFactory,
                                                       _precisionModel, HandleSRID ? header.SrsId : -1);
                // NOTE: GeoPackage handle SRID in header, so no need to read this also in wkb;
                const bool dontHandleSRID = false;
                var        wkbReader      = new WKBReader(services)
                {
                    HandleSRID      = dontHandleSRID,
                    HandleOrdinates = HandleOrdinates,
                    RepairRings     = RepairRings
                };
                var geom = wkbReader.Read(stream);
                if (HandleSRID)
                {
                    geom.SRID = header.SrsId;
                }
                return(geom);
            }
        }
 private static IGeometry FromWkb(string wkb, IGeometry clip = null)
 {
     WKBReader reader = new WKBReader(GeometryFactory.Default);
     byte[] bytes = WKBReader.HexToBytes(wkb);
     IGeometry geom =  reader.Read(bytes);
     if (clip != null) 
         geom = geom.Intersection(clip);
     return geom;
 }
 public void OracleWKBBigEndianReadTest()
 {
     IGeometry result = null;
     using (Stream stream = new FileStream(blobFile, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         WKBReader wkbreader = new WKBReader();
         result = wkbreader.Read(stream);
     }
     Debug.WriteLine(result.ToString());
     Assert.IsNotNull(result);
 }
Exemple #6
0
        /// <summary>
        /// Convert hex-coded WKB to geometry object
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static IGeometry WKBhex2Geom(string hexString)
        {
            if (String.IsNullOrWhiteSpace(hexString))
            {
                return(null);
            }

            try
            {
                var       reader         = new NetTopologySuite.IO.WKBReader();
                var       geometryBinary = NetTopologySuite.IO.WKBReader.HexToBytes(hexString);
                IGeometry result         = reader.Read(geometryBinary);
                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #7
0
        /// <summary>
        /// Reads a sequence of geometries.<br/>
        /// If an <see cref="Offset"/> is specified, geometries read up to the offset count are skipped.
        /// If a <see cref="Limit"/> is specified, no more than <see cref="Limit"/> geometries are read.
        /// </summary>
        /// <param name="streamReader">The stream reader to use.</param>
        /// <exception cref="IOException">Thrown if an I/O exception was encountered</exception>
        /// <exception cref="ParseException">Thrown if an error occured reading a geometry</exception>
        private ReadOnlyCollection <Geometry> Read(StreamReader streamReader)
        {
            var geoms = new List <Geometry>();
            int count = 0;

            while (!IsAtEndOfFile(streamReader) && !IsAtLimit(geoms))
            {
                string line = streamReader.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                var g = _wkbReader.Read(WKBReader.HexToBytes(line));
                if (count >= Offset)
                {
                    geoms.Add(g);
                }
                count++;
            }
            return(geoms.AsReadOnly());
        }
        public void TestMaximumPrecisionDigitsFormatting()
        {
            IGeometryFactory factory = GeometryFactory.Default;

            WKBReader wkbreader = new WKBReader(factory);
            IGeometry wkb1 = wkbreader.Read(test00_Geom0_WkbByteArray);            
            Assert.IsNotNull(wkb1);
            TestValid(wkb1);

            IGeometry wkb2 = wkbreader.Read(test00_Geom1_WkbByteArray);
            Assert.IsNotNull(wkb2);
            TestValid(wkb2);

            Exception ex = TryOverlay(wkb1, wkb2);
            Assert.IsNotNull(ex);
            Assert.IsTrue(ex.GetType() == typeof(TopologyException));

            string tos1 = writer.Write(wkb1);
            Assert.IsNotNull(tos1);            
            string tos2 = writer.Write(wkb2);
            Assert.IsNotNull(tos2);            

            WKTReader reader = new WKTReader(factory);
            IGeometry wkt1 = reader.Read(tos1);
            Assert.IsNotNull(wkt1);
            Assert.IsTrue(wkt1.IsValid);

            IGeometry wkt2 = reader.Read(tos2);
            Assert.IsNotNull(wkt2);
            Assert.IsTrue(wkt2.IsValid);

            Assert.IsTrue(wkb1.EqualsExact(wkt1), "First geometry pair must be equal!");
            Assert.IsTrue(wkb2.EqualsExact(wkt2), "Second geometry pair must be equal!");

            ex = TryOverlay(wkt1, wkt2);
            Assert.IsNotNull(ex, "Operation must fail!");
            Assert.IsTrue(ex.GetType() == typeof(TopologyException));          
        }
        /// <summary>
        /// Creates a <see cref="GeoAPI.Geometries.IGeometry"/> based on the Well-known binary representation.
        /// </summary>
        /// <param name="reader">A <see cref="System.IO.BinaryReader">BinaryReader</see> used to read the Well-known binary representation.</param>
        /// <param name="factory">The factory to create the result geometry</param>
        /// <returns>A <see cref="GeoAPI.Geometries.IGeometry"/> based on the Well-known binary representation.</returns>
        public static IGeometry Parse(BinaryReader reader, IGeometryFactory factory)
        {
            WKBReader wkb = new WKBReader();
            return wkb.Read(reader.BaseStream);

            /*
            // Get the first Byte in the array. This specifies if the WKB is in
            // XDR (big-endian) format of NDR (little-endian) format.
            var byteOrder = reader.ReadByte();

            // Get the type of this geometry.
            var type = ReadUInt32(reader, (WkbByteOrder) byteOrder);

            switch ((WKBGeometryType) type)
            {
                case WKBGeometryType.wkbPoint:
                    return CreateWKBPoint(reader, (WkbByteOrder) byteOrder, factory);

                case WKBGeometryType.wkbLineString:
                    return CreateWKBLineString(reader, (WkbByteOrder)byteOrder, factory);

                case WKBGeometryType.wkbPolygon:
                    return CreateWKBPolygon(reader, (WkbByteOrder)byteOrder, factory);

                case WKBGeometryType.wkbMultiPoint:
                    return CreateWKBMultiPoint(reader, (WkbByteOrder)byteOrder, factory);

                case WKBGeometryType.wkbMultiLineString:
                    return CreateWKBMultiLineString(reader, (WkbByteOrder)byteOrder, factory);

                case WKBGeometryType.wkbMultiPolygon:
                    return CreateWKBMultiPolygon(reader, (WkbByteOrder)byteOrder, factory);

                case WKBGeometryType.wkbGeometryCollection:
                    return CreateWKBGeometryCollection(reader, (WkbByteOrder)byteOrder, factory);

                default:
                    if (!Enum.IsDefined(typeof (WKBGeometryType), type))
                        throw new ArgumentException("Geometry type not recognized");
                    else
                        throw new NotSupportedException("Geometry type '" + type + "' not supported");
            }
            */
        }
        /**
           * Reads a shape from a byte array, using an internal format written by
           * {@link #writeShapeToBytes(com.spatial4j.core.shape.Shape)}.
           */
        public Shape ReadShapeFromBytes(byte[] array, int offset, int length)
        {
            using (var stream = new MemoryStream(array, offset, length, false))
            using (var bytes = new BinaryReader(stream))
            {

                var type = bytes.ReadByte();
                if (type == TYPE_POINT)
                {
                    return new NtsPoint(((NtsSpatialContext)Ctx).GetGeometryFactory().CreatePoint(new Coordinate(bytes.ReadDouble(), bytes.ReadDouble())), Ctx);
                }

                if (type == TYPE_BBOX)
                {
                    return new RectangleImpl(
                        bytes.ReadDouble(), bytes.ReadDouble(),
                        bytes.ReadDouble(), bytes.ReadDouble(), Ctx);
                }

                if (type == TYPE_GEOM)
                {
                    var reader = new WKBReader(((NtsSpatialContext)Ctx).GetGeometryFactory());
                    try
                    {
                        IGeometry geom = reader.Read(stream);

                        CheckCoordinates(geom);
                        return new NtsGeometry(geom, (NtsSpatialContext)Ctx, true);
                    }
                    catch (ParseException ex)
                    {
                        throw new InvalidShapeException("error reading WKT", ex);
                    }
                    catch (IOException ex)
                    {
                        throw new InvalidShapeException("error reading WKT", ex);
                    }
                }

                throw new InvalidShapeException("shape not handled: " + type);
            }
        }
 private static IGeometry ReadGeometryFromWkbHexString(String wkbHexFile, IGeometryFactory geomFact)
 {
     var reader = new WKBReader();
     var wkbHex = CleanHex(wkbHexFile);
     return reader.Read(WKBReader.HexToBytes(wkbHex));
 }