Hi,
I have a coordinatess of one random point let's say XY = (412,992.581, 48,111.728) in meters in some local projected coordinate system. I would like to obtain the coordinates of the closest raster cell center X1Y1 and snap (change) the first coordinates to the same values. Is this even possible?
Solved! Go to Solution.
I have done something like that in the past, but not in Python so I dont have any code example.
You will need the origin of the raster, number of columns and rows as well as cell size. Get those with arcpy.Describe
The cell (column, row) can then be calculated as
column = int(randomPointX - originX/cellsize)
row = int(randomPointY - originY/cellsize)
The cell center is then:
centerX = originX + (column*cellsize) + 0.5*cellsize
centerY = originY + (row*cellsize) + 0.5*cellsize
Lastly you can assign these coordinates to your random point (or create a new).
I hope this helps, otherwise, just ask.
Regards
Carsten
I have done something like that in the past, but not in Python so I dont have any code example.
You will need the origin of the raster, number of columns and rows as well as cell size. Get those with arcpy.Describe
The cell (column, row) can then be calculated as
column = int(randomPointX - originX/cellsize)
row = int(randomPointY - originY/cellsize)
The cell center is then:
centerX = originX + (column*cellsize) + 0.5*cellsize
centerY = originY + (row*cellsize) + 0.5*cellsize
Lastly you can assign these coordinates to your random point (or create a new).
I hope this helps, otherwise, just ask.
Regards
Carsten
Code examples are provided in the links I provided. Arcpy can access the functionality of the provided tools without having to develop first-principles coding access to the data
Hey Carsten and Dan, Thank you for your help. Really like your solution and I was just about post my solution that is very similar if not the same and ask if it would work in any case.
Could you evalute and comment please:
First I round the coordinates down to the nearest integer. I believe that "math.trunc()" will do that.
Second, divide by cell size and round down the result to the nearest integer.
Third, multiply by cell size. The result are the coordinates of the lower left corner of the corresponding cell.
And finally add the1/2 of the cell size to the coordiantes from the third step.
Can you explain why have you subtracted the origin coordinates from the random coordinates?
Will this work in any situation on any raster in any coorddinate system?
What if the coordinates were in decimal degrees?
Again thank you for your help!
Aleš
Your procedure looks like overkill when you can obtain them using the procedure I described. In any event, you should be working with projected data, rounding decimal degree coordinates produces different planar differences depending on your poleward location
Ales,
As far as I can see, your procedure is the same as mine. I would not however, round the ccordinates of the random point.
I subtracted the origin coordinates in order to get the cell coordinate (column row)in relation to the rasters origin. But you are correct that it might not be necessary in this case, as you don't need the (column, row), but only the center coordinate. That depend whether your raster cells aligns with cells with origin (0,0) in your coordinate system.
The method above will not work with decimal degrees, only projected coordinate systems. In this case I would do as Dan suggests.
Just remember that you will need a Desktop Advanced license in order to use the Near tool. As an alternative you could check out the spatial operators on geometries. I think there is a geometry.Near(anotherGeometry). I think i would also consider extracting the area just around the random point in the raster before Raster to Points in order to cut down the number of points.
Regards
Carsten
You could also try a Spatial Join rather than the Near tool if you don't have the appropriate license
Dan,
Good suggestion. I sometime get so absorbed in programming that I forget the standard tools 🙂
However, check out the spatial operators anyway. The are incredibly effective (and fun). I have had big performance improvements compared to standard tools.
Regards
Carsten
Carsten, I've decided to try your method drom the third post but I can't get the origins. I've been searching the help but no succes. Would you be so kind and direct me to the correct page.
Thank you both for your help, as I said for now I'm going to try with Carsten's method as I would like to do this in, I don't know how to say this, "calculational" way:) just for exercise to learn some thing about Python and programming. However I will definitly try the other methods too.
Regards
Aleš