Delete urban_tree_canopy_in_durham2.py
Browse files- urban_tree_canopy_in_durham2.py +0 -301
urban_tree_canopy_in_durham2.py
DELETED
@@ -1,301 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""Urban_Tree_Canopy_in_Durham2
|
3 |
-
|
4 |
-
Automatically generated by Colaboratory.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1X59zPtI7ydiX10ZnfjsNGvnKNTXgwrWs
|
8 |
-
"""
|
9 |
-
|
10 |
-
! pip install datasets
|
11 |
-
import csv
|
12 |
-
import json
|
13 |
-
import os
|
14 |
-
from typing import List
|
15 |
-
import datasets
|
16 |
-
import logging
|
17 |
-
from datasets import DatasetBuilder, DownloadManager, SplitGenerator, Split
|
18 |
-
|
19 |
-
import zipfile
|
20 |
-
import json
|
21 |
-
import pandas as pd
|
22 |
-
import geopandas as gpd
|
23 |
-
|
24 |
-
import os
|
25 |
-
import pandas as pd
|
26 |
-
import geopandas as gpd
|
27 |
-
import zipfile
|
28 |
-
import tempfile
|
29 |
-
import shutil
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
class Urban_Tree_Canopy_in_Durham2(DatasetBuilder):
|
34 |
-
# Define the `_info` method, which provides dataset metadata
|
35 |
-
def _info(self):
|
36 |
-
return DatasetInfo(
|
37 |
-
description="A description of the dataset.",
|
38 |
-
features=Features(
|
39 |
-
{
|
40 |
-
"objectid": Value("int32"),
|
41 |
-
"streetaddr": Value("string"),
|
42 |
-
"city_x": Value("string"),
|
43 |
-
"zipcode_x": Value("string"),
|
44 |
-
"facilityid_x": Value("string"),
|
45 |
-
"present_x": Value("string"),
|
46 |
-
"genus_x": Value("string"),
|
47 |
-
"species_x": Value("string"),
|
48 |
-
"commonname_x": Value("string"),
|
49 |
-
"plantingda": Value("datetime"),
|
50 |
-
"diameterin_x": Value("float"),
|
51 |
-
"heightft_x": Value("float"),
|
52 |
-
"condition_x": Value("string"),
|
53 |
-
"contractwo": Value("string"),
|
54 |
-
"neighborho": Value("string"),
|
55 |
-
"program_x": Value("string"),
|
56 |
-
"plantingw_x": Value("string"),
|
57 |
-
"plantingco": Value("string"),
|
58 |
-
"underpwerl": Value("string"),
|
59 |
-
"matureheig": Value("float"),
|
60 |
-
"globalid_x": Value("string"),
|
61 |
-
"created_us": Value("string"),
|
62 |
-
"created_da": Value("datetime"),
|
63 |
-
"last_edite": Value("string"),
|
64 |
-
"last_edi_1": Value("datetime"),
|
65 |
-
"isoprene_x": Value("float"),
|
66 |
-
"monoterpen": Value("float"),
|
67 |
-
"vocs_x": Value("float"),
|
68 |
-
"coremoved_": Value("float"),
|
69 |
-
"coremove_1": Value("float"),
|
70 |
-
"o3removed_": Value("float"),
|
71 |
-
"o3remove_1": Value("float"),
|
72 |
-
"no2removed": Value("float"),
|
73 |
-
"no2remov_1": Value("float"),
|
74 |
-
"so2removed": Value("float"),
|
75 |
-
"so2remov_1": Value("float"),
|
76 |
-
"pm10remove": Value("float"),
|
77 |
-
"pm10remo_1": Value("float"),
|
78 |
-
"pm25remove": Value("float"),
|
79 |
-
"o2producti": Value("float"),
|
80 |
-
"replaceval": Value("float"),
|
81 |
-
"carbonstor": Value("float"),
|
82 |
-
"carbonst_1": Value("float"),
|
83 |
-
"grosscarse": Value("float"),
|
84 |
-
"grosscar_1": Value("float"),
|
85 |
-
"avoidrunof": Value("float"),
|
86 |
-
"avoidrun_1": Value("float"),
|
87 |
-
"polremoved": Value("float"),
|
88 |
-
"polremov_1": Value("float"),
|
89 |
-
"totannbene": Value("float"),
|
90 |
-
"leafarea_s": Value("float"),
|
91 |
-
"potevapotr": Value("float"),
|
92 |
-
"evaporatio": Value("float"),
|
93 |
-
"transpirat": Value("float"),
|
94 |
-
"h2ointerce": Value("float"),
|
95 |
-
"avoidrunva": Value("float"),
|
96 |
-
"avoidrun_2": Value("float"),
|
97 |
-
"carbonavoi": Value("float"),
|
98 |
-
"carbonav_1": Value("float"),
|
99 |
-
"heating_mb": Value("float"),
|
100 |
-
"heating_do": Value("float"),
|
101 |
-
"heating_kw": Value("float"),
|
102 |
-
"heating__1": Value("float"),
|
103 |
-
"cooling_kw": Value("float"),
|
104 |
-
"cooling_do": Value("float"),
|
105 |
-
"totalenerg": Value("float"),
|
106 |
-
"geometry_x": Value("string"),
|
107 |
-
"x": Value("float"),
|
108 |
-
"y": Value("float"),
|
109 |
-
"streetaddress_x": Value("string"),
|
110 |
-
"city_y": Value("string"),
|
111 |
-
"zipcode_y": Value("string"),
|
112 |
-
"facilityid_y": Value("string"),
|
113 |
-
"present_y": Value("string"),
|
114 |
-
"genus_y": Value("string"),
|
115 |
-
"species_y": Value("string"),
|
116 |
-
"commonname_y": Value("string"),
|
117 |
-
"plantingdate_x": Value("datetime"),
|
118 |
-
"diameterin_y": Value("float"),
|
119 |
-
"heightft_y": Value("float"),
|
120 |
-
"condition_y": Value("string"),
|
121 |
-
"contractwork_x": Value("string"),
|
122 |
-
"neighborhood_x": Value("string"),
|
123 |
-
"program_y": Value("string"),
|
124 |
-
"plantingw_y": Value("string"),
|
125 |
-
"plantingcond_x": Value("string"),
|
126 |
-
"underpwerlins_x": Value("string"),
|
127 |
-
"matureheight_x": Value("float"),
|
128 |
-
"globalid_y": Value("string"),
|
129 |
-
"created_user_x": Value("string"),
|
130 |
-
"created_date_x": Value("datetime"),
|
131 |
-
"last_edited_user_x": Value("string"),
|
132 |
-
"last_edited_date_x": Value("datetime"),
|
133 |
-
"isoprene_y": Value("float"),
|
134 |
-
"monoterpene_x": Value("float"),
|
135 |
-
"vocs_y": Value("float"),
|
136 |
-
"coremoved_ozperyr_x": Value("float"),
|
137 |
-
"coremoved_dolperyr_x": Value("float"),
|
138 |
-
"o3removed_ozperyr_x": Value("float"),
|
139 |
-
"o3removed_dolperyr_x": Value("float"),
|
140 |
-
"no2removed_ozperyr_x": Value("float"),
|
141 |
-
"no2removed_dolperyr_x": Value("float"),
|
142 |
-
"so2removed_ozperyr_x": Value("float"),
|
143 |
-
"so2removed_dolperyr_x": Value("float"),
|
144 |
-
"pm10removed_dolperyr_y":Value("float"),
|
145 |
-
"pm25removed_ozperyr_y":Value("float"),
|
146 |
-
"o2production_lbperyr_y":Value("float"),
|
147 |
-
"replacevalue_dol_y":Value("float"),
|
148 |
-
"carbonstorage_lb_y":Value("float"),
|
149 |
-
"carbonstorage_dol_y":Value("float"),
|
150 |
-
"grosscarseq_lbperyr_y":Value("float"),
|
151 |
-
"grosscarseq_dolperyr_y":Value("float"),
|
152 |
-
"avoidrunoff_ft2peryr":Value("float"),
|
153 |
-
|
154 |
-
}
|
155 |
-
),
|
156 |
-
supervised_keys=None,
|
157 |
-
homepage="https://github.com/AuraMa111/Urban_Tree_Canopy_in_Durham",
|
158 |
-
citation="A citation or reference to the source of the dataset.",
|
159 |
-
)
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
165 |
-
"""Returns SplitGenerators."""
|
166 |
-
|
167 |
-
# Download files and get their local file paths
|
168 |
-
csv_path = dl_manager.download("https://huggingface.co/datasets/Ziyuan111/Urban_Tree_Canopy_in_Durham2/raw/main/Trees_%2526_Planting_Sites.csv")
|
169 |
-
zip_path = dl_manager.download("https://huggingface.co/datasets/Ziyuan111/Urban_Tree_Canopy_in_Durham2/raw/main/TreesPlanting_Sites.zip")
|
170 |
-
geojson_path = dl_manager.download("https://huggingface.co/datasets/Ziyuan111/Urban_Tree_Canopy_in_Durham2/raw/main/Trees_%2526_Planting_Sites.geojson")
|
171 |
-
|
172 |
-
return [
|
173 |
-
# Define a SplitGenerator for the CSV file
|
174 |
-
datasets.SplitGenerator(
|
175 |
-
name=datasets.Split.TRAIN,
|
176 |
-
gen_kwargs={
|
177 |
-
"filepath": csv_path,
|
178 |
-
"split": "csv",
|
179 |
-
},
|
180 |
-
),
|
181 |
-
# Define a SplitGenerator for the ZIP file
|
182 |
-
datasets.SplitGenerator(
|
183 |
-
name=datasets.Split.VALIDATION,
|
184 |
-
gen_kwargs={
|
185 |
-
"filepath": zip_path,
|
186 |
-
"split": "zip",
|
187 |
-
},
|
188 |
-
),
|
189 |
-
# Define a SplitGenerator for the GEOJSON file
|
190 |
-
datasets.SplitGenerator(
|
191 |
-
name=datasets.Split.TEST,
|
192 |
-
gen_kwargs={
|
193 |
-
"filepath": geojson_path,
|
194 |
-
"split": "geojson",
|
195 |
-
},
|
196 |
-
),
|
197 |
-
]
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
def _generate_examples(self, filepaths, split):
|
202 |
-
"""Yields examples as (key, example) tuples."""
|
203 |
-
|
204 |
-
# Assuming filepaths is a dictionary like:
|
205 |
-
# {'csv': 'path_to_csv', 'zip': 'path_to_zip', 'geojson': 'path_to_geojson'}
|
206 |
-
csv_df = pd.read_csv(filepaths['csv'])
|
207 |
-
csv_df.columns = csv_df.columns.str.lower().str.replace(' ', '_')
|
208 |
-
csv_df['objectid'] = csv_df['objectid'].astype(int)
|
209 |
-
|
210 |
-
with zipfile.ZipFile(filepaths['zip'], 'r') as z:
|
211 |
-
temp_dir = tempfile.mkdtemp()
|
212 |
-
z.extractall(path=temp_dir)
|
213 |
-
shapefile_path = next((os.path.join(temp_dir, name) for name in z.namelist() if name.endswith('.shp')), None)
|
214 |
-
if shapefile_path is None:
|
215 |
-
raise FileNotFoundError("No shapefile found in the ZIP archive.")
|
216 |
-
shp_gdf = gpd.read_file(shapefile_path)
|
217 |
-
shp_gdf.columns = shp_gdf.columns.str.lower().str.replace(' ', '_')
|
218 |
-
shp_gdf['objectid'] = shp_gdf['objectid'].astype(int)
|
219 |
-
shutil.rmtree(temp_dir)
|
220 |
-
|
221 |
-
geojson_gdf = gpd.read_file(filepaths['geojson'])
|
222 |
-
geojson_gdf.columns = geojson_gdf.columns.str.lower().str.replace(' ', '_')
|
223 |
-
geojson_gdf['objectid'] = geojson_gdf['objectid'].astype(int)
|
224 |
-
|
225 |
-
# Merge the dataframes on 'objectid'
|
226 |
-
combined_gdf = shp_gdf.merge(csv_df, on='objectid', how='inner')
|
227 |
-
combined_gdf = combined_gdf.merge(geojson_gdf, on='objectid', how='left')
|
228 |
-
|
229 |
-
# Yield the combined data
|
230 |
-
for idx, row in combined_gdf.iterrows():
|
231 |
-
# Yield each row as an example, using the index as the key
|
232 |
-
yield idx, row.to_dict()
|
233 |
-
|
234 |
-
|
235 |
-
def plot_spatial_distribution(self, gdf, lat_col='latitude', lon_col='longitude', color_col='species', hover_col='species'):
|
236 |
-
"""
|
237 |
-
Visualize the spatial distribution of the data using Plotly.
|
238 |
-
|
239 |
-
Parameters:
|
240 |
-
- gdf: GeoDataFrame to be visualized.
|
241 |
-
- lat_col: String, name of the column with latitude values.
|
242 |
-
- lon_col: String, name of the column with longitude values.
|
243 |
-
- color_col: String, name of the column to determine the color of points.
|
244 |
-
- hover_col: String, name of the column to show when hovering over points.
|
245 |
-
"""
|
246 |
-
center_lat = gdf[lat_col].mean()
|
247 |
-
center_lon = gdf[lon_col].mean()
|
248 |
-
|
249 |
-
fig = px.scatter_mapbox(gdf,
|
250 |
-
lat=lat_col,
|
251 |
-
lon=lon_col,
|
252 |
-
color=color_col,
|
253 |
-
hover_name=hover_col,
|
254 |
-
center={"lat": center_lat, "lon": center_lon},
|
255 |
-
zoom=10,
|
256 |
-
height=600,
|
257 |
-
width=800)
|
258 |
-
|
259 |
-
fig.update_layout(mapbox_style="open-street-map")
|
260 |
-
fig.show()
|
261 |
-
def plot_correlation_heatmap(self, gdf, columns, figsize=(10, 8), cmap='coolwarm'):
|
262 |
-
"""
|
263 |
-
Plot a heatmap of the correlation matrix for selected columns in the GeoDataFrame.
|
264 |
-
|
265 |
-
Parameters:
|
266 |
-
- gdf: GeoDataFrame containing the data.
|
267 |
-
- columns: List of columns to include in the correlation matrix.
|
268 |
-
- figsize: Tuple of figure size dimensions (width, height).
|
269 |
-
- cmap: Colormap for the heatmap.
|
270 |
-
"""
|
271 |
-
# Select only the columns with environmental data
|
272 |
-
env_data = gdf[columns]
|
273 |
-
|
274 |
-
# Compute the correlation matrix
|
275 |
-
corr = env_data.corr()
|
276 |
-
|
277 |
-
# Set up the matplotlib figure
|
278 |
-
plt.figure(figsize=figsize)
|
279 |
-
|
280 |
-
# Generate a heatmap
|
281 |
-
sns.heatmap(corr, annot=True, fmt=".2f", cmap=cmap, square=True, linewidths=.5, cbar_kws={"shrink": .5})
|
282 |
-
|
283 |
-
# Optional: Adjust the layout
|
284 |
-
plt.tight_layout()
|
285 |
-
|
286 |
-
# Show the plot
|
287 |
-
plt.show()
|
288 |
-
def load_dataset():
|
289 |
-
builder = UrbanTreeCanopyInDurham2()
|
290 |
-
return builder.as_dataset()
|
291 |
-
# Usage example:
|
292 |
-
# data_processor = DataProcessor()
|
293 |
-
# for key, example in data_processor._generate_examples(csv_path, zip_path, geojson_zip_path):
|
294 |
-
# # Do something with key and example
|
295 |
-
# Usage example:
|
296 |
-
# data_processor = DataProcessor()
|
297 |
-
# for key, example in data_processor._generate_examples(csv_path, zip_path, geojson_zip_path):
|
298 |
-
# # Do something with key and example
|
299 |
-
# combined_gdf = data_processor.merge_dataframes(csv_df, shp_gdf, geojson_gdf)
|
300 |
-
# data_processor.plot_spatial_distribution(combined_gdf, lat_col='y', lon_col='x', color_col='species_x', hover_col='species_x')
|
301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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