-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShapefile add overstory data
64 lines (55 loc) · 2.46 KB
/
Shapefile add overstory data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
from shapely.geometry import Point, Polygon, MultiPolygon, mapping
import geopandas as gpd
import pandas as pd
%matplotlib inline
# need %matplotlib inline to view graphs
#import shape files and data file
# when using geopandas.read_file(r"") use only '/' in the pathway instead of '//'
hg_pl77=gpd.read_file(r"WS77_Hugo_Inventory_Plots_2013.shp")
hg_pl80=gpd.read_file(r"WS80_Hugo_Inventory_Plots_2013.shp")
llp=gpd.read_file(r"LLPC_corrected.shp")
OverstoryData=gpd.read_file(r"OverstoryData.csv", header = 0,
dtype={'ws': 'str', 'plot':'str','year':'datetime64[ns]',
'tree':'str', 'history':'category',
'species':'category', 'sp_cd':'category',
'dbh_avg':np.float64, 'tr_ht_avg':np.float64,
'BA_m2_ha':np.float64, 'BM_Mg_ha': np.float64
}
)
OverstoryData.round(2) #round to 2 decimals
#drop second set of geometry points and only use 'geometry_x'
llp=llp.drop('POINT_X', axis=1)
llp=llp.drop('POINT_Y', axis=1)
#set column datatypes as strings
hg_pl77['NAME']=hg_pl77['NAME'].astype('str')
hg_pl80['NAME']=hg_pl80['NAME'].astype('str')
llp['plot_num']=llp['plot_num'].astype('str')
# add common column in all files
hg_pl77['plotNum']='77_'+hg_pl77.NAME
hg_pl80['plotNum']='80_'+hg_pl80.NAME
llp['plotNum']='77_' +llp.plot_num
#use that column to merge the three files
geoWS1=pd.concat([hg_pl77, hg_pl80, llp])
#create "plotNum" column in data file
OverstoryData['plotNum']=OverstoryData['ws']+'_'+OverstoryData['plot']
# use new column to merge data with shapefile
geoWS=geoWS1.merge(OverstoryData, on = 'plotNum')
#drop unnecessary columns
geoWS=geoWS.drop({'geometry_y', 'plot_num', 'NAME', 'OBJECTID', 'field_1'}, axis=1)
# select column to be used as polygon
geoWS=gpd.GeoDataFrame(geoWS, geometry='geometry_x')
geoWS=geoWS.rename_geometry('geometry')
#set datatypes so that duplicates are able to be dropped
geoWS['year']=geoWS['year'].astype('str')
geoWS['ws']=geoWS['ws'].astype('str')
geoWS['plot']=geoWS['plot'].astype('str')
geoWS['history']=geoWS['history'].astype('str')
geoWS['tree']=geoWS['tree'].astype('str')
geoWS.drop_duplicates(subset=['year', 'ws', 'plot', 'history', 'tree'], inplace=True, keep='last')
#export files
geoWS1.to_csv("geoWS1.shp")
geoWS.to_file("WSdataShape.shp")
geoWS.to_csv("WSgeoData.csv")
#plot map for viewing
geoWS.plot()