get ASCII row and column info

1546
5
Jump to solution
02-12-2013 11:50 AM
DavidReeves
New Contributor II
I have a point shapefile and an ascii grid. When you view an ascii grid in a text editor the header tells you the xy coordinates of the center then the values are all displayed in neat rows and columns. What I am trying to find out is what column and row those points fall in. When both are added to ArcMap I can find out the value of the cell, but I want the column and row number. Anyone have any clue how this can be accomplished?

ncols  1926 nrows  2752 xllcenter 533993.691804 yllcenter 3247644.275250 cellsize 15.000000 NODATA_value -9999 -9999  -9999  -9999  -9999  -9999  ... -9999  -9999  -9999  0.0  0.0  ... -9999  -9999  -9999  0.0  0.0  ... -9999  -9999  -9999  123.4  123.4  ... ...
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidReeves
New Contributor II
That's exactly what I ended up doing.
Now I just need to figure out what to do with interpreting the decimal result.
Ex. is 37.604 in the 37th row or the 38th row? I'm thinking 38th.

file = open(r"C:\WorkSpace\Grid.asc")  nCols = file.readline() nRows = file.readline() xllCenter = file.readline() yllCenter = file.readline() cellSize = file.readline()  file.close()  nCols = re.sub('ncols', '', re.sub('\s','',nCols)) nRows = re.sub('nrows', '', re.sub('\s','',nRows)) xllCenter = float(re.sub('xllcenter', '', re.sub('\s','',xllCenter))) yllCenter = float(re.sub('yllcenter', '', re.sub('\s','',yllCenter))) cellSize = float(re.sub('cellsize', '', re.sub('\s','',cellSize)))  # subtract half the cell size to get the coordinates at the lower left corner of the cell, not the exact center xllCenterAdjust = xllCenter - 7.5 yllCenterAdjust = yllCenter - 7.5  pointX = 534557.7644 pointY = 3248152.003  xColumn = ((pointX - xllCenterAdjust)/cellSize) yColumn = ((pointY - yllCenterAdjust)/cellSize)  print xColumn print yColumn

View solution in original post

0 Kudos
5 Replies
RaphaelR
Occasional Contributor II
Hi David,

if you´re looking for the row/column of a single point it should be:
Column = (CurrentXcoordinate - XLLcenter) / Cellsize
Row = (nrows -1) - (CurrentYcoordinate - YLLcenter) / Cellsize
0 Kudos
DavidReeves
New Contributor II
That's exactly what I ended up doing.
Now I just need to figure out what to do with interpreting the decimal result.
Ex. is 37.604 in the 37th row or the 38th row? I'm thinking 38th.

file = open(r"C:\WorkSpace\Grid.asc")  nCols = file.readline() nRows = file.readline() xllCenter = file.readline() yllCenter = file.readline() cellSize = file.readline()  file.close()  nCols = re.sub('ncols', '', re.sub('\s','',nCols)) nRows = re.sub('nrows', '', re.sub('\s','',nRows)) xllCenter = float(re.sub('xllcenter', '', re.sub('\s','',xllCenter))) yllCenter = float(re.sub('yllcenter', '', re.sub('\s','',yllCenter))) cellSize = float(re.sub('cellsize', '', re.sub('\s','',cellSize)))  # subtract half the cell size to get the coordinates at the lower left corner of the cell, not the exact center xllCenterAdjust = xllCenter - 7.5 yllCenterAdjust = yllCenter - 7.5  pointX = 534557.7644 pointY = 3248152.003  xColumn = ((pointX - xllCenterAdjust)/cellSize) yColumn = ((pointY - yllCenterAdjust)/cellSize)  print xColumn print yColumn
0 Kudos
DavidReeves
New Contributor II

Column = (CurrentXcoordinate - XLLcenter) / Cellsize
Row = (nrows -1) - (CurrentYcoordinate - YLLcenter) / Cellsize


Why did you include "(nrows-1) -" to calculate the row?

I used the script in my previous post to find the row and column. To check my work, I changed the value in the resulting ASCII row & column to something unique and set ArcMap to display that value with a unique color. I expected the point to fall in that cell, but it was way off (more than "nrows-1"). What could be causing this?
0 Kudos
curtvprice
MVP Esteemed Contributor
Why did you include "(nrows-1) -" to calculate the row?


Row values, by convention, start at  the top with row zero located at the highest y values.

In the old days of image processing, the terminology was line and sample. I believe this convention goes back to TV and also when we often looked at raster images printed out using coded values on line printers.

Those were the days.
0 Kudos
DavidReeves
New Contributor II
The results were slightly off and it took me a bit to figure out why.

xllcenter & yllcenter provide the coordinates at the center of the most lower left ASC cell. In order to find the coordinates at the lower left corner of the lower left cell, you need to subtract half of the cell size from both coordinates.

So, with a cell size of 15 meters
xllCenter = xllCenter - 7.5
yllCenter = yllCenter - 7.5
0 Kudos