Transforming Raster Values

Rescaling raster values to range 0-255

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Rescales a raster so all values are integers between 0-255
version(1.0);

# Change the directory below to correspond to your file location.
baseDir = "c:/Program Files/LandSerf/data/landscriptExamples/";

# Open the raster 
dem = open(baseDir&"dem.srf");

# Create rescaled raster.
rescaledDEM = new(dem);
minZ = info(dem,"min");
maxZ = info(dem,"max");
range = maxZ-minZ;
rescaledDEM = round(255*(dem-minZ)/range);

# Give rescaled raster a grey scale and save it.
colouredit(rescaledDEM,"rules","0 0, 255 255");
save(rescaledDEM,baseDir&"rescaledDEM.srf");

Growing raster cells

This example uses a series of nested focal operations to grow the boundaries of non-null areas in a raster by one pixel. The newly grown cells are saved in their own raster (coastline).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Creates some fractal islands, then grows the coastlines by 1 pixel. 
version(1.0); 
 
# Change the directory below to correspond to your file location. 
baseDir = "c:/Program Files/LandSerf/data/landscriptExamples/"; 
 
# Create empty raster. 
fracSurf = newraster(1000,1000,10,10,250,250,"Fractal"); 
 
# Fill raster with fractal surface. 
fractal(fracSurf,"2.01"); 
islands = new(fracSurf); 
 
# Use data range to get 'sea level'; 
midPoint = info(fracSurf,"median"); 
 
# Flood all values below 'sea level' 
fracSurf = ifelse(fracSurf < midPoint, null(), fracSurf); 
 
# Grow coastline 
islands = ifelse(fracSurf != null(), fracSurf, 
             ifelse(fracSurf[0,-1] != null(), fracSurf[0,-1], 
                ifelse(fracSurf[0,1] != null(),  fracSurf[0,1], 
                   ifelse(fracSurf[-1,0] != null(), fracSurf[-1,0], 
                      ifelse(fracSurf[1,0] != null(), fracSurf[1,0],null()))))); 
 
# Find coastline by taking difference between original and grown islands. 
coastline = new(fracSurf); 
coastline = islands-fracSurf; 
 
# Save original and expanded islands. 
colouredit(islands,"land1"); 
edit(islands,"title","Fractal islands"); 
save(islands,baseDir & "islands.srf"); 
 
colouredit(coastline,"rules","0 255, 1 180 180 0"); 
edit(coastline,"title","Fractal coastline"); 
save(coastline,baseDir & "coastline.srf");