Beispiel #1
0
        /// <summary>
        /// Reads a <see cref="GeoAPI.Geometries.Coordinate"/> from the current position.
        /// It's akin to <see cref="WktShapeParser.State"/> but for
        /// a NTS Coordinate.  Only the first 2 numbers are parsed; any remaining are ignored.
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        protected virtual Coordinate Coordinate(WktShapeParser.State state)
        {
            double x = m_ctx.NormX(state.NextDouble());

            m_ctx.VerifyX(x);
            double y = m_ctx.NormY(state.NextDouble());

            m_ctx.VerifyY(y);
            state.SkipNextDoubles();
            return(new Coordinate(x, y));
        }
Beispiel #2
0
 protected override IShape ParseShapeByType(WktShapeParser.State state, string shapeType)
 {
     if (shapeType.Equals("POLYGON", StringComparison.OrdinalIgnoreCase))
     {
         return(ParsePolygonShape(state));
     }
     else if (shapeType.Equals("MULTIPOLYGON", StringComparison.OrdinalIgnoreCase))
     {
         return(ParseMulitPolygonShape(state));
     }
     return(base.ParseShapeByType(state, shapeType));
 }
Beispiel #3
0
        /// <summary>
        /// Reads a NTS Coordinate sequence from the current position.
        /// <code>
        ///   '(' coordinate (',' coordinate )* ')'
        /// </code>
        /// </summary>
        protected virtual Coordinate[] CoordinateSequence(WktShapeParser.State state)
        {
            List <Coordinate> sequence = new List <Coordinate>();

            state.NextExpect('(');
            do
            {
                sequence.Add(Coordinate(state));
            } while (state.NextIf(','));
            state.NextExpect(')');
            return(sequence.ToArray(/*new Coordinate[sequence.Count]*/));
        }
Beispiel #4
0
        /// <summary>
        /// Reads a list of NTS Coordinate sequences from the current position.
        /// <code>
        ///   '(' coordinateSequence (',' coordinateSequence )* ')'
        /// </code>
        /// </summary>
        protected virtual IList <Coordinate[]> CoordinateSequenceList(WktShapeParser.State state)
        {
            IList <Coordinate[]> sequenceList = new List <Coordinate[]>();

            state.NextExpect('(');
            do
            {
                sequenceList.Add(CoordinateSequence(state));
            } while (state.NextIf(','));
            state.NextExpect(')');
            return(sequenceList);
        }
Beispiel #5
0
        /// <summary>
        /// Bypasses <see cref="NtsSpatialContext.MakeLineString(IList{Shapes.IPoint})"/> so that we can more
        /// efficiently get the <see cref="LineString"/> without creating a <see cref="List{T}">List{Shapes.IPoint}</see>.
        /// </summary>
        protected override IShape ParseLineStringShape(WktShapeParser.State state)
        {
            if (!m_ctx.UseNtsLineString)
            {
                return(base.ParseLineStringShape(state));
            }

            if (state.NextIfEmptyAndSkipZM())
            {
                return(m_ctx.MakeLineString(new List <Shapes.IPoint>()));
            }

            GeometryFactory geometryFactory = m_ctx.GeometryFactory;

            Coordinate[] coordinates = CoordinateSequence(state);
            return(MakeShapeFromGeometry(geometryFactory.CreateLineString(coordinates)));
        }
Beispiel #6
0
        /// <summary>
        /// Parses a MULTIPOLYGON shape from the raw string.
        /// <code>
        ///   '(' polygon (',' polygon )* ')'
        /// </code>
        /// </summary>
        protected virtual IShape ParseMulitPolygonShape(WktShapeParser.State state)
        {
            if (state.NextIfEmptyAndSkipZM())
            {
                return(m_ctx.MakeCollection(new List <IShape>()));
            }

            IList <IShape> polygons = new List <IShape>();

            state.NextExpect('(');
            do
            {
                polygons.Add(ParsePolygonShape(state));
            } while (state.NextIf(','));
            state.NextExpect(')');

            return(m_ctx.MakeCollection(polygons));
        }
Beispiel #7
0
        /// <summary>
        /// Reads a polygon, returning a NTS polygon.
        /// </summary>
        protected virtual IPolygon Polygon(WktShapeParser.State state)
        {
            GeometryFactory geometryFactory = m_ctx.GeometryFactory;

            IList <Coordinate[]> coordinateSequenceList = CoordinateSequenceList(state);

            ILinearRing shell = geometryFactory.CreateLinearRing(coordinateSequenceList[0]);

            ILinearRing[] holes = null;
            if (coordinateSequenceList.Count > 1)
            {
                holes = new ILinearRing[coordinateSequenceList.Count - 1];
                for (int i = 1; i < coordinateSequenceList.Count; i++)
                {
                    holes[i - 1] = geometryFactory.CreateLinearRing(coordinateSequenceList[i]);
                }
            }
            return(geometryFactory.CreatePolygon(shell, holes));
        }
Beispiel #8
0
        /// <summary>
        /// Parses a POLYGON shape from the raw string. It might return a <see cref="IRectangle"/>
        /// if the polygon is one.
        /// <code>
        ///   coordinateSequenceList
        /// </code>
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        protected virtual IShape ParsePolygonShape(WktShapeParser.State state)
        {
            IGeometry geometry;

            if (state.NextIfEmptyAndSkipZM())
            {
                GeometryFactory geometryFactory = m_ctx.GeometryFactory;
                geometry = geometryFactory.CreatePolygon(geometryFactory.CreateLinearRing(
                                                             new Coordinate[] { }), null);
            }
            else
            {
                geometry = Polygon(state);
                if (geometry.IsRectangle)
                {
                    //TODO although, might want to never convert if there's a semantic difference (e.g. geodetically)
                    return(MakeRectFromPoly(geometry));
                }
            }
            return(MakeShapeFromGeometry(geometry));
        }