-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
142 lines (108 loc) · 4.22 KB
/
utils.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
import os
import numpy as np
import matplotlib.colors as colors
from scipy.io import loadmat
import json
from xml.dom import minidom
import rasterio
import subprocess
from osgeo import ogr, osr
from geojson import Polygon
from datetime import datetime
def clip_tiff_by_shapefile(tiff_file, shapefile):
out_file = tiff_file[:-4] + '_clip.tif'
# XXX: hacky, eventually port to native gdal
command = ["gdalwarp", "-dstnodata", "nan", "-cutline", shapefile, tiff_file, out_file]
try:
output = subprocess.check_output(command)
except:
print("Clipping " + tiff_file + " failed!")
return out_file
def convert_mat_to_json(filename, outfilename, source_epsg=32611, target_epsg=4326):
mat = loadmat(filename)
X = mat['xb'][0]
Y = mat['yb'][0]
ring = ogr.Geometry(ogr.wkbLinearRing)
for x, y in zip(X, Y):
ring.AddPoint(x, y)
polygon = ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(ring)
source = osr.SpatialReference()
source.ImportFromEPSG(source_epsg)
target = osr.SpatialReference()
target.ImportFromEPSG(target_epsg)
transform = osr.CoordinateTransformation(source, target)
polygon.Transform(transform)
for i in range(0, polygon.GetGeometryCount()):
point = polygon.GetGeometryRef(i)
point.FlattenTo2D()
aoi = json.loads(polygon.ExportToJson())
with open(outfilename, 'w') as f:
json.dump(aoi, f)
def convert_mat_to_aoi_bbox(filename, buf=1000, source_epsg=32611, target_epsg=4326):
mat = loadmat(filename)
x = mat['xb']
y = mat['yb']
xmax = x.max() + buf
xmin = x.min() - buf
ymax = y.max() + buf
ymin = y.min() - buf
# XXX: OGR/Planet API expects ring of points defining polygon
bbox = [(xmax, ymax), (xmax, ymin), (xmin, ymin), (xmin, ymax), (xmax, ymax)]
ring = ogr.Geometry(ogr.wkbLinearRing)
for p in bbox:
ring.AddPoint(p[0], p[1])
polygon = ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(ring)
source = osr.SpatialReference()
source.ImportFromEPSG(source_epsg)
target = osr.SpatialReference()
target.ImportFromEPSG(target_epsg)
transform = osr.CoordinateTransformation(source, target)
polygon.Transform(transform)
for i in range(0, polygon.GetGeometryCount()):
point = polygon.GetGeometryRef(i)
point.FlattenTo2D()
aoi = polygon.ExportToJson()
return aoi
def load_image(filename, metadata_filename):
with rasterio.open(filename) as src:
band_blue = src.read(1)
with rasterio.open(filename) as src:
band_green = src.read(2)
with rasterio.open(filename) as src:
band_red = src.read(3)
xmldoc = minidom.parse(metadata_filename)
nodes = xmldoc.getElementsByTagName("ps:bandSpecificMetadata")
coeff = {}
for node in nodes:
band_num = node.getElementsByTagName("ps:bandNumber")[0].firstChild.data
if band_num in ['1', '2', '3', '4']:
i = int(band_num)
value = node.getElementsByTagName("ps:reflectanceCoefficient")[0].firstChild.data
coeff[i] = float(value)
band_blue = band_blue*coeff[1]
band_green = band_green*coeff[2]
band_red = band_red*coeff[3]
return np.stack([band_red, band_blue, band_green], axis=-1)
def print_json(data):
print(json.dumps(data, indent=2))
def rfc3339(date_obj):
# XXX: Assumes date_obj is UTC +0
# TODO : TZ conversion
rfc_fmt = '%Y-%m-%dT%H:%M:%SZ'
return datetime.strftime(date_obj, rfc_fmt)
class MidpointNormalize(colors.Normalize):
"""
Taken from Planet tutorial by Dana Bauer and others
https://github.com/planetlabs/notebooks/blob/master/jupyter-notebooks/ndvi/ndvi_planetscope.ipynb
Original Credit: Joe Kington, http://chris35wills.github.io/matplotlib_diverging_colorbar/
"""
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
self.midpoint = midpoint
colors.Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip=None):
# I'm ignoring masked values and all kinds of edge cases to make a
# simple example...
x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
return np.ma.masked_array(np.interp(value, x, y), np.isnan(value))