public async Task Create(Area region) { string areaId = Guid.NewGuid().ToString(); region.AreaID = areaId; region.Points = GooglePoints.Decode(region.EncodedPolygon).Select(x => new LocationPoint(x.Latitude, x.Longitude)).ToList(); region.EncodedPolygon = null; List <Tile> tiles = this.GetCoordinateTile(region.Points.Select(x => new PointF((float)x.Lat, (float)x.Lng)).ToList(), true).tiles; AreaMaster areaMaster = new AreaMaster() { AreaID = areaId, AreaName = region.AreaName, IsPredefine = false, EncodedPolygon = GooglePoints.EncodeBase64(region.Points.Select(x => new CoordinateEntity(Convert.ToDouble(x.Lat), Convert.ToDouble(x.Lng)))), EncodedCompletedTiles = GooglePoints.EncodeBase64(tiles.Where(x => !x.IsPartialTile).Select(x => new CoordinateEntity(x.Row, x.Column))), EncodedPartialTiles = GooglePoints.EncodeBase64(tiles.Where(x => x.IsPartialTile).Select(x => new CoordinateEntity(x.Row, x.Column))) }; List <Task> tasks = new List <Task>(); tasks.Add(Task.Factory.StartNew(() => { this.context.SaveAsync <AreaMaster>(areaMaster).Wait(); })); List <Area> areas = this.transformRegion(region, tiles); SaveAreas(areas); Task.WaitAll(tasks.ToArray()); }
public RasterizeObject GetCoordinateTile(List <PointF> points, bool withRasterize, List <PointF> boundingBox = null, int zoomlevel = 14, string encodedTiles = null) { try { RasterizeObject rasterizeObject = new RasterizeObject(); rasterizeObject.tiles = new List <Tile>(); object lockObj = new object(); if (withRasterize) { string postData = @"{""zoom"": " + zoomlevel + @","; if (!string.IsNullOrEmpty(encodedTiles)) { postData += postData = @"""encodedTile"": """ + encodedTiles + @""""; } else if (points != null && points.Count() > 0) { string encodedString = GooglePoints.EncodeBase64(points.Select(x => new CoordinateEntity(x.X, x.Y))); postData += @"""encodedPolygon"": """ + encodedString + @""""; } if (boundingBox != null && boundingBox.Count() > 0) { string boundingBoxPostData = JSONHelper.GetString(boundingBox.Select(x => new LocationPoint { Lat = Convert.ToDecimal(x.X), Lng = Convert.ToDecimal(x.Y) }).ToList()); postData += @",""boundingBox"": " + boundingBoxPostData; } postData += "}"; string responseFromServer = responseFromServer = this.PostData(LambdaResourceType.areas, postData); if (boundingBox != null && boundingBox.Count() > 0) { return(JSONHelper.GetObject <RasterizeObject>(responseFromServer)); } else { RasterizeObject obj = new RasterizeObject(); obj.tiles = JSONHelper.GetObject <List <Tile> >(responseFromServer); return(obj); } } else { List <Tile> tilesCoordinates = new List <Tile>(); foreach (var point in points) { tilesCoordinates.Add(new Tile() { Zoom = zoomlevel, Lat = point.X, Lng = point.Y }); } ; Parallel.ForEach(tilesCoordinates.ChunkBy(200), tilesCoordinate => { string postData = JSONHelper.GetString(tilesCoordinate); string responseFromServer = this.PostData(LambdaResourceType.listings, postData); //string responseFromServer = this.CallRasterizationLambda(postData); lock (lockObj) { rasterizeObject.tiles.AddRange(JSONHelper.GetObject <List <Tile> >(responseFromServer)); } }); } return(rasterizeObject); } catch (Exception ex) { throw ex; } }
public static void migrateAreas(DataRow[] dtAreas) { List <Area> areaMigration = new List <Area>(); foreach (DataRow area in dtAreas) { Area obj = new Area(); obj.AreaID = area[0].ToString(); obj.AreaName = area[1].ToString(); obj.URLName = area[2].ToString(); obj.URLPath = area[3].ToString(); obj.StateFIPS = area[4].ToString(); obj.FIPS = area[5].ToString(); obj.State = area[6].ToString(); obj.USPSCity = area[7].ToString(); obj.AreaTypeID = area[8].ToString(); obj.SubTypeID = area[9].ToString(); obj.OriginalPolygon = area[10].ToString(); obj.Latitude = Convert.ToDecimal(area[11].ToString()); obj.Longitude = Convert.ToDecimal(area[12].ToString()); obj.North = area[13].ToString(); obj.South = area[14].ToString(); obj.East = area[15].ToString(); obj.West = area[16].ToString(); obj.TopLeveeaID = area[17].ToString(); obj.SourceID = area[18].ToString(); obj.SourceKey = area[19].ToString(); obj.AreaStatus = !area.Table.Columns.Contains("Status") ? string.Empty : area[21].ToString(); areaMigration.Add(obj); } List <AreaMaster> areaMaster = new List <AreaMaster>(); List <Area> areas = new List <Area>(); foreach (Area obj in areaMigration) { var tempPoints = obj.OriginalPolygon.Replace("MULTIPOLYGON", "").Replace("POLYGON", "").Replace("(", "").Replace(")", "").Split(",").Select(x => x.Trim()).Where(x => x.Length > 0).Select(x => new LocationPoint() { Lng = Convert.ToDecimal(x.Substring(0, x.IndexOf(" ")).Trim()), Lat = Convert.ToDecimal(x.Substring(x.IndexOf(" "), x.Length - x.IndexOf(" ")).Trim()) }).ToList(); obj.Points = tempPoints; List <Tile> rasterizePoints = regionServiceInstance.GetCoordinateTile(obj.Points.Select(x => new PointF((float)x.Lat, (float)x.Lng)).ToList(), true).tiles; AreaMaster areaMasterObj = new AreaMaster(); areaMasterObj.AreaID = obj.AreaID; areaMasterObj.AreaName = obj.AreaName; areaMasterObj.EncodedPolygon = GooglePoints.EncodeBase64(tempPoints.Select(x => new CoordinateEntity(Convert.ToDouble(x.Lat), Convert.ToDouble(x.Lng)))); areaMasterObj.EncodedCompletedTiles = GooglePoints.EncodeBase64(rasterizePoints.Where(x => !x.IsPartialTile).Select(x => new CoordinateEntity(Convert.ToDouble(x.Row), Convert.ToDouble(x.Column)))); areaMasterObj.EncodedPartialTiles = GooglePoints.EncodeBase64(rasterizePoints.Where(x => x.IsPartialTile).Select(x => new CoordinateEntity(Convert.ToDouble(x.Row), Convert.ToDouble(x.Column)))); areaMasterObj.IsPredefine = true; areaMaster.Add(areaMasterObj); foreach (var point in rasterizePoints) { Area tempObj = (Area)obj.Clone(); tempObj.Tile = regionServiceInstance.GetTileStr((int)point.Row, (int)point.Column); tempObj.Type = RecordType.Area; tempObj.OriginalPolygon = ""; tempObj.Points = null; areas.Add(tempObj); } } List <AttributeDefinition> areaAttributeDefinition = new List <AttributeDefinition>() { new AttributeDefinition { AttributeName = "Tile", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "AreaID", AttributeType = ScalarAttributeType.S } }; Projection projection = new Projection() { ProjectionType = "INCLUDE", NonKeyAttributes = new List <string> { "AreaName" } }; List <LocalSecondaryIndex> localSecondaryIndexes = new List <LocalSecondaryIndex>(); List <KeySchemaElement> areaIDKeySchema = new List <KeySchemaElement>() { new KeySchemaElement { AttributeName = "Tile", KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = "AreaID", KeyType = KeyType.RANGE } }; localSecondaryIndexes.Add(new LocalSecondaryIndex() { IndexName = "AreaIDIndex", Projection = projection, KeySchema = areaIDKeySchema }); regionServiceInstance.CreateTempTable(regionServiceInstance.areaTableName, areaAttributeDefinition, null, localSecondaryIndexes, "Tile", "AreaID").Wait(); List <LocalSecondaryIndex> areaMasterLocalSecondaryIndexes = new List <LocalSecondaryIndex>(); List <KeySchemaElement> areaMasterAreaIDKeySchema = new List <KeySchemaElement>() { new KeySchemaElement { AttributeName = "AreaID", KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = "AreaName", KeyType = KeyType.RANGE } }; areaMasterLocalSecondaryIndexes.Add(new LocalSecondaryIndex() { IndexName = "AreaIDIndex", Projection = new Projection() { ProjectionType = "INCLUDE", NonKeyAttributes = new List <string> { "IsPredefine" } }, KeySchema = areaMasterAreaIDKeySchema }); List <KeySchemaElement> areaMasterAreaTileKeySchema = new List <KeySchemaElement>() { new KeySchemaElement { AttributeName = "AreaID", KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = "IsPredefine", KeyType = KeyType.RANGE } }; areaMasterLocalSecondaryIndexes.Add(new LocalSecondaryIndex() { IndexName = "AreaTileIndex", Projection = new Projection() { ProjectionType = "INCLUDE", NonKeyAttributes = new List <string> { "EncodedTiles" } }, KeySchema = areaMasterAreaTileKeySchema }); areaMasterLocalSecondaryIndexes.Add(new LocalSecondaryIndex() { IndexName = "AreaPolygonIndex", Projection = new Projection() { ProjectionType = "INCLUDE", NonKeyAttributes = new List <string> { "EncodedPolygon" } }, KeySchema = areaMasterAreaTileKeySchema }); List <AttributeDefinition> areaMasterAttributeDefinition = new List <AttributeDefinition>() { new AttributeDefinition { AttributeName = "AreaID", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "AreaName", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "IsPredefine", AttributeType = ScalarAttributeType.N }, }; regionServiceInstance.CreateTempTable(regionServiceInstance.areaMasterTableName, areaMasterAttributeDefinition, null, areaMasterLocalSecondaryIndexes, "AreaID", "AreaName").Wait(); foreach (var obj in areaMaster.ToList().ChunkBy(100)) { try { System.Console.WriteLine("adding Area Master chunk"); var batch = regionServiceInstance.context.CreateBatchWrite <AreaMaster>(); batch.AddPutItems(obj); batch.ExecuteAsync().Wait(); System.Console.WriteLine("Area Master Chunk added"); //regionServiceInstance.context.SaveAsync(obj).Wait(); } catch (Exception e) { System.Console.WriteLine(obj); System.Console.WriteLine(e); } } int count = 1; foreach (var obj in areas.ToList().ChunkBy(100)) { try { System.Console.WriteLine("adding Area chunk. index: " + count); //Parallel.ForEach(obj, obj2 => //{ // regionServiceInstance.context.SaveAsync<Area>(obj2).Wait(); //}); var batch = regionServiceInstance.context.CreateBatchWrite <Area>(); batch.AddPutItems(obj); batch.ExecuteAsync().Wait(); System.Console.WriteLine("Area Chunk added. index: " + count); count += 1; } catch (Exception e) { System.Console.WriteLine(obj); System.Console.WriteLine(e); } } }