public bool TryWriteGeometry(JsonWriter writer, object value, JsonSerializer serializer)
        {
            ObjectType geomType = _shapeConverter.GetObjectType(value);

            if (!_shapeConverter.CanConvert(geomType))
            {
                return(false);
            }

            switch (geomType)
            {
            case ObjectType.Point:
            case ObjectType.LineString:
            case ObjectType.Polygon:
            case ObjectType.MultiPoint:
            case ObjectType.MultiLineString:
            case ObjectType.MultiPolygon:
                break;

            default:
                return(false);
            }

            writer.WriteStartObject();
            writer.WritePropertyName("type");
            writer.WriteValue(Enum.GetName(typeof(ObjectType), geomType));

            writer.WritePropertyName("coordinates");

            switch (geomType)
            {
            case ObjectType.Point:
                WriteJsonCoordinate(writer, _shapeConverter.FromPoint(value));
                break;

            case ObjectType.LineString:
                WriteJsonCoordinates(writer, _shapeConverter.FromLineString(value));
                break;

            case ObjectType.MultiPoint:
                WriteJsonCoordinates(writer, _shapeConverter.FromMultiPoint(value));
                break;

            case ObjectType.Polygon:
                WriteJsonCoordinatesEnumerable(writer, _shapeConverter.FromPolygon(value));
                break;

            case ObjectType.MultiLineString:
                WriteJsonCoordinatesEnumerable(writer, _shapeConverter.FromMultiLineString(value));
                break;

            case ObjectType.MultiPolygon:
                WriteJsonCoordinatesEnumerable2(writer, _shapeConverter.FromMultiPolygon(value));
                break;
            }

            writer.WriteEndObject();
            return(true);
        }
Esempio n. 2
0
        private void AppendPolygon(StringBuilder builder, object polygon)
        {
            var p = _shapeConverter.FromPolygon(polygon);

            builder.Append("POLYGON");
            AppendDimensions(builder, GetDimensions(p));
            builder.Append(" ");
            AppendPolygonInner(builder, p);
        }
Esempio n. 3
0
        private object ParsePolygonInner(WktTokenQueue tokens, Dimensions dimensions)
        {
            if (tokens.NextTokenIs("EMPTY"))
            {
                tokens.Dequeue();
                return(_shapeConverter.FromPolygon(new CoordinateInfo[0][][]));
            }

            var linestrings = ParseLineStrings(tokens, dimensions);

            return(_shapeConverter.ToPolygon(linestrings));
        }