Mapstache.Utf8Grid.CreateUtfGridJson C# (CSharp) Метод

CreateUtfGridJson() публичный Метод

public CreateUtfGridJson ( ) : string
Результат string
        public string CreateUtfGridJson()
        {
            //bitmap.Save("c:\\temp\\utg8.png");
            var bitmapData = _bitmap.LockBits(new Rectangle(0, 0, _bitmap.Width, _bitmap.Height), ImageLockMode.ReadOnly,
                                             _bitmap.PixelFormat);

            var ptr = bitmapData.Scan0;
            int bytes = Math.Abs(bitmapData.Stride) * _bitmap.Height;
            var rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            var uniqueValues = new List<int>();
            var grid = new int[_bitmap.Width, _bitmap.Height];

            for (int row = 0; row < _bitmap.Height; row++)
            {
                var start = row * bitmapData.Stride;
                for (int x = 0; x < _bitmap.Width; x++)
                {
                    var value = RgbToInt(start, rgbValues, x);
                    if (uniqueValues.Contains(value) == false)
                    {
                        uniqueValues.Add(value);
                    }
                    grid[x, row] = value;
                }
            }
            uniqueValues.Sort();
            _bitmap.UnlockBits(bitmapData);

            for (int y = 0; y < _bitmap.Height; y++)
            {
                var sb = new StringBuilder();
                for (int x = 0; x < _bitmap.Width; x++)
                {
                    var key = (grid[x, y]);
                    var id = uniqueValues.IndexOf(key);
                    id = id + 32;
                    if (id >= 34)
                    {
                        id = id + 1;
                    }
                    if (id >= 92)
                    {
                        id = id + 1;
                    }
                    sb.Append(char.ConvertFromUtf32(id));
                }
                this.Grid.Add(sb.ToString());
            }

            if (uniqueValues.Contains(0))
            {
                // remove 0 since that is taken care of by ""
                uniqueValues.Remove(0);
                this.Keys.Add("");
            }

            uniqueValues.ForEach(value => this.Keys.Add(value.ToString()));

            var json = JsonConvert.SerializeObject(this, Formatting.Indented);
            return json;
        }

Usage Example

Пример #1
0
        public ActionResult States(int x,int y, int z)
        {
            var key = string.Format(@"states\{0}\{1}\{1}", x, y, z);
            var cachedJson = this.HttpContext.Cache[key] as string;
            if (cachedJson != null)
            {
                return new ContentResult() {Content = cachedJson, ContentType = "application/json"};
            }

            const int utfgridResolution = 2;

            using (var utf8Grid = new Utf8Grid(utfgridResolution, x, y, z))
            {
                var bbox = GetBoundingBoxInLatLngWithMargin(x, y, z);
                if (bbox.Bottom > 0)
                {
                    var states = new StatesRepository().Query(bbox.ToSqlGeography());
                    int i = 1;
                    foreach (var state in states)
                    {
                        var geography = (SqlGeography) state["geom"];
                        var projected = ((SqlGeography) state["geom"]).FromLonLat().MakeValid();
                        var wkt = projected.STAsText().ToSqlString().Value;
                        utf8Grid.FillPolygon(geography, i,
                                             new {NAME = state["STATE_NAME"], POP2005 = state["POP2000"], Wkt = wkt});
                        i = i + 1;
                    }
                }
                cachedJson = utf8Grid.CreateUtfGridJson();
                this.HttpContext.Cache.Insert(key, cachedJson);
                return new ContentResult() { Content = cachedJson, ContentType = "application/json" };
            }
        }
All Usage Examples Of Mapstache.Utf8Grid::CreateUtfGridJson