-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# file = "clusterIds_temporal-(perc_50%,1980-2015).tif" | ||
""" | ||
$(TYPEDSIGNATURES) | ||
Creates vector polygons for all connected regions of pixels in the raster | ||
sharing a common pixel value. | ||
# Usage | ||
$(METHODLIST) | ||
# Arguments | ||
- `options`: other parameters to | ||
- `diag` (default `false`): `false` (4 connected) or `true` (8 connected). | ||
- `bands` (default nothing): , bands to be proceeded (e.g, 1:n). If `nothing`, | ||
it is all bands. | ||
- `drive`: "ESRI Shapefile", "OpenFileGDB", "GPKG" | ||
- `mask`: `img_mask = mask ? band : C_NULL`. All pixels in the mask band with a | ||
value other than zero will be considered suitable for collection as polygons. | ||
# References | ||
- https://gdal.org/programs/gdal_polygonize.html | ||
- https://github.com/JuliaGeo/GDAL.jl/blob/master/test/drivers.jl | ||
- https://gdal.org/api/gdal_alg.html#_CPPv414GDALPolygonize15GDALRasterBandH15GDALRasterBandH9OGRLayerHiPPc16GDALProgressFuncPv | ||
""" | ||
function gdal_polygonize(raster_file, fout="out.gdb"; | ||
options=String[], diag=false, | ||
bands=nothing, | ||
fieldname="grid", nodata=NaN, mask=false, verbose=false, overwrite=true) | ||
|
||
if overwrite && (isfile(fout) || isdir(fout)) | ||
rm(fout; recursive=true) | ||
end | ||
if diag | ||
options = [options..., "8CONNECTED=8"] | ||
end | ||
|
||
ds_raster = GDAL.gdalopen(raster_file, GDAL.GA_ReadOnly) | ||
n = nband(raster_file) | ||
bands === nothing && (bands = 1:n) | ||
|
||
# drive = GDAL.gdalgetdriverbyname("ESRI Shapefile") | ||
driver = find_shortname(fout) | ||
_drive = GDAL.gdalgetdriverbyname(driver) | ||
ds_shp = GDAL.gdalcreate(_drive, fout, 0, 0, 0, GDAL.GDT_Unknown, C_NULL) | ||
|
||
# REF = "WGS_1984" | ||
REF = GDAL.osrnewspatialreference(C_NULL) | ||
GDAL.osrimportfromepsg(REF, 4326) | ||
|
||
for i = bands | ||
verbose && println(i) | ||
layername = "b_$i" | ||
band = GDAL.gdalgetrasterband(ds_raster, i) | ||
|
||
if !isnan(nodata) | ||
GDAL.gdalsetrasternodatavalue(band, nodata) | ||
end | ||
# dst_ds = GDAL.ogr_dr_createdatasource(drv, dst_filename, C_NULL) | ||
layer = GDAL.gdaldatasetcreatelayer(ds_shp, layername, REF, GDAL.wkbPolygon, C_NULL) | ||
|
||
fielddefn = GDAL.ogr_fld_create(fieldname, GDAL.OFTInteger) | ||
field = GDAL.ogr_l_createfield(layer, fielddefn, GDAL.TRUE) | ||
|
||
img_mask = mask ? band : C_NULL | ||
|
||
GDAL.gdalpolygonize( | ||
band, | ||
img_mask, | ||
layer, field, | ||
options, C_NULL, C_NULL) | ||
end | ||
|
||
GDAL.gdalclose(ds_shp) | ||
GDAL.gdalclose(ds_raster) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# using ArchGDAL | ||
# GDAL.gdalgetgeotransform(ds) | ||
function getgeotransform!( | ||
dataset::Ptr, | ||
transform::Vector{Cdouble}, | ||
)::Vector{Cdouble} | ||
@assert length(transform) == 6 | ||
result = GDAL.gdalgetgeotransform(dataset, pointer(transform)) | ||
if result != GDAL.CE_None | ||
# The default geotransform. | ||
transform .= (0.0, 1.0, 0.0, 0.0, 0.0, 1.0) | ||
end | ||
return transform | ||
end | ||
|
||
getgeotransform(dataset::Ptr)::Vector{Cdouble} = | ||
getgeotransform!(dataset, Vector{Cdouble}(undef, 6)) | ||
|
||
width(band) = GDAL.gdalgetrasterbandxsize(band) # error at here | ||
height(band) = GDAL.gdalgetrasterbandysize(band) | ||
|
||
function gdalinfo(file::AbstractString) | ||
# nband = nband(file) | ||
ds = gdal_open(file) | ||
gt = getgeotransform(ds) | ||
band = GDAL.gdalgetrasterband(ds, 1) | ||
w, h = width(band), height(band) | ||
gdal_close(ds) | ||
|
||
dx, dy = gt[2], gt[end] | ||
x0 = gt[1] #+ dx/2 | ||
y0 = gt[4] #- dy/2 | ||
x1 = x0 + w * dx | ||
y1 = y0 + h * dy | ||
|
||
lon = x0+dx/2:dx:x1 | ||
lat = y0+dy/2:dy:y1 | ||
b = st_bbox(lon, lat) # b = bbox(x0, y0, x1, y1) | ||
|
||
Dict( | ||
"file" => basename(file), | ||
"bbox" => b, | ||
"cellsize" => [dx, dy], | ||
"dims" => [lon, lat], | ||
"size" => Int64.([w, h, nband(file)]) | ||
) # convert to tuple | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters