#Urban Forestry Biomass in Portland (a data Visualization example)
#波特兰的城市林业生物质(数据可视化示例)
import os
os.getcwd()
#import warnings
#warnings.filterwarnings('ignore')
from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import haversine as hs
# USDA forest service data and tools
# https://www.fia.fs.usda.gov/tools-data/index.php
# Urban Datamart
# https://experience.arcgis.com/experience/3641cea45d614ab88791aef54f3a1849/page/Urban-Datamart/
df_plots = pd.read_csv('ID_PLOT.csv',header=0, na_filter = False)
#from datetime import datetime
#dateparse = lambda dates: [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in dates]
#df = pd.read_csv('test.dat', parse_dates=['datetime'], date_parser=dateparse)
#distance bewteen coordinates loc1=(28.426846,77.088834) loc2=(28.394231,77.050308) hs.haversine(loc1,loc2)
portlandOR = (45.5152, -122.6784)
df_plots['distance'] = df_plots.apply(lambda x: hs.haversine((x['lat'], x['lon']), portlandOR), axis=1)
df_portland_plots = df_plots[df_plots['distance'] < 13]
df_portland_plots = df_portland_plots[['plotid','lat','lon']]
df_portland_plots.head()
plotid | lat | lon | |
---|---|---|---|
187 | 2928128 | 45.547943 | -122.647210 |
188 | 2928176 | 45.507767 | -122.639613 |
189 | 2928154 | 45.567521 | -122.690310 |
190 | 2928232 | 45.460718 | -122.677429 |
191 | 2928144 | 45.541062 | -122.692661 |
df_trees = pd.read_csv('ID_TREE.csv',header=0, na_filter = False, low_memory=False)
df_trees['bg_carbon'] = pd.to_numeric(df_trees['bg_carbon'])
df_trees['ag_biomass'] = pd.to_numeric(df_trees['ag_biomass'])
df_portland_trees = df_trees[df_trees['plotid'].isin(df_portland_plots['plotid'])]
df_portland_trees.head()
index | plotid | visit_nbr | statecd | unitcd | countycd | retired_plot | subp | condid | tree | ... | bg_carbon | cn | plt_cn | sbp_cn | cnd_cn | mtre_cn | prev_plt_cn | prev_tre_cn | tree_site_index | ag_biomass | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1000 | 1000 | 2928572 | 1 | 41 | 0 | 51 | 88093 | 1 | 1 | 4 | ... | 51.497621 | 707747966126144 | 495034990126144 | 707747944126144 | 707747943126144 | 707747960126144.0 | 529.001813 | |||
1001 | 1001 | 2928575 | 1 | 41 | 0 | 51 | 95783 | 1 | 1 | 12 | ... | 45.984293 | 707748877126144 | 495034992126144 | 707748821126144 | 707748820126144 | 707748845126144.0 | 471.172744 | |||
1002 | 1002 | 2928575 | 1 | 41 | 0 | 51 | 95783 | 1 | 1 | 11 | ... | 37.200024 | 707748876126144 | 495034992126144 | 707748821126144 | 707748820126144 | 707748844126144.0 | 380.063648 | |||
1003 | 1003 | 2928575 | 1 | 41 | 0 | 51 | 95783 | 1 | 1 | 18 | ... | 25.322044 | 707748883126144 | 495034992126144 | 707748821126144 | 707748820126144 | 707748851126144.0 | 256.072548 | |||
1004 | 1004 | 2928575 | 1 | 41 | 0 | 51 | 95783 | 1 | 1 | 22 | ... | 19.942075 | 707748887126144 | 495034992126144 | 707748821126144 | 707748820126144 | 707748855126144.0 | 200.422168 |
5 rows × 96 columns
df_portland_plot_trees = df_portland_trees.groupby('plotid').aggregate({'index':'count', 'bg_carbon':'sum','ag_biomass':'sum'})
df_portland_plot_trees = pd.merge( df_portland_plot_trees, df_portland_plots, on='plotid', how='left')
df_portland_plot_trees = df_portland_plot_trees.rename(columns={"index": "count", "bg_carbon": "carbon", "ag_biomass": "biomass"})
#df_portland_plot_trees
import json
json_object = df_portland_plot_trees.to_json(orient='records')
with open("portland_plot_trees.json", "w") as outfile:
outfile.write(json_object)