1+
2+ #####
3+ # Step 3 - Rasters and numpy
4+ #####
5+
6+ # Numpy is a exceptionally powerful package for Python that gives incredible scientific computing functionality.
7+ # Some basic functionality has been built into arcpy that allows us to interact with it in a fairly seamless manner.
8+
9+ # Review the code below, this is a simple conversion that we are doing, converting Meters to Feet in an elevation
10+ # raster. Of course you could do this using Raster Calculator, but what if you want to do more complex statistics,
11+ # model simulations and so on?
12+
13+ # Be aware that the size of the raster is crucial as the resulting numpy array will reside in your computer
14+ # memory, so no 10gb files!
15+
16+ import arcpy
17+
18+ arcpy .env .overwriteOutput = True
19+ arcpy .env .workspace = r"Z:\Andy's Documents\Teaching and students\URI\NRS - GIS Python Course\Github\Course_ArcGIS_Python\Classes\12_Rasters\Step_3_Data"
20+ inRas = arcpy .Raster ("etopo10" )
21+ lowerLeft = arcpy .Point (inRas .extent .XMin ,inRas .extent .YMin )
22+ cellSize = inRas .meanCellWidth
23+
24+ arr = arcpy .RasterToNumPyArray (inRas ,nodata_to_value = 0 )
25+
26+ # Print the resulting array
27+ print arr .shape
28+
29+ # Now lets convert m to feet
30+ arrFeet = arr * 3.28084
31+
32+ newRaster = arcpy .NumPyArrayToRaster (arrFeet , lowerLeft , cellSize , value_to_nodata = 0 )
33+ newRaster .save ("etopo10_ft.tif" )
34+
35+
36+ # Task: Using the etopo10 dataset and a NumPyArray, extract only land values, replacing them with null data. Hint:
37+ # arr[arr < 0] = -9999
38+
39+ import arcpy
40+
41+ arcpy .env .workspace = r"Z:\Andy's Documents\Teaching and students\URI\NRS - GIS Python Course\Github\Course_ArcGIS_Python\Classes\12_Rasters\Step_3_Data"
42+ inRas = arcpy .Raster ("etopo10" )
43+ lowerLeft = arcpy .Point (inRas .extent .XMin ,inRas .extent .YMin )
44+ cellSize = inRas .meanCellWidth
45+
46+ arr = arcpy .RasterToNumPyArray (inRas ,nodata_to_value = 0 )
47+
48+ # Print the resulting array
49+ print arr .shape
50+
51+ # Now lets convert m to feet
52+ arr [arr < 0 ] = - 9999
53+
54+ newRaster = arcpy .NumPyArrayToRaster (arr , lowerLeft , cellSize , value_to_nodata = - 9999 )
55+ newRaster .save ("etopo10_land.tif" )
0 commit comments