Here is my solution to create a customized mask from raster, that covers the entire surface of interest:
- Convert raster to points
- Identify points of cells overlapping my surface of interest
- Select points, export to a table
- Use a custom function to extract cells of raster with list of points, and create the raster mask.
Below is my code, screenshot of final result on arcgis is attached
import arcpy
from arcpy import env as e,SpatialReference
import os
from arcpy import da as d
from arcpy.sa import *
import pandas as pd
import numpy as np
from arcgis.features import *
projGDB = r"C:\Users\bigar\Documents\ArcGIS\Projects\MooreaHydro2024\MooreaHydro2024.gdb"
e.workspace = r"C:\Users\bigar\Documents\ArcGIS\Projects\MooreaHydro2024\MooreaHydro2024.gdb"
e.overwriteOutput = True
pd.set_option('display.max_rows', None)
def createmyMask(inRaster,pointlist):
## Creates a raster mask by extracting raster cells by coordinates.
desc=arcpy.Describe(inRaster)
rastermask=ExtractByPoints(in_raster=inRaster, points=pointlist,extraction_area="INSIDE")
rastermask.save("C:/Users/bigar/Documents/ArcGIS/Projects/MooreaHydro2024/MooreaHydro2024.gdb/"+desc.baseName+"Mask")
desc2=arcpy.Describe(rastermask)
print("Mask {} was created".format(desc2.baseName))
def extractXY(gdbTable):
df = pd.DataFrame(GeoAccessor.from_table(gdbTable))
# df = df.astype({'X': int, 'Y':int})
coordlist=[]
for index, row in df.loc[:, ['X', 'Y']].iterrows():
# print(type(int(row['X'])), type(int(row['Y'])))
coordlist.append(arcpy.Point(int(row['X']),int(row['Y'])))
print(coordlist)
return coordlist
if __name__ == "__main__":
XYlist = extractXY(r"C:\Users\bigar\Documents\ArcGIS\Projects\MooreaHydro2024\MooreaHydro2024.gdb\Opunohu_MODIS_CELL_COORD")
createmyMask("C:/Users/bigar/Documents/ArcGIS/Projects/MooreaHydro2024/MooreaHydro2024.gdb/MOD16A3GF_061_ET_500m_doy2004001_aid0001_reproj", XYlist)