Ejemplo n.º 1
0
        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);
                }
            }
        }