Python 2.7 将点在光栅图层上的位置转换为点在NumPy阵列中的位置

Python 2.7 将点在光栅图层上的位置转换为点在NumPy阵列中的位置,python-2.7,numpy,scikit-image,Python 2.7,Numpy,Scikit Image,我第一次使用scikit image package的类来查找成本光栅上两点之间的最小成本路径,该成本光栅使用该工具从ArcGIS转换为NumPy阵列。但是,MCP类属性的一部分是开始和结束索引。 我不知道如何获取一组点(具有lat和long坐标的ArcGIS shapefile)并将其转换为从具有空间数据的光栅生成的NumPy阵列上的位置索引。我知道我可以将光栅单元OID指定给ArcGIS中的点,但我不确定如何将其转移到NumPy阵列上的索引。有人知道这是否可行吗?好吧,这是我能自己解决的唯一

我第一次使用scikit image package的类来查找成本光栅上两点之间的最小成本路径,该成本光栅使用该工具从ArcGIS转换为NumPy阵列。但是,MCP类属性的一部分是开始和结束索引。

我不知道如何获取一组点(具有lat和long坐标的ArcGIS shapefile)并将其转换为从具有空间数据的光栅生成的NumPy阵列上的位置索引。我知道我可以将光栅单元OID指定给ArcGIS中的点,但我不确定如何将其转移到NumPy阵列上的索引。有人知道这是否可行吗?

好吧,这是我能自己解决的唯一办法:

import arcpy
from arcpy.sa import *
import numpy as np
arcpy.CheckOutExtension("Spatial")
arcpy.env.extent = "MAXOF" # this ensures the extents of all my rasters will be the same, VERY IMPORTANT

extract = ExtractByMask("cost.img", "points.shp") # this step creates a raster that only has two value cells (where my points are) but the extent is that of my cost raster and the rest of the cells are NoData
extract = Con(extract>0,500,0) # changes the value of those two points to 500, a value much higher than the other cells, choose whatever value (positive or negative) that you know won't be in your cost raster
cost = arcpy.RasterToNumPyArray("cost.img")
calc = arcpy.RasterToNumPyArray(extract)
points = np.where(calc==500) #This produces the indices of all the entries in the array that are 500. However, the output is not in (12, 8), (17, 4) format, it looks like: (array([12, 17]), array([8, 4])) so must create the point indices with the next two lines
start = points[0][0], points[1][0]
end = points[0][1], points[1][1]

现在我有了mcp过程的开始和结束索引。如果起点和终点的方向性在您的分析中很重要,那么您必须调整它,以便能够在阵列中识别它们,但这不会太困难。

我对ArcGIS一无所知,但我可能会建议解决您的问题的方法。在数组的顶部和底部添加一行零,并指定这些零的起点和终点。这样,移动起点/终点的成本将为零。如果我正确理解您的建议,则这仅适用于起点和终点位于阵列边缘的情况。不幸的是,它们不是,因为这将是一个很好的解决方案。看起来光栅索引和数组索引密切相关?