2018-12-28 15:22:00 +00:00
|
|
|
import numpy as np
|
|
|
|
import pykrige.kriging_tools as kt
|
|
|
|
from pykrige import OrdinaryKriging
|
|
|
|
import rasterio as rio
|
2018-12-29 10:33:58 +00:00
|
|
|
from ..util.utils import *
|
2018-12-28 15:22:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KrigingSpiData(object):
|
|
|
|
|
2018-12-29 02:43:07 +00:00
|
|
|
def __init__(self, data, save_path, XY):
|
2018-12-28 15:22:00 +00:00
|
|
|
self.save_path = save_path
|
|
|
|
self.data = data
|
2018-12-29 02:43:07 +00:00
|
|
|
self.XY = XY
|
2018-12-28 15:22:00 +00:00
|
|
|
self.get_conf()
|
|
|
|
|
2018-12-29 02:43:07 +00:00
|
|
|
def get_minmax(self):
|
|
|
|
delta = 0.1
|
2018-12-29 10:22:45 +00:00
|
|
|
miny = self.XY['Y'].min() - delta
|
|
|
|
minx = self.XY['X'].min() - delta
|
|
|
|
maxy = self.XY['Y'].max() + delta
|
|
|
|
maxx = self.XY['X'].max() + delta
|
2018-12-29 10:18:08 +00:00
|
|
|
# print(miny, minx, maxy, maxx)
|
2018-12-29 02:43:07 +00:00
|
|
|
return (minx, miny, maxx, maxy)
|
|
|
|
|
2018-12-28 15:22:00 +00:00
|
|
|
def get_conf(self):
|
2018-12-29 02:43:07 +00:00
|
|
|
(minx, miny, maxx, maxy) = self.get_minmax()
|
2018-12-28 15:22:00 +00:00
|
|
|
res = 0.003
|
|
|
|
self.gridx = np.arange(minx, maxx, res)
|
|
|
|
self.gridy = np.arange(miny, maxy, res)
|
2018-12-29 10:39:34 +00:00
|
|
|
self.height = len(self.gridy)
|
|
|
|
self.width = len(self.gridx)
|
2018-12-28 15:22:00 +00:00
|
|
|
from rasterio.transform import from_origin
|
2018-12-29 10:22:45 +00:00
|
|
|
self.transform = from_origin(minx - res / 2, maxy + res / 2, res, res)
|
2018-12-28 15:22:00 +00:00
|
|
|
|
|
|
|
def write_tif(self, kriging_data):
|
|
|
|
out_meta = {"driver": "GTiff",
|
|
|
|
"height": self.height,
|
|
|
|
"width": self.width,
|
|
|
|
"transform": self.transform}
|
2018-12-29 11:53:15 +00:00
|
|
|
# with rio.open(self.save_path, "w", count=1, dtype='float64', crs='+proj=latlong', **out_meta) as dest:
|
2018-12-29 12:05:47 +00:00
|
|
|
with rio.open(self.save_path, "w", count=1, dtype='float64', crs="{'init': 'epsg:3857', 'no_defs': True}", **out_meta) as dest:
|
2018-12-28 15:22:00 +00:00
|
|
|
dest.write_band(1, kriging_data)
|
|
|
|
|
2018-12-29 02:43:07 +00:00
|
|
|
def kriging(self):
|
|
|
|
# self.data = np.random.rand(179)
|
2018-12-29 10:18:08 +00:00
|
|
|
# print(self.data)
|
2018-12-29 02:43:07 +00:00
|
|
|
|
|
|
|
OK = OrdinaryKriging(np.asarray(self.XY['X']), np.asarray(self.XY['Y']), self.data, variogram_model='spherical', verbose=False,
|
2018-12-28 15:22:00 +00:00
|
|
|
enable_plotting=False)
|
|
|
|
z, ss = OK.execute('grid', self.gridx, self.gridy)
|
|
|
|
self.write_tif(z)
|
2018-12-29 10:33:58 +00:00
|
|
|
cut_raster_by_geo(self.save_path, self.save_path, get_region_geo())
|
2018-12-28 15:22:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
kd = KrigingSpiData("D:/work/3.scala-gis/geotrellis-through/geotrellis/bj_197901.txt", "D://")
|
2018-12-29 08:02:42 +00:00
|
|
|
kd.kriging()
|