"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"To authorize access needed by Earth Engine, open the following\n",
" URL in a web browser and follow the instructions:
\n",
" https://code.earthengine.google.com/client-auth?scopes=https%3A//www.googleapis.com/auth/earthengine%20https%3A//www.googleapis.com/auth/devstorage.full_control&request_id=N2k51_3KlWyDMzkGlo643Wk_l0IPnklpaQQOSvYk2dQ&tc=crQOWG4pmg47yxmWUnwVu6HXophHiEjRwbnTQQ8fEgE&cc=MhT2NNOp4YX4THyajW6RiFrlrPJkepnEE-K22cmgVAc
\n",
" The authorization workflow will generate a code, which you should paste in the box below.
\n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter verification code: 4/1AfJohXm1Q2OIsWUh6yS1wa_hkd8jaAgs6fcbsbITq1gIO8I-MQlMQQxggm4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Successfully saved authorization token.\n"
]
}
],
"source": [
"# To use geemap or any other Google Earth Engine related features in python, we need to authenticate it first.\n",
"ee.Authenticate()\n",
"ee.Initialize()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5c0f2c43-70d8-4c75-bab9-832e2ed236ae",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/f489c3d72f8b8dfc015d2f92cf761e13-32b9f58921f6c3423ee48e00c134c595:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2013.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/2e8be02da7ace0babafc4ba8e401b948-024582f8415b405ee6f0f9fcff801e36:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2014.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/697c714f320af33b69e0845ac86f3979-5670d67253ad2922f84e041647aa7d23:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2015.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/49e98b868aea51e09ce733a10eeed15e-af5ce036a3f13b7ffd1b21789ce448c7:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2016.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/f648eb54c0b08a6bdd28bd5de34acc96-f8234e08706bf6b632df7533fe9ff835:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2017.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/250dd7414e06dfb8942eebaae02f2e1f-048196115b2dc375bc0fc93b4a98a1f1:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2018.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/2530255c343bca01e9428b3dc63540f6-e3b2549321a076a78e9192c0218ce230:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2019.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/1afd37d2086bdd71529dd1f8cf9c69d5-9a5acf177d8273c3d694da4dbfd2dbc3:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2020.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/16dc6d65b11ed8921f44bfa66bad2ed7-7694bfd845a38b8a00330fd01c7e107f:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2021.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/509a30d7dff25f669c7c7e7137600adc-0c7b5d220d35ce5aeeea0e19f17a5114:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2022.csv\n",
"Computing ... \n",
"Generating URL ...\n",
"Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/tables/6ec96da8c6f72926f3f48298cceb938e-1d05331e4887ae790f8176420c51c8be:getFeatures\n",
"Please wait ...\n",
"Data downloaded to /home/jovyan/work/GGIS407_23Fall/Project/cdl_stats_2023.csv\n"
]
}
],
"source": [
"for YEAR in range(2013, 2024):\n",
" \n",
" # Specify the buffer storage path \n",
" output_buffer = os.path.normpath(f\"{buffer_dir}/sampled_buffer_{YEAR}.shp\") \n",
" \n",
" # Convert our shapefile to ee (Earth Engine) objects\n",
" buffer = geemap.shp_to_ee(output_buffer)\n",
" \n",
" # Note that since the 2023 CDL have not be published yet, we use the 2022 CDL as a proxy\n",
" if YEAR == 2023:\n",
" dataset = ee.Image(\"USDA/NASS/CDL/\" + str(2022))\n",
" else:\n",
" dataset = ee.Image(\"USDA/NASS/CDL/\" + str(YEAR))\n",
" \n",
" # Select the corresponding band in CDL dataset for crop type information\n",
" cropland = ee.Image(dataset.select('cropland'))\n",
" \n",
" # Set up the path for CDL statistics output\n",
" cdl_stats = os.path.normpath(f'{current_directory}/cdl_stats_{YEAR}.csv')\n",
" \n",
" # Use zonal_statistics_by_group to calculate the area ratio of each crop type within each buffer\n",
" # statistics_type can be either 'SUM' or 'PERCENTAGE'\n",
" # denominator can be used to convert square meters to other areal units, such as square kilometers\n",
" geemap.zonal_statistics_by_group(\n",
" cropland.clip(buffer),\n",
" buffer,\n",
" cdl_stats,\n",
" statistics_type='PERCENTAGE',\n",
" #denominator=1000000,\n",
" #decimal_places=2,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "897d7b4a-eb0f-4c00-a004-dd84daba41e4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Use the glob module to get a list of CSV file paths in a directory.\n",
"file_paths = glob.glob('cdl_stats_*')\n",
"\n",
"# Create an empty list to store DataFrames, one for each CSV file\n",
"dataframes = []\n",
"\n",
"# Read each CSV file into a DataFrame and append it to the list of DataFrames\n",
"for file_path in file_paths:\n",
" df = pd.read_csv(file_path)\n",
" dataframes.append(df)\n",
"\n",
"# Concatenate the DataFrames vertically to merge them while retaining all columns\n",
"merged_df = pd.concat(dataframes, ignore_index=True)\n",
"\n",
"# Fill missing values (empty entries) with 0\n",
"merged_df.fillna(0, inplace=True)\n",
"\n",
"# Replace 'column_to_remove' with the name of the column you want to remove\n",
"column_to_remove = 'system:index'\n",
"if column_to_remove in merged_df.columns:\n",
" merged_df.drop(columns=[column_to_remove], inplace=True)\n",
"column_to_remove = 'Class_sum'\n",
"if column_to_remove in merged_df.columns:\n",
" merged_df.drop(columns=[column_to_remove], inplace=True)\n",
"\n",
"# Save the merged DataFrame to a new CSV file if needed\n",
"merged_df.to_csv('merged_file.csv', index=False)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "023a6639-a503-46d8-ac95-fd585cbb338e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Index(['Class_61', 'Class_190', 'Class_195', 'Class_141', 'Class_152',\n",
" 'Class_21', 'Class_123', 'Class_36', 'Class_131', 'Class_142',\n",
" 'Class_121', 'Class_176', 'Class_124', 'Class_1', 'Class_3', 'Class_5',\n",
" 'id', 'year', 'Class_111', 'Class_122', 'Class_24', 'Class_37',\n",
" 'Class_212', 'Class_2', 'Class_4', 'Class_76', 'Class_33', 'Class_23',\n",
" 'Class_112', 'Class_143', 'Class_6', 'Class_41', 'Class_42', 'Class_69',\n",
" 'Class_75', 'Class_205', 'Class_28', 'Class_216', 'Class_59',\n",
" 'Class_217'],\n",
" dtype='object')"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"merged_df.columns"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "12742314-3ec3-4aee-b368-26cc62676d5a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Load the CSV file containing the crop mapping\n",
"codebook_df = pd.read_csv('CDL_crop_codebook.csv')\n",
"\n",
"# Create a custom mapping dictionary from the codebook DataFrame\n",
"custom_mapping = dict(zip(codebook_df['Codes'], codebook_df['Class_Names']))\n",
"\n",
"# Iterate through the DataFrame's column names\n",
"for col_name in merged_df.columns:\n",
" if (col_name != 'id') & (col_name != 'year'):\n",
"\n",
" # Extract the numerical part from the column name\n",
" num = int(col_name.split('_')[1])\n",
"\n",
" # Look up the corresponding crop name from the custom mapping\n",
" if num in custom_mapping:\n",
" crop_name = custom_mapping[num]\n",
" merged_df.rename(columns={col_name: crop_name}, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "8bb5c1db-2f7c-4299-bf18-c7fa4cda29bb",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Index(['Fallow/Idle Cropland', 'Woody Wetlands', 'Herbaceous Wetlands',\n",
" 'Deciduous Forest', 'Shrubland', 'Barley', 'Developed/Med Intensity',\n",
" 'Alfalfa', 'Barren', 'Evergreen Forest', 'Developed/Open Space',\n",
" 'Grass/Pasture', 'Developed/High Intensity', 'Corn', 'Rice', 'Soybeans',\n",
" 'id', 'year', 'Open Water', 'Developed/Low Intensity', 'Winter Wheat',\n",
" 'Other Hay/Non Alfalfa', 'Oranges', 'Cotton', 'Sorghum', 'Walnuts',\n",
" 'Safflower', 'Spring Wheat', 'Perennial Ice/Snow ', 'Mixed Forest',\n",
" 'Sunflower', 'Sugarbeets', 'Dry Beans', 'Grapes', 'Almonds',\n",
" 'Triticale', 'Oats', 'Peppers', 'Sod/Grass Seed', 'Pomegranates'],\n",
" dtype='object')"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"merged_df.columns"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "c3c5b738-9fab-4f94-b3f3-657865910678",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Fallow/Idle Cropland | \n",
" Woody Wetlands | \n",
" Herbaceous Wetlands | \n",
" Deciduous Forest | \n",
" Shrubland | \n",
" Barley | \n",
" Developed/Med Intensity | \n",
" Alfalfa | \n",
" Barren | \n",
" Evergreen Forest | \n",
" ... | \n",
" Sunflower | \n",
" Sugarbeets | \n",
" Dry Beans | \n",
" Grapes | \n",
" Almonds | \n",
" Triticale | \n",
" Oats | \n",
" Peppers | \n",
" Sod/Grass Seed | \n",
" Pomegranates | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.0 | \n",
" 0.851855 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.0 | \n",
" 1.000000 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.0 | \n",
" 0.000000 | \n",
" 1.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 3 | \n",
" 0.0 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 4 | \n",
" 1.0 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 963 | \n",
" 0.0 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 1.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 964 | \n",
" 0.0 | \n",
" 0.942866 | \n",
" 0.057134 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 965 | \n",
" 0.0 | \n",
" 0.000000 | \n",
" 0.236111 | \n",
" 0.0 | \n",
" 0.25 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 966 | \n",
" 0.0 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 967 | \n",
" 0.0 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 0.00 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
968 rows × 40 columns
\n",
"
"
],
"text/plain": [
" Fallow/Idle Cropland Woody Wetlands Herbaceous Wetlands \\\n",
"0 0.0 0.851855 0.000000 \n",
"1 0.0 1.000000 0.000000 \n",
"2 0.0 0.000000 1.000000 \n",
"3 0.0 0.000000 0.000000 \n",
"4 1.0 0.000000 0.000000 \n",
".. ... ... ... \n",
"963 0.0 0.000000 0.000000 \n",
"964 0.0 0.942866 0.057134 \n",
"965 0.0 0.000000 0.236111 \n",
"966 0.0 0.000000 0.000000 \n",
"967 0.0 0.000000 0.000000 \n",
"\n",
" Deciduous Forest Shrubland Barley Developed/Med Intensity Alfalfa \\\n",
"0 0.0 0.00 0.0 0.0 0.0 \n",
"1 0.0 0.00 0.0 0.0 0.0 \n",
"2 0.0 0.00 0.0 0.0 0.0 \n",
"3 0.0 0.00 0.0 0.0 0.0 \n",
"4 0.0 0.00 0.0 0.0 0.0 \n",
".. ... ... ... ... ... \n",
"963 0.0 1.00 0.0 0.0 0.0 \n",
"964 0.0 0.00 0.0 0.0 0.0 \n",
"965 0.0 0.25 0.0 0.0 0.0 \n",
"966 0.0 0.00 0.0 0.0 0.0 \n",
"967 0.0 0.00 0.0 0.0 0.0 \n",
"\n",
" Barren Evergreen Forest ... Sunflower Sugarbeets Dry Beans Grapes \\\n",
"0 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"1 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"2 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"3 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"4 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
".. ... ... ... ... ... ... ... \n",
"963 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"964 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"965 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"966 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"967 0.0 0.0 ... 0.0 0.0 0.0 0.0 \n",
"\n",
" Almonds Triticale Oats Peppers Sod/Grass Seed Pomegranates \n",
"0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 0.0 0.0 0.0 \n",
".. ... ... ... ... ... ... \n",
"963 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"964 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"965 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"966 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"967 0.0 0.0 0.0 0.0 0.0 0.0 \n",
"\n",
"[968 rows x 40 columns]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"merged_df"
]
},
{
"cell_type": "markdown",
"id": "be7ccfcf-69ad-4a33-9961-a7f2842e8c0b",
"metadata": {},
"source": [
"### Example: Getting all Points with >50% Corn"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d5f198c5-3ccb-4da7-b4c4-dfd90b8bbf49",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"We got 24 points with larger than 50% of its 100-meter buffer covered by corn in our sample!!!\n"
]
}
],
"source": [
"corn_df = merged_df[merged_df['Corn'] > 0.5]\n",
"print(f\"We got {len(corn_df)} points with larger than 50% of its 100-meter buffer covered by corn in our sample!!!\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "169190b8-57ec-4dbf-98db-e34719b012af",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[4373626,\n",
" 10747177,\n",
" 17932911,\n",
" 146770529,\n",
" 147172132,\n",
" 151970324,\n",
" 180432061,\n",
" 144993828,\n",
" 142121284,\n",
" 119643906,\n",
" 109229187,\n",
" 113250058,\n",
" 141789316,\n",
" 21407838,\n",
" 8919418,\n",
" 69743841,\n",
" 103853866,\n",
" 68025771,\n",
" 103937493,\n",
" 82995323,\n",
" 73540094,\n",
" 41257012,\n",
" 43245038,\n",
" 44267370]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"corn_df['id'].tolist()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "5cfd8c9b-fcc0-45b0-ab7a-c6213aac8d73",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"joined_corn_df = pd.merge(corn_df['id'], filtered_df, on='id')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2514972e-89c4-4e70-bd0c-6b34b108c6b9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c97a80bb-62a8-40c1-baaf-2f8b4ecb5cdd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" observed_on | \n",
" latitude | \n",
" longitude | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 4373626 | \n",
" 2016-10-15 | \n",
" 38.147981 | \n",
" -121.457291 | \n",
"
\n",
" \n",
" 1 | \n",
" 10747177 | \n",
" 2018-04-08 | \n",
" 46.996799 | \n",
" -119.587029 | \n",
"
\n",
" \n",
" 2 | \n",
" 17932911 | \n",
" 2018-10-28 | \n",
" 38.745567 | \n",
" -108.135105 | \n",
"
\n",
" \n",
" 3 | \n",
" 146770529 | \n",
" 2023-01-17 | \n",
" 38.207037 | \n",
" -121.503203 | \n",
"
\n",
" \n",
" 4 | \n",
" 147172132 | \n",
" 2023-01-21 | \n",
" 38.206757 | \n",
" -121.504154 | \n",
"
\n",
" \n",
" 5 | \n",
" 151970324 | \n",
" 2023-03-21 | \n",
" 43.036608 | \n",
" -112.481733 | \n",
"
\n",
" \n",
" 6 | \n",
" 180432061 | \n",
" 2023-08-27 | \n",
" 38.891274 | \n",
" -87.134136 | \n",
"
\n",
" \n",
" 7 | \n",
" 144993828 | \n",
" 2022-12-23 | \n",
" 38.197551 | \n",
" -121.501237 | \n",
"
\n",
" \n",
" 8 | \n",
" 142121284 | \n",
" 2022-11-13 | \n",
" 38.160591 | \n",
" -121.476371 | \n",
"
\n",
" \n",
" 9 | \n",
" 119643906 | \n",
" 2022-05-30 | \n",
" 40.221247 | \n",
" -111.707807 | \n",
"
\n",
" \n",
" 10 | \n",
" 109229187 | \n",
" 2022-03-15 | \n",
" 44.688756 | \n",
" -92.962979 | \n",
"
\n",
" \n",
" 11 | \n",
" 113250058 | \n",
" 2022-04-28 | \n",
" 44.688790 | \n",
" -92.965724 | \n",
"
\n",
" \n",
" 12 | \n",
" 141789316 | \n",
" 2022-11-11 | \n",
" 34.937956 | \n",
" -106.056497 | \n",
"
\n",
" \n",
" 13 | \n",
" 21407838 | \n",
" 2019-03-19 | \n",
" 42.457908 | \n",
" -89.118133 | \n",
"
\n",
" \n",
" 14 | \n",
" 8919418 | \n",
" 2017-05-08 | \n",
" 33.705039 | \n",
" -80.501501 | \n",
"
\n",
" \n",
" 15 | \n",
" 69743841 | \n",
" 2021-02-17 | \n",
" 38.212669 | \n",
" -121.501287 | \n",
"
\n",
" \n",
" 16 | \n",
" 103853866 | \n",
" 2021-12-28 | \n",
" 38.171917 | \n",
" -121.512593 | \n",
"
\n",
" \n",
" 17 | \n",
" 68025771 | \n",
" 2021-01-13 | \n",
" 38.164362 | \n",
" -121.516341 | \n",
"
\n",
" \n",
" 18 | \n",
" 103937493 | \n",
" 2021-11-25 | \n",
" 38.157489 | \n",
" -121.465637 | \n",
"
\n",
" \n",
" 19 | \n",
" 82995323 | \n",
" 2021-06-12 | \n",
" 45.280931 | \n",
" -92.814049 | \n",
"
\n",
" \n",
" 20 | \n",
" 73540094 | \n",
" 2021-04-10 | \n",
" 45.544775 | \n",
" -93.860925 | \n",
"
\n",
" \n",
" 21 | \n",
" 41257012 | \n",
" 2020-03-29 | \n",
" 43.756469 | \n",
" -116.987699 | \n",
"
\n",
" \n",
" 22 | \n",
" 43245038 | \n",
" 2020-04-16 | \n",
" 44.041486 | \n",
" -92.563167 | \n",
"
\n",
" \n",
" 23 | \n",
" 44267370 | \n",
" 2020-04-29 | \n",
" 41.083733 | \n",
" -96.245114 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" id observed_on latitude longitude\n",
"0 4373626 2016-10-15 38.147981 -121.457291\n",
"1 10747177 2018-04-08 46.996799 -119.587029\n",
"2 17932911 2018-10-28 38.745567 -108.135105\n",
"3 146770529 2023-01-17 38.207037 -121.503203\n",
"4 147172132 2023-01-21 38.206757 -121.504154\n",
"5 151970324 2023-03-21 43.036608 -112.481733\n",
"6 180432061 2023-08-27 38.891274 -87.134136\n",
"7 144993828 2022-12-23 38.197551 -121.501237\n",
"8 142121284 2022-11-13 38.160591 -121.476371\n",
"9 119643906 2022-05-30 40.221247 -111.707807\n",
"10 109229187 2022-03-15 44.688756 -92.962979\n",
"11 113250058 2022-04-28 44.688790 -92.965724\n",
"12 141789316 2022-11-11 34.937956 -106.056497\n",
"13 21407838 2019-03-19 42.457908 -89.118133\n",
"14 8919418 2017-05-08 33.705039 -80.501501\n",
"15 69743841 2021-02-17 38.212669 -121.501287\n",
"16 103853866 2021-12-28 38.171917 -121.512593\n",
"17 68025771 2021-01-13 38.164362 -121.516341\n",
"18 103937493 2021-11-25 38.157489 -121.465637\n",
"19 82995323 2021-06-12 45.280931 -92.814049\n",
"20 73540094 2021-04-10 45.544775 -93.860925\n",
"21 41257012 2020-03-29 43.756469 -116.987699\n",
"22 43245038 2020-04-16 44.041486 -92.563167\n",
"23 44267370 2020-04-29 41.083733 -96.245114"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"joined_corn_df[['id','observed_on','latitude','longitude']]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "fead6681-f25b-43fc-aae5-47d556655498",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Convert values from two columns to lists\n",
"lat_list = joined_corn_df['latitude'].values.tolist()\n",
"lon_list = joined_corn_df['longitude'].values.tolist()\n",
"List_coord = list(zip(lat_list, lon_list))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "3e760f10-4521-4fea-9c89-770a50dba912",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[(38.1479810716, -121.4572906494),\n",
" (46.9967990222, -119.5870287034),\n",
" (38.745567, -108.135105),\n",
" (38.2070368747, -121.5032030789),\n",
" (38.2067568135, -121.5041537504),\n",
" (43.0366083333, -112.4817333333),\n",
" (38.8912740294, -87.1341360773),\n",
" (38.1975511174, -121.5012371103),\n",
" (38.1605905419, -121.4763713378),\n",
" (40.2212469, -111.7078073),\n",
" (44.6887559587, -92.9629791467),\n",
" (44.6887903001, -92.9657239561),\n",
" (34.9379556365, -106.0564966812),\n",
" (42.4579083333, -89.1181333333),\n",
" (33.7050387545, -80.5015014287),\n",
" (38.2126693424, -121.5012868844),\n",
" (38.1719167, -121.51259327),\n",
" (38.1643617, -121.5163415),\n",
" (38.1574892772, -121.4656373089),\n",
" (45.280931488, -92.814049077),\n",
" (45.5447747988, -93.8609251621),\n",
" (43.7564685844, -116.9876991212),\n",
" (44.0414864328, -92.5631665438),\n",
" (41.0837329679, -96.2451140081)]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"List_coord"
]
},
{
"cell_type": "markdown",
"id": "6c508e1e-7b45-4eac-8128-6142ed28c6f9",
"metadata": {},
"source": [
"\n",
"## Google Street View Fetching"
]
},
{
"cell_type": "markdown",
"id": "98c54455-ec7f-4021-bc2b-22b10152cc3c",
"metadata": {},
"source": [
"### Function: downloadGSVMetaData(LIST_COORDINATES, TASK)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "006ef5a0-36f3-4ce2-96df-c08b3ee35925",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def downloadGSVMetaData(LIST_COORDINATES, TASK):\n",
"\n",
" \"\"\"\n",
" Downloads Google Street View metadata for a list of coordinates.\n",
"\n",
" Parameters:\n",
" - LIST_COORDINATES (list of tuples): A list of latitude and longitude coordinates to search for GSV imagery.\n",
" - TASK (str): A task identifier to be used in the generated metadata CSV file name.\n",
"\n",
" This function searches for GSV imagery around the provided coordinates and saves metadata to a CSV file.\n",
" \"\"\"\n",
" \n",
" # Generate the file path for the metadata CSV file\n",
" path = os.path.normpath(\"GSV_METADATA_{}.csv\".format(TASK))\n",
" \n",
" # Open the metadata CSV file for appending\n",
" metadata = open(path, \"a\")\n",
" \n",
" # Create a CSV writer object with newline terminator\n",
" writer = csv.writer(metadata, lineterminator='\\n')\n",
" \n",
" # Write the header row to the CSV file\n",
" writer.writerow(['ID', 'Panoid', 'Lat', 'Lon', 'Heading', 'Pitch', 'Roll', 'Year', 'Month']) \n",
" \n",
" # Initialize an index\n",
" index = 0\n",
" \n",
" # Iterate through the list of coordinates (LAT, LON)\n",
" for LAT, LON in LIST_COORDINATES:\n",
" try:\n",
" # Use the search_panoramas function to get panorama information\n",
" panoids = search_panoramas(lat=LAT, lon=LON)\n",
" except:\n",
" # Handle errors if the search_panoramas function fails\n",
" print(\"Index out of range error at lat={}, lon={}: no GSV imagery in this area.\".format(LAT, LON))\n",
" print(\"Searching GSV Images around...({}, {})\".format(LAT, LON))\n",
" if len(panoids)>0: \n",
" for id in range(len(panoids)):\n",
" if panoids[id].date:\n",
" year = int(panoids[id].date[:4])\n",
" month= int(panoids[id].date[5:])\n",
" index += 1\n",
" metadata = open(path, \"a\")\n",
" writer = csv.writer(metadata, lineterminator = '\\n')\n",
" writer.writerow([index, panoids[id].pano_id,panoids[id].lat,panoids[id].lon,panoids[id].heading,panoids[id].pitch, panoids[id].roll,year,month])\n",
" print(index) \n",
"\n",
" else:\n",
" print('no GSV imagery found in this area')\n",
" continue"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "d191f79c-3f32-49d6-994a-346a472fc4df",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Searching GSV Images around...(38.1479810716, -121.4572906494)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(46.9967990222, -119.5870287034)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.745567, -108.135105)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.2070368747, -121.5032030789)\n",
"Searching GSV Images around...(38.2067568135, -121.5041537504)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(43.0366083333, -112.4817333333)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.8912740294, -87.1341360773)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.1975511174, -121.5012371103)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.1605905419, -121.4763713378)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(40.2212469, -111.7078073)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(44.6887559587, -92.9629791467)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(44.6887903001, -92.9657239561)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(34.9379556365, -106.0564966812)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(42.4579083333, -89.1181333333)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(33.7050387545, -80.5015014287)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.2126693424, -121.5012868844)\n",
"Searching GSV Images around...(38.1719167, -121.51259327)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.1643617, -121.5163415)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(38.1574892772, -121.4656373089)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(45.280931488, -92.814049077)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(45.5447747988, -93.8609251621)\n",
"1\n",
"Searching GSV Images around...(43.7564685844, -116.9876991212)\n",
"Searching GSV Images around...(44.0414864328, -92.5631665438)\n",
"no GSV imagery found in this area\n",
"Searching GSV Images around...(41.0837329679, -96.2451140081)\n",
"no GSV imagery found in this area\n"
]
}
],
"source": [
"downloadGSVMetaData(List_coord, 'CORN')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7a71f389-ea12-49ed-91bb-844c042d79ee",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ID | \n",
" Panoid | \n",
" Lat | \n",
" Lon | \n",
" Heading | \n",
" Pitch | \n",
" Roll | \n",
" Year | \n",
" Month | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" ID | \n",
" Panoid | \n",
" Lat | \n",
" Lon | \n",
" Heading | \n",
" Pitch | \n",
" Roll | \n",
" Year | \n",
" Month | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" YdyDug5KipDP88_g3LlzcA | \n",
" 38.2613558375869 | \n",
" -121.4406002341907 | \n",
" 169.7516479492188 | \n",
" 93.66971588134766 | \n",
" 0.8723912239074707 | \n",
" 2007 | \n",
" 6 | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" zjOWG3903_sas4CstIlUWg | \n",
" 38.26135944130262 | \n",
" -121.440610609495 | \n",
" 169.9563446044922 | \n",
" 88.99956512451172 | \n",
" 1.432158350944519 | \n",
" 2015 | \n",
" 8 | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" 3kA8skjnYcwFXRB4WofR8w | \n",
" 38.26133969218689 | \n",
" -121.4406068604355 | \n",
" 169.4710083007812 | \n",
" 89.78001403808594 | \n",
" 1.364865064620972 | \n",
" 2016 | \n",
" 11 | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" 5lZ2CQ6r13vrIyEE1fB9CA | \n",
" 30.62476293717697 | \n",
" -97.39541297004071 | \n",
" 341.0656433105469 | \n",
" 94.39850616455078 | \n",
" 1.765115857124329 | \n",
" 2008 | \n",
" 2 | \n",
"
\n",
" \n",
" 5 | \n",
" 5 | \n",
" 4v3QNsEpA7dGAcmNV77-tg | \n",
" 30.62476069881489 | \n",
" -97.39543697001042 | \n",
" 156.5848693847656 | \n",
" 89.1475830078125 | \n",
" 0.5970472097396851 | \n",
" 2009 | \n",
" 5 | \n",
"
\n",
" \n",
" 6 | \n",
" 6 | \n",
" 62v-z86n9AVfzwO_a25BNg | \n",
" 30.62474089233968 | \n",
" -97.39541231024785 | \n",
" 339.2624206542969 | \n",
" 89.32353973388672 | \n",
" 4.39376974105835 | \n",
" 2011 | \n",
" 5 | \n",
"
\n",
" \n",
" 7 | \n",
" 7 | \n",
" FjH1CP_ZEqDyOVnTeVPKSg | \n",
" 30.62474800067029 | \n",
" -97.39540703190518 | \n",
" 339.759521484375 | \n",
" 89.33876037597656 | \n",
" 1.649017810821533 | \n",
" 2019 | \n",
" 2 | \n",
"
\n",
" \n",
" 8 | \n",
" 8 | \n",
" 72C1xZJJiczquY1mNCcz4w | \n",
" 30.62477748232022 | \n",
" -97.39543499063116 | \n",
" 339.3891906738281 | \n",
" 90.19628143310547 | \n",
" 2.001705646514893 | \n",
" 2021 | \n",
" 12 | \n",
"
\n",
" \n",
" 9 | \n",
" 9 | \n",
" Tg0ssRBHpm9nbY6Qz-pCSQ | \n",
" 46.33407233763008 | \n",
" -119.3732652217733 | \n",
" 340.61767578125 | \n",
" 89.03545379638672 | \n",
" 2.730541944503784 | \n",
" 2012 | \n",
" 4 | \n",
"
\n",
" \n",
" 10 | \n",
" 10 | \n",
" eFWOzRvfUvGS5fqcHXGVeQ | \n",
" 45.54476323631403 | \n",
" -93.86085421527041 | \n",
" 89.4974594116211 | \n",
" 95.06910705566406 | \n",
" 1.029643774032593 | \n",
" 2009 | \n",
" 6 | \n",
"
\n",
" \n",
" 34 | \n",
" 1 | \n",
" eFWOzRvfUvGS5fqcHXGVeQ | \n",
" 45.54476323631403 | \n",
" -93.86085421527041 | \n",
" 89.4974594116211 | \n",
" 95.06910705566406 | \n",
" 1.029643774032593 | \n",
" 2009 | \n",
" 6 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ID Panoid Lat Lon \\\n",
"0 ID Panoid Lat Lon \n",
"1 1 YdyDug5KipDP88_g3LlzcA 38.2613558375869 -121.4406002341907 \n",
"2 2 zjOWG3903_sas4CstIlUWg 38.26135944130262 -121.440610609495 \n",
"3 3 3kA8skjnYcwFXRB4WofR8w 38.26133969218689 -121.4406068604355 \n",
"4 4 5lZ2CQ6r13vrIyEE1fB9CA 30.62476293717697 -97.39541297004071 \n",
"5 5 4v3QNsEpA7dGAcmNV77-tg 30.62476069881489 -97.39543697001042 \n",
"6 6 62v-z86n9AVfzwO_a25BNg 30.62474089233968 -97.39541231024785 \n",
"7 7 FjH1CP_ZEqDyOVnTeVPKSg 30.62474800067029 -97.39540703190518 \n",
"8 8 72C1xZJJiczquY1mNCcz4w 30.62477748232022 -97.39543499063116 \n",
"9 9 Tg0ssRBHpm9nbY6Qz-pCSQ 46.33407233763008 -119.3732652217733 \n",
"10 10 eFWOzRvfUvGS5fqcHXGVeQ 45.54476323631403 -93.86085421527041 \n",
"34 1 eFWOzRvfUvGS5fqcHXGVeQ 45.54476323631403 -93.86085421527041 \n",
"\n",
" Heading Pitch Roll Year Month \n",
"0 Heading Pitch Roll Year Month \n",
"1 169.7516479492188 93.66971588134766 0.8723912239074707 2007 6 \n",
"2 169.9563446044922 88.99956512451172 1.432158350944519 2015 8 \n",
"3 169.4710083007812 89.78001403808594 1.364865064620972 2016 11 \n",
"4 341.0656433105469 94.39850616455078 1.765115857124329 2008 2 \n",
"5 156.5848693847656 89.1475830078125 0.5970472097396851 2009 5 \n",
"6 339.2624206542969 89.32353973388672 4.39376974105835 2011 5 \n",
"7 339.759521484375 89.33876037597656 1.649017810821533 2019 2 \n",
"8 339.3891906738281 90.19628143310547 2.001705646514893 2021 12 \n",
"9 340.61767578125 89.03545379638672 2.730541944503784 2012 4 \n",
"10 89.4974594116211 95.06910705566406 1.029643774032593 2009 6 \n",
"34 89.4974594116211 95.06910705566406 1.029643774032593 2009 6 "
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gsv_df = pd.read_csv('GSV_METADATA_CORN.csv').drop_duplicates()\n",
"gsv_df"
]
},
{
"cell_type": "markdown",
"id": "3f42a1b1-7344-4955-9f0a-bbf972da54a2",
"metadata": {},
"source": [
"### Download GSV Panoramas"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95212647-62df-4642-a49c-6ac213cb4dd0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/YdyDug5KipDP88_g3LlzcA_38.2613558375869_-121.4406002341907_169.7516479492188_2007_6.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/zjOWG3903_sas4CstIlUWg_38.26135944130262_-121.440610609495_169.9563446044922_2015_8.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/3kA8skjnYcwFXRB4WofR8w_38.26133969218689_-121.4406068604355_169.4710083007812_2016_11.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/5lZ2CQ6r13vrIyEE1fB9CA_30.62476293717697_-97.39541297004071_341.0656433105469_2008_2.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/4v3QNsEpA7dGAcmNV77-tg_30.62476069881489_-97.39543697001042_156.5848693847656_2009_5.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/62v-z86n9AVfzwO_a25BNg_30.62474089233968_-97.39541231024785_339.2624206542969_2011_5.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/FjH1CP_ZEqDyOVnTeVPKSg_30.62474800067029_-97.39540703190518_339.759521484375_2019_2.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/72C1xZJJiczquY1mNCcz4w_30.62477748232022_-97.39543499063116_339.3891906738281_2021_12.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/Tg0ssRBHpm9nbY6Qz-pCSQ_46.33407233763008_-119.3732652217733_340.61767578125_2012_4.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/eFWOzRvfUvGS5fqcHXGVeQ_45.54476323631403_-93.86085421527041_89.4974594116211_2009_6.jpg\n",
"Saved Successfully!\n",
"/home/jovyan/work/GGIS407_23Fall/Project/panorama/eFWOzRvfUvGS5fqcHXGVeQ_45.54476323631403_-93.86085421527041_89.4974594116211_2009_6.jpg\n"
]
}
],
"source": [
"Panoids = gsv_df['Panoid'].iloc[1:len(gsv_df)].tolist()\n",
"\n",
"# Set panoramas directory\n",
"pano_dir = os.path.normpath(f\"{current_directory}/panorama/\")\n",
"if not os.path.exists(pano_dir):\n",
" os.mkdir(pano_dir)\n",
"\n",
"for Panoid in Panoids:\n",
" meta_info = gsv_df[gsv_df['Panoid'] == Panoid]\n",
" save_dir = f\"{pano_dir}/{Panoid}_{meta_info['Lat'].values[0]}_{meta_info['Lon'].values[0]}_{meta_info['Heading'].values[0]}_{meta_info['Year'].values[0]}_{meta_info['Month'].values[0]}.jpg\"\n",
" print(save_dir)\n",
" try:\n",
" image = get_panorama(Panoid)\n",
" except:\n",
" print(str(Panoid) + \" has problems with downloading...\")\n",
" continue\n",
" image.save(save_dir, \"jpeg\")\n",
" print('Saved Successfully!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e18e77c5-cec6-4d84-a471-380a858bf3d1",
"metadata": {},
"outputs": [],
"source": [
"def tiles_info(panoid, width, length, zoom=5):\n",
" \n",
" \"\"\"\n",
" Generate a list of a panorama's tiles and their position.\n",
" The format is (x, y, filename, fileurl)\n",
" \"\"\"\n",
"\n",
" image_url = \"https://cbks2.google.com/cbk?cb_client=maps_sv.tactile&authuser=%200&output=tile&hl=en&panoid={}&zoom={}&x={}&y={}\"\n",
"\n",
" # The tiles positions\n",
" coord = list(itertools.product(range(length), range(width)))\n",
" tiles = [(x, y, \"%s_%dx%d.jpg\" % (panoid, x, y), image_url.format(panoid, zoom, x, y)) for x, y in coord]\n",
"\n",
" return tiles"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e7f4874-7033-45e9-bf51-5b94c49774db",
"metadata": {},
"outputs": [],
"source": [
"def download_panorama_v3(panoid, width,length,zoom=5,panorama_path=None, disp=False):\n",
"\n",
" '''\n",
"\n",
" save image information in a buffer. \n",
"\n",
" input:\n",
"\n",
" panoid: which is an id of image on google maps\n",
"\n",
" zoom: larger number -> higher resolution, from 1 to 5, better less than 3, some location will fail when zoom larger than 3\n",
"\n",
" disp: verbose of downloading progress, basically you don't need it\n",
"\n",
" output:\n",
"\n",
" panorama image (uncropped)\n",
"\n",
" '''\n",
"\n",
" tile_width = 512\n",
"\n",
" tile_height = 512\n",
"\n",
" img_w, img_h = int(np.ceil(416*(2**zoom)/tile_width)*tile_width), int(np.ceil(416*( 2**(zoom-1) )/tile_width)*tile_width)\n",
"\n",
" tiles = tiles_info( panoid, length,width,zoom=zoom)\n",
"\n",
" valid_tiles = []\n",
"\n",
" # function of download_tiles\n",
"\n",
" for i, tile in enumerate(tiles):\n",
"\n",
" x, y, fname, url = tile\n",
"\n",
" if disp and i % 20 == 0:\n",
"\n",
" print(\"Image %d / %d\" % (i, len(tiles)))\n",
"\n",
" if x*tile_width < img_w and y*tile_height < img_h: # tile is valid\n",
"\n",
" # Try to download the image file\n",
"\n",
" while True:\n",
"\n",
" try:\n",
"\n",
" response = requests.get(url, stream=True)\n",
"\n",
" break\n",
"\n",
" except requests.ConnectionError:\n",
"\n",
" print(\"Connection error. Trying again in 2 seconds.\")\n",
"\n",
" time.sleep(2)\n",
"\n",
" valid_tiles.append( Image.open(BytesIO(response.content)) )\n",
"\n",
" del response\n",
"\n",
" \n",
"\n",
" # function to stich\n",
"\n",
" panorama = Image.new('RGB', (img_w, img_h))\n",
"\n",
" i = 0\n",
"\n",
" for x, y, fname, url in tiles:\n",
"\n",
" if x*tile_width < img_w and y*tile_height < img_h: # tile is valid\n",
"\n",
" tile = valid_tiles[i]\n",
"\n",
" i+=1\n",
"\n",
" panorama.paste(im=tile, box=(x*tile_width, y*tile_height))\n",
"\n",
" im.imwrite(panorama_path, panorama)\n",
"\n",
" # return np.array(panorama)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24670d7e-ec75-42a4-817e-327374276643",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}