private static IMultiPolygon ReadMultiPolygon(JsonTextReader jreader) { if (jreader == null) { throw new ArgumentNullException("reader", "A valid JSON reader object is required."); } IMultiPolygon areas = null; if (jreader.TokenClass == JsonTokenClass.Array) { jreader.ReadToken(JsonTokenClass.Array); List <IPolygon> polygons = new List <IPolygon>(); while (jreader.TokenClass == JsonTokenClass.Array) { jreader.ReadToken(JsonTokenClass.Array); //Read the outer shell ILinearRing shell = null; if (jreader.TokenClass == JsonTokenClass.Array) { Coordinate[] coordinates = new Coordinate[] { }; Read(ref coordinates, jreader); shell = new NetTopologySuite.Geometries.LinearRing(coordinates); } //Read all the holes List <ILinearRing> list = new List <ILinearRing>(); while (jreader.TokenClass == JsonTokenClass.Array) { Coordinate[] coordinates = new Coordinate[] { }; Read(ref coordinates, jreader); ILinearRing hole = new NetTopologySuite.Geometries.LinearRing(coordinates); list.Add(hole); } jreader.ReadToken(JsonTokenClass.EndArray); //An outer shell was found so a polygon can be created if (shell != null) { IPolygon area = new NetTopologySuite.Geometries.Polygon(shell, list.ToArray()); polygons.Add(area); } } jreader.ReadToken(JsonTokenClass.EndArray); areas = new NetTopologySuite.Geometries.MultiPolygon(polygons.ToArray()); } return(areas); }
/// <summary> /// Remove all holes in a multipolygon /// </summary> /// <param name="multiPolygon"></param> /// <returns></returns> public static IGeometry RemoveHoles(this MultiPolygon multiPolygon) { IGeometry newGeometry = null; for (var i = 0; i < multiPolygon.NumGeometries; i++) { var polygon = new Polygon((ILinearRing)((IPolygon)multiPolygon.GetGeometryN(i)).ExteriorRing); newGeometry = i == 0 ? polygon : newGeometry.Union(polygon); } return(newGeometry); }
public static Polygon FromGeometryMultiPolygon(NetTopologySuite.Geometries.MultiPolygon multiPolygon) { if (multiPolygon == null) { throw new ArgumentNullException(nameof(multiPolygon)); } var boundingBox = new BoundingBox2D( multiPolygon.EnvelopeInternal.MinX, multiPolygon.EnvelopeInternal.MinY, multiPolygon.EnvelopeInternal.MaxX, multiPolygon.EnvelopeInternal.MaxY ); var linearRings = multiPolygon .Geometries .Cast <NetTopologySuite.Geometries.Polygon>() .SelectMany(polygon => new [] { polygon.Shell }.Concat(polygon.Holes)) .ToArray(); var offset = 0; var parts = new int[linearRings.Length]; for (var index = 0; index < linearRings.Length; index++) { parts[index] = offset; var line = linearRings[index]; offset += line.NumPoints; } var points = linearRings .SelectMany(line => line.Coordinates) .Select(coordinate => new Point(coordinate.X, coordinate.Y)) .ToArray(); return(new Polygon(boundingBox, parts, points)); }
public void SingleActionAwardInsideOfGeoFenceRuleSucceeds() { var actionId = Guid.NewGuid().ToString("N"); var realmRefId = Guid.NewGuid(); var player = new Player() { RealmRefId = realmRefId, }; player.Tags.Add("Fuu"); var actions = new Actions() { new Common.Action() { RealmRefId = realmRefId, ReleasedOn = DateTimeOffset.UtcNow.AddSeconds(-60), ActionId = actionId, } }; var sr = new List <SmartContext <ActionRequest> >() { new SmartContext <ActionRequest>() { Latitude = 33.753746, Longitude = -84.386330, Data = new ActionRequest() { ActionId = actionId, OccurredOn = DateTimeOffset.UtcNow.AddDays(-10) } }, new SmartContext <ActionRequest>() { Latitude = 33.753746, Longitude = -84.386330, Data = new ActionRequest() { ActionId = actionId, OccurredOn = DateTimeOffset.UtcNow.AddHours(-36) } }, new SmartContext <ActionRequest>() { Latitude = 33.753746, Longitude = -84.386330, Data = new ActionRequest() { ActionId = actionId, OccurredOn = DateTimeOffset.UtcNow.AddHours(-24) } }, new SmartContext <ActionRequest>() { Latitude = 33.753746, Longitude = -84.386330, Data = new ActionRequest() { ActionId = actionId, OccurredOn = DateTimeOffset.UtcNow.AddHours(-8) } }, new SmartContext <ActionRequest>() { Latitude = 33.753746, Longitude = -84.386330, Data = new ActionRequest() { ActionId = actionId, OccurredOn = DateTimeOffset.UtcNow.AddMinutes(-1) } } }; NetTopologySuite.Geometries.MultiPolygon insideOf = null; var jsonSerializer = NetTopologySuite.IO.GeoJsonSerializer.Create(); using (StreamReader file = File.OpenText(@"GeorgiaGeoJsonData.json")) using (JsonTextReader reader = new JsonTextReader(file)) { insideOf = jsonSerializer.Deserialize <NetTopologySuite.Geometries.MultiPolygon>(reader); } var coins = new Coins().LoadTestData(realmRefId); var awards = new Awards().LoadTestData(coins); var goal = new Goal().LoadTestData(realmRefId, awards); var goalTriggers = new GoalTriggers().LoadTestData(realmRefId, goal, actions, insideOf, null, null);; foreach (var goalTrigger in goalTriggers) { var events = new PlayerActionEvents(); events.AddRange( sr.Select(e => new PlayerActionEvent() { OccurredAt = new NetTopologySuite.Geometries.Point(e.Longitude, e.Latitude) }.Map(e.Data, actions) ) ); var qualifyingActionRequests = goalTrigger.ValidateSteps(events); Assert.IsTrue(qualifyingActionRequests.Count > 0); } // the action is "read an ad" // step 1 read an ad on tuesday // step 2 read an ad on thursday // convert requests to Events }
private void Project() { if (geo == null) { return; } polygonsPixel = new List <Point[]>(); const float MARGIN_PERC = 0.05F; RangeD range = new RangeD((maxX - minX), (maxY - minY)); RangeD scale = new RangeD(ClientSize.Width * (1 - 2 * MARGIN_PERC) / range.Width, ClientSize.Height * (1 - 2 * MARGIN_PERC) / range.Height); Point margin; if (scale.Width < scale.Height) { scale = new RangeD(scale.Width, scale.Width); margin = new Point((int)(ClientSize.Width * MARGIN_PERC), (int)((ClientSize.Height - (range.Height * scale.Height)) / 2)); } else { scale = new RangeD(scale.Height, scale.Height); margin = new Point((int)( (ClientSize.Width - (range.Width * scale.Width)) / 2), (int)(ClientSize.Height * MARGIN_PERC)); } List <GeoAPI.Geometries.IGeometry> polygons = new List <GeoAPI.Geometries.IGeometry>(); if (geo.OgcGeometryType == GeoAPI.Geometries.OgcGeometryType.Polygon) { polygons.Add(geo); } else { NetTopologySuite.Geometries.MultiPolygon mp = geo as NetTopologySuite.Geometries.MultiPolygon; polygons.AddRange(mp.Geometries); } foreach (var polygon in polygons) { List <Point> points = new List <Point>(); foreach (var p in polygon.Coordinates) { var y = p.Y; if (InvertY) { y *= -1; } Point point = new Point(margin.X + (int)((p.X - minX) * scale.Width), margin.Y + (int)((y - minY) * scale.Height)); points.Add(point); } polygonsPixel.Add(points.ToArray()); } }