def match_class(df): shandong = df.to_crs(3857) box = gutil.shp2box(shandong, (3600, 2400), 0.15, 1) paper = gnp.frombox(*box, dtype=np.uint8) idx = gmt.build_index(glob('../data/class/*.tif')) gmt.match_idx(idx, paper, out='in', order=0) gio.write_tif(paper, '../data/result/shandong_class.tif')
def match_multi(rasters, out, chan='all', step=10, order=1): if chan == 'all': chan = list(range(rasters[0].channels())) if isinstance(chan, int): chan = [chan] if isinstance(out, tuple): out = gnp.frombox(*out, len(chan), rasters[0].dtype) for raster in rasters: match_one(raster, out, chan, step, order) return out
def draw_class(): # ===== look up table ===== lut = np.array([[0 ,0 ,0 ], [168,168,0 ], [20 ,119,73 ], [169,208,95 ], [56 ,168,0 ], [126,206,244], [0 ,86 ,154], [112,168,0 ], [147,47 ,20 ], [202,202,202], [0 ,255,197], [255,255,255]], dtype=np.uint8) # ===== read shape file and make a paper ===== liaoning = gio.read_shp('../data/shape/shandong.shp') liaoning = liaoning.to_crs(3857) box = gutil.shp2box(liaoning, (3600, 2400), 0.15, 1) paper = gnp.frombox(*box, dtype=np.uint8) # ===== match the class tif into paper fs = glob('../data/class/*.tif') idx = gmt.build_index(fs) gmt.match_idx(idx, paper, out='in', order=0) msk = paper * 0 gdraw.draw_polygon(msk, liaoning, 255, 0) paper[msk==0] = 11 body = [('图例', 'simhei', 72), ('rect', 1, '农田'), ('rect', 2, '森林'), ('rect', 3, '草地'), ('rect', 4, '灌丛'), ('rect', 5, '湿地'), ('rect', 6, '水体'), ('rect', 7, '苔原'), ('rect', 8, '隔水层'), ('rect', 9, '裸地'), ('rect', 10, '冰雪')] # 底图,位置,内容,空隙,矩形尺寸及线宽,字体字号颜色,外边框宽度 gdraw.draw_style(paper, 60, -60, body, mar=(20, 30), recsize=(120,60,0), font=('simsun', 60, 0), color=0, box=0) gdraw.draw_unit(paper, -120, -60, 0.3, 30, ('times', 48), 0, 'km', 3, anc='r') gdraw.draw_text(paper, '山东省土地利用类型', 80, 60, 0, ('simkai', 128)) gdraw.draw_N(paper, -240, 240, ('simhei', 100), 2, 100, 0) gdraw.draw_polygon(paper, liaoning, 0, 2) gdraw.draw_bound(paper, 5, 5, -5, -5, 0, 2, clear=None) return paper.lookup(lut)
def crs_trans_test(): path = '../data/modis/MOD09Q1.A2019017.h27v05.006.2019030120430.hdf' raster = gio.read_hdf(path, 0) outshp = gutil.box2shp(*raster.getbox()).to_crs(3857) box = gutil.shp2box(outshp, 1000, 0, 1) paper = gnp.frombox(*box, dtype=np.int16) gmt.match_one(raster, paper, out='in') plt.imshow(paper) plt.show()
def match_multi_test(): shandong = gio.read_shp('../data/shape/shandong.shp') shandong = shandong.to_crs(3857) info = gutil.shp2box(shandong, (2048, 1536), 0.05, 1) paper = gnp.frombox(*info, dtype=np.int16) fs = glob('../data/modis/*.hdf') rasters = [gio.read_hdf(i, 0) for i in fs] gmt.match_multi(rasters, paper, out='in') plt.imshow(paper) plt.show()
def match_one_test(): shandong = gio.read_shp('../data/shape/shandong.shp') shandong = shandong.to_crs(3857) info = gutil.shp2box(shandong, (2048, 1536), 0.05, 1) paper = gnp.frombox(*info, dtype=np.int16) path = '../data/modis/MOD09Q1.A2019017.h27v05.006.2019030120430.hdf' raster = gio.read_hdf(path, 0) gmt.match_one(raster, paper, out='in') plt.imshow(paper) plt.show()
def draw_simple(): shandong = gio.read_shp('../data/shape/shandong.shp') shandong = shandong.to_crs(3857) box = gutil.shp2box(shandong, (3600, 2400), 0.1, 1) paper = gnp.frombox(*box, dtype=np.uint8) paper[:] = 255 gdraw.draw_polygon(paper, shandong, 0, 2) gdraw.draw_ruler(paper, 80, 50, -80, -50, 1, 4326, ('times', 32), 0, 2, 5) gdraw.draw_lab(paper, shandong, 'name', 0, ('simhei', 32), 'ct') gdraw.draw_unit(paper, -180, -100, 0.3, 30, ('times', 48), 0, 'km', 3, anc='r') gdraw.draw_text(paper, '山东省', 180, 120, 0, ('simkai', 128)) gdraw.draw_N(paper, -240, 240, ('simhei', 100), 2, 100, 0) return paper
def match_idx_test(): shandong = gio.read_shp('../data/shape/shandong.shp') shandong = shandong.to_crs(3857) info = gutil.shp2box(shandong, (2048, 768 * 2), 0.05, 1) paper = gnp.frombox(*info, dtype=np.int16) fs = glob('../data/modis/*.hdf') idx = gmt.build_index(fs) gmt.match_idx(idx, paper, out='in', chan=[0]) plt.imshow(paper) plt.show()
def match_idx(idx, out, chan='all', step=10, order=1): if chan == 'all': chan = list(range(len(idx['channels'][0]))) if isinstance(chan, int): chan = [chan] if isinstance(out, tuple): shape, crs, mat = out else: shape, crs, mat = out.shape, out.crs, out.mat idx = idx.to_crs(gutil.makecrs(crs).to_proj4()) box = gutil.box2shp(shape, crs, mat)[0] for i in idx.index: if box.intersects(idx.loc[i]['geometry']): print(chan) raster = gio.read_raster(idx.loc[i]['path'], chan) if isinstance(out, tuple): out = gnp.frombox(*out, len(chan), raster.dtype) print('merge ...', idx.loc[i]['path'], end=' ') match_one(raster, out, chan, step, order) print('end') return out
def match_one(raster, out, chan='all', step=10, order=1): if chan == 'all': chan = list(range(raster.channels())) if isinstance(chan, int): chan = [chan] if isinstance(out, tuple): out = gnp.frombox(*out, len(chan), raster.dtype) imgd, prjd, md = out, out.crs, out.mat img, prjs, ms = raster, raster.crs, raster.mat hs, ws = img.shape[:2] xx = np.linspace(0, ws, 100).reshape((-1, 1)) * [1, 0] yy = np.linspace(0, hs, 100).reshape((-1, 1)) * [0, 1] xy = np.vstack((xx, xx + [0, hs], yy, yy + [ws, 0])) xy = mp2pm(xy, ms, prjs, prjd, md) #.astype(np.int) (left, top), (right, bot) = xy.min(axis=0), xy.max(axis=0) left, right = np.clip((left, right), 0, imgd.shape[1]) top, bot = np.clip((top, bot), 0, imgd.shape[0]) hb, wb = bot - top, right - left # 像素数目 nh, nw = int(hb // step + 1), int(wb // step + 1) xy = np.mgrid[top:bot:nh * 1j, left:right:nw * 1j].reshape((2, -1)).T xs, ys = mp2pm(xy[:, ::-1], md, prjd, prjs, ms).T xs.shape = ys.shape = (nh, nw) #block.shape intleft, intright = floor(left), ceil(right) inttop, intbot = floor(top), ceil(bot) rc = np.mgrid[inttop:intbot, intleft:intright].reshape((2, -1)) frc = rc.astype(np.float32) + 0.5 frc[0] = (frc[0] - top) / (bot - top) * (nh - 1) frc[1] = (frc[1] - left) / (right - left) * (nw - 1) xs = map_coordinates(xs, frc, order=1) ys = map_coordinates(ys, frc, order=1) for i in chan: #range(img.channels()): rcs, dcs = img.channels(i), imgd.channels(i) vs = map_coordinates(rcs, np.array([ys, xs]) - 0.5, order=order) #prefilter=False) vs[np.isnan(vs)] = 0 dcs[tuple(rc)] = np.maximum(dcs[tuple(rc)], vs) return imgd
def draw_grade(): shandong = gio.read_shp('../data/shape/shandong.shp') shandong = shandong.to_crs(3857) box = gutil.shp2box(shandong, (3600, 2400), 0.1, 1) paper = gnp.frombox(*box, dtype=np.uint8) areas = shandong.area grade_lut = np.array([3]*60 + [2]*30 + [1]*10, dtype=np.uint8) vs = (areas-areas.min())/(areas.max()-areas.min())*99 grade = grade_lut[vs.astype(int)] print(grade) gdraw.draw_polygon(paper, shandong, grade, 0) gdraw.draw_polygon(paper, shandong, 4, 2) gdraw.draw_ruler(paper, 80, 50, -80, -50, 1, 4326, ('times', 32), 4, 2, 5) gdraw.draw_lab(paper, shandong, 'name', 4, ('simhei', 32), 'ct') gdraw.draw_unit(paper, -180, -100, 0.3, 30, ('times', 48), 4, 'km', 3, anc='r') gdraw.draw_text(paper, '山东省', 180, 120, 4, ('simkai', 128)) gdraw.draw_N(paper, -240, 240, ('simhei', 100), 2, 100, 4) body = [('图例', 'simhei', 72), ('rect', 1, '特大城市'), ('rect', 2, '中型城市'), ('rect', 3, '一般城市')] # 底图,位置,内容,空隙,矩形尺寸及线宽,字体字号颜色,外边框宽度 gdraw.draw_style(paper, 150, -90, body, mar=(20, 30), recsize=(120,60,2), font=('simsun', 60, 4), color=4, box=0) lut = np.array([[255,255,255], [255,200,100], [255,255,128], [255,255,200], [0 ,0 ,0 ]], dtype=np.uint8) return paper.lookup(lut)
def city_label(df): shandong = df.to_crs(3857) box = gutil.shp2box(shandong, (3600, 2400), 0.15, 1) paper = gnp.frombox(*box, dtype=np.uint8) gdraw.draw_polygon(paper, shandong, np.arange(len(shandong)) + 1, 0) gio.write_tif(paper, '../data/result/shandong_label.tif')
import geonumpy as gnp import numpy as np if __name__ == '__main__': mat = np.array([[0, 1, 0], [0, 0, 1]]) garr = gnp.geoarray(np.ones((5, 5)), crs=4326, mat=mat) print(garr.crs) print(garr.mat) garr2 = garr + 1 print(garr2.crs) print(garr2.mat) garr3 = garr[1::2, 1::2] print(garr3.crs) print(garr3.mat) box = garr.getbox() print(box) garr4 = gnp.frombox(*box, dtype=np.uint8) print(garr4.getbox()) garr_mc = gnp.geoarray(np.ones((5, 5, 3)), crs=4326, mat=mat) print(garr_mc.crs) print(garr_mc.mat)
def run(self, tps, snap, data, para=None): box = gutil.shp2box(snap, para['scale'] or (para['width'], para['height']), para['mar']) IPy.show_img([gnp.frombox(*box, para['chan'], dtype=para['type'])], tps.title + '-boximg')