public static JObject ParseGeoJson(string json) { JObject o = JToken.Parse(json) as JObject; if (o != null) { return(GeoJsonUtils.ExtractGeoJson(o)); } return((JObject)null); }
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); }
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); }