-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmosaic-demo.py
125 lines (116 loc) · 4.02 KB
/
mosaic-demo.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
.ExifTool_config has to be in place for exiftool to write the custom tags.
"""
import os,glob
from xml.dom.minidom import parse
exifSw = 'exiftool -overwrite_original_in_place -ResolutionUnit=inches -XPosition=%s -YPosition=%s -XResolution=1 -YResolution=1 %s'
enblendSw = 'enblend -f %sx%s -a -o %s.tif @%s'
#############################################################################
# got to have this at ~/.ExifTool_config
def exifToolsConfig():
etConfig ="""%Image::ExifTool::UserDefined = (
# All EXIF tags are added to the Main table, and WriteGroup is used to
# specify where the tag is written (default is ExifIFD if not specified):
'Image::ExifTool::Exif::Main' => {
0xd000 => {
Name => 'XResolution',
Writable => 'int16u',
},{
Name => 'YResolution',
Writable => 'int16u',
},{
Name => 'XPosition',
Writable => 'int16u',
},{
Name => 'YPosition',
Writable => 'int16u',
},{
Name => 'ResolutionUnit'
Writable => 'string',
}
# add more user-defined EXIF tags here...
},
);
print "LOADED!\n";"""
return etConfig
#############################################################################
def transSw(img):
# check on making scanline width 78 to match nona
os.chdir('scanline')
basename = os.path.basename(img).split('.')[0]
wSw = ('gdalwarp -of VRT -s_srs EPSG:26918 -t_srs EPSG:4326 -dstalpha -co ALPHA=YES -srcnodata "255" -dstnodata "255" ../%s.jpg %s.vrt')% (basename,basename)
print os.getcwd()
print wSw
os.system(wSw)
tSw = ('gdal_translate -co BLOCKYSIZE=256 -a_nodata "0 0 0" %s.vrt %s.tif') % (basename,basename)
os.system(tSw)
os.chdir('../')
def makeVrt(vrt):
vrtSw = 'gdalbuildvrt %s.vrt *.tif' % (vrt)
os.system(vrtSw)
def parseVrt(vrt):
vrtBasename = os.path.basename(vrt).split('.')[0]
enList = '%s.list' % vrtBasename
enListFile = open(vrtBasename+'.list','w')
vrtInfo = parse(vrt)
GeoTransform = vrtInfo.getElementsByTagName('GeoTransform')
for gt in GeoTransform:
geot = gt.firstChild.data.split(',')
pixelX = float(geot[1])
pixelY = float(geot[5])
# Get ULX,ULY
ULX = float(geot[0]) + (pixelX/2)
ULY = float(geot[3]) + (pixelY/2)
tfw = open(vrtBasename+'.tfw','w')
tfwTxt = '%s\n0\n0\n%s\n%s\n%s' % (pixelX,pixelY,ULX,ULY)
tfw.write(tfwTxt)
tfw.close()
VRTDataset = vrtInfo.getElementsByTagName('VRTDataset')
for (name,value) in VRTDataset[0].attributes.items():
if name == 'rasterXSize':
rasterXSize = value
if name == 'rasterYSize':
rasterYSize = value
print 'Mosaic size is:' ,rasterXSize, rasterYSize
band1 = vrtInfo.getElementsByTagName('VRTRasterBand')[0]
sources = band1.getElementsByTagName('SimpleSource')
if len(sources) == 0:
sources = band1.getElementsByTagName('ComplexSource')
for source in sources:
SourceFilename = source.getElementsByTagName('SourceFilename')
for node in SourceFilename:
image_id = node.firstChild.data
imageListTxt = '%s\n'%image_id
enListFile.write(imageListTxt)
SrcRect = source.getElementsByTagName('SrcRect')
DstRect = source.getElementsByTagName('DstRect')
loop = 0
for (name, value) in DstRect[loop].attributes.items():
#print name,value
if name == 'xSize': # image width
xSize = value
if name == 'ySize': # image height
ySize = value
if name == 'xOff': # x offset into mosaic
xOff = value
if name == 'yOff': # y offset into mosaic
yOff = value
addExif = exifSw % (xOff,yOff,image_id)
os.system(addExif)
print image_id,xSize,ySize,xOff,yOff
enListFile.close()
return rasterXSize, rasterYSize, enList, vrtBasename
def procJpgs():
if not os.path.isdir('scanline'):
os.mkdir('scanline')
jpgs = glob.glob('*.jpg')
for jpg in jpgs:
print jpg
transSw(jpg)
procJpgs()
os.chdir('scanline')
makeVrt('mosaic')
vrtIn = 'mosaic.vrt'
mosaicXSize, mosaicYSize, mosaicList, mosaicBasename = parseVrt(vrtIn)
enSw = enblendSw % ( mosaicXSize, mosaicYSize, mosaicBasename, mosaicList )
os.system(enSw)