daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2. /// Create valid <see cref="IGeometry"/> list from the incoming
  3. /// JSON.
  4. /// </summary>
  5. /// <param name="geometryType">Only a single type of geometry is supported in each
  6. /// query result, so the <paramref name="json"/> value is limited to one of the
  7. /// <see cref="EsriGeometryType"/>s.</param>
  8. /// <param name="json">JSON query result from <see cref="DownloadResource"/> which
  9. /// is the raw JSON string.</param>
  10. /// <returns>A list of point, polyline, polygon geometries in the same order
  11. /// in which they are shown in the JSON input string.</returns>
  12. public static IList<IGeometry> BuildGeometryFromJson(EsriGeometryType geometryType, string json)
  13. {
  14.     JObject jsonObject = JObject.Parse(json);
  15.     IEnumerable<JToken> geometries = from g in jsonObject["features"].Children()
  16.                      select g["geometry"];
  17.  
  18.     IList<IGeometry> list;
  19.     switch (geometryType)
  20.     {
  21.         case EsriGeometryType.EsriGeometryPoint:
  22.             IList<IGeometry> points = (from p in geometries
  23.                           select new Point((double) p["x"], (double) p["y"], Double.NaN))
  24.                 .Cast<IGeometry>()
  25.                 .ToList();
  26.             list = points;
  27.             break;
  28.  
  29.         case EsriGeometryType.EsriGeometryPolygon:
  30.             throw new NotImplementedException("Polygon deserialization from JSON has not been completed yet.");
  31.            
  32.         case EsriGeometryType.EsriGeometryPolyline:
  33.             IList<IGeometry> lineStrings = new List<IGeometry>();
  34.  
  35.             IEnumerable<JEnumerable<JToken>> allPaths = from p in jsonObject["features"].Children()["geometry"]
  36.                                     select p["paths"].Children();
  37.  
  38.             foreach (var eachPolylineInPath in allPaths)
  39.             {
  40.                 ICoordinate[] linePoints = (from line in eachPolylineInPath.Children()
  41.                                select new Coordinate((double) line[0], (double) line[1], double.NaN))
  42.                     .Cast<ICoordinate>()
  43.                     .ToArray();
  44.                 lineStrings.Add(new LineString(linePoints));
  45.             }
  46.  
  47.             list = lineStrings;
  48.             break;
  49.  
  50.         default:
  51.             throw new ApplicationException(String.Format(Resources.UnsupportedGeometryType, geometryType));
  52.     }
  53.  
  54.     return list;
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top