Пример #1
0
        /// <summary>
        /// Reads GeoJson and returns the feature.
        /// </summary>
        /// <param name="jsonReader"></param>
        /// <returns></returns>
        public static Feature ReadFeature(JsonReader jsonReader)
        {
            var      type     = string.Empty;
            Geometry geometry = null;
            GeometryAttributeCollection attributes = null;

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndArray)
                {
                    return(null);
                }

                if (jsonReader.TokenType == JsonToken.EndObject)
                { // end of geometry.
                    break;
                }

                if (jsonReader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)jsonReader.Value == "type")
                    { // the geometry type.
                        type = jsonReader.ReadAsString();
                    }
                    else if ((string)jsonReader.Value == "geometry")
                    { // the geometry.
                        geometry = GeoJsonConverter.ReadGeometry(jsonReader);
                    }
                    else if ((string)jsonReader.Value == "properties")
                    { // the properties/attributes.
                        attributes = GeoJsonConverter.ReadAttributes(jsonReader);
                    }
                }
            }
            switch (type)
            {
            case "Feature":
                if (geometry == null)
                {
                    throw new Exception("No geometry found.");
                }
                if (attributes != null)
                {
                    return(new Feature(geometry, attributes));
                }
                return(new Feature(geometry));
            }
            throw new Exception("Invalid type.");
        }
Пример #2
0
        public static Feature ReadFeature(JsonReader jsonReader)
        {
            string   str      = string.Empty;
            Geometry geometry = (Geometry)null;
            GeometryAttributeCollection attributes = (GeometryAttributeCollection)null;

            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndArray)
                {
                    return((Feature)null);
                }
                if (jsonReader.TokenType != JsonToken.EndObject)
                {
                    if (jsonReader.TokenType == JsonToken.PropertyName)
                    {
                        if ((string)jsonReader.Value == "type")
                        {
                            str = jsonReader.ReadAsString();
                        }
                        else if ((string)jsonReader.Value == "geometry")
                        {
                            geometry = GeoJsonConverter.ReadGeometry(jsonReader);
                        }
                        else if ((string)jsonReader.Value == "properties")
                        {
                            attributes = GeoJsonConverter.ReadAttributes(jsonReader);
                        }
                    }
                }
                else
                {
                    break;
                }
            }
            if (!(str == "Feature"))
            {
                throw new Exception("Invalid type.");
            }
            if (geometry == null)
            {
                throw new Exception("No geometry found.");
            }
            if (attributes != null)
            {
                return(new Feature(geometry, attributes));
            }
            return(new Feature(geometry));
        }