In [2]:
#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
In [3]:
# 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)
In [4]:
#distance bewteen coordinates loc1=(28.426846,77.088834) loc2=(28.394231,77.050308) hs.haversine(loc1,loc2)
portlandOR = (45.5152, -122.6784)
In [5]:
df_plots['distance'] = df_plots.apply(lambda x: hs.haversine((x['lat'], x['lon']), portlandOR), axis=1)
In [6]:
df_portland_plots = df_plots[df_plots['distance'] < 13]
In [7]:
df_portland_plots = df_portland_plots[['plotid','lat','lon']]
df_portland_plots.head()
Out[7]:
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
In [8]:
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']) 
In [9]:
df_portland_trees = df_trees[df_trees['plotid'].isin(df_portland_plots['plotid'])]
In [10]:
df_portland_trees.head()
Out[10]:
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

In [11]:
 df_portland_plot_trees = df_portland_trees.groupby('plotid').aggregate({'index':'count', 'bg_carbon':'sum','ag_biomass':'sum'})
In [12]:
df_portland_plot_trees  = pd.merge( df_portland_plot_trees, df_portland_plots, on='plotid', how='left')
In [13]:
df_portland_plot_trees = df_portland_plot_trees.rename(columns={"index": "count", "bg_carbon": "carbon", "ag_biomass": "biomass"})
In [14]:
#df_portland_plot_trees
In [15]:
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)