Advertisement
HawkeyeHS

ARC GIS

Jun 26th, 2024
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | Software | 0 0
  1. import geopandas as gpd
  2. import pandas as pd
  3. from deepforest import utilities
  4. import os
  5. import matplotlib.pyplot as plt
  6. import rasterio as rio
  7.  
  8. # Load the data
  9. df= gpd.read_file('/home/hawkeye/Desktop/qgis-tutorial/sequoia_stems_forBen.shp')
  10. def create_bounding_box(easting, northing, box_size=30):
  11.     half_size = box_size / 2
  12.     return {
  13.         'xmin': easting - half_size,
  14.         'xmax': easting + half_size,
  15.         'ymin': northing - half_size,
  16.         'ymax': northing + half_size
  17.     }
  18. bounding_boxes = df.apply(lambda row: create_bounding_box(row['Easting'], row['Northng']), axis=1)
  19.  
  20. # Convert bounding boxes to a DataFrame
  21. bounding_boxes_df = pd.DataFrame(list(bounding_boxes))
  22.  
  23. # Combine with original data
  24. result_df = pd.concat([df, bounding_boxes_df], axis=1)
  25.  
  26. url='https://map.dfg.ca.gov/arcgis/rest/services/Base_Remote_Sensing/NAIP_2022/ImageServer'
  27.  
  28. print(result_df.head(1))
  29.  
  30. for idx, row in result_df.iterrows():
  31.     xmin, ymin, xmax, ymax = row['xmin'], row['ymin'], row['xmax'], row['ymax']
  32.     print(xmin, ymin, xmax, ymax)
  33.     tmp_dir='./naip_images'
  34.     os.makedirs(tmp_dir, exist_ok=True)
  35.     image_name = f"image_{idx}.tif"
  36.     filename = utilities.download_ArcGIS_REST(url, xmin, ymin, xmax, ymax, savedir=tmp_dir, image_name=image_name)
  37.     # Check the saved file exists
  38.     assert os.path.exists(f"{tmp_dir}/{image_name}")
  39.  
  40.     # Confirm file has crs and show
  41.     with rio.open("{}/{}".format(tmp_dir, image_name)) as src:
  42.         assert src.crs is not None
  43.         # Show
  44.         plt.imshow(src.read().transpose(1,2,0))
  45.         plt.show()
  46.        
  47.     break
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement