/// <summary> /// Initializes static members of the <see cref="GisTools"/> class. /// </summary> static GisTools() { CoordinateConversionManager = new CoordinateConversionManager(); GridCellManager = new GridCellManager(); GeoJsonUtils = new GeoJsonUtils(); GeometryTools = new GeometryTools(); }
static void Main(string[] args) { Console.WriteLine("Fact: " + (GeometryFactory2Double.Instance != null)); GeometryFactory2Base <double> factory = GeometryFactory2Double.Instance; Console.WriteLine("Factory is " + (factory == null ? "null" : "not null")); if (factory != null) { Point2 <double> pt = factory.ConstructPoint(43, 22); Console.WriteLine("Created point: " + pt.ToString()); Ring2 <double> rg = factory.ConstructRing(new Point2 <double>[] { factory.ConstructPoint(22.45, 33.33), factory.ConstructPoint(23.45, 33.33), factory.ConstructPoint(22.45, 35.33) }); Polygon2 <double> pl = factory.ConstructPolygon(rg); if (rg != null) { Console.WriteLine("Created ring: " + rg.ToString()); object o = Osrs.Numerics.Spatial.Postgres.NpgSpatialUtils.ToPGis(rg); Console.WriteLine("Created PG Geometry: " + (o != null).ToString()); } if (pl != null) { Console.WriteLine(GeoJsonUtils.ToGeoJson(pl).ToString()); } } Console.WriteLine("ALL DONE"); Console.ReadLine(); }
public static JObject ToJson(Point2 <double> shape) { if (shape != null) { return(GeoJsonUtils.ToGeoJson(shape)); } return(null); }
public void Parse3() { const string json = "{\"type\":\"Feature\",\"properties\":{\"name\":\"My feature\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116,100]}}"; GeoJsonObject obj = GeoJsonUtils.Parse(json); Assert.IsNotNull(obj); GeoJsonFeature feature = obj as GeoJsonFeature; Assert.IsNotNull(feature); Assert.IsNotNull(feature.Geometry); Assert.IsNotNull(feature.Properties); GeoJsonPoint point = feature.Geometry as GeoJsonPoint; Assert.IsNotNull(point); Assert.AreEqual(9.536067, point.X, "X"); Assert.AreEqual(55.708116, point.Y, "Y"); Assert.AreEqual(100, point.Altitude, "Altitude"); Assert.AreEqual("My feature", feature.Properties.Name); Assert.AreEqual(null, feature.Properties.Description); }
public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel) { if (context.Request.Method == "POST") { if (method.Equals("all", StringComparison.OrdinalIgnoreCase)) { Get(user, context, cancel); return; } else if (method.Equals("find", StringComparison.OrdinalIgnoreCase)) { try { JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token["name"] != null) { GetName(token["name"].ToString(), user, context, cancel); return; } else if (token["orgid"] != null) { GetByOwner(JsonUtils.ToId(token["orgid"]), user, context, cancel); return; } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("in", StringComparison.OrdinalIgnoreCase)) { try { HashSet <CompoundIdentity> ids = JsonUtils.ToIds(JsonUtils.GetDataPayload(context.Request)); if (ids != null) { GetIds(ids, user, context, cancel); return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("parent", StringComparison.OrdinalIgnoreCase)) { try { JToken token = JsonUtils.GetDataPayload(context.Request); CompoundIdentity id = JsonUtils.ToId(token["id"]); CompoundIdentity parentid = JsonUtils.ToId(token["parentid"]); HashSet <CompoundIdentity> cids = JsonUtils.ToIds(token["childid"]); bool del = token["remove"] != null ? (bool)token["remove"] : false; if (parentid != null && cids != null) { if (del) { Remove(parentid, cids, user, context, cancel); return; } else { Add(parentid, cids, user, context, cancel); return; } } else if (id != null) { GetParent(id, user, context, cancel); return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("children", StringComparison.OrdinalIgnoreCase)) { try { CompoundIdentity cid = JsonUtils.ToId(JsonUtils.GetDataPayload(context.Request)); if (cid != null) { GetChildren(cid, user, context, cancel); return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("create", StringComparison.OrdinalIgnoreCase)) { CompoundIdentity owning_orgid = null; string name = null; string desc = null; SiteProviderBase provider = null; JToken token = null; try { //payload and provider token = JsonUtils.GetDataPayload(context.Request); provider = SiteManager.Instance.GetSiteProvider(user); if (provider != null && token != null) { //required inputs name = token["name"].ToString(); owning_orgid = JsonUtils.ToId(token["orgid"]); if (owning_orgid != null && !string.IsNullOrEmpty(name)) { desc = token["desc"] != null ? token["desc"].ToString() : null; HashSet <CompoundIdentity> pids = JsonUtils.ToIds(token["parentid"]); //could be >= 1, or null //create Site site = null; site = provider.Create(owning_orgid, name, desc); if (site != null) { //add parents if necessary bool result = true; if (pids != null) { foreach (CompoundIdentity p in pids) { result &= provider.AddParent(site.Identity, p); //parents are not returned with newly created site. This was just a convenience for REST /create } } if (result == true) { JObject jsite = Jsonifier.ToJson(site); if (jsite != null) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, jsite.ToString())); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("delete", StringComparison.OrdinalIgnoreCase)) { try { JToken t = JsonUtils.GetDataPayload(context.Request); HashSet <CompoundIdentity> cids = JsonUtils.ToIds(t); SiteProviderBase provider = SiteManager.Instance.GetSiteProvider(user); if (provider != null && cids != null) { bool result = true; foreach (CompoundIdentity cid in cids) { result &= provider.Delete(cid); } if (result == true) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("update", StringComparison.OrdinalIgnoreCase)) { CompoundIdentity cid = null; CompoundIdentity org_cid = null; HashSet <CompoundIdentity> pids = null; string name = null; string desc = null; bool dirty = false; try { //provider and token JToken token = JsonUtils.GetDataPayload(context.Request); SiteProviderBase provider = SiteManager.Instance.GetSiteProvider(user); if (provider != null && token != null) { //GUID must be provided cid = JsonUtils.ToId(token["id"]); //fetch stored object Site site = provider.Get(cid); if (site != null) { //## REQUIRED ## //name if (token.SelectToken("name") != null) { name = token["name"].ToString(); if (!string.IsNullOrEmpty(name)) { site.Name = name; dirty = true; } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //name is required and not nullable return; } } //owning org if (token.SelectToken("orgid") != null) { org_cid = JsonUtils.ToId(token["orgid"]); if (org_cid != null) { site.OwningOrganizationIdentity = org_cid; dirty = true; } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //owning org is required and not nullable return; } } //## OPTIONALS ## //description if (token.SelectToken("desc") != null) { desc = (token["desc"] != null) ? token["desc"].ToString() : null; site.Description = desc; dirty = true; } //geom if (token.SelectToken("geom") != null) { IGeometry2 <double> geom = Jsonifier.IsNullOrEmpty(token["geom"]) ? null : GeoJsonUtils.ParseGeometry(token["geom"].ToString()); site.Location = geom; dirty = true; } //altgeom if (token.SelectToken("altgeom") != null) { Point2 <double> altgeom = Jsonifier.IsNullOrEmpty(token["altgeom"]) ? null : GeoJsonUtils.ParseGeometry(token["altgeom"].ToString()) as Point2 <double>; site.LocationMark = altgeom; dirty = true; } //update bool result = true; if (dirty) { //update result &= provider.Update(site); if (result == false) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } //site hierarchy if (token.SelectToken("parentid") != null) { pids = JsonUtils.ToIds(token["parentid"]); //new parents IEnumerable <Site> existing_parents = provider.GetParents(cid); //existing parents if (pids == null) //clear all parent assignments { foreach (Site p in existing_parents) { result &= provider.RemoveParent(cid, p.Identity); } } else { //remove unlisted, keep listed foreach (Site p in existing_parents) { if (pids.Contains(p.Identity) == false) { result &= provider.RemoveParent(cid, p.Identity); } } //add new foreach (CompoundIdentity new_pid in pids) { bool contains = false; foreach (Site p in existing_parents) { if (p.Identity == new_pid) { contains = true; } break; } if (contains == false) { result &= provider.AddParent(cid, new_pid); } } } } if (result == true) { //return ok - no values were modified RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); return; } } } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }