I have a regularly spaced model grid, 5000 by 5000 feet (517,300 cells), which I need to convert to a numpy array. I am investigation a couple of different methods, using arcpy, by which I can convert the grid to a numpy array using the values in a particular field. They are as follows:
import osimport arcpyimport datetimeimport numpy as nparcpy.CheckOutExtension("Spatial")arcpy.env.workspace = r'..\gis\geom.gdb'arcpy.env.overwriteOutput = Truegrid = r'..\gis\grid_5000ft_01.gdb\grid'out_table = r'in_memory\{}'.format('TOP_sas')arcpy.sa.ZonalStatisticsAsTable(grid, 'OID', 'geodas_fasft', out_table, 'DATA', 'ALL')grid_lyr = r'in_memory\grid_lyr'table_vwe = r'in_memory\table_vwe'arcpy.MakeFeatureLayer_management(grid, grid_lyr)arcpy.MakeTableView_management(out_table, table_vwe)arcpy.AddJoin_management(grid_lyr, 'OID', table_vwe, 'OID_', 'KEEP_ALL')print [field.name for field in arcpy.ListFields(grid_lyr)]start = datetime.datetime.now()arcpy.PolygonToRaster_conversion(grid_lyr, '{}.MEAN'.format('TOP_sas'), r'in_memory\ras', '#', '#', 5000.)a = arcpy.RasterToNumPyArray(r'in_memory\ras')print (datetime.datetime.now() - start) / 60.b = np.zeros((739, 700), dtype=np.float)start = datetime.datetime.now()with arcpy.da.SearchCursor(grid_lyr, ['grid.row', 'grid.col', '{}.MEAN'.format('TOP_sas')]) as cur: for row in cur: (r, c, val) = row a[r-1, c-1] = valprint (datetime.datetime.now() - start) / 60.a.savetxt('{}.ref'.format('TOP_sas_a'))b.savetxt('{}.ref'.format('TOP_sas_b'))
أكثر...
- Polygon to raster conversion followed by a raster to numpy array conversion, and
- Iteration over the rows of the grid, pulling row/col indices from each row and assigning values to an empty array.
import osimport arcpyimport datetimeimport numpy as nparcpy.CheckOutExtension("Spatial")arcpy.env.workspace = r'..\gis\geom.gdb'arcpy.env.overwriteOutput = Truegrid = r'..\gis\grid_5000ft_01.gdb\grid'out_table = r'in_memory\{}'.format('TOP_sas')arcpy.sa.ZonalStatisticsAsTable(grid, 'OID', 'geodas_fasft', out_table, 'DATA', 'ALL')grid_lyr = r'in_memory\grid_lyr'table_vwe = r'in_memory\table_vwe'arcpy.MakeFeatureLayer_management(grid, grid_lyr)arcpy.MakeTableView_management(out_table, table_vwe)arcpy.AddJoin_management(grid_lyr, 'OID', table_vwe, 'OID_', 'KEEP_ALL')print [field.name for field in arcpy.ListFields(grid_lyr)]start = datetime.datetime.now()arcpy.PolygonToRaster_conversion(grid_lyr, '{}.MEAN'.format('TOP_sas'), r'in_memory\ras', '#', '#', 5000.)a = arcpy.RasterToNumPyArray(r'in_memory\ras')print (datetime.datetime.now() - start) / 60.b = np.zeros((739, 700), dtype=np.float)start = datetime.datetime.now()with arcpy.da.SearchCursor(grid_lyr, ['grid.row', 'grid.col', '{}.MEAN'.format('TOP_sas')]) as cur: for row in cur: (r, c, val) = row a[r-1, c-1] = valprint (datetime.datetime.now() - start) / 60.a.savetxt('{}.ref'.format('TOP_sas_a'))b.savetxt('{}.ref'.format('TOP_sas_b'))
أكثر...