Oft-sample-within-polys.bash

From Open Foris Wiki

Jump to: navigation, search

This script samples pixel values from an image within areas determined by training data polygons (shapefile)

Usage: oft-sample-within-polys.bash <image> <shapefile_basename> <shapefile_class_fieldname> <size_of_sample> [-sample_only]

Examples: oft-sample-within-polys.bash p170r64.tif p170r64_poly ID 1000


oft-sample-within-polys.bash p170r64.tif p170r64_poly ID 2000 -sample_only

Output is named sample_shapefile_basename.txt

Specifications:

  • Sample size (nbr of pixels) is given by the user
  • The sample is distributed within classes in relation to class frequencies
  • Output is a text file to be used e.g. in k-nn
  • A histogram is also printed out, sample size per class is shown in last column
  • It is possible to pick a new sample by running the script with option -sample_only (do not delete greyvals_shapefile_basename.txt if you are going to re-run)
  • At this point the image and the shapefile need to be in the same projection


An excerpt of output text file produced from a SR Landsat image:

(order is pixel_id x y class band1 band2 band3 band4 band5 band6 band7)


1979.00 452280.00 -552120.00 21.00 449.00 826.00 822.00 2739.00 2455.00 2540.00 1483.00

802.00 437520.00 -545790.00 21.00 409.00 669.00 727.00 2621.00 2349.00 2497.00 1444.00

1718.00 444720.00 -547740.00 21.00 412.00 748.00 823.00 2501.00 2427.00 2455.00 1523.00

999.00 437490.00 -546060.00 21.00 487.00 825.00 948.00 2543.00 2588.00 2540.00 1681.00

937.00 437610.00 -545970.00 21.00 470.00 826.00 948.00 2582.00 2507.00 2540.00 1680.00

379.00 447660.00 -543450.00 21.00 393.00 710.00 823.00 2187.00 2348.00 2455.00 1443.00


Exercise:

cd /home/...


  • Now run the script in the command line within input-raster landsat_t1.tif and input-shapefile landuse.shp; name refers to the shapefile ID. If you look at the attribute table of landuse.shp you see, that you could also use the column id. Here we chose name to make it more transparent. 100 is the sample size chosen for this exercise.

Note: In the command line the extension .shp of the shapefile is not included!

oft-sample-within-polys.bash landsat_t1.tif landuse name 100

  • Output are three text files:
    • greyvalues - greyvals_landuse.txt
    • histogram - histogramlanduse.txt
    • sample output - sample_landuse.txt

Here you can see an excerpt of sample_landuse.txt

Order is pixel_id x y class band1 band2 band3 band4 band5 band6 band7:

10557.00 772650.00 -2404770.00 5.00 53.00 26.00 28.00 54.00 81.00 131.00 39.00
94788.00 773490.00 -2431680.00 1.00 51.00 24.00 25.00 45.00 65.00 127.00 33.00
201536.00 774750.00 -2439390.00 1.00 54.00 25.00 27.00 50.00 71.00 130.00 35.00
88531.00 771450.00 -2431110.00 1.00 47.00 21.00 18.00 37.00 48.00 126.00 21.00
123374.00 774150.00 -2433990.00 1.00 54.00 24.00 30.00 35.00 75.00 132.00 42.00



Back to Open Foris Toolkit Main Page

Back to Tools & Exercises



Personal tools