private static ValueList GetPixelData(IMap map, IEnvelope extent, HashSet <string> includeFields) { ValueList result = new ValueList(); for (int i = 0; i < map.LayerCount; i += 1) { ILayer layer = map.get_Layer(i); if (!layer.Visible) { continue; } IIdentify id = layer as IIdentify; if (id == null) { continue; } IArray data = id.Identify(extent); if (data != null) { for (int j = 0; j < data.Count; j += 1) { object foundObj = data.get_Element(j); IRasterIdentifyObj2 raster = foundObj as IRasterIdentifyObj2; IRowIdentifyObject row = foundObj as IRowIdentifyObject; if (raster != null) { int propertyIndex = 0; string property; string value; while (true) { try { raster.GetPropAndValues(propertyIndex, out property, out value); if ((!"NoData".Equals(value)) && ((includeFields == null) || includeFields.Contains(property))) { result.Add(property, value); } propertyIndex += 1; } catch { break; } } continue; } else if (row != null) { IFields fields = row.Row.Fields; for (int k = 0; k < fields.FieldCount; k += 1) { string fieldName = fields.get_Field(k).Name; if ((includeFields == null) ? (!result.ContainsKey(fieldName)) : includeFields.Contains(fieldName)) { result.Add(fieldName, row.Row.get_Value(k)); } } } } break; } } return(result); }
private static Dictionary <string, object> CollectData(IMap map, IEnvelope extent, HashSet <string> includeFields) { const int tileSize = 128; double cellWidth = extent.Width / (tileSize - 1); double cellHeight = extent.Height / (tileSize - 1); Dictionary <ValueList, List <Cell> > dataCells = new Dictionary <ValueList, List <Cell> >(); for (int y = 0; y < tileSize; y += 1) { for (int x = 0; x < tileSize; x += 1) { EnvelopeClass pixelExtent = new EnvelopeClass(); pixelExtent.XMin = extent.XMin + x * cellWidth; pixelExtent.XMax = pixelExtent.XMin + cellWidth; pixelExtent.YMax = extent.YMax - y * cellHeight; pixelExtent.YMin = pixelExtent.YMax - cellHeight; ValueList cellData = GetPixelData(map, pixelExtent, includeFields); if (cellData.Count > 0) { if (dataCells.ContainsKey(cellData)) { dataCells[cellData].Add(new Cell(y, x)); } else { List <Cell> cells = new List <Cell>(); cells.Add(new Cell(y, x)); dataCells[cellData] = cells; } } } } StringBuilder[] grid = new StringBuilder[tileSize]; for (int i = 0; i < tileSize; i += 1) { grid[i] = new StringBuilder(new String(' ', tileSize)); } List <string> keys = new List <string>(); keys.Add(""); Dictionary <string, object> data = new Dictionary <string, object>(); int keyIndex = 0; foreach (KeyValuePair <ValueList, List <Cell> > d in dataCells) { if (d.Key.Count > 0) { char code = EncodeChar(keys.Count); string key = keyIndex.ToString(); foreach (Cell cell in d.Value) { grid[cell.Row][cell.Col] = code; } keys.Add(key); data.Add(key, d.Key); keyIndex += 1; } } if (keys.Count > 1) { Dictionary <string, object> result = new Dictionary <string, object>(); result.Add("grid", grid.Select(sb => sb.ToString()).ToArray()); result.Add("keys", keys.ToArray()); result.Add("data", data); return(result); } else { return(null); } }