コード例 #1
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
        private static Position FetchCoordinates(JArray arr, PositionType hint)
        {
            if (arr == null || arr.Count <= 0)
            {
                return((Position)null);
            }
            if (arr[0].Type != JTokenType.Array)
            {
                try
                {
                    return(new Position(Extensions.Value <double>((IEnumerable <JToken>)arr[0]), Extensions.Value <double>((IEnumerable <JToken>)arr[1]), hint));
                }
                catch
                {
                }
                return((Position)null);
            }
            PositionSet positionSet = new PositionSet(hint);

            foreach (JToken jtoken in arr)
            {
                if (jtoken != null && jtoken.Type == JTokenType.Array)
                {
                    Position position = GeoJsonUtils.FetchCoordinates(jtoken as JArray);
                    if (position != null)
                    {
                        positionSet.Positions.Add(position);
                    }
                }
            }
            return((Position)positionSet);
        }
コード例 #2
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
 public static JObject ToGeoJson(IGeometry2 <double> geom)
 {
     if (geom is Geometry2Bag <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as Geometry2Bag <double>));
     }
     if (geom is PolygonBag2 <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as PolygonBag2 <double>));
     }
     if (geom is Polygon2 <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as Polygon2 <double>));
     }
     if (geom is Polyline2 <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as Polyline2 <double>));
     }
     if (geom is PolylineBag2 <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as PolylineBag2 <double>));
     }
     if (geom is Point2 <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as Point2 <double>));
     }
     if (geom is PointBag2 <double> )
     {
         return(GeoJsonUtils.ToGeoJson(geom as PointBag2 <double>));
     }
     return((JObject)null);
 }
コード例 #3
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
        public static JObject ParseGeoJson(string json)
        {
            JObject o = JToken.Parse(json) as JObject;

            if (o != null)
            {
                return(GeoJsonUtils.ExtractGeoJson(o));
            }
            return((JObject)null);
        }
コード例 #4
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
        public static IGeometry2 <double> ParseGeometry(string json)
        {
            JObject o = GeoJsonUtils.ParseGeoJson(json);

            if (o != null)
            {
                return(GeoJsonUtils.ExtractGeometry(o));
            }
            return(null);
        }
コード例 #5
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
        public static JObject ToGeoJson(Geometry2Bag <double> geom)
        {
            if (geom == null)
            {
                return((JObject)null);
            }
            JObject jobject = new JObject();

            jobject.Add("type", (JToken) new JValue("GeometryCollection"));
            JArray jarray = new JArray();

            jobject.Add("geometries", (JToken)jarray);
            foreach (IGeometry2 <double> geometry in geom)
            {
                JToken jtoken;
                if (geometry is PolygonBag2 <double> )
                {
                    jtoken = (JToken)GeoJsonUtils.ToGeoJson(geometry as PolygonBag2 <double>);
                }
                else if (geometry is Polygon2 <double> )
                {
                    jtoken = (JToken)GeoJsonUtils.ToGeoJson(geometry as Polygon2 <double>);
                }
                else if (geometry is Polyline2 <double> )
                {
                    jtoken = (JToken)GeoJsonUtils.ToGeoJson(geometry as Polyline2 <double>);
                }
                else if (geometry is PolylineBag2 <double> )
                {
                    jtoken = (JToken)GeoJsonUtils.ToGeoJson(geometry as PolylineBag2 <double>);
                }
                else if (geometry is Point2 <double> )
                {
                    jtoken = (JToken)GeoJsonUtils.ToGeoJson(geometry as Point2 <double>);
                }
                else
                {
                    if (!(geometry is PointBag2 <double>))
                    {
                        return(null);
                    }
                    jtoken = (JToken)GeoJsonUtils.ToGeoJson(geometry as PointBag2 <double>);
                }
                if (jtoken == null)
                {
                    return(null);
                }
                jarray.Add(jtoken);
            }
            return(jobject);
        }
コード例 #6
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
 private static Position FetchCoordinates(JArray arr)
 {
     return(GeoJsonUtils.FetchCoordinates(arr, PositionType.Unknown));
 }
コード例 #7
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
 public static JObject ExtractGeoJson(JObject o)
 {
     if (o != null)
     {
         JToken  jtoken1  = o["type"];
         JObject jobject1 = (JObject)null;
         if (jtoken1 != null && jtoken1.Type == JTokenType.String)
         {
             string str = jtoken1.ToString();
             if (!string.IsNullOrEmpty(str))
             {
                 if ("Point" == str || "Polygon" == str || ("LineString" == str || "MultiLineString" == str) || ("MultiPoint" == str || "MultiPolygon" == str || "GeometryCollection" == str))
                 {
                     return(o);
                 }
                 JToken jtoken2 = o["crs"];
                 if (jtoken2 != null && jtoken2.Type == JTokenType.Object)
                 {
                     jobject1 = jtoken2 as JObject;
                 }
                 if ("Feature" == str)
                 {
                     JToken jtoken3 = o["geometry"];
                     if (jtoken3 != null && jtoken3.Type == JTokenType.Object)
                     {
                         o = GeoJsonUtils.ExtractGeoJson(jtoken3 as JObject);
                         if (o != null)
                         {
                             if (jobject1 != null)
                             {
                                 JToken jtoken4 = o["crs"];
                                 if (jtoken4 == null || jtoken4.Type == JTokenType.None || jtoken4.Type == JTokenType.Null)
                                 {
                                     o.Add("crs", (JToken)jobject1);
                                 }
                             }
                             return(o);
                         }
                     }
                 }
                 else if ("FeatureCollection" == str)
                 {
                     JToken jtoken3 = o["features"];
                     if (jtoken3 != null && jtoken3.Type == JTokenType.Array)
                     {
                         JArray jarray1 = jtoken3 as JArray;
                         if (jarray1 != null && jarray1.Count > 0)
                         {
                             JArray  jarray2  = new JArray();
                             JObject jobject2 = new JObject();
                             jobject2.Add("type", (JToken)"GeometryCollection");
                             jobject2.Add("geometries", (JToken)jarray2);
                             if (jobject1 != null)
                             {
                                 jobject2.Add("crs", (JToken)jobject1);
                             }
                             foreach (JToken jtoken4 in jarray1)
                             {
                                 if (jtoken4 != null && jtoken4.Type == JTokenType.Object)
                                 {
                                     JToken jtoken5 = (JToken)GeoJsonUtils.ExtractGeoJson(jtoken4 as JObject);
                                     if (jtoken5 != null)
                                     {
                                         jarray2.Add(jtoken5);
                                     }
                                 }
                             }
                             return(jobject2);
                         }
                     }
                 }
             }
         }
     }
     return((JObject)null);
 }
コード例 #8
0
ファイル: GeoJsonUtils.cs プロジェクト: OSRS/Oncor_OsrsLegacy
        public static IGeometry2 <double> ExtractGeometry(JObject o)
        {
            IGeometry2 <double> geometry1 = null;

            o = GeoJsonUtils.ExtractGeoJson(o);
            if (o != null)
            {
                JObject jobject = (JObject)null;
                string  str1    = (string)null;
                KeyValuePair <string, JObject> crs = GeoJsonUtils.GetCrs(o);
                if (crs.Key != null)
                {
                    jobject = crs.Value;
                    str1    = crs.Key;
                }
                JToken jtoken1 = o["type"];
                if (jtoken1 != null && jtoken1.Type == JTokenType.String)
                {
                    string str2 = jtoken1.ToString();
                    if (!string.IsNullOrEmpty(str2))
                    {
                        string str3 = str2;
                        if (str3 == "Feature")
                        {
                            JToken jtoken2 = o["geometry"];
                            if (jtoken2 == null || jtoken2.Type != JTokenType.Object)
                            {
                                return(null);
                            }
                            JObject o1 = jtoken2 as JObject;
                            if (jobject != null)
                            {
                                o1["crs"] = (JToken)jobject;
                            }
                            return(GeoJsonUtils.ExtractGeometry(o1));
                        }
                        if (str3 == "FeatureCollection")
                        {
                            JToken jtoken2 = o["features"];
                            if (jtoken2 != null && jtoken2.Type == JTokenType.Array)
                            {
                                JArray jarray = (JArray)jtoken2;
                                List <IGeometry2 <double> > list = new List <IGeometry2 <double> >();
                                foreach (JToken jtoken3 in jarray)
                                {
                                    if (jtoken3 != null && jtoken3.Type == JTokenType.Object)
                                    {
                                        JObject o1 = jtoken2 as JObject;
                                        if (jobject != null && o1["crs"] == null)
                                        {
                                            o1["crs"] = (JToken)jobject;
                                        }
                                        IGeometry2 <double> geometry2 = GeoJsonUtils.ExtractGeometry(o1);
                                        if (geometry2 == null)
                                        {
                                            return(null);
                                        }
                                        list.Add(geometry2);
                                    }
                                }
                                //return (Geometry)(Geometry.DefaultFactory.CreateGeometryCollection((IGeometry[])list.ToArray()) as GeometryCollection);
                                return(null); //right now we can't handle mixed geometry types
                            }
                        }
                        else if (str3 == "GeometryCollection")
                        {
                            JToken jtoken2 = o["geometries"];
                            if (jtoken2 != null && jtoken2.Type == JTokenType.Array)
                            {
                                JArray jarray = (JArray)jtoken2;
                                List <IGeometry2 <double> > list = new List <IGeometry2 <double> >();
                                foreach (JToken jtoken3 in jarray)
                                {
                                    if (jtoken3 != null && jtoken3.Type == JTokenType.Object)
                                    {
                                        JObject o1 = jtoken2 as JObject;
                                        if (jobject != null && o1["crs"] == null)
                                        {
                                            o1["crs"] = (JToken)jobject;
                                        }
                                        IGeometry2 <double> geometry2 = GeoJsonUtils.ExtractGeometry(o1);
                                        if (geometry2 == null)
                                        {
                                            return(null);
                                        }
                                        list.Add(geometry2);
                                    }
                                }
                                //return (Geometry)(Geometry.DefaultFactory.CreateGeometryCollection((IGeometry[])list.ToArray()) as GeometryCollection);
                                return(null); //right now we can't handle mixed geometry types
                            }
                        }
                        else
                        {
                            JToken jtoken2 = o["coordinates"];
                            if (jtoken2 != null && jtoken2.Type == JTokenType.Array)
                            {
                                JArray arr = jtoken2 as JArray;
                                if (arr != null)
                                {
                                    if ("Point" == str2)
                                    {
                                        geometry1 = GeometryUtils.BuildPoint(GeoJsonUtils.FetchCoordinates(arr, PositionType.Point));
                                    }
                                    else if ("MultiPoint" == str2)
                                    {
                                        geometry1 = GeometryUtils.BuildMultiPoint(GeoJsonUtils.FetchCoordinates(arr, PositionType.MultiPoint));
                                    }
                                    else if ("LineString" == str2)
                                    {
                                        geometry1 = GeometryUtils.BuildLineString(GeoJsonUtils.FetchCoordinates(arr, PositionType.LineString));
                                    }
                                    else if ("MultiLineString" == str2)
                                    {
                                        geometry1 = GeometryUtils.BuildMultiLineString(GeoJsonUtils.FetchCoordinates(arr, PositionType.MultiLineString));
                                    }
                                    else if ("Polygon" == str2)
                                    {
                                        geometry1 = GeometryUtils.BuildPolygon(GeoJsonUtils.FetchCoordinates(arr, PositionType.Polygon));
                                    }
                                    else if ("MultiPolygon" == str2)
                                    {
                                        geometry1 = GeometryUtils.BuildMultiPolygon(GeoJsonUtils.FetchCoordinates(arr, PositionType.MultiPolygon));
                                    }
                                    //if (geometry1 != null && jobject != null)
                                    //    geometry1.UserData = (object)str1;  //TODO - deal with the JTable issue
                                    return(geometry1);
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }