forked from lmotta/scripts-for-gis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
footprint_extent.py
executable file
·155 lines (137 loc) · 4.34 KB
/
footprint_extent.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : footprint_extent
Description : Create extent from image
Arguments : Image
-------------------
begin : 2015-04-08
copyright : (C) 2015 by Luiz Motta
email : motta dot luiz at gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os, sys
from optparse import OptionParser
from osgeo import ( gdal, ogr, osr )
import osgeo.gdalconst
def printStatus(status, newLine=False):
if newLine:
ch = "\n"
else:
ch = ""
sys.stdout.write( "\r%s" % ( status.ljust(100) + ch ) )
sys.stdout.flush()
def run( inImage ):
def getInRaster():
def getCoordinate( coef, line, col ):
return { 'x': coef[0] + col * coef[1] + line * coef[2], 'y': coef[3] + col * coef[4] + line * coef[5] }
#
gdal.AllRegister()
ds = gdal.Open( inImage, osgeo.gdalconst.GA_ReadOnly )
if ds is None:
return None
# Spatial Reference
pr = ds.GetProjectionRef()
if pr == '':
return None
sr = osr.SpatialReference()
sr.ImportFromWkt( pr )
#
coef = ds.GetGeoTransform()
coordUL = getCoordinate( coef, 0, 0 )
coordBR = getCoordinate( coef, ds.RasterYSize, ds.RasterXSize )
#
ds.FlushCache()
#
return ( sr, coordUL, coordBR )
def createOutLayer():
( path_file, ext ) = os.path.splitext( inImage )
outGeojson = "%s_extent%s" % ( path_file, ".geojson" )
outDriver = ogr.GetDriverByName( driveName )
#
if os.path.exists( outGeojson ):
outDriver.DeleteDataSource( outGeojson )
#
outDataSource = outDriver.CreateDataSource( outGeojson )
#
if outDataSource is None:
return None
#
outLayer = outDataSource.CreateLayer( outGeojson, sr, ogr.wkbPolygon )
if outLayer is None:
return None
# Fields
fieldPath = ogr.FieldDefn( "path", ogr.OFTString)
outLayer.CreateField( fieldPath )
fieldImage = ogr.FieldDefn( "image", ogr.OFTString)
outLayer.CreateField( fieldImage )
#
return ( outGeojson, outDataSource, outLayer )
def createGeom():
#
# p2 -- p3
# | |
# p1 -- p4
#
p1 = "%f %f" % ( coordUL['x'], coordBR['y'] )
p2 = "%f %f" % ( coordUL['x'], coordUL['y'] )
p3 = "%f %f" % ( coordBR['x'], coordUL['y'] )
p4 = "%f %f" % ( coordBR['x'], coordBR['y'] )
sWkt = "POLYGON ((%s))" % ", ".join( [ p1, p2, p3, p4, p1 ] )
geom = ogr.CreateGeometryFromWkt( sWkt )
geom.AssignSpatialReference( sr )
return geom
if not os.path.exists( inImage ):
printStatus( "File '%s' not exist" % inImage, True )
return 1
data = getInRaster()
if data is None:
return 1
( sr, coordUL, coordBR ) = data
driveName = "GeoJSON"
data = createOutLayer()
if data is None:
return 1
( outGeojson, outDataSource, outLayer ) = data
# Populate
featureDefn = outLayer.GetLayerDefn()
outFeat = ogr.Feature( featureDefn )
#
( path, image) = os.path.split( inImage)
outFeat.SetField( "path", path )
outFeat.SetField2( "image", image )
outFeat.SetGeometry( createGeom() )
#
outLayer.CreateFeature( outFeat )
#
outFeat.Destroy()
outDataSource.Destroy()
#
printStatus( "Create extent layer: '%s'" % outGeojson, True )
#
return 0
def main():
usage = "usage: %prog image"
parser = OptionParser(usage)
# define options
(opts, args) = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
return 1
elif len(args) == 0:
print("No image provided. Nothing to do!")
parser.print_help()
return 1
else:
return run( args[0] )
if __name__ == "__main__":
sys.exit( main() )