Anda di halaman 1dari 6

#-------------------------------------------------------------------------------

# Name: FishingModel.py
# Purpose: This code creates grids needed to build the commercial fishing
# area model.
# 4 grids correlating to the Avg +-1 StDev are created for each
# vessel (7 total vessels) for:
# 1) port distance
# 2) depth
# 3) slope
# 4) shoreline distance
# The output grids are set equal to 1 (cell meets mean +- 1 StDev
# criteria) and 0 (cell does not meet mean +- 1 StDev criteria).
# The final model sums all binary variable grids (28 grids total)

# and identifies cells with largest values.
# These cells correlate to the greatest probable area that will
# contain fishing activity.
#
# Author: Ben Sciance
#
# Created: 05/09/2014 (day/month/year)
# Copyright: (c) mbs7038 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import arcpy
from arcpy import env
from arcpy.sa import *
# setup workspace that holds the summary statistics table that the fishing model
# will be based off of.
arcpy.env.workspace=r"C:\Users\mbs7038\Documents\fish\spatialAnalysis
\updatedSTATS"
# define path for saving predictor grids.
predictor_path=r"C:\Users\mbs7038\Documents\fish\model\UpdatedModel\PredictorUP
.gdb"
# setup environment conditions.
arcpy.env.cellsize=r"C:\Users\mbs7038\Documents\fish\bathy.gdb\Bathy_DEM"
arcpy.env.extent=r"C:\Users\mbs7038\Documents\fish\NMFS_Grid.gdb\NMFS_Grid"
arcpy.env.outputCoordinateSystem=r"C:\Users\mbs7038\Documents\fish\NMFS_Grid.gdb
\NMFS_Grid"
# Check out the ArcGIS Spatial Analyst extension license.
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput=True
#====================================================================
# Part 1 Setup paths
print 'Part 1 ======================================================='
# get the summary stats table to work with.
statTab=r"C:\Users\mbs7038\Documents\fish\spatialAnalysis\updatedSTATS
\sumSTATS.dbf"
# field parameters to be incorporated into the model (from statTab):
# port distance.
MeanDist="MEAN_PortD"
StDevDist="STD_PortDi"
# distance from shore.
MeanShore="MEAN_Shore"
StDevShore="STD_ShoreD"
# seafloor depth.
MeanDepth="MEAN_DEP"
StDevDepth="STD_DEP"
# seafloor slope.
MeanSlope="MEAN_SLOP"
StDevSlope="STD_SLOP"
# get the bathy grid.
bathy=r"C:\Users\mbs7038\Documents\fish\bathy.gdb\Bathy_DEM"
# get the slope grid.
slope=r"C:\Users\mbs7038\Documents\fish\bathy.gdb\Bathy_Slope"
# get the distance from shore grid.
shore=r"C:\Users\mbs7038\Documents\fish\shorelineEucDist.gdb
\Shoreline_EucDist_grid"
# get the port distance grids.
print '1) listing port distance grids:'
arcpy.env.workspace=r"C:\Users\mbs7038\Documents\fish\EucDistUpdate.gdb"
dists=arcpy.ListRasters('*')
dists.sort()
print '{0}'.format(dists)
# predictor grid save path:
savepath=r"H:\FisheriesData\predictorVars.gdb"
# final model save path:
modelPath=r"H:\FisheriesData\FinalModel.gdb"
#==========================================================================
# Part 2: get model parameters pertaining to each vessel.
print 'Part 2 ============================================================='
# create lists to hold model parameters.
# 1) port distance
x_Dist=[]
SD_Dist=[]
# 2) seafloor depth
x_Depth=[]
SD_Depth=[]
# 3) seafloor slope
x_Slope=[]
SD_Slope=[]
# 4) distance from shore
x_Shore=[]
SD_Shore=[]
# 5) Vessel Name
vName=[]
# setup cursor to iterate through each row (each row corresponds to a vessel)
# in # the table.
cursor=arcpy.SearchCursor(statTab)
for vessel in cursor:
vesselName=vessel.getValue("VESSEL")
print vesselName
vName.append(vesselName)
print 'obtaining model parameters for {0}'.format(vesselName)
# Port distance
MEAN=vessel.getValue(MeanDist)
print '{0} avg port dist is {1}'.format(vesselName,MEAN)
x_Dist.append(MEAN)
SD=vessel.getValue(StDevDist)
print'{0} port dist stdev is {1}'.format(vesselName,SD)
SD_Dist.append(SD)
# Depth
dMEAN=vessel.getValue(MeanDepth)
print '{0} avg depth is {1}'.format(vesselName,dMEAN)
x_Depth.append(dMEAN)
dSD=vessel.getValue(StDevDepth)
print '{0} depth stdev is {1}'.format(vesselName,dSD)
SD_Depth.append(dSD)
# Slope
sMEAN=vessel.getValue(MeanSlope)
print '{0} avg slope is {1}'.format(vesselName,sMEAN)
x_Slope.append(sMEAN)
sSD=vessel.getValue(StDevSlope)
print '{0} slope stdev is {1}'.format(vesselName,sSD)
SD_Slope.append(sSD)
# Distance from shore
shMEAN=vessel.getValue(MeanShore)
print '{0} avg shore dist is {1}'.format(vesselName,shMEAN)
x_Shore.append(shMEAN)
shSD=vessel.getValue(StDevShore)
print '{0} shore dist stdev is {1}'.format(vesselName,shSD)
SD_Shore.append(shSD)
#========================================================================
# Part 3: Build model one variable at a time.
# Each vessel will have 4 binary variable grids associated with it.
# - Port distance, depth, slope, and shore distance binary grids.
print 'Part 3 =========================================================='
for i in range (8):
#=============================================
# 1) create port distance model.
print 'creating port distance model for {0}'.format(vName[i])
minDist=x_Dist[i]-SD_Dist[i]
maxDist=x_Dist[i]+SD_Dist[i]
# create binary grid for port distance.
# cells = 1 fall between avg +- 1 standard deviation.
distGrid=Con((Raster(dists[i])>=minDist)&(Raster(dists[i])<=maxDist),1,0)
distSave=savepath + "/" + vName[i] +"DistModel"
distGrid.save(distSave)
#=============================================
# 2) create depth model.
# **Keep in mind that the mean depth value is negative and the StDev is a

# positive value.
print'creating depth model for {0}'.format(vName[i])
maxDepth=x_Depth[i]-SD_Depth[i]
minDepth=x_Depth[i]+SD_Depth[i]
# create binary grids for depth.
# cells = 1 fall between avg +- 1 standard deviation.
depthGrid=Con((Raster(bathy)>=maxDepth)&(Raster(bathy)<=minDepth),1,0)
depthSave=savepath + "/" + vName[i] +"DepthModel"
depthGrid.save(depthSave)
#=============================================
# 3) create slope model.
print 'creating slope model for {0}'.format(vName[i])
minSlope=x_Slope[i]-SD_Slope[i]
maxSlope=x_Slope[i]+SD_Slope[i]
# create binary grid for slope.
# cells = 1 fall between avg +- 1 standard deviation.
slopeGrid=Con((Raster(slope)>=minSlope)&(Raster(slope)<=maxSlope),1,0)
slopeSave=savepath + "/" + vName[i] +"SlopeModel"
slopeGrid.save(slopeSave)
#=============================================
# 4) create distance from shore model.
print 'creating shore distance model for {0}'.format(vName[i])
minShore=x_Shore[i]-SD_Shore[i]
maxShore=x_Shore[i]+SD_Shore[i]
# create binary grid for shoreline distance.
# cells = 1 fall between avg +- 1 standard deviation.
shoreGrid=Con((Raster(shore)>=minShore)&(Raster(shore)<=maxShore),1,0)
shoreSave=savepath + "/" + vName[i] +"ShoreModel"
shoreGrid.save(shoreSave)
#===============================================================================
# Part 4: Construct fishing models by summing all binary variable grids.
print 'Part 4 ================================================================'
arcpy.env.workspace=r"H:\FisheriesData\predictorVars.gdb"
# list all rasters in the workspace except for those associated with total
# fishing activity.
print 'listing predictor model variable grids.'
rasters=[raster for raster in arcpy.ListRasters() if not raster.startswith('tota
l')]
rasters.sort()
#====================================================
# 1) build model off of individual vessel parameters.
# Iterate through and sum each grid.
# Create index, z
z=0
print 'building final model'
for raster in rasters:
print z
print 'adding {0} to final model.'.format(raster)
# sum grids together
if z==0:
outsum=Raster(raster)
z+=1 # same as saying z=z+1
else:
print 'summing {0}'.format(raster)
outsum=outsum+Raster(raster)
z+=1
if z==28:
break
print 'saving final model.'
outsum.save(modelPath + "/" + "Fishing_Model")
#====================================================
# 2) build model off of combined vessel parameters.
# list predictor grids for combined vessels.
totals=arcpy.ListRasters('total*')
print totals
# create index j.
j=0
print'building final combined vessel model.'
for total in totals:
print j
print 'adding {0} to final combined model.'.format(total)
# sum grids together
if j==0:
totsum=Raster(total)
j+=1 # same as saying j=j+1
else:
print 'summing {0}'.format(total)
totsum=totsum+Raster(total)
j+=1
if j==4:
break
print 'saving final combined vessel model.'
totsum.save(modelPath + "/" + "Combined_Fishing_Model")
print 'script is complete!!!!!'

Anda mungkin juga menyukai