def subset_s2(path, file, aoi):
# Read Data________________________________________________________________
print("SUBSET: Read Product...")
sentinel = ProductIO.readProduct(path+file)
print("SUBSET: Done reading!")
# Get Band Names and print info
name = sentinel.getName()
print("SUBSET: Image ID: %s" % name)
band_names = sentinel.getBandNames()
print("SUBSET: Bands: %s" % (list(band_names)))
# Preprocessing ___________________________________________________________
# Resampling
parameters = HashMap()
parameters.put('targetResolution', 10)
print("SUBSET: resample target resolution: 10m")
product_resample = snappy.GPF.createProduct('Resample', parameters, sentinel)
# Geojson to wkt
with open(aoi) as f:
gjson = json.load(f)
for feature in gjson['features']:
polygon = (feature['geometry'])
str_poly = json.dumps(polygon)
gjson_poly = geojson.loads(str_poly)
poly_shp = shape(gjson_poly)
wkt = poly_shp.wkt
# Subset
geom = WKTReader().read(wkt)
op = SubsetOp()
op.setSourceProduct(product_resample)
op.setGeoRegion(geom)
product_sub = op.getTargetProduct()
# Write Data_______________________________________________________
print("SUBSET: Writing subset.")
subset = path + name + '_subset_'
ProductIO.writeProduct(product_sub, subset, "BEAM-DIMAP")
print("SUBSET: Done and saved in %s" % path)
评论列表
文章目录