2018-12-09 14:05:41 +00:00
|
|
|
from geopandas import *
|
|
|
|
from pandas import *
|
|
|
|
from shapely.geometry import Point
|
|
|
|
import rasterio as rio
|
|
|
|
import rasterio.mask
|
|
|
|
from rasterio.warp import reproject, Resampling
|
2018-12-26 14:19:17 +00:00
|
|
|
import os
|
|
|
|
import glob
|
2018-12-09 14:05:41 +00:00
|
|
|
|
2018-12-29 08:17:03 +00:00
|
|
|
DATA_BASE_PATH = '/home/g214/data_from_chenhao/data_analyse/'
|
2018-12-29 10:05:53 +00:00
|
|
|
TEMP_PATH = '/home/g214/temp/'
|
2018-12-29 10:33:58 +00:00
|
|
|
BJ_REGION = '/home/g214/data_from_chenhao/data_analyse/boundary/bj_region/bj_region.shp'
|
2018-12-29 11:41:27 +00:00
|
|
|
|
|
|
|
|
2018-12-29 08:17:03 +00:00
|
|
|
# DATA_BASE_PATH = r"D:\7. business\7.baoji\code\drought_analyse\data/"
|
2018-12-09 14:05:41 +00:00
|
|
|
|
|
|
|
def get_buffer_data(gdf, crs={'init': 'epsg:4326', 'no_defs': True}, dis=1):
|
|
|
|
data = gdf
|
|
|
|
if not data.crs:
|
|
|
|
data.crs = crs
|
|
|
|
else:
|
|
|
|
crs = data.crs
|
|
|
|
|
|
|
|
data = data.to_crs(epsg=3857)
|
|
|
|
buffer = data.buffer(dis)
|
|
|
|
data.set_geometry(buffer, inplace=True)
|
|
|
|
data.to_crs(crs=crs, inplace=True)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def read_xzqh(path):
|
2018-12-28 13:51:57 +00:00
|
|
|
shp_bjqh = GeoDataFrame.from_file(path, encoding='utf-8')
|
2018-12-16 19:26:27 +00:00
|
|
|
shp_bjqh = shp_bjqh[['geometry', 'county']]
|
2018-12-09 14:05:41 +00:00
|
|
|
shp_bjqh.to_crs(epsg=4326, inplace=True)
|
|
|
|
return shp_bjqh
|
|
|
|
|
|
|
|
|
|
|
|
def intersection(gdf1, gdf2):
|
2018-12-24 15:36:52 +00:00
|
|
|
geo_list = []
|
2018-12-24 16:01:22 +00:00
|
|
|
county = []
|
|
|
|
index_gdf1 = 0
|
2018-12-24 15:36:52 +00:00
|
|
|
for geo in gdf1.geometry:
|
2018-12-24 16:10:33 +00:00
|
|
|
index_gdf2 = 0
|
2018-12-24 15:36:52 +00:00
|
|
|
for geo2 in gdf2.geometry:
|
|
|
|
if geo.intersects(geo2):
|
|
|
|
intersection_geo = geo.intersection(geo2)
|
|
|
|
geo_list.append(intersection_geo)
|
2018-12-24 16:01:22 +00:00
|
|
|
if 'county' in gdf1:
|
2018-12-24 16:08:01 +00:00
|
|
|
county.append(gdf1.iloc[index_gdf1].county)
|
2018-12-24 16:35:01 +00:00
|
|
|
elif 'county' in gdf2:
|
2018-12-24 16:08:01 +00:00
|
|
|
county.append(gdf2.iloc[index_gdf2].county)
|
2018-12-24 16:10:33 +00:00
|
|
|
index_gdf2 += 1
|
|
|
|
index_gdf1 += 1
|
2018-12-24 16:01:22 +00:00
|
|
|
|
2018-12-24 16:12:28 +00:00
|
|
|
gdf = GeoDataFrame(geometry=geo_list, crs=gdf1.crs)
|
2018-12-27 17:48:36 +00:00
|
|
|
if county and len(county) > 0:
|
|
|
|
gdf['county'] = county
|
2018-12-24 16:35:01 +00:00
|
|
|
# print(gdf.head())
|
2018-12-24 16:12:28 +00:00
|
|
|
return gdf
|
2018-12-24 15:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# def intersection(gdf1, gdf2):
|
|
|
|
# try:
|
|
|
|
# from geopandas.tools import overlay
|
|
|
|
# data = overlay(gdf1, gdf2, how='intersection', use_sindex=False)
|
|
|
|
# data.crs = gdf1.crs
|
|
|
|
# return data
|
|
|
|
# except Exception as e:
|
|
|
|
# print(str(e))
|
|
|
|
# return None
|
2018-12-09 14:05:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def intersection_xzqh(gdf):
|
2018-12-16 19:43:00 +00:00
|
|
|
bjqh = read_xzqh(DATA_BASE_PATH + 'boundary/bj.shp')
|
2018-12-09 14:05:41 +00:00
|
|
|
intersection_data = intersection(gdf, bjqh)
|
|
|
|
return intersection_data
|
|
|
|
|
|
|
|
|
|
|
|
def join(gdf1, gdf2):
|
|
|
|
return geopandas.sjoin(gdf1, gdf2, how="inner", op='intersects')
|
|
|
|
|
|
|
|
|
2018-12-26 14:19:17 +00:00
|
|
|
def cut_raster_by_geo(path, new_path, geo):
|
2018-12-29 10:33:58 +00:00
|
|
|
src = rio.open(path)
|
|
|
|
# gdf = geo.to_crs(epsg=src.crs)
|
|
|
|
feature = [geo.__geo_interface__]
|
|
|
|
out_image, out_transform = rio.mask.mask(src, feature, crop=True, nodata=src.nodata)
|
|
|
|
out_meta = src.meta.copy()
|
|
|
|
src.close()
|
|
|
|
out_meta.update({"driver": "GTiff",
|
|
|
|
"height": out_image.shape[1],
|
|
|
|
"width": out_image.shape[2],
|
|
|
|
"transform": out_transform})
|
|
|
|
dest = rio.open(new_path, "w", **out_meta)
|
|
|
|
dest.write(out_image)
|
|
|
|
dest.close()
|
2018-12-26 14:19:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_new_raster_name(path):
|
|
|
|
return os.path.join(os.path.dirname(path), "region", os.path.basename(path))
|
|
|
|
|
|
|
|
|
2018-12-29 10:33:58 +00:00
|
|
|
def get_region_geo():
|
|
|
|
shp_data = GeoDataFrame.from_file(BJ_REGION)
|
2018-12-26 14:19:17 +00:00
|
|
|
region = shp_data.loc[0].geometry
|
2018-12-29 10:33:58 +00:00
|
|
|
return region
|
|
|
|
|
2018-12-29 11:41:27 +00:00
|
|
|
|
2018-12-29 10:33:58 +00:00
|
|
|
def cut_files_by_shp(files, shp_path):
|
|
|
|
region = get_region_geo()
|
2018-12-26 14:19:17 +00:00
|
|
|
for file in files:
|
|
|
|
cut_raster_by_geo(file, get_new_raster_name(file), region)
|
|
|
|
|
|
|
|
|
2018-12-29 10:33:58 +00:00
|
|
|
def cut_dir_by_shp(dir, shp_path):
|
|
|
|
files = filter(os.path.isfile, glob.glob(dir + "*"))
|
|
|
|
cut_files_by_shp(files, shp_path)
|
|
|
|
|
|
|
|
|
2018-12-29 11:41:27 +00:00
|
|
|
def agg_raster_by_raster(path1, path2=os.path.join(TEMP_PATH, 'temp.tif'), type='area'):
|
|
|
|
raster1 = rio.open(path1)
|
|
|
|
raster2 = rio.open(path2)
|
|
|
|
|
|
|
|
bjqh = read_xzqh(DATA_BASE_PATH + 'boundary/bj.shp').to_crs(epsg=3857)
|
|
|
|
|
|
|
|
res_county = {}
|
|
|
|
|
|
|
|
for i in range(0, len(bjqh)):
|
|
|
|
geo = bjqh.iloc[i].geometry
|
|
|
|
county_name = bjqh.iloc[i].county
|
|
|
|
feature = [geo.__geo_interface__]
|
2018-12-29 11:46:24 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
raster1_image, raster1_transform = rio.mask.mask(raster1, feature, crop=True, nodata=raster1.nodata)
|
|
|
|
raster2_image, raster2_transform = rio.mask.mask(raster2, feature, crop=True, nodata=raster2.nodata)
|
|
|
|
raster2_reproject = np.empty(shape=(raster1_image.shape[0], # same number of bands
|
|
|
|
round(raster1_image.shape[1]),
|
|
|
|
round(raster1_image.shape[2])))
|
|
|
|
reproject(
|
|
|
|
raster2_image, raster2_reproject,
|
|
|
|
src_transform=raster2_transform,
|
|
|
|
dst_transform=raster1_transform,
|
|
|
|
src_crs='+proj=latlong',
|
|
|
|
dst_crs='+proj=latlong',
|
|
|
|
resampling=Resampling.bilinear)
|
|
|
|
|
|
|
|
# out_image_slope_reproject = np.where(out_image_slope_reproject == raster2.nodata, 1000, out_image_slope_reproject)
|
|
|
|
# out_image_slope_reproject = out_image_slope_reproject.astype(int)
|
|
|
|
for i in range(0, raster1_image.shape[1]):
|
|
|
|
for j in range(0, raster1_image.shape[2]):
|
|
|
|
data1 = raster1_image[0, i, j]
|
|
|
|
data2 = raster2_image[0, i, j]
|
|
|
|
print(data1, data2)
|
|
|
|
if data2 != raster2.nodata and data2 < 0:
|
|
|
|
if type == 'area':
|
|
|
|
if data1 != raster2.nodata and data1 > 0:
|
|
|
|
if county_name in res_county:
|
|
|
|
res_county[county_name] = res_county[county_name] + 1
|
|
|
|
else:
|
|
|
|
res_county[county_name] = 1
|
|
|
|
else:
|
2018-12-29 11:41:27 +00:00
|
|
|
if county_name in res_county:
|
2018-12-29 11:46:24 +00:00
|
|
|
res_county[county_name] = res_county[county_name] + data1
|
2018-12-29 11:41:27 +00:00
|
|
|
else:
|
2018-12-29 11:46:24 +00:00
|
|
|
res_county[county_name] = data1
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
2018-12-29 11:41:27 +00:00
|
|
|
return res_county
|
|
|
|
|
|
|
|
|
2018-12-09 14:05:41 +00:00
|
|
|
def agg_raster_by_gdf_value(path, gdf):
|
|
|
|
# crs = '+proj=aea +lat_1=25 +lat_2=47 +lat_0=0 +lon_0=105 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
|
|
|
|
with rio.open(path) as src:
|
|
|
|
gdf = gdf.to_crs(epsg=3857)
|
|
|
|
sum = {}
|
|
|
|
for i in range(0, len(gdf)):
|
|
|
|
geo = gdf.iloc[i].geometry
|
|
|
|
try:
|
|
|
|
feature = [GeoSeries(geo).__geo_interface__['features'][0]['geometry']]
|
|
|
|
out_image, out_transform = rio.mask.mask(src, feature, crop=True, nodata=src.nodata)
|
|
|
|
res = np.nansum(np.where(out_image == src.nodata, np.nan, out_image))
|
|
|
|
if 'county' in gdf:
|
|
|
|
county = gdf.iloc[i].county
|
2018-12-17 12:19:03 +00:00
|
|
|
print(county)
|
2018-12-09 14:05:41 +00:00
|
|
|
if county in sum.keys():
|
|
|
|
sum[county] = sum.get(county) + res
|
|
|
|
else:
|
|
|
|
sum[county] = res
|
|
|
|
elif 'all' in sum.keys():
|
|
|
|
sum['all'] = sum.get('all') + res
|
|
|
|
else:
|
|
|
|
sum['all'] = res
|
|
|
|
# for band in out_image:
|
|
|
|
# for row in band:
|
|
|
|
# for column in row:
|
|
|
|
# if column != src.nodata:
|
|
|
|
# sum += column
|
|
|
|
# print(sum)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
return sum
|
|
|
|
|
|
|
|
|
|
|
|
def agg_raster_by_gdf_area(path, gdf):
|
|
|
|
# crs = '+proj=aea +lat_1=25 +lat_2=47 +lat_0=0 +lon_0=105 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
|
|
|
|
with rio.open(path) as src:
|
|
|
|
gdf = gdf.to_crs(epsg=3857)
|
|
|
|
sum = {}
|
|
|
|
for i in range(0, len(gdf)):
|
|
|
|
geo = gdf.iloc[i].geometry
|
|
|
|
try:
|
|
|
|
feature = [GeoSeries(geo).__geo_interface__['features'][0]['geometry']]
|
|
|
|
out_image, out_transform = rio.mask.mask(src, feature, crop=True, nodata=src.nodata)
|
|
|
|
out_image = np.where(out_image == 1, 1, np.nan)
|
|
|
|
print(out_image)
|
|
|
|
res = np.nansum(out_image) * src.width * src.height
|
|
|
|
if 'county' in gdf:
|
|
|
|
county = gdf.iloc[i].county
|
|
|
|
if county in sum.keys():
|
|
|
|
sum[county] = sum.get(county) + res
|
|
|
|
else:
|
|
|
|
sum[county] = res
|
|
|
|
elif 'all' in sum.keys():
|
|
|
|
sum['all'] = sum.get('all') + res
|
|
|
|
else:
|
|
|
|
sum['all'] = res
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return sum
|
|
|
|
|
|
|
|
|
2018-12-17 12:19:03 +00:00
|
|
|
def agg_prevent_disater_by_gdf_area(path, gdf):
|
|
|
|
# crs = '+proj=aea +lat_1=25 +lat_2=47 +lat_0=0 +lon_0=105 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
|
|
|
|
with rio.open(path) as src:
|
|
|
|
gdf = gdf.to_crs(epsg=3857)
|
|
|
|
sum = {}
|
|
|
|
for i in range(0, len(gdf)):
|
|
|
|
geo = gdf.iloc[i].geometry
|
|
|
|
try:
|
|
|
|
feature = [GeoSeries(geo).__geo_interface__['features'][0]['geometry']]
|
|
|
|
out_image, out_transform = rio.mask.mask(src, feature, crop=True, nodata=src.nodata)
|
|
|
|
out_image = np.where(out_image == 1, 1, np.nan)
|
|
|
|
print(out_image)
|
|
|
|
res = np.nansum(out_image) * src.width * src.height
|
|
|
|
name = str(gdf.iloc[i].name)
|
|
|
|
if name in sum.keys():
|
|
|
|
sum[name] = sum.get(name) + res
|
|
|
|
else:
|
|
|
|
sum[name] = res
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return sum
|
|
|
|
|
|
|
|
|
2018-12-09 14:05:41 +00:00
|
|
|
def agg_gdf_by_gdf_area(gdf1, gdf2):
|
|
|
|
intersection_data = intersection(gdf1, gdf2)
|
|
|
|
area = intersection_data.to_crs(epsg=3857).area
|
|
|
|
intersection_data['area'] = area
|
|
|
|
intersection_data = intersection_data[['name', 'area']]
|
|
|
|
# print(intersection_data.head())
|
2018-12-17 12:19:03 +00:00
|
|
|
return intersection_data.groupby('name')['area'].sum()
|
2018-12-09 14:05:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def agg_raster_by_gdf_area_and_aspect_slope(path, path_aspect, path_slope, gdf):
|
|
|
|
with rio.open(path) as src:
|
|
|
|
with rio.open(path_aspect) as src1:
|
|
|
|
with rio.open(path_slope) as src2:
|
|
|
|
gdf = gdf.to_crs(epsg=3857)
|
|
|
|
res_flat = 0
|
|
|
|
res_30 = 0
|
|
|
|
res_more_than_30 = 0
|
|
|
|
for i in range(0, len(gdf)):
|
|
|
|
geo = gdf.iloc[i].geometry
|
|
|
|
try:
|
|
|
|
feature = [GeoSeries(geo).__geo_interface__['features'][0]['geometry']]
|
|
|
|
out_image, out_transform = rio.mask.mask(src, feature, crop=True, nodata=src.nodata)
|
|
|
|
out_image_aspect, out_transform_aspect = rio.mask.mask(src1, feature, crop=True,
|
|
|
|
nodata=src1.nodata)
|
|
|
|
out_image_slope, _ = rio.mask.mask(src2, feature, crop=True, nodata=src2.nodata)
|
|
|
|
out_image_aspect_reproject = np.empty(shape=(out_image.shape[0], # same number of bands
|
2018-12-24 15:36:52 +00:00
|
|
|
round(out_image.shape[1]),
|
|
|
|
round(out_image.shape[2])))
|
2018-12-09 14:05:41 +00:00
|
|
|
|
|
|
|
out_image_slope_reproject = np.empty(shape=(out_image.shape[0], # same number of bands
|
2018-12-24 15:36:52 +00:00
|
|
|
round(out_image.shape[1]),
|
|
|
|
round(out_image.shape[2])))
|
2018-12-09 14:05:41 +00:00
|
|
|
reproject(
|
|
|
|
out_image_aspect, out_image_aspect_reproject,
|
|
|
|
src_transform=out_transform_aspect,
|
|
|
|
dst_transform=out_transform,
|
2018-12-11 13:41:08 +00:00
|
|
|
src_crs='+proj=latlong',
|
|
|
|
dst_crs='+proj=latlong',
|
2018-12-09 14:05:41 +00:00
|
|
|
resampling=Resampling.bilinear)
|
|
|
|
reproject(
|
|
|
|
out_image_slope, out_image_slope_reproject,
|
|
|
|
src_transform=out_transform_aspect,
|
|
|
|
dst_transform=out_transform,
|
|
|
|
# src_crs={'init': 'EPSG:3857'},
|
|
|
|
# dst_crs={'init': 'EPSG:3857'},
|
|
|
|
resampling=Resampling.bilinear)
|
2018-12-24 15:36:52 +00:00
|
|
|
out_image_slope_reproject = np.where(out_image_slope_reproject == src1.nodata, 1000,
|
|
|
|
out_image_slope_reproject)
|
2018-12-09 14:05:41 +00:00
|
|
|
out_image_slope_reproject = out_image_slope_reproject.astype(int)
|
2018-12-24 15:36:52 +00:00
|
|
|
out_image_aspect_reproject = np.where(out_image_aspect_reproject == src2.nodata, 1000,
|
|
|
|
out_image_aspect_reproject)
|
2018-12-09 14:05:41 +00:00
|
|
|
out_image_aspect_reproject = out_image_aspect_reproject.astype(int)
|
|
|
|
for i in range(0, out_image.shape[1]):
|
|
|
|
for j in range(0, out_image.shape[2]):
|
|
|
|
aspect = out_image_aspect_reproject[0, i, j]
|
|
|
|
slope = out_image_slope_reproject[0, i, j]
|
|
|
|
print(aspect, slope, out_image[0, i, j])
|
|
|
|
if out_image[0, i, j] > 0:
|
|
|
|
if slope <= 0:
|
|
|
|
print('slope')
|
|
|
|
if aspect < 0:
|
|
|
|
res_flat += 1
|
|
|
|
elif slope < 30:
|
|
|
|
res_30 += 1
|
|
|
|
else:
|
|
|
|
res_more_than_30 += 1
|
|
|
|
print(res_flat, res_30, res_more_than_30)
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
return (res_flat, res_30, res_more_than_30)
|
2018-12-10 17:45:22 +00:00
|
|
|
|
2018-12-24 15:36:52 +00:00
|
|
|
|
2018-12-10 17:45:22 +00:00
|
|
|
def merge_gdf_geo_to_geojson(gdf):
|
2018-12-24 15:36:52 +00:00
|
|
|
return gdf.to_json()
|
2018-12-26 14:19:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-12-26 14:59:59 +00:00
|
|
|
# cut_dir_by_shp('/home/g214/data_from_chenhao/data_analyse/station/interpolation/rain/', '/home/g214/data_from_chenhao/data_analyse/boundary/bj_region/bj_region.shp')
|
2018-12-29 11:41:27 +00:00
|
|
|
cut_dir_by_shp('/home/g214/data_from_chenhao/data_analyse/station/interpolation/temp/',
|
|
|
|
'/home/g214/data_from_chenhao/data_analyse/boundary/bj_region/bj_region.shp')
|
|
|
|
# cut_dir_by_shp('../data/', '../bj_region/bj_region.shp')
|