Ejemplo n.º 1
0
        static IEnumerable <FloridaCity> PopulateCitiesFromJsonFile(FloridaCountiesDbContext dbContext)
        {
            FeatureCollection fileContent = null;
            var jsonSerializer            = GeoJsonSerializer.Create();

            using (StreamReader jsonStreamReader = File.OpenText(CITIES_JSON_DATAFILE)) {
                using (JsonTextReader jsonTextReader = new JsonTextReader(jsonStreamReader)) {
                    jsonTextReader.SupportMultipleContent = false;
                    while (jsonTextReader.Read())
                    {
                        if (jsonTextReader.TokenType == JsonToken.StartObject)
                        {
                            fileContent = jsonSerializer.Deserialize <FeatureCollection>(jsonTextReader);
                        }
                    }
                }
            }

            if (fileContent != null)
            {
                return(fileContent.Select(feature => {
                    FloridaCity floridaCity = new FloridaCity()
                    {
                        Id = Convert.ToInt32(feature.Attributes["AUTOID"]),
                        PlaceFP = Convert.ToInt32(feature.Attributes["PLACEFP"]),
                        BebrId = Convert.ToInt32(feature.Attributes["BEBR_ID"]),
                        Name = feature.Attributes["NAME"].ToString(),
                        Notes = feature.Attributes["NOTES"]?.ToString(),
                        Description = feature.Attributes["DESCRIPT"]?.ToString(),
                        EntryCreationDate = DateTime.Parse(feature.Attributes["FGDLAQDATE"].ToString()),
                        Area = (double)feature.Attributes["SHAPE_AREA"],
                        Perimeter = (double)feature.Attributes["SHAPE_LEN"]
                    };

                    try {
                        string countyName = feature.Attributes["COUNTY"].ToString().Split(',')[0];
                        countyName = countyName == "ST LUCIE" ? "ST. LUCIE" : countyName;
                        countyName = countyName == "ST JOHNS" ? "ST. JOHNS" : countyName;
                        floridaCity.CountyId = dbContext.Counties.First(county => county.Name == countyName).Id;
                    } catch {
                        throw;
                    }

                    switch (feature.Geometry.GeometryType)
                    {
                    case "Polygon":
                        Polygon polygon = feature.Geometry as Polygon;
                        polygon = polygon.Shell.IsCCW ? polygon :
                                  new Polygon(polygon.Shell.Reverse() as LinearRing, polygon.Holes)
                        {
                            SRID = 4326
                        };

                        for (int i = 0; i < polygon.Holes.Length; i++)
                        {
                            LinearRing hole = polygon.Holes[i];
                            hole = hole.IsCCW ? hole.Reverse() as LinearRing : hole;

                            polygon.Holes[i] = hole;
                        }

                        floridaCity.Shape = polygon;
                        break;

                    case "MultiPolygon":
                        MultiPolygon multiPolygon = feature.Geometry as MultiPolygon;
                        for (int i = 0; i < multiPolygon.Geometries.Length; i++)
                        {
                            Polygon p = multiPolygon.Geometries[i] as Polygon;

                            p = p.Shell.IsCCW ? p :
                                new Polygon(p.Shell.Reverse() as LinearRing, p.Holes)
                            {
                                SRID = 4326
                            };

                            for (int j = 0; j < p.Holes.Length; j++)
                            {
                                LinearRing hole = p.Holes[j];
                                hole = hole.IsCCW ? hole.Reverse() as LinearRing : hole;

                                p.Holes[j] = hole;
                            }

                            multiPolygon.Geometries[i] = p;
                        }


                        floridaCity.Shape = multiPolygon;
                        break;
                    }

                    return floridaCity;
                }));
            }
            else
            {
                return(null);
            }
        }