Upload urbantreecanopyindurham2dataset.py
Browse files
urbantreecanopyindurham2dataset.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""UrbanTreeCanopyInDurham2Dataset
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1X59zPtI7ydiX10ZnfjsNGvnKNTXgwrWs
|
8 |
+
"""
|
9 |
+
|
10 |
+
from datasets import DatasetInfo, Features, Value, load_dataset, BuilderConfig, DatasetBuilder
|
11 |
+
import csv
|
12 |
+
import json
|
13 |
+
import os
|
14 |
+
from typing import List
|
15 |
+
import datasets
|
16 |
+
import logging
|
17 |
+
from datasets import DownloadManager, SplitGenerator, Split
|
18 |
+
import zipfile
|
19 |
+
import pandas as pd
|
20 |
+
import geopandas as gpd
|
21 |
+
import tempfile
|
22 |
+
import shutil
|
23 |
+
import plotly.express as px
|
24 |
+
from datasets import GeneratorBasedBuilder
|
25 |
+
|
26 |
+
class UrbanTreeCanopyInDurham2Dataset(GeneratorBasedBuilder):
|
27 |
+
|
28 |
+
def _info(self):
|
29 |
+
return DatasetInfo(
|
30 |
+
description="Urban_Tree_Canopy_in_Durham2",
|
31 |
+
features=Features(
|
32 |
+
{
|
33 |
+
"objectid": Value("int32"),
|
34 |
+
"streetaddr": Value("string"),
|
35 |
+
"city_x": Value("string"),
|
36 |
+
"zipcode_x": Value("string"),
|
37 |
+
"species_x": Value("string"),
|
38 |
+
"commonname_x": Value("string"),
|
39 |
+
"plantingda": datasets.Value("timestamp[us]"),
|
40 |
+
"diameterin_x": Value("float"),
|
41 |
+
"heightft_x": Value("float"),
|
42 |
+
"condition_x": Value("string"),
|
43 |
+
"program_x": Value("string"),
|
44 |
+
"matureheig": Value("float"),
|
45 |
+
"created_da": datasets.Value("timestamp[us]"),
|
46 |
+
"last_edi_1": datasets.Value("timestamp[us]"),
|
47 |
+
"geometry_x": Value("string"),
|
48 |
+
"x": Value("float"),
|
49 |
+
"y": Value("float"),
|
50 |
+
"coremoved_": Value("float"),
|
51 |
+
"coremove_1": Value("float"),
|
52 |
+
"o3removed_": Value("float"),
|
53 |
+
"o3remove_1": Value("float"),
|
54 |
+
"no2removed": Value("float"),
|
55 |
+
"no2remov_1": Value("float"),
|
56 |
+
"so2removed": Value("float"),
|
57 |
+
"so2remov_1": Value("float"),
|
58 |
+
"pm10remove": Value("float"),
|
59 |
+
"pm10remo_1": Value("float"),
|
60 |
+
"pm25remove": Value("float"),
|
61 |
+
"o2producti": Value("float"),
|
62 |
+
}
|
63 |
+
),
|
64 |
+
supervised_keys=None,
|
65 |
+
homepage="https://github.com/AuraMa111/Urban_Tree_Canopy_in_Durham",
|
66 |
+
citation="A citation or reference to the source of the dataset.",
|
67 |
+
)
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
def _split_generators(self, dl_manager):
|
72 |
+
csv_url = "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy"
|
73 |
+
geojson_url = "https://drive.google.com/uc?export=download&id=1jpFVanNGy7L5tVO-Z_nltbBXKvrcAoDo"
|
74 |
+
|
75 |
+
# Extract the file ID from the SHP Google Drive sharing URL and construct a direct download link
|
76 |
+
shp_file_id = "1DYcug0xiWYlsKZorbbEcrjZWEAB0y4MY"
|
77 |
+
shp_url = f"https://drive.google.com/uc?export=download&id={shp_file_id}"
|
78 |
+
|
79 |
+
# Use dl_manager to download the files
|
80 |
+
csv_path = dl_manager.download_and_extract(csv_url)
|
81 |
+
shp_path = dl_manager.download_and_extract(shp_url)
|
82 |
+
geojson_path = dl_manager.download_and_extract(geojson_url)
|
83 |
+
|
84 |
+
# Assuming the paths are directories, construct the full paths to the files
|
85 |
+
csv_file_path = os.path.join(csv_path, 'Trees_%26_Planting_Sites.csv')
|
86 |
+
shp_file_path = os.path.join(shp_path, 'GS_TreeInventory.shp') # Adjust if necessary
|
87 |
+
geojson_file_path = os.path.join(geojson_path, 'Trees_%26_Planting_Sites.geojson')
|
88 |
+
|
89 |
+
# Now you can return the paths for use in your data processing
|
90 |
+
return [
|
91 |
+
datasets.SplitGenerator(
|
92 |
+
name=datasets.Split.TRAIN,
|
93 |
+
gen_kwargs={
|
94 |
+
"csv_path": csv_file_path,
|
95 |
+
"shp_path": shp_file_path,
|
96 |
+
"geojson_path": geojson_file_path,
|
97 |
+
},
|
98 |
+
),
|
99 |
+
]
|
100 |
+
|
101 |
+
def _generate_examples(self, csv_path, shp_path, geojson_path):
|
102 |
+
"""Yields examples as (key, example) tuples."""
|
103 |
+
|
104 |
+
# Load the datasets
|
105 |
+
csv_df = pd.read_csv(csv_path)
|
106 |
+
shp_gdf = gpd.read_file(shp_path)
|
107 |
+
with open(geojson_path, 'r') as f:
|
108 |
+
geojson_data = json.load(f)
|
109 |
+
geojson_gdf = gpd.GeoDataFrame.from_features(geojson_data["features"])
|
110 |
+
|
111 |
+
# Standardize column names
|
112 |
+
csv_df.columns = csv_df.columns.str.lower().str.replace(' ', '_')
|
113 |
+
shp_gdf.columns = shp_gdf.columns.str.lower().str.replace(' ', '_')
|
114 |
+
geojson_gdf.columns = geojson_gdf.columns.str.lower().str.replace(' ', '_')
|
115 |
+
|
116 |
+
# Convert 'objectid' to int
|
117 |
+
csv_df['objectid'] = csv_df['objectid'].astype(int)
|
118 |
+
shp_gdf['objectid'] = shp_gdf['objectid'].astype(int)
|
119 |
+
geojson_gdf['objectid'] = geojson_gdf['objectid'].astype(int)
|
120 |
+
|
121 |
+
# Merge the dataframes on 'objectid'
|
122 |
+
combined_gdf = shp_gdf.merge(csv_df, on='objectid', how='inner')
|
123 |
+
combined_gdf = combined_gdf.merge(geojson_gdf, on='objectid', how='inner')
|
124 |
+
combined_gdf=combined_gdf[["objectid", "streetaddr", "city_x", "zipcode_x",
|
125 |
+
"species_x", "commonname_x", "plantingda", "diameterin_x",
|
126 |
+
"heightft_x", "condition_x", "program_x", "matureheig",
|
127 |
+
"created_da", "last_edi_1", "geometry_x",
|
128 |
+
"x", "y",
|
129 |
+
"coremoved_", "coremove_1",
|
130 |
+
"o3removed_", "o3remove_1",
|
131 |
+
"no2removed", "no2remov_1",
|
132 |
+
"so2removed", "so2remov_1",
|
133 |
+
"pm10remove", "pm10remo_1",
|
134 |
+
"pm25remove", "o2producti",
|
135 |
+
]]
|
136 |
+
|
137 |
+
# Yield the combined data
|
138 |
+
for idx, row in combined_gdf.iterrows():
|
139 |
+
# Yield each row as an example, using the index as the key
|
140 |
+
yield idx, row.to_dict()
|
141 |
+
|
142 |
+
@staticmethod
|
143 |
+
def plot_spatial_distribution(combined_gdf, lat_col='x', lon_col='y', color_col='species_x', hover_col='species_x'):
|
144 |
+
# Calculate the mean latitude and longitude for the center of the map
|
145 |
+
center_lat = combined_gdf[lat_col].mean()
|
146 |
+
center_lon = combined_gdf[lon_col].mean()
|
147 |
+
|
148 |
+
# Create a scatter mapbox plot
|
149 |
+
fig = px.scatter_mapbox(combined_gdf,
|
150 |
+
lat=lat_col,
|
151 |
+
lon=lon_col,
|
152 |
+
color=color_col,
|
153 |
+
hover_name=hover_col,
|
154 |
+
center={"lat": center_lat, "lon": center_lon},
|
155 |
+
zoom=10,
|
156 |
+
height=600,
|
157 |
+
width=800)
|
158 |
+
|
159 |
+
# Set the mapbox style to "open-street-map"
|
160 |
+
fig.update_layout(mapbox_style="open-street-map")
|
161 |
+
|
162 |
+
# Display the figure
|
163 |
+
fig.show()
|
164 |
+
|