|     Documentation    |     Premium Support

Query Z values from Projected GridFeatureLayer


I have a GridFeatureLayer where the internal projection is lat/long and I’m displaying it in a projected map. The projection looks just fine, but when I try and query the GridCell[,] array, I am returning incorrect values of Z.

Here’s the code I’ve always used based on one of the samples. To be honest, this code looks like it would only work on projected data. But, since GridCell[,] is a 2D array and gfl.CellSize is static I am not sure how one could make this work on a lat/long grid.

Please let me know if it is possible to get the correct Z value.

_cells = gfl.GenerateGridMatrix();
_gridRect = gfl.GetBoundingBox();

double cellSize = gfl.CellSize;

int binX = Convert.ToInt32((WorldPointR.X - _gridRect.UpperLeftPoint.X) / cellSize);
int binY = Convert.ToInt32((_gridRect.UpperLeftPoint.Y - WorldPointR.Y) / cellSize);

double z = _cells[binY, binX].Value;


Hey @Damian_Hite,

You’re right in that the CellSize needs to change depending on the projection. Here’s how you can calculate that:

private void WinformsMap1OnMapClick(object sender, MapClickWinformsMapEventArgs e)
    var _cells = gridFeatureLayer.GenerateGridMatrix();
    var _gridRect = gridFeatureLayer.GetBoundingBox();

    double projectedColCellSize = (_gridRect.LowerRightPoint.X - _gridRect.UpperLeftPoint.X) / gridFeatureLayer.NumberOfColumns;
    double projectedRowCellSize = (_gridRect.UpperLeftPoint.Y - _gridRect.LowerRightPoint.Y) / gridFeatureLayer.NumberOfRows;

    int columnIndex = Convert.ToInt32(Math.Floor(Math.Round((e.WorldLocation.X - _gridRect.UpperLeftPoint.X) / projectedColCellSize, 8)));
    int rowIndex = Convert.ToInt32(Math.Floor(Math.Round((_gridRect.UpperLeftPoint.Y - e.WorldLocation.Y) / projectedRowCellSize, 8)));

    double z = _cells[rowIndex, columnIndex].Value;

    Debug.WriteLine($"Row: {rowIndex}, Column: {columnIndex}, Value: {z}");

So, all that really needs done is to recalculate the cell sizes based on the projected bounding box. You need to figure out both the row and column cell size because the projection can warp the dimensions. There is, however, another problem. Projections can also skew the grid in such a way that it could display as a rhomboid rather than a perfect grid. For example, my test data is in EPSG:2276 and I’m projecting it to EPSG:3857 and you’ll see a slight skew:


So, when the user would click on the upper right cell, they would get the (0,2) position in the grid instead of (0,0). I think the best way to handle this is to recreate the grid based on the projection that you want to use on your map, otherwise this kind of thing will unfortunately happen.