durhamTreesPlantingSitesDataset / durhamtreesplantingsitesdataset.py
Ziyuan111's picture
Upload durhamtreesplantingsitesdataset.py
46e5b29 verified
# -*- coding: utf-8 -*-
"""durhamTreesPlantingSitesDataset
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Hvt3Y131OjTl7oGQGS55S_v7-aYu1Yj8
"""
from datasets import DatasetBuilder, DownloadManager, DatasetInfo, SplitGenerator, Split
from datasets.features import Features, Value, ClassLabel
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import csv
import json
import os
from typing import List
import datasets
import logging
logging.basicConfig(level=logging.INFO)
class durhamTreesPlantingSitesDataset(DatasetBuilder):
VERSION = "1.0.0"
_URLS = {
"csv": "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy",
"geojson": "https://drive.google.com/uc?export=download&id=1jpFVanNGy7L5tVO-Z_nltbBXKvrcAoDo"
}
def _info(self):
# Specifies the dataset's features
return DatasetInfo(
description="This dataset contains information about tree planting sites from CSV and GeoJSON files.",
features=Features({
"geometry": Value("string"), # Geometry feature, usually spatial data (GeoJSON format)
"OBJECTID": Value("int64"), # Unique identifier for each record
"streetaddress": Value("string"), # Street address of the tree planting site
"city": Value("string"), # City where the tree planting site is located
"zipcode": Value("float64"), # Zip code of the tree planting site (as float due to your data types)
"facilityid": Value("int64"), # Identifier for the facility
"present": Value("string"), # Presence status, assumed to be string
"genus": Value("string"), # Genus of the tree
"species": Value("string"), # Species of the tree
"commonname": Value("string"), # Common name of the tree
"plantingdate": Value("string"), # Planting date of the tree (consider converting to "datetime" for actual dates)
"diameterin": Value("float64"), # Diameter in inches
"heightft": Value("string"), # Height in feet (as a string, consider "float64" for numerical values)
"condition": Value("string"), # Condition of the tree
"contractwork": Value("string"), # Contract work information
"neighborhood": Value("string"), # Neighborhood where the tree is located
"program": Value("string"), # Program under which the tree was planted
"plantingw": Value("string"), # Width available for planting
"plantingcond": Value("string"), # Planting condition
"underpwerlins": Value("string"), # Whether the tree is under power lines
"GlobalID": Value("string"), # Global identifier
"created_user": Value("string"), # User who created the record
"created_date": Value("string"), # Date when the record was created
"last_edited_user": Value("string"), # User who last edited the record
"last_edited_date": Value("string"), # Date when the record was last edited
"isoprene": Value("float64"), # Isoprene emission rate
"monoterpene": Value("float64"), # Monoterpene emission rate
"vocs": Value("float64"), # Volatile Organic Compounds removed or emitted
"coremoved_ozperyr": Value("float64"), # Carbon monoxide removed, in ounces per year
"coremoved_dolperyr": Value("float64"), # Monetary value of carbon monoxide removal per year
"o3removed_ozperyr": Value("float64"), # Ozone removed, in ounces per year
"o3removed_dolperyr": Value("float64"), # Monetary value of ozone removal per year
"no2removed_ozperyr": Value("float64"), # Nitrogen dioxide removed, in ounces per year
"no2removed_dolperyr": Value("float64"), # Monetary value of nitrogen dioxide removal per year
"so2removed_ozperyr": Value("float64"), # Sulfur dioxide removed, in ounces per year
"so2removed_dolperyr": Value("float64"), # Monetary value of sulfur dioxide removal per year
"pm10removed_ozperyr": Value("float64"), # Particulate matter (10 micrometers or less) removed, in ounces per year
"pm10removed_dolperyr": Value("float64"), # Monetary value of particulate matter removal per year
"pm25removed_ozperyr": Value("float64"), # Particulate matter (2.5 micrometers or less) removed, in ounces per year
"o2production_lbperyr": Value("float64"), # Oxygen production, in pounds per year
"replacevalue_dol": Value("float64"), # Replacement value in dollars
"carbonstorage_lb": Value("float64"), # Carbon storage, in pounds
"carbonstorage_dol": Value("float64"), # Monetary value of carbon storage
"grosscarseq_lbperyr": Value("float64"), # Gross carbon sequestration, in pounds per year
"grosscarseq_dolperyr": Value("float64"), # Monetary value of gross carbon sequestration per year
"avoidrunoff_ft2peryr": Value("float64"), # Avoided runoff, in square feet per year
"avoidrunoff_dol2peryr": Value("float64"), # Monetary value of avoided runoff per year
"polremoved_ozperyr": Value("float64"), # Pollutants removed, in ounces per year
"polremoved_dolperyr": Value("float64"), # Monetary value of pollutant removal per year
"totannbenefits_dolperyr": Value("float64"), # Total annual benefits in dollars per year
"leafarea_sqft": Value("float64"), # Leaf area in square feet
"potevapotran_cuftperyr": Value("float64"), # Potential evapotranspiration, in cubic feet per year
"evaporation_cuftperyr": Value("float64"), # Evaporation, in cubic feet per year
"transpiration_cuftperyr": Value("float64"), # Transpiration, in cubic feet per year
"h2ointercept_cuftperyr": Value("float64"), # Water interception, in cubic feet per year
"avoidrunval_cuftperyr": Value("float64"), # Avoided runoff volume, in cubic feet per year
"avoidrunval_dol2peryr": Value("float64"), # Monetary value of avoided runoff volume per year
"carbonavoid_lbperyr": Value("float64"), # Avoided carbon emissions, in pounds per year
"carbonavoid_dolperyr": Value("float64"), # Monetary value of avoided carbon emissions per year
"heating_mbtuperyr": Value("float64"), # Heating energy saved, in MBTU per year
"heating_dolperyrmbtu": Value("float64"), # Monetary value of heating energy saved per MBTU per year
"heating_kwhperyr": Value("float64"), # Heating energy saved, in kWh per year
"heating_dolperyrmwh": Value("float64"), # Monetary value of heating energy saved per kWh per year
"cooling_kwhperyr": Value("float64"),
"X": Value("float64"), # X coordinate (longitude if geographic)
"Y": Value("float64"), # Y coordinate (latitude if geographic)
}),
supervised_keys=None,
homepage="https://github.com/AuraMa111?tab=repositories",
citation="Citation for the dataset",
)
def _split_generators(self, dl_manager: DownloadManager):
# Downloads the data and defines the splits
urls_to_download = self._URLS
downloaded_files = dl_manager.download_and_extract(urls_to_download)
return [
SplitGenerator(name=Split.TRAIN, gen_kwargs={
"csv_path": downloaded_files["csv"],
"geojson_path": downloaded_files["geojson"]
}),
# If you have additional splits, define them here
]
def _generate_examples(self, csv_path, geojson_path):
# Log the information about the CSV file being processed
logging.info("Generating examples from CSV = %s", csv_path)
# Load the CSV data into a pandas DataFrame
csv_data = pd.read_csv(csv_path)
# Log the information about the GeoJSON file being processed
logging.info("Generating examples from GeoJSON = %s", geojson_path)
# Load the GeoJSON data into a GeoDataFrame
geojson_data = gpd.read_file(geojson_path)
# Merge the CSV data with the GeoJSON data on the 'OBJECTID' column
merged_data = geojson_data.merge(csv_data, on='OBJECTID')
# Drop columns with suffix '_y' that might have been created during the merge
merged_data.drop(columns=[col for col in merged_data if col.endswith('_y')], inplace=True)
# Rename columns to remove suffix '_x'
merged_data.rename(columns=lambda x: x.rstrip('_x'), inplace=True)
# Select the desired columns
columns_to_extract = [ "geometry", # Geometry feature, usually spatial data (GeoJSON format)
"OBJECTID", # Unique identifier for each record
"streetaddress", # Street address of the tree planting site
"city", # City where the tree planting site is located
"zipcode", # Zip code of the tree planting site (as float due to your data types)
"facilityid", # Identifier for the facility
"present", # Presence status, assumed to be string
"genus", # Genus of the tree
"species", # Species of the tree
"commonname", # Common name of the tree
"plantingdate", # Planting date of the tree (consider converting to "datetime" for actual dates)
"diameterin", # Diameter in inches
"heightft", # Height in feet (as a string, consider "float64" for numerical values)
"condition", # Condition of the tree
"contractwork", # Contract work information
"neighborhood", # Neighborhood where the tree is located
"program", # Program under which the tree was planted
"plantingw", # Width available for planting
"plantingcond", # Planting condition
"underpwerlins", # Whether the tree is under power lines
"GlobalID", # Global identifier
"created_user", # User who created the record
"created_date", # Date when the record was created
"last_edited_user", # User who last edited the record
"last_edited_date", # Date when the record was last edited
"isoprene", # Isoprene emission rate
"monoterpene", # Monoterpene emission rate
"vocs", # Volatile Organic Compounds removed or emitted
"coremoved_ozperyr", # Carbon monoxide removed, in ounces per year
"coremoved_dolperyr", # Monetary value of carbon monoxide removal per year
"o3removed_ozperyr", # Ozone removed, in ounces per year
"o3removed_dolperyr", # Monetary value of ozone removal per year
"no2removed_ozperyr", # Nitrogen dioxide removed, in ounces per year
"no2removed_dolperyr", # Monetary value of nitrogen dioxide removal per year
"so2removed_ozperyr", # Sulfur dioxide removed, in ounces per year
"so2removed_dolperyr", # Monetary value of sulfur dioxide removal per year
"pm10removed_ozperyr", # Particulate matter (10 micrometers or less) removed, in ounces per year
"pm10removed_dolperyr", # Monetary value of particulate matter removal per year
"pm25removed_ozperyr", # Particulate matter (2.5 micrometers or less) removed, in ounces per year
"o2production_lbperyr", # Oxygen production, in pounds per year
"replacevalue_dol", # Replacement value in dollars
"carbonstorage_lb", # Carbon storage, in pounds
"carbonstorage_dol", # Monetary value of carbon storage
"grosscarseq_lbperyr", # Gross carbon sequestration, in pounds per year
"grosscarseq_dolperyr", # Monetary value of gross carbon sequestration per year
"avoidrunoff_ft2peryr", # Avoided runoff, in square feet per year
"avoidrunoff_dol2peryr", # Monetary value of avoided runoff per year
"polremoved_ozperyr", # Pollutants removed, in ounces per year
"polremoved_dolperyr", # Monetary value of pollutant removal per year
"totannbenefits_dolperyr", # Total annual benefits in dollars per year
"leafarea_sqft", # Leaf area in square feet
"potevapotran_cuftperyr", # Potential evapotranspiration, in cubic feet per year
"evaporation_cuftperyr", # Evaporation, in cubic feet per year
"transpiration_cuftperyr", # Transpiration, in cubic feet per year
"h2ointercept_cuftperyr", # Water interception, in cubic feet per year
"avoidrunval_cuftperyr", # Avoided runoff volume, in cubic feet per year
"avoidrunval_dol2peryr", # Monetary value of avoided runoff volume per year
"carbonavoid_lbperyr", # Avoided carbon emissions, in pounds per year
"carbonavoid_dolperyr", # Monetary value of avoided carbon emissions per year
"heating_mbtuperyr", # Heating energy saved, in MBTU per year
"heating_dolperyrmbtu", # Monetary value of heating energy saved per MBTU per year
"heating_kwhperyr", # Heating energy saved, in kWh per year
"heating_dolperyrmwh", # Monetary value of heating energy saved per kWh per year
"cooling_kwhperyr",
"X", # X coordinate (longitude if geographic)
"Y"]
# Create the final DataFrame with the selected columns
df = merged_data[columns_to_extract]
# Iterate over each row in the DataFrame and yield it
for index, row in df.iterrows():
# Convert the row to a dictionary, it's more convenient for yielding
yield index, row.to_dict()