| 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=True,type=str) |
| 22 |
parser.add_argument("-extent","--extent",help="Path of the vector dataset",dest="extent",required=False,type=str) |
| 23 |
parser.add_argument("-output","--output",help="Path of the output vector dataset",dest="output",required=False,type=str) |
| 24 |
parser.add_argument("-nodata","--nodata",help="nodata value to put in cut region",dest="nodata",required=False,default=0,type=int) |
| 25 |
parser.add_argument("-cut_to_cutline","--cut_to_cutline",help="Crop the extent of the target dataset to the extent of the cutline, setting the outside area to nodata",dest="cut_to_cutline",required=False,type=bool,default=True) |
| 26 |
parser.add_argument("-cut_in_cutline","--cut_in_cutline",help="Crop the extent of the target dataset to the extent of the cutline, setting the inner area to nodata",dest="cut_in_cutline",required=False,type=bool,default=False) |
| 27 |
args = parser.parse_args() |
| 28 |
|
| 29 |
try: |
| 30 |
print("createJim") |
| 31 |
jim0=jl.createJim(args.input) |
| 32 |
rules=['centroid','min','max','mean','stdev'] |
| 33 |
print("createVector") |
| 34 |
jlv=jl.createVector(args.extent) |
| 35 |
bb=jlv.getBoundingBox() |
| 36 |
if os.path.basename(args.extent)=='nuts_italy.sqlite': |
| 37 |
if args.cut_to_cutline: |
| 38 |
print("cut out") |
| 39 |
print("milano") |
| 40 |
jim_milano=jim0.cropOgr(jlv,{'ln':'milano','crop_to_cutline':True,'nodata':args.nodata,'align':True}) |
| 41 |
bb=jim_milano.getBoundingBox() |
| 42 |
jim1=jim0.crop({'ulx':bb[0],'uly':bb[1],'lrx':bb[2],'lry':bb[3],'verbose':2}) |
| 43 |
print(jim1.getBoundingBox()) |
| 44 |
print(jim_milano.getBoundingBox()) |
| 45 |
if jim1.getBoundingBox() != jim_milano.getBoundingBox(): |
| 46 |
raise ValueError("Error: Bounding box milano not equal with crop and cropOgr") |
| 47 |
print("lodi") |
| 48 |
jim_lodi=jim0.cropOgr(jlv,{'ln':'lodi','crop_to_cutline':True,'nodata':args.nodata,'align':True}) |
| 49 |
elif args.cut_in_cutline: |
| 50 |
print("cut within") |
| 51 |
|
| 52 |
jim_milano=jim0.cropOgr(jlv,{'ln':'milano','crop_in_cutline':True,'nodata':args.nodata,'align':True}) |
| 53 |
jim_lodi=jim0.cropOgr(jlv,{'ln':'lodi','crop_in_cutline':True,'nodata':args.nodata,'align':True}) |
| 54 |
milanofn=os.path.join(os.path.dirname(args.output),'milano.tif') |
| 55 |
print("write") |
| 56 |
lodifn=os.path.join(os.path.dirname(args.output),'lodi.tif') |
| 57 |
jim_milano.write({'filename':milanofn}) |
| 58 |
jim_milano.close() |
| 59 |
jim_lodi.write({'filename':lodifn}) |
| 60 |
jim_lodi.close() |
| 61 |
else: |
| 62 |
jim0.cropOgr(jlv,{'align':True}).write({'filename':args.output}).close() |
| 63 |
jim0.close() |
| 64 |
jlv.close() |
| 65 |
print("Success: crop") |
| 66 |
except: |
| 67 |
print("Failed: crop") |