In [19]:
import geopandas as gp
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sb
from shapely.geometry import Polygon
import os
plt.rcParams['figure.figsize'] = (10, 20)
%matplotlib inline
In [20]:
base_file = "../data/AvalDet_20190424_155109_ref_20190313_trno_087_VV/AvalDet_20190424_155109_ref_20190313_trno_087_VV.shp"
gdf = gp.read_file(base_file)
gdf.drop(index=1, inplace=True)
gdf.head()
Out[20]:
In [21]:
gdf.columns
Out[21]:
The following columns are used in the import routine
geometry -> SHAPE
dem_min -> hoydeStoppSkred_moh
Important: skredID needs to be the same in all tables.
In [22]:
import_list = ['time', 'area', 'dem_min', 'asp_median', 'slp_mean', 'slp_max', 'slp_min', 'geometry']
In [23]:
# Add a name to the polygon
gdf['_name'] = "Original"
# convert t_0 (the time the reference image was taken) into a datetime object and give it a descriptive name
gdf['_reference_date'] = pd.to_datetime(gdf['t_0']) # this actually overwrites the existing column "refdate", but since it is a duplicate of t_0 we don't care.
# convert t_1 (the time the activity image was taken) into a datetime object and give it a descriptive name
gdf['_detection_date'] = pd.to_datetime(gdf['t_1'])
In [24]:
aval_map = gdf.plot(column="area", linewidth=0.3, edgecolor='black', cmap="OrRd", alpha=0.9)
In [25]:
plg = gdf['geometry'][0]
print(type(plg), plg)
In [26]:
# Define the base coordinates for the test polygons
e_base = 20.94
n_base= 69.71
scn = []
In [ ]:
le = e_base + 0.005 # left_easting
re = le + 0.002 # right_easting
bn = n_base + 0.005 # bottom_northing
tn = bn + 0.002 # top_northing
p1 = Polygon([(le, bn), (re, bn), (re, tn), (le, tn)])
# Create a copy of the original polygon and alter its properties
scn.append(gdf.copy(deep=True))
scn[0]['_name'] = "Scenario 1"
scn[0]['geometry'] = p1
print(scn[0].crs)
scn[0].to_crs({'init': 'epsg:32633'}, inplace=True)
print(scn[0].crs)
scn[0]['area'] = scn[0]['geometry'].area
scn[0].to_crs(gdf.crs, inplace=True)
scn[0]['dem_min'] = 800
scn[0]['asp_median'] = 180
scn[0]['slp_mean'] = 9.0
scn[0]['slp_max'] = 12.0
scn[0]['slp_min'] = 4.0
scn[0]['time'] = '2019-04-27T15:51:09.025897'
scn[0]['_dis_id'] = 1 # used only to merge (dissolve) polygons for verification
In [28]:
scn[0].filter(import_list).head()
Out[28]:
In [29]:
le = e_base + 0.006 # left_easting
re = le + 0.002 # right_easting
bn = n_base + 0.004 # bottom_northing
tn = bn + 0.002 # top_northing
p2 = Polygon([(le, bn), (re, bn), (re, tn), (le, tn)])
# Create a copy of the original polygon and alter its properties
scn.append(gdf.copy(deep=True))
scn[1]['_name'] = "Scenario 2"
scn[1]['geometry'] = p2
scn[1].to_crs({'init': 'epsg:32633'}, inplace=True)
scn[1]['area'] = scn[1]['geometry'].area
scn[1].to_crs(gdf.crs, inplace=True)
scn[1]['dem_min'] = 820
scn[1]['asp_median'] = 200
scn[1]['slp_mean'] = 10.0
scn[1]['slp_max'] = 15.0
scn[1]['slp_min'] = 6.0
scn[1]['time'] = '2019-04-28T15:51:09.025897'
scn[1]['_dis_id'] = 1 # used only to merge (dissolve) polygons for verification
In [30]:
le = e_base + 0.0055 # left_easting
re = le + 0.002 # right_easting
bn = n_base + 0.0055 # bottom_northing
tn = bn + 0.002 # top_northing
p3 = Polygon([(le, bn), (re, bn), (re, tn), (le, tn)])
# Create a copy of the original polygon and alter its properties
scn.append(gdf.copy(deep=True))
scn[2]['_name'] = "Scenario 3"
scn[2]['geometry'] = p3
scn[2].to_crs({'init': 'epsg:32633'}, inplace=True)
scn[2]['area'] = scn[2]['geometry'].area
scn[2].to_crs(gdf.crs, inplace=True)
scn[2]['dem_min'] = 850
scn[2]['asp_median'] = 205
scn[2]['slp_mean'] = 9.0
scn[2]['slp_max'] = 13.0
scn[2]['slp_min'] = 5.0
scn[2]['time'] = '2019-05-12T15:51:09.025897'
scn[2]['_dis_id'] = 1 # used only to merge (dissolve) polygons for verification
In [31]:
le = e_base + 0.007 # left_easting
re = le + 0.003 # right_easting
bn = n_base + 0.008 # bottom_northing
tn = bn + 0.003 # top_northing
p4 = Polygon([(le, bn), (re, bn), (re, tn), (le, tn)])
# Create a copy of the original polygon and alter its properties
scn.append(gdf.copy(deep=True))
scn[3]['_name'] = "Scenario 4"
scn[3]['geometry'] = p4
scn[3].to_crs({'init': 'epsg:32633'}, inplace=True)
scn[3]['area'] = scn[3]['geometry'].area
scn[3].to_crs(gdf.crs, inplace=True)
scn[3]['dem_min'] = 820
scn[3]['asp_median'] = 105
scn[3]['slp_mean'] = 3.0
scn[3]['slp_max'] = 8.0
scn[3]['slp_min'] = 5.2
scn[3]['time'] = '2019-04-28T15:51:09.025897'
scn[3]['_dis_id'] = 1 # used only to merge (dissolve) polygons for verification
In [32]:
le = e_base + 0.006 # left_easting
re = le + 0.002 # right_easting
bn = n_base + 0.006 # bottom_northing
tn = bn + 0.005 # top_northing
p5 = Polygon([(le, bn), (re, bn), (re, tn), (le, tn)])
# Create a copy of the original polygon and alter its properties
scn.append(gdf.copy(deep=True))
scn[4]['_name'] = "Scenario 5"
scn[4]['geometry'] = p5
scn[4].to_crs({'init': 'epsg:32633'}, inplace=True)
scn[4]['area'] = scn[4]['geometry'].area
scn[4].to_crs(gdf.crs, inplace=True)
scn[4]['dem_min'] = 820
scn[4]['asp_median'] = 105
scn[4]['slp_mean'] = 3.0
scn[4]['slp_max'] = 8.0
scn[4]['slp_min'] = 5.2
scn[4]['time'] = '2019-04-28T15:51:09.025897'
scn[4]['_dis_id'] = 1 # used only to merge (dissolve) polygons for verification
In [33]:
ax = gdf.plot()
scn[0].plot(ax=ax, edgecolor='black', alpha=0.6)
scn[1].plot(ax=ax, edgecolor='black', alpha=0.6)
scn[2].plot(ax=ax, edgecolor='black', alpha=0.6)
scn[3].plot(ax=ax, edgecolor='black', alpha=0.6)
scn[4].plot(ax=ax, edgecolor='black', alpha=0.6)
Out[33]:
In [34]:
for df in [gdf, scn[0], scn[1]]:
print(df['_name'], df['area'], df.crs)
In [35]:
test_scns = pd.concat([scn[0], scn[1], scn[2], scn[3], scn[4]])
new_dir = '../data/scns'
if not os.path.exists(new_dir):
os.mkdir(new_dir)
test_scns.drop(['_detection_date', '_reference_date', '_dis_id'], axis=1).to_file(filename='../data/scns/test_scns.shp')
dissolved_scns = test_scns.dissolve(by='_dis_id')
In [36]:
for i in range(5):
new_dir = '../data/scn_{0}'.format(i)
if not os.path.exists(new_dir):
os.mkdir(new_dir)
scn[i].drop(['_detection_date', '_reference_date', '_dis_id'], axis=1).to_file(filename='../data/scn_{0}/scn_{0}.shp'.format(i, i))