Basic operations with NUMPY


#1

import arcpy
import numpy

arr = arcpy.RasterToNumPyArray ("C:raster", nodata_to_value=0)

"raster matrix"
print arr.shape

"raster dimensions"
print arr.ndim

zero = zeros((5, 3))
print arr [10, 20]

from numpy import*
arr = zeros((3, 4))
print arr

COMPAS
import arcpy
from numpy import *

array = arcpy.RasterToNumPyArray ("C:/Users/Acer/Desktop/skuska/idw_ex.tif", nodata_to_value=0)
vysledok = zeros(array.shape)
rozmery = array.shape
max_riadky = rozmery [0]
max_stlpce = rozmery [1]
print max_riadky
print max_stlpce

riadky_poz = 20
stlpce_poz = 20

originalna_vyska = array [riadky_poz, stlpce_poz]

test = 0

i = riadky_poz
j = stlpce_poz

while test == 0:
if 1 < i < max_riadky:
if array [i, j] <= originalna_vyska:
vysledok [i, j] = 1
i = i + 1
else:
test = 1
else:
test = 1

test = 0

i = riadky_poz
j = stlpce_poz

while test == 0:
if 1 < i < max_riadky:
if array [i, j] <= originalna_vyska:
vysledok [i, j] = 1
i = i - 1
else:
test = 1
else:
test = 1

test = 0

i = riadky_poz
j = stlpce_poz

while test == 0:
if 1 < j < max_stlpce:
if array [i, j] <= originalna_vyska:
vysledok [i, j] = 1
j = j - 1
else:
test = 1
else:
test = 1

test = 0

i = riadky_poz
j = stlpce_poz

while test == 0:
if 1 < j < max_stlpce:
if array [i, j] <= originalna_vyska:
vysledok [i, j] = 1
j = j + 1
else:
test = 1
else:
test = 1

raster = arcpy.NumPyArrayToRaster (vysledok)
raster.save ("C:/Users/Acer/Desktop/skuska/vysledok.tif")


Arcpy_directional derivations
#2

For reference: https://docs.scipy.org/doc/numpy-dev/user/quickstart.html


#3

This is code for implementation of directional derivation (x) to ArcGis Toolbox.

import numpy as np
import arcpy

folder = arcpy.GetParameterAsText(0)
raster = arcpy.GetParameterAsText(1)
arr = arcpy.RasterToNumPyArray(raster)

x_x, y_y = arr.shape
result_dx = np.zeros(shape=(x_x,y_y)

for column in range (1,(y_y-1)):
for row in range (0, x_x):
result_dx[row,column] = (arr[row,column+1]-arr[row,column-1])/20

Code for directional derivations:
import numpy as np
import arcpy

arr = arcpy.RasterToNumPyArray("C:/dem.tif")

x_x, y_y = arr.shape

result = np.zeros(shape = x_x,y_y))

result_dx = np.zeros(shape=(x_x,y_y))
result_dy = np.zeros(shape=(x_x,y_y))

for column in range (1,y_y-1)):
for row in range (0, x_x):
result_dx[row,column] = (arr[rrow,column+1]-arr[row,column-1])/20

for column in range (0,(y_y)):
for row in range (1,x_x-1):
result_dy[row,scolumn] = (arr[row-1,column]-arr[row+1,column])/20

myRaster = arcpy.NumPyArrayToRaster(result_dx,x_cell_size=10,y_cell_size=10)
myRaster.save("C:dx.tif")

myRaster = arcpy.NumPyArrayToRaster(result_dy,x_cell_size=10,y_cell_size=10)
myRaster.save("C:/dy.tif")

`


#4

import arcpy
from numpy import *

array = arcpy.RasterToNumPyArray ("C:dem.tif", nodata_to_value=0)

result = zeros (array.shape)

shape = array.shape
rows = shape [0]
columns = shape [1]

for i in range (1, rows-1):
for j in range (1,columns-1):
max = 0
min = 0
diff = 0
if array [i+1, j+1] > max:
max = array [i+1, j+1]
if array [i+1, j+1] < min:
min = array [i+1, j+1]
diff = max - min
result [i, j] = diff
raster = arcpy.NumPyArrayToRaster (result)
raster.save ("C:.tif")


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.