| 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 raster dataset",dest="input",required=False,type=str) |
| 22 |
parser.add_argument("-ulx","--ulx",help="left margin of bounding box",dest="ulx",required=False,type=float,default=16.1) |
| 23 |
parser.add_argument("-lrx","--lrx",help="lrx margin of bounding box",dest="lrx",required=False,type=float,default=16.6) |
| 24 |
parser.add_argument("-uly","--uly",help="uly margin of bounding box",dest="uly",required=False,type=float,default=48.6) |
| 25 |
parser.add_argument("-lry","--lry",help="lry margin of bounding box",dest="lry",required=False,type=float,default=47.2) |
| 26 |
parser.add_argument("-dx","--dx",help="Resolution in x",dest="dx",required=False,type=float) |
| 27 |
parser.add_argument("-dy","--dy",help="Resolution in y",dest="dy",required=False,type=float) |
| 28 |
parser.add_argument("-t_srs","--t_srs",help="Target spatial reference system of bounding box",dest="t_srs",required=False,type=str,default='4326') |
| 29 |
parser.add_argument("-output","--output",help="Path of the output vector dataset",dest="output",required=True,type=str) |
| 30 |
parser.add_argument("-random","--random",help="Number of random pixels to select",dest="random",required=False,type=int,default=10) |
| 31 |
args = parser.parse_args() |
| 32 |
|
| 33 |
|
| 34 |
if True: |
| 35 |
rules=['median'] |
| 36 |
if args.output: |
| 37 |
output=args.output |
| 38 |
oformat='SQLite' |
| 39 |
else: |
| 40 |
output='mem01' |
| 41 |
oformat='Memory' |
| 42 |
|
| 43 |
openDict={'t_srs':'epsg:'+args.t_srs} |
| 44 |
openDict.update({'ulx':args.ulx,'uly':args.uly,'lrx':args.lrx,'lry':args.lry}) |
| 45 |
if(args.dx): |
| 46 |
openDict.update({'dx':args.dx}) |
| 47 |
if(args.dy): |
| 48 |
openDict.update({'dy':args.dy}) |
| 49 |
if args.input: |
| 50 |
refpath=args.input |
| 51 |
else: |
| 52 |
refpath='/eos/jeodpp/data/base/Landcover/EUROPE/CorineLandCover/CLC2012/VER18-5/Data/GeoTIFF/250m/g250_clc12_V18_5.tif' |
| 53 |
openDict.update({'s_srs':'epsg:3035'}) |
| 54 |
|
| 55 |
|
| 56 |
jim_ref=jl.createJim(refpath,**openDict) |
| 57 |
|
| 58 |
|
| 59 |
classDict={} |
| 60 |
classDict['urban']=2 |
| 61 |
classDict['agriculture']=12 |
| 62 |
classDict['forest']=25 |
| 63 |
classDict['water']=41 |
| 64 |
classDict['rest']=50 |
| 65 |
sorted(classDict.values()) |
| 66 |
|
| 67 |
print(classDict) |
| 68 |
|
| 69 |
classFrom=list(range(0,50)) |
| 70 |
classTo=[50]*50 |
| 71 |
print("classFrom: {}".format(classFrom)) |
| 72 |
print("classTo: {}".format(classTo)) |
| 73 |
for i in range(0,50): |
| 74 |
if i>=1 and i<10: |
| 75 |
classTo[i]=classDict['urban'] |
| 76 |
elif i>=11 and i<22: |
| 77 |
classTo[i]=classDict['agriculture'] |
| 78 |
elif i>=23 and i<25: |
| 79 |
classTo[i]=classDict['forest'] |
| 80 |
elif i>=40 and i<45: |
| 81 |
classTo[i]=classDict['water'] |
| 82 |
else: |
| 83 |
classTo[i]=classDict['rest'] |
| 84 |
|
| 85 |
jim_ref=jim_ref.reclass({'class':classFrom,'reclass':classTo}) |
| 86 |
jim_ref.write({'filename':'/vsimem/reference.tif'}) |
| 87 |
|
| 88 |
labels=classDict.values() |
| 89 |
print(labels) |
| 90 |
for classname in classDict: |
| 91 |
print("class: ",classname) |
| 92 |
label=classDict[classname] |
| 93 |
srcnodata=list(classDict.values()) |
| 94 |
srcnodata.remove(label) |
| 95 |
srcnodata.append(255) |
| 96 |
print(srcnodata) |
| 97 |
print("extract") |
| 98 |
v=jim_ref.extractSample({'ln':classname,'random':args.random,'rule':rules,'output':output,'oformat':oformat,'bandname':['label'],'mask':'/vsimem/reference.tif','msknodata':srcnodata,'buffer':1,'co':'OVERWRITE=YES'}) |
| 99 |
print("write") |
| 100 |
v.write() |
| 101 |
print("close") |
| 102 |
v.close() |
| 103 |
jim_ref.close() |
| 104 |
print("Success: extractogr") |
| 105 |
try: |
| 106 |
print("debug0") |
| 107 |
except: |
| 108 |
print("Failed: extractogr") |