TreesPlantingSitesDataset / treesplantingsitesdataset.py
Ziyuan111's picture
Upload treesplantingsitesdataset.py
130fd97 verified
# -*- coding: utf-8 -*-
"""TreesPlantingSitesDataset
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
class TreesPlantingSitesDataset(DatasetBuilder):
VERSION = "1.0.0"
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({
"OBJECTID": Value("int32"), # 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("int32"), # Zip code of the tree planting site
"facilityid": Value("int32"), # Identifier for the facility
"neighborhood": Value("string"), # Neighborhood where the tree planting site is located
"plantingwidth": Value("string"), # Width available for planting
"plantingcondition": Value("string"), # Condition of the planting site
"matureheight": Value("string"), # Expected mature height of the tree
"GlobalID": Value("string"), # Global unique 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
"geometry": Value("string") # Geometry feature from GeoJSON
}),
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 = {
"csv": "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy",
"geojson": "https://drive.google.com/uc?export=download&id=1jpFVanNGy7L5tVO-Z_nltbBXKvrcAoDo"
}
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):
# Load the data into DataFrame and GeoDataFrame
csv_data = pd.read_csv(csv_path)
geojson_data = gpd.read_file(geojson_path)
# Merge the CSV data with the GeoJSON data on the 'OBJECTID' column
gdf = geojson_data.merge(csv_data, on='OBJECTID')
columns_to_extract = [
"OBJECTID", "streetaddress", "city", "zipcode", "facilityid", "present",
"neighborhood", "plantingwidth", "plantingcondition", "underpowerlines",
"matureheight", "GlobalID", "created_user", "created_date",
"last_edited_user", "last_edited_date", "geometry"
]
# Extract the specified columns
extracted_gdf = gdf[columns_to_extract]
# Basic statistics: Count the number of planting sites
number_of_planting_sites = gdf['present'].value_counts()
print("Number of planting sites:", number_of_planting_sites)
# Spatial analysis: Group by neighborhood to see the distribution of features
neighborhood_analysis = gdf.groupby('neighborhood').size()
print("Distribution by neighborhood:", neighborhood_analysis)
# Visual analysis: Plot the points on a map
gdf.plot(marker='*', color='green', markersize=5)
plt.title('TreesPlantingSitesDataset')
plt.show() # Make sure to display the plot if running in a script
# Iterate over the rows in the GeoDataFrame and yield examples
for id_, row in extracted_gdf.iterrows():
yield id_, {
"OBJECTID": row["OBJECTID"],
"streetaddress": row["streetaddress"],
"city": row["city"],
"zipcode": row["zipcode"],
"facilityid": row["facilityid"],
"neighborhood": row["neighborhood"],
"plantingwidth": row["plantingwidth"],
"plantingcondition": row["plantingcondition"],
"matureheight": row["matureheight"],
"GlobalID": row["GlobalID"],
"created_user": row["created_user"],
"created_date": row["created_date"],
"last_edited_user": row["last_edited_user"],
"last_edited_date": row["last_edited_date"],
"geometry": row["geometry"].wkt if row["geometry"] else None # Ensure geometry is in Well-Known Text (WKT) format or handled as desired
}