-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdal-polygonize.py
executable file
·55 lines (39 loc) · 1.26 KB
/
gdal-polygonize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from gc import callbacks
import os
from osgeo import gdal, ogr,osr
import sys
from alive_progress import alive_bar
gdal.UseExceptions() # This Allows GDAL to Throw python exceptions
#
# Get Raster DataSources
#
# Directory where file is
tif = str(os.path.expanduser(
'~') + "/Área de Trabalho/MapBiomas/1985/brasil_collection-6_lclu_coverage_brasil_coverage_1985.tif")
# open tif and info
tifFile = gdal.Open(tif)
print(tifFile.GetMetadata())
# get raster band
try:
band_num = 1
srcband = tifFile.GetRasterBand(band_num)
except RuntimeError as err:
print("Band ( %i ) not found " % band_num)
print(err)
sys.exit("Exit")
#
# Create Output DataSource
#
spatial_ref=osr.SpatialReference()
spatial_ref.SetFromUserInput('EPSG:4326')
tif_Layername = 'brasil_collection-6_lclu_coverage_brasil_coverage_1985'
DriveSearch = ogr.GetDriverByName('GPKG')
NewNameFile = DriveSearch.CreateDataSource(tif_Layername + '.gpkg')
tif_layer = NewNameFile.CreateLayer(tif_Layername, srs=None )
field = ogr.FieldDefn('Color_Gradient', ogr.OFTInteger)
tif_layer.CreateField(field)
tif_field=tif_layer.GetLayerDefn().GetFieldIndex("class")
with alive_bar as progressbar:
gdal.Polygonize(srcband, None, tif_layer, tif_field , [], callback=None)
#close .tif
tifFile = None