| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import argparse |
| 16 |
import os |
| 17 |
import math |
| 18 |
import jiplib as jl |
| 19 |
|
| 20 |
parser=argparse.ArgumentParser() |
| 21 |
parser.add_argument("-input","--input",help="Path of the input raster dataset",dest="input",required=True,type=str) |
| 22 |
parser.add_argument("-reference","--reference",help="Path of the reference raster dataset",dest="reference",required=True,type=str) |
| 23 |
parser.add_argument("-model","--model",help="Path of the model output filename used for training",dest="model",required=False,type=str) |
| 24 |
parser.add_argument("-training","--training",help="Path of the training vector dataset with raster information",dest="training",required=False,type=str) |
| 25 |
parser.add_argument("-output","--output",help="Path of the classification output raster dataset",dest="output",required=False,type=str) |
| 26 |
parser.add_argument("-sampleSize","--sampleSize",help="Sample size used for training svm or ann",dest="sampleSize",required=False,type=int,default=100) |
| 27 |
parser.add_argument("-classifier","--classifier",help="classifier (sml, svm, ann)",dest="classifier",required=True,type=str,default="sml") |
| 28 |
args = parser.parse_args() |
| 29 |
|
| 30 |
|
| 31 |
if True: |
| 32 |
jim=jl.createJim(args.input) |
| 33 |
|
| 34 |
classDict={} |
| 35 |
classDict['urban']=2 |
| 36 |
classDict['agriculture']=12 |
| 37 |
classDict['forest']=25 |
| 38 |
classDict['water']=41 |
| 39 |
classDict['rest']=50 |
| 40 |
classFrom=list(range(0,50)) |
| 41 |
classTo=[50]*50 |
| 42 |
for i in range(0,50): |
| 43 |
if i>=1 and i<10: |
| 44 |
classTo[i]=classDict['urban'] |
| 45 |
elif i>=11 and i<22: |
| 46 |
classTo[i]=classDict['agriculture'] |
| 47 |
elif i>=23 and i<25: |
| 48 |
classTo[i]=classDict['forest'] |
| 49 |
elif i>=40 and i<45: |
| 50 |
classTo[i]=classDict['water'] |
| 51 |
else: |
| 52 |
classTo[i]=classDict['rest'] |
| 53 |
|
| 54 |
print("jim.getProjection() {}".format(jim.getProjection())) |
| 55 |
jim_ref=jl.createJim(filename=args.reference,dx=jim.getDeltaX(),dy=jim.getDeltaY(),ulx=jim.getUlx(),uly=jim.getUly(),lrx=jim.getLrx(),lry=jim.getLry(),t_srs=jim.getProjection()) |
| 56 |
jim_ref=jim_ref.reclass({'class':classFrom,'reclass':classTo}) |
| 57 |
|
| 58 |
if args.classifier == "sml": |
| 59 |
print("SML classifier") |
| 60 |
reflist=jl.JimList([jim_ref]) |
| 61 |
jim.d_band2plane(); |
| 62 |
|
| 63 |
|
| 64 |
sml=jim.classifySML(reflist,{'class':sorted(classDict.values()),'verbose':1}) |
| 65 |
sml.write({'filename':'/tmp/sml_classes.tif'}) |
| 66 |
|
| 67 |
sml_class=sml.statProfile({'function':'maxindex'}).reclass({'class':list(range(0,sml.nrOfBand())),'reclass':sorted(classDict.values())}) |
| 68 |
if args.output: |
| 69 |
sml_class.write({'filename':args.output}) |
| 70 |
sml_class.close() |
| 71 |
else: |
| 72 |
srcnodata=[0] |
| 73 |
dict={'srcnodata':srcnodata} |
| 74 |
if args.training: |
| 75 |
trainingfn=args.training |
| 76 |
dict.update({'oformat':'ESRI Shapefile'}) |
| 77 |
else: |
| 78 |
dict.update({'oformat':'Memory'}) |
| 79 |
trainingfn='training.shp' |
| 80 |
dict.update({'output':trainingfn}) |
| 81 |
dict.update({'class':sorted(classDict.values())}) |
| 82 |
sampleSize=-args.sampleSize |
| 83 |
dict.update({'threshold':sampleSize}) |
| 84 |
dict.update({'bandname':['B02','B03','B04','B08']}) |
| 85 |
|
| 86 |
|
| 87 |
sample=jim.extractImg(jim_ref,dict) |
| 88 |
if args.classifier == "svm": |
| 89 |
|
| 90 |
|
| 91 |
sample.train({'method':args.classifier,'label':'label','bandname':['B02','B03','B04','B08'],'model':args.model}) |
| 92 |
|
| 93 |
|
| 94 |
sample.close() |
| 95 |
|
| 96 |
svm_class=jim.classify({'method':'svm','model':args.model}) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if args.output: |
| 102 |
svm_class.write({'filename':args.output}) |
| 103 |
svm_class.close() |
| 104 |
elif args.classifier == "ann": |
| 105 |
|
| 106 |
sample.train({'method':args.classifier,'label':'label','model':args.model}) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
sample.close() |
| 112 |
|
| 113 |
ann_class=jim.classify({'method':'ann','model':args.model}) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if args.output: |
| 119 |
ann_class.write({'filename':args.output}) |
| 120 |
ann_class.close() |
| 121 |
else: |
| 122 |
print("Error: classifier",args.classifier,"not implemented") |
| 123 |
throw() |
| 124 |
|
| 125 |
jim.close() |
| 126 |
print("Success: classify") |
| 127 |
try: |
| 128 |
print("ok") |
| 129 |
except: |
| 130 |
print("Failed: classify") |
| 131 |
jim.close() |