Beispiel #1
0
        public static FeatureCollection ReadFeatureCollection(JsonReader jsonReader)
        {
            string         str         = string.Empty;
            List <Feature> featureList = (List <Feature>)null;

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndObject)
            {
                if (jsonReader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)jsonReader.Value == "type")
                    {
                        str = jsonReader.ReadAsString();
                    }
                    else if ((string)jsonReader.Value == "features")
                    {
                        featureList = GeoJsonConverter.ReadFeatureArray(jsonReader);
                    }
                }
            }
            if (!(str == "FeatureCollection"))
            {
                throw new Exception("Invalid type.");
            }
            if (featureList == null)
            {
                return(new FeatureCollection());
            }
            return(new FeatureCollection((IEnumerable <Feature>)featureList));
        }
Beispiel #2
0
        /// <summary>
        /// Reads GeoJson and returns the feature collection.
        /// </summary>
        /// <param name="jsonReader"></param>
        /// <returns></returns>
        public static FeatureCollection ReadFeatureCollection(JsonReader jsonReader)
        {
            var            type     = string.Empty;
            List <Feature> features = null;

            while (jsonReader.Read())
            {
                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 == "features")
                    { // the geometry.
                        features = GeoJsonConverter.ReadFeatureArray(jsonReader);
                    }
                }
            }
            switch (type)
            {
            case "FeatureCollection":
                if (features == null)
                {
                    return(new FeatureCollection());
                }
                return(new FeatureCollection(features));
            }
            throw new Exception("Invalid type.");
        }
Beispiel #3
0
        internal static List <object> ReadCoordinateArrays(JsonReader jsonReader)
        {
            List <object> objectList = new List <object>();

            jsonReader.Read();
            if (jsonReader.TokenType == JsonToken.StartArray)
            {
                while (jsonReader.TokenType == JsonToken.StartArray)
                {
                    objectList.Add((object)GeoJsonConverter.ReadCoordinateArrays(jsonReader));
                    jsonReader.Read();
                }
            }
            else if (jsonReader.TokenType == JsonToken.Float)
            {
                while (jsonReader.TokenType != JsonToken.EndArray)
                {
                    objectList.Add((object)(double)jsonReader.Value);
                    jsonReader.Read();
                }
            }
            else
            {
                throw new Exception(string.Format("Invalid token in coordinates array: {0}", (object)jsonReader.TokenType.ToInvariantString()));
            }
            return(objectList);
        }
Beispiel #4
0
        /// <summary>
        /// Reads GeoJson and returns a list of coordinates.
        /// </summary>
        /// <param name="jsonReader"></param>
        /// <returns></returns>
        internal static List <object> ReadCoordinateArrays(JsonReader jsonReader)
        {
            var coordinates = new List <object>();

            jsonReader.Read(); // move to next array start.
            if (jsonReader.TokenType == JsonToken.StartArray)
            {
                while (jsonReader.TokenType == JsonToken.StartArray)
                {
                    coordinates.Add(GeoJsonConverter.ReadCoordinateArrays(jsonReader));
                    jsonReader.Read(); // move to next array start.
                }
            }
            else if (jsonReader.TokenType == JsonToken.Float)
            {
                while (jsonReader.TokenType != JsonToken.EndArray)
                {
                    coordinates.Add((double)jsonReader.Value);
                    jsonReader.Read();
                }
            }
            else
            {
                throw new Exception(string.Format("Invalid token in coordinates array: {0}", jsonReader.TokenType.ToInvariantString()));
            }
            return(coordinates);
        }
Beispiel #5
0
        /// <summary>
        /// Reads GeoJson and returns the geometry.
        /// </summary>
        /// <param name="jsonReader"></param>
        /// <returns></returns>
        internal static Geometry ReadGeometry(JsonReader jsonReader)
        {
            var             geometryType = string.Empty;
            var             coordinates  = new List <object>();
            List <Geometry> geometries   = null;

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

                if (jsonReader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)jsonReader.Value == "type")
                    { // the geometry type.
                        geometryType = jsonReader.ReadAsString();
                    }
                    else if ((string)jsonReader.Value == "geometries")
                    { // the geometries if a geometry collection.
                        geometries = GeoJsonConverter.ReadGeometryArray(jsonReader);
                    }
                    else if ((string)jsonReader.Value == "coordinates")
                    {                      // the coordinates.
                        jsonReader.Read(); // move to first array start.
                        coordinates = GeoJsonConverter.ReadCoordinateArrays(jsonReader);
                    }
                }
            }

            // data has been read, instantiate the actual object.
            switch (geometryType)
            {
            case "Point":
                return(GeoJsonConverter.BuildPoint(coordinates));

            case "LineString":
                return(GeoJsonConverter.BuildLineString(coordinates));

            case "Polygon":
                return(GeoJsonConverter.BuildPolygon(coordinates));

            case "MultiPoint":
                return(GeoJsonConverter.BuildMultiPoint(coordinates));

            case "MultiLineString":
                return(GeoJsonConverter.BuildMultiLineString(coordinates));

            case "MultiPolygon":
                return(GeoJsonConverter.BuildMultiPolygon(coordinates));

            case "GeometryCollection":
                return(GeoJsonConverter.BuildGeometryCollection(geometries));
            }
            throw new Exception(string.Format("Unknown geometry type: {0}", geometryType));
        }
Beispiel #6
0
        public static string ToGeoJson(this LineString geometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }
            JTokenWriter jtokenWriter = new JTokenWriter();
            LineString   geometry1    = geometry;

            GeoJsonConverter.Write((JsonWriter)jtokenWriter, geometry1);
            return(jtokenWriter.Token.ToString());
        }
Beispiel #7
0
        /// <summary>
        /// Reads GeoJson and returns the list of features.
        /// </summary>
        /// <param name="jsonReader"></param>
        /// <returns></returns>
        internal static List <Feature> ReadFeatureArray(JsonReader jsonReader)
        {
            var features = new List <Feature>();

            jsonReader.Read();
            while (jsonReader.TokenType != JsonToken.EndArray)
            {
                features.Add(GeoJsonConverter.ReadFeature(jsonReader));
                jsonReader.Read();
            }
            return(features);
        }
Beispiel #8
0
        public static string ToGeoJson(this FeatureCollection featureCollection)
        {
            if (featureCollection == null)
            {
                throw new ArgumentNullException("featureCollection");
            }
            JTokenWriter      jtokenWriter       = new JTokenWriter();
            FeatureCollection featureCollection1 = featureCollection;

            GeoJsonConverter.Write((JsonWriter)jtokenWriter, (IEnumerable <Feature>)featureCollection1);
            return(jtokenWriter.Token.ToString());
        }
Beispiel #9
0
        /// <summary>
        /// Generates GeoJson for the given feature collection.
        /// </summary>
        public static string ToGeoJson(this IEnumerable <Feature> featureCollection)
        {
            if (featureCollection == null)
            {
                throw new ArgumentNullException("featureCollection");
            }

            var jsonWriter = new JTokenWriter();

            GeoJsonConverter.Write(jsonWriter, featureCollection);
            return(jsonWriter.Token.ToString());
        }
Beispiel #10
0
        /// <summary>
        /// Generates GeoJson for the given geometry collection.
        /// </summary>
        /// <param name="geometryCollection"></param>
        public static string ToGeoJson(this MultiPolygon geometryCollection)
        {
            if (geometryCollection == null)
            {
                throw new ArgumentNullException("geometryCollection");
            }

            var jsonWriter = new JTokenWriter();

            GeoJsonConverter.Write(jsonWriter, geometryCollection);
            return(jsonWriter.Token.ToString());
        }
Beispiel #11
0
        public static string ToGeoJson(this GeometryCollection geometryCollection)
        {
            if (geometryCollection == null)
            {
                throw new ArgumentNullException("geometryCollection");
            }
            JTokenWriter       jtokenWriter        = new JTokenWriter();
            GeometryCollection geometryCollection1 = geometryCollection;

            GeoJsonConverter.Write((JsonWriter)jtokenWriter, geometryCollection1);
            return(jtokenWriter.Token.ToString());
        }
Beispiel #12
0
        /// <summary>
        /// Generates GeoJson for this geometry.
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        public static string ToGeoJson(this Point geometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }

            var jsonWriter = new JTokenWriter();

            GeoJsonConverter.Write(jsonWriter, geometry);
            return(jsonWriter.Token.ToString());
        }
Beispiel #13
0
        /// <summary>
        /// Reads GeoJson and returns the list of geometries..
        /// </summary>
        /// <param name="jsonReader"></param>
        /// <returns></returns>
        internal static List <Geometry> ReadGeometryArray(JsonReader jsonReader)
        {
            var geometries = new List <Geometry>();

            jsonReader.Read();
            while (jsonReader.TokenType != JsonToken.EndArray)
            {
                geometries.Add(GeoJsonConverter.ReadGeometry(jsonReader));
                jsonReader.Read();
            }
            return(geometries);
        }
Beispiel #14
0
        /// <summary>
        /// Generates GeoJson for the given feature.
        /// </summary>
        /// <param name="feature"></param>
        public static string ToGeoJson(this Feature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("feature");
            }

            var jsonWriter = new JTokenWriter();

            GeoJsonConverter.Write(jsonWriter, feature);
            return(jsonWriter.Token.ToString());
        }
Beispiel #15
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.");
        }
Beispiel #16
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));
        }
Beispiel #17
0
        /// <summary>
        /// Generates GeoJson for the given geometry.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="geometry"></param>
        internal static void Write(JsonWriter writer, Geometry geometry)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }

            if (geometry is LineairRing)
            {
                GeoJsonConverter.Write(writer, geometry as LineairRing);
            }
            else if (geometry is Point)
            {
                GeoJsonConverter.Write(writer, geometry as Point);
            }
            else if (geometry is LineString)
            {
                GeoJsonConverter.Write(writer, geometry as LineString);
            }
            else if (geometry is Polygon)
            {
                GeoJsonConverter.Write(writer, geometry as Polygon);
            }
            else if (geometry is MultiPoint)
            {
                GeoJsonConverter.Write(writer, geometry as MultiPoint);
            }
            else if (geometry is MultiPolygon)
            {
                GeoJsonConverter.Write(writer, geometry as MultiPolygon);
            }
            else if (geometry is MultiLineString)
            {
                GeoJsonConverter.Write(writer, geometry as MultiLineString);
            }
            else if (geometry is GeometryCollection)
            {
                GeoJsonConverter.Write(writer, geometry as GeometryCollection);
            }
            else
            {
                throw new Exception(string.Format("Unknown geometry of type: {0}", geometry.GetType()));
            }
        }
Beispiel #18
0
        internal static List <Feature> ReadFeatureArray(JsonReader jsonReader)
        {
            List <Feature> featureList = new List <Feature>();

            jsonReader.Read();
            while (jsonReader.TokenType != JsonToken.EndArray)
            {
                Feature feature = GeoJsonConverter.ReadFeature(jsonReader);
                if (feature == null)
                {
                    return(featureList);
                }
                featureList.Add(feature);
                jsonReader.Read();
            }
            return(featureList);
        }
Beispiel #19
0
 /// <summary>
 /// Builds the geometry from the given coordinates.
 /// </summary>
 /// <param name="coordinates"></param>
 /// <returns></returns>
 internal static MultiPolygon BuildMultiPolygon(List <object> coordinates)
 {
     if (coordinates == null)
     {
         throw new ArgumentNullException();
     }
     if (coordinates.Count >= 1)
     {
         var polygons = new List <Polygon>();
         foreach (List <object> coordinates1 in coordinates)
         {
             polygons.Add(GeoJsonConverter.BuildPolygon(coordinates1));
         }
         return(new MultiPolygon(polygons));
     }
     throw new Exception("Invalid coordinate collection.");
 }
Beispiel #20
0
        internal static MultiPolygon BuildMultiPolygon(List <object> coordinates)
        {
            if (coordinates == null)
            {
                throw new ArgumentNullException();
            }
            if (coordinates.Count < 1)
            {
                throw new Exception("Invalid coordinate collection.");
            }
            List <Polygon> polygonList = new List <Polygon>();

            foreach (List <object> coordinate in coordinates)
            {
                polygonList.Add(GeoJsonConverter.BuildPolygon(coordinate));
            }
            return(new MultiPolygon((IEnumerable <Polygon>)polygonList));
        }
Beispiel #21
0
 public static void Write(JsonWriter writer, Feature feature)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (feature == null)
     {
         throw new ArgumentNullException("feature");
     }
     writer.WriteStartObject();
     writer.WritePropertyName("type");
     writer.WriteValue("Feature");
     writer.WritePropertyName("properties");
     GeoJsonConverter.Write(writer, feature.Attributes);
     writer.WritePropertyName("geometry");
     GeoJsonConverter.Write(writer, feature.Geometry);
     writer.WriteEndObject();
 }
Beispiel #22
0
 internal static void Write(JsonWriter writer, GeometryCollection geometryCollection)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (geometryCollection == null)
     {
         throw new ArgumentNullException("geometryCollection");
     }
     writer.WriteStartObject();
     writer.WritePropertyName("type");
     writer.WriteValue("GeometryCollection");
     writer.WritePropertyName("geometries");
     writer.WriteStartArray();
     foreach (Geometry geometry in (GeometryCollectionBase <Geometry>)geometryCollection)
     {
         GeoJsonConverter.Write(writer, geometry);
     }
     writer.WriteEndArray();
     writer.WriteEndObject();
 }
Beispiel #23
0
 public static void Write(JsonWriter writer, IEnumerable <Feature> featureCollection)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (featureCollection == null)
     {
         throw new ArgumentNullException("featureCollection");
     }
     writer.WriteStartObject();
     writer.WritePropertyName("type");
     writer.WriteValue("FeatureCollection");
     writer.WritePropertyName("features");
     writer.WriteStartArray();
     foreach (Feature feature in featureCollection)
     {
         GeoJsonConverter.Write(writer, feature);
     }
     writer.WriteEndArray();
     writer.WriteEndObject();
 }
Beispiel #24
0
        internal static Geometry ReadGeometry(JsonReader jsonReader)
        {
            string          s           = string.Empty;
            List <object>   coordinates = new List <object>();
            List <Geometry> geometries  = (List <Geometry>)null;

            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndObject)
            {
                if (jsonReader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)jsonReader.Value == "type")
                    {
                        s = jsonReader.ReadAsString();
                    }
                    else if ((string)jsonReader.Value == "geometries")
                    {
                        geometries = GeoJsonConverter.ReadGeometryArray(jsonReader);
                    }
                    else if ((string)jsonReader.Value == "coordinates")
                    {
                        jsonReader.Read();
                        coordinates = GeoJsonConverter.ReadCoordinateArrays(jsonReader);
                    }
                }
            }
            // ISSUE: reference to a compiler-generated method
            long stringHash = s.GetHashCode();// \u003CPrivateImplementationDetails\u003E.ComputeStringHash(s);

            if (stringHash <= 2386032169U)
            {
                if ((int)stringHash != 1547714260)
                {
                    if ((int)stringHash != 2050635977)
                    {
                        if ((int)stringHash == -1908935127 && s == "Polygon")
                        {
                            return((Geometry)GeoJsonConverter.BuildPolygon(coordinates));
                        }
                    }
                    else if (s == "MultiLineString")
                    {
                        return((Geometry)GeoJsonConverter.BuildMultiLineString(coordinates));
                    }
                }
                else if (s == "MultiPolygon")
                {
                    return((Geometry)GeoJsonConverter.BuildMultiPolygon(coordinates));
                }
            }
            else if (stringHash <= 3786658501U)
            {
                if ((int)stringHash != -600749884)
                {
                    if ((int)stringHash == -508308795 && s == "GeometryCollection")
                    {
                        return((Geometry)GeoJsonConverter.BuildGeometryCollection(geometries));
                    }
                }
                else if (s == "MultiPoint")
                {
                    return((Geometry)GeoJsonConverter.BuildMultiPoint(coordinates));
                }
            }
            else if ((int)stringHash != -358027471)
            {
                if ((int)stringHash == -200799438 && s == "LineString")
                {
                    return((Geometry)GeoJsonConverter.BuildLineString(coordinates));
                }
            }
            else if (s == "Point")
            {
                return((Geometry)GeoJsonConverter.BuildPoint(coordinates));
            }
            throw new Exception(string.Format("Unknown geometry type: {0}", (object)s));
        }
Beispiel #25
0
 public static FeatureCollection ToFeatureCollection(this string geoJson)
 {
     return(GeoJsonConverter.ReadFeatureCollection((JsonReader) new JsonTextReader((TextReader) new StringReader(geoJson))));
 }
Beispiel #26
0
        /// <summary>
        /// Reads GeoJson and returns the geometry.
        /// </summary>
        /// <param name="geoJson"></param>
        /// <returns></returns>
        public static Geometry ToGeometry(this string geoJson)
        {
            var jsonReader = new JsonTextReader(new StringReader(geoJson));

            return(GeoJsonConverter.ReadGeometry(jsonReader));
        }
Beispiel #27
0
        /// <summary>
        /// Reads GeoJson and returns the feature collection.
        /// </summary>
        /// <param name="geoJson"></param>
        /// <returns></returns>
        public static FeatureCollection ToFeatureCollection(this string geoJson)
        {
            var jsonReader = new JsonTextReader(new StringReader(geoJson));

            return(GeoJsonConverter.ReadFeatureCollection(jsonReader));
        }
Beispiel #28
0
 public static Geometry ToGeometry(this string geoJson)
 {
     return(GeoJsonConverter.ReadGeometry((JsonReader) new JsonTextReader((TextReader) new StringReader(geoJson))));
 }