In [1]:
%pylab inline
In [2]:
import logging
import os
import numpy as np
import rasterio as rio
import lmdb
from caffe.proto.caffe_pb2 import Datum
import caffe.io
from rasterio._io import RasterReader
In [6]:
from glob import glob
In [8]:
sources =glob('/home/shared/srp/try2/*.tif')
print len(sources)
In [42]:
pos_regions = rasterio.open(r'/home/liux13/Desktop/tmp/pos_regions-epsg-26949.tif')
In [46]:
pos_mask = pos_regions.read(1) > 0
print pos_mask.shape, pos_mask.dtype
In [52]:
imshow(pos_mask[4000:6000, 4000:6000], extent=(4000,5000, 4000,5000), cmap=cm.binary)
colorbar();
In [10]:
from tqdm import tnrange, tqdm_notebook,
In [164]:
points = []
for i in tnrange(len(sources)):
ds = rasterio.open(sources[i])
data = ds.read()
mask = data.sum(0) > 1
indices = np.nonzero(mask)
xy = np.c_[ds.affine*(indices[1], indices[0])]
pos_mask_indices = np.c_[~pos_regions.affine*xy.T].T.round().astype(int)
pos_mask_indices = np.roll(pos_mask_indices, 1, 0)
within_pos_mask = (pos_mask_indices[0] >= 0)
within_pos_mask = (pos_mask_indices[1] >= 0)
within_pos_mask &= (pos_mask_indices[0] < pos_mask.shape[0])
within_pos_mask &= (pos_mask_indices[1] < pos_mask.shape[1])
pos_mask_indices = pos_mask_indices[:, within_pos_mask]
negative_mask = pos_mask[pos_mask_indices[0], pos_mask_indices[1]] == False
xy = xy[within_pos_mask,:][negative_mask, :]
points.append(xy)
points= np.concatenate(points)
In [180]:
print "There are {:,d} negative examples".format(len(points))
In [167]:
import fiona
import shapely.geometry
In [174]:
vectors = fiona.open(r'/home/liux13/Desktop/tmp/boxes_section11.shp')
shapes = [shapely.geometry.shape(f['geometry']) for f in vectors if f['geometry'] is not None]
centers = np.row_stack([np.r_[s.centroid.xy] for s in shapes])
In [179]:
print "There are {:,d} positive examples".format(len(centers))
In [194]:
def get_angle(shape):
verts = np.column_stack(s.xy)
verts
dx, dy = (verts[2]-verts[1])
angle = np.degrees(np.arctan2(dy, dx))
return angle
angles = np.r_[[get_angle(s) for s in shapes]]
print angles.astype(int)
In [195]:
np.savez('/home/shared/srp/sample_locations_epsg26949.npz', neg_xy=points, pos_xy=centers, pos_angles=angles)
In [196]:
gt = np.load('/home/shared/srp/sample_locations_epsg26949.npz')
In [199]:
print gt.keys()`b
In [200]:
pos_xy = gt['pos_xy']
print pos_xy.shape
This is how we can plot rasters with goerefernced vectors on top
First,
import rasterio.plot
Then you can do this:
figsize(15,15)
rasterio.plot.show( (pos_regions, 1), cmap=cm.binary_r, ax=gca())
xlim(232440.0, 232510.0)
ylim(252140.0, 252210.0)
scatter(xy[:,0], xy[:,1], lw=0, s=1)
scatter(pxy[:,0], pxy[:,1], lw=0, c='yellow')
The important part is the second line, where I pass a tuple with the datsaet and the band.
Also important is I pass the current axis in to rasterio.plot.show
.
In [205]:
np.random.randn(2)
Out[205]:
In [ ]:
In [ ]: