Ziyuan111 commited on
Commit
130fd97
·
verified ·
1 Parent(s): c9cdec5

Upload treesplantingsitesdataset.py

Browse files
Files changed (1) hide show
  1. treesplantingsitesdataset.py +108 -0
treesplantingsitesdataset.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """TreesPlantingSitesDataset
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1Hvt3Y131OjTl7oGQGS55S_v7-aYu1Yj8
8
+ """
9
+
10
+ from datasets import DatasetBuilder, DownloadManager, DatasetInfo, SplitGenerator, Split
11
+ from datasets.features import Features, Value, ClassLabel
12
+ import pandas as pd
13
+ import geopandas as gpd
14
+ import matplotlib.pyplot as plt
15
+
16
+ class TreesPlantingSitesDataset(DatasetBuilder):
17
+ VERSION = "1.0.0"
18
+
19
+ def _info(self):
20
+ # Specifies the dataset's features
21
+ return DatasetInfo(
22
+ description="This dataset contains information about tree planting sites from CSV and GeoJSON files.",
23
+ features=Features({
24
+ "OBJECTID": Value("int32"), # Unique identifier for each record
25
+ "streetaddress": Value("string"), # Street address of the tree planting site
26
+ "city": Value("string"), # City where the tree planting site is located
27
+ "zipcode": Value("int32"), # Zip code of the tree planting site
28
+ "facilityid": Value("int32"), # Identifier for the facility
29
+ "neighborhood": Value("string"), # Neighborhood where the tree planting site is located
30
+ "plantingwidth": Value("string"), # Width available for planting
31
+ "plantingcondition": Value("string"), # Condition of the planting site
32
+ "matureheight": Value("string"), # Expected mature height of the tree
33
+ "GlobalID": Value("string"), # Global unique identifier
34
+ "created_user": Value("string"), # User who created the record
35
+ "created_date": Value("string"), # Date when the record was created
36
+ "last_edited_user": Value("string"), # User who last edited the record
37
+ "last_edited_date": Value("string"), # Date when the record was last edited
38
+ "geometry": Value("string") # Geometry feature from GeoJSON
39
+ }),
40
+ supervised_keys=None,
41
+ homepage="https://github.com/AuraMa111?tab=repositories",
42
+ citation="Citation for the dataset",
43
+ )
44
+
45
+ def _split_generators(self, dl_manager: DownloadManager):
46
+ # Downloads the data and defines the splits
47
+ urls_to_download = {
48
+ "csv": "https://drive.google.com/uc?export=download&id=18HmgMbtbntWsvAySoZr4nV1KNu-i7GCy",
49
+ "geojson": "https://drive.google.com/uc?export=download&id=1jpFVanNGy7L5tVO-Z_nltbBXKvrcAoDo"
50
+ }
51
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
52
+
53
+ return [
54
+ SplitGenerator(name=Split.TRAIN, gen_kwargs={
55
+ "csv_path": downloaded_files["csv"],
56
+ "geojson_path": downloaded_files["geojson"]
57
+ }),
58
+ # If you have additional splits, define them here
59
+ ]
60
+
61
+ def _generate_examples(self, csv_path, geojson_path):
62
+ # Load the data into DataFrame and GeoDataFrame
63
+ csv_data = pd.read_csv(csv_path)
64
+ geojson_data = gpd.read_file(geojson_path)
65
+
66
+ # Merge the CSV data with the GeoJSON data on the 'OBJECTID' column
67
+ gdf = geojson_data.merge(csv_data, on='OBJECTID')
68
+ columns_to_extract = [
69
+ "OBJECTID", "streetaddress", "city", "zipcode", "facilityid", "present",
70
+ "neighborhood", "plantingwidth", "plantingcondition", "underpowerlines",
71
+ "matureheight", "GlobalID", "created_user", "created_date",
72
+ "last_edited_user", "last_edited_date", "geometry"
73
+ ]
74
+
75
+ # Extract the specified columns
76
+ extracted_gdf = gdf[columns_to_extract]
77
+ # Basic statistics: Count the number of planting sites
78
+ number_of_planting_sites = gdf['present'].value_counts()
79
+ print("Number of planting sites:", number_of_planting_sites)
80
+
81
+ # Spatial analysis: Group by neighborhood to see the distribution of features
82
+ neighborhood_analysis = gdf.groupby('neighborhood').size()
83
+ print("Distribution by neighborhood:", neighborhood_analysis)
84
+
85
+ # Visual analysis: Plot the points on a map
86
+ gdf.plot(marker='*', color='green', markersize=5)
87
+ plt.title('TreesPlantingSitesDataset')
88
+ plt.show() # Make sure to display the plot if running in a script
89
+
90
+ # Iterate over the rows in the GeoDataFrame and yield examples
91
+ for id_, row in extracted_gdf.iterrows():
92
+ yield id_, {
93
+ "OBJECTID": row["OBJECTID"],
94
+ "streetaddress": row["streetaddress"],
95
+ "city": row["city"],
96
+ "zipcode": row["zipcode"],
97
+ "facilityid": row["facilityid"],
98
+ "neighborhood": row["neighborhood"],
99
+ "plantingwidth": row["plantingwidth"],
100
+ "plantingcondition": row["plantingcondition"],
101
+ "matureheight": row["matureheight"],
102
+ "GlobalID": row["GlobalID"],
103
+ "created_user": row["created_user"],
104
+ "created_date": row["created_date"],
105
+ "last_edited_user": row["last_edited_user"],
106
+ "last_edited_date": row["last_edited_date"],
107
+ "geometry": row["geometry"].wkt if row["geometry"] else None # Ensure geometry is in Well-Known Text (WKT) format or handled as desired
108
+ }